From 01b795d8718cae14c7809316ac7b2acf459b0dba Mon Sep 17 00:00:00 2001 From: Eduardo Sandalo Porto Date: Tue, 6 Aug 2024 11:24:09 -0300 Subject: [PATCH 1/2] Changes to the parser to report error range --- Cargo.lock | 3 +- Cargo.toml | 3 ++ src/ast.rs | 40 +++++++++++------------- src/hvm.h | 4 +-- tests/snapshots/run__file@empty.hvm.snap | 2 +- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb158a19..fcbd28be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,8 +5,7 @@ version = 3 [[package]] name = "TSPL" version = "0.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52dfd6238b1461b99635b26585a85b4e7b9c786cc0481b3c540ae5f590b6dfb6" +source = "git+https://github.com/HigherOrderCO/TSPL.git?branch=range-info#f46f88debf9e0363e7ec33f554dc1798c9a62737" dependencies = [ "highlight_error", ] diff --git a/Cargo.toml b/Cargo.toml index b08dea98..475c475c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,6 @@ cuda = [] [dev-dependencies] insta = { version = "1.39.0", features = ["glob"] } + +[patch.crates-io] +TSPL = { git = "https://github.com/HigherOrderCO/TSPL.git", branch = "range-info"} diff --git a/src/ast.rs b/src/ast.rs index 56db241c..c5b3a745 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -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}; @@ -37,11 +37,13 @@ pub struct Book { // Parser // ------ +pub type ParseResult = std::result::Result; + new_parser!(CoreParser); impl<'i> CoreParser<'i> { - pub fn parse_numb_sym(&mut self) -> Result { + pub fn parse_numb_sym(&mut self) -> ParseResult { self.consume("[")?; // numeric casts @@ -97,33 +99,29 @@ impl<'i> CoreParser<'i> { return Ok(Numb(num.0)); } - pub fn parse_numb_lit(&mut self) -> Result { + pub fn parse_numb_lit(&mut self) -> ParseResult { 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::("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 { - 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::().map_err(|err| format!("{err:?}")) - } - } - - pub fn parse_numb(&mut self) -> Result { + pub fn parse_numb(&mut self) -> ParseResult { self.skip_trivia(); // Parses symbols (SYM) @@ -135,7 +133,7 @@ impl<'i> CoreParser<'i> { } } - pub fn parse_tree(&mut self) -> Result { + pub fn parse_tree(&mut self) -> ParseResult { self.skip_trivia(); //println!("aaa ||{}", &self.input[self.index..]); match self.peek_one() { @@ -194,7 +192,7 @@ impl<'i> CoreParser<'i> { } } - pub fn parse_net(&mut self) -> Result { + pub fn parse_net(&mut self) -> ParseResult { let root = self.parse_tree()?; let mut rbag = Vec::new(); self.skip_trivia(); @@ -210,7 +208,7 @@ impl<'i> CoreParser<'i> { Ok(Net { root, rbag }) } - pub fn parse_book(&mut self) -> Result { + pub fn parse_book(&mut self) -> ParseResult { let mut defs = BTreeMap::new(); while !self.is_eof() { self.consume("@")?; @@ -527,7 +525,7 @@ impl Net { } impl Book { - pub fn parse(code: &str) -> Result { + pub fn parse(code: &str) -> ParseResult { CoreParser::new(code).parse_book() } diff --git a/src/hvm.h b/src/hvm.h index b4930e25..b9b59500 100644 --- a/src/hvm.h +++ b/src/hvm.h @@ -206,8 +206,8 @@ typedef struct Tup { extern Tup readback_tup(Net* net, Book* book, Port port, u32 size); typedef struct Str { - u32 text_len; - char text_buf[256]; + u32 len; + char *buf; } Str; // Reads a constructor-encoded string (of length at most 255 characters), diff --git a/tests/snapshots/run__file@empty.hvm.snap b/tests/snapshots/run__file@empty.hvm.snap index 1460dd1e..54fc2b68 100644 --- a/tests/snapshots/run__file@empty.hvm.snap +++ b/tests/snapshots/run__file@empty.hvm.snap @@ -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 From 254fcd19046ad92a2571701a00bb6ced81b0ef4b Mon Sep 17 00:00:00 2001 From: Eduardo Sandalo Porto Date: Wed, 7 Aug 2024 09:55:36 -0300 Subject: [PATCH 2/2] Bump version and TSPL --- Cargo.lock | 7 ++++--- Cargo.toml | 7 ++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fcbd28be..140801f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,8 +4,9 @@ version = 3 [[package]] name = "TSPL" -version = "0.0.12" -source = "git+https://github.com/HigherOrderCO/TSPL.git?branch=range-info#f46f88debf9e0363e7ec33f554dc1798c9a62737" +version = "0.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe639519d49b56c98fd4fde7a5a7be01b5563862341a783b9bc2eb58f5120d8b" dependencies = [ "highlight_error", ] @@ -162,7 +163,7 @@ checksum = "809e18805660d7b6b2e2b9f316a5099521b5998d5cba4dda11b5157a21aaef03" [[package]] name = "hvm" -version = "2.0.21" +version = "2.0.22" dependencies = [ "TSPL", "cc", diff --git a/Cargo.toml b/Cargo.toml index 475c475c..817e5da0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" @@ -30,6 +30,3 @@ cuda = [] [dev-dependencies] insta = { version = "1.39.0", features = ["glob"] } - -[patch.crates-io] -TSPL = { git = "https://github.com/HigherOrderCO/TSPL.git", branch = "range-info"}