Skip to content

Commit

Permalink
Merge pull request #409 from HigherOrderCO/parser-update
Browse files Browse the repository at this point in the history
Update TSPL to report parsing error range
  • Loading branch information
edusporto authored Aug 7, 2024
2 parents a5e6788 + 2a207a6 commit 3494839
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "hvm"
description = "A massively parallel, optimal functional runtime in Rust."
license = "Apache-2.0"
version = "2.0.21"
version = "2.0.22"
edition = "2021"
rust-version = "1.74"
build = "build.rs"
Expand All @@ -13,7 +13,7 @@ name = "hvm"
path = "src/lib.rs"

[dependencies]
TSPL = "0.0.12"
TSPL = "0.0.13"
clap = "4.5.2"
highlight_error = "0.1.1"
num_cpus = "1.0"
Expand Down
40 changes: 19 additions & 21 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use TSPL::{new_parser, Parser};
use TSPL::{new_parser, Parser, ParseError};
use highlight_error::highlight_error;
use crate::hvm;
use std::fmt::{Debug, Display};
Expand Down Expand Up @@ -37,11 +37,13 @@ pub struct Book {
// Parser
// ------

pub type ParseResult<T> = std::result::Result<T, ParseError>;

new_parser!(CoreParser);

impl<'i> CoreParser<'i> {

pub fn parse_numb_sym(&mut self) -> Result<Numb, String> {
pub fn parse_numb_sym(&mut self) -> ParseResult<Numb> {
self.consume("[")?;

// numeric casts
Expand Down Expand Up @@ -97,33 +99,29 @@ impl<'i> CoreParser<'i> {
return Ok(Numb(num.0));
}

pub fn parse_numb_lit(&mut self) -> Result<Numb, String> {
pub fn parse_numb_lit(&mut self) -> ParseResult<Numb> {
let ini = self.index;
let num = self.take_while(|x| x.is_alphanumeric() || x == '+' || x == '-' || x == '.');
let mut num_parser = CoreParser::new(num);
let end = self.index;
Ok(Numb(if num.contains('.') || num.contains("inf") || num.contains("NaN") {
let val: f32 = num.parse().map_err(|err| format!("invalid number literal: {}\n{}", err, highlight_error(ini, end, self.input)))?;
let val: f32 = num.parse()
.map_err(|err| {
let msg = format!("invalid number literal: {}\n{}", err, highlight_error(ini, end, self.input));
self.expected_and::<Numb>("number literal", &msg).unwrap_err()
})?;
hvm::Numb::new_f24(val)
} else if num.starts_with('+') || num.starts_with('-') {
let val = Self::parse_int(&num[1..])? as i32;
*num_parser.index() += 1;
let val = num_parser.parse_u64()? as i32;
hvm::Numb::new_i24(if num.starts_with('-') { -val } else { val })
} else {
let val = Self::parse_int(num)? as u32;
let val = num_parser.parse_u64()? as u32;
hvm::Numb::new_u24(val)
}.0))
}

fn parse_int(input: &str) -> Result<u64, String> {
if let Some(rest) = input.strip_prefix("0x") {
u64::from_str_radix(rest, 16).map_err(|err| format!("{err:?}"))
} else if let Some(rest) = input.strip_prefix("0b") {
u64::from_str_radix(rest, 2).map_err(|err| format!("{err:?}"))
} else {
input.parse::<u64>().map_err(|err| format!("{err:?}"))
}
}

pub fn parse_numb(&mut self) -> Result<Numb, String> {
pub fn parse_numb(&mut self) -> ParseResult<Numb> {
self.skip_trivia();

// Parses symbols (SYM)
Expand All @@ -135,7 +133,7 @@ impl<'i> CoreParser<'i> {
}
}

pub fn parse_tree(&mut self) -> Result<Tree, String> {
pub fn parse_tree(&mut self) -> ParseResult<Tree> {
self.skip_trivia();
//println!("aaa ||{}", &self.input[self.index..]);
match self.peek_one() {
Expand Down Expand Up @@ -194,7 +192,7 @@ impl<'i> CoreParser<'i> {
}
}

pub fn parse_net(&mut self) -> Result<Net, String> {
pub fn parse_net(&mut self) -> ParseResult<Net> {
let root = self.parse_tree()?;
let mut rbag = Vec::new();
self.skip_trivia();
Expand All @@ -210,7 +208,7 @@ impl<'i> CoreParser<'i> {
Ok(Net { root, rbag })
}

pub fn parse_book(&mut self) -> Result<Book, String> {
pub fn parse_book(&mut self) -> ParseResult<Book> {
let mut defs = BTreeMap::new();
while !self.is_eof() {
self.consume("@")?;
Expand Down Expand Up @@ -527,7 +525,7 @@ impl Net {
}

impl Book {
pub fn parse(code: &str) -> Result<Self, String> {
pub fn parse(code: &str) -> ParseResult<Self> {
CoreParser::new(code).parse_book()
}

Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ expression: rust_output
input_file: tests/programs/empty.hvm
---
exit status: 101
thread 'main' panicked at src/ast.rs:547:41:
thread 'main' panicked at src/ast.rs:545:41:
missing `@main` definition
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 comments on commit 3494839

Please sign in to comment.