diff --git a/CHANGELOG.md b/CHANGELOG.md index e8a9db5..d197fdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.9.0...Unreleased) - ReleaseDate +* [Fixed] wrong error position * [Fixed] escaped_ideitifier at macro name [#28](https://github.com/dalance/sv-parser/issues/28) * [Fixed] begin_keywords "1364-2001-noconfig" failure [#28](https://github.com/dalance/sv-parser/issues/28) * [Changed] update nom to 6.0.0 diff --git a/sv-parser-parser/src/lib.rs b/sv-parser-parser/src/lib.rs index aae0169..7351bd4 100644 --- a/sv-parser-parser/src/lib.rs +++ b/sv-parser-parser/src/lib.rs @@ -95,11 +95,21 @@ pub fn sv_parser(s: Span) -> IResult { source_text(s) } +pub fn sv_parser_incomplete(s: Span) -> IResult { + nom_packrat::init!(); + source_text_incomplete(s) +} + pub fn lib_parser(s: Span) -> IResult { nom_packrat::init!(); library_text(s) } +pub fn lib_parser_incomplete(s: Span) -> IResult { + nom_packrat::init!(); + library_text_incomplete(s) +} + pub fn pp_parser(s: Span) -> IResult { nom_packrat::init!(); preprocessor_text(s) diff --git a/sv-parser-parser/src/source_text/library_source_text.rs b/sv-parser-parser/src/source_text/library_source_text.rs index d2e9181..1f76a32 100644 --- a/sv-parser-parser/src/source_text/library_source_text.rs +++ b/sv-parser-parser/src/source_text/library_source_text.rs @@ -5,6 +5,14 @@ use crate::*; #[tracable_parser] #[packrat_parser] pub(crate) fn library_text(s: Span) -> IResult { + let (s, a) = many0(white_space)(s)?; + let (s, (b, _)) = many_till(library_description, eof)(s)?; + Ok((s, LibraryText { nodes: (a, b) })) +} + +#[tracable_parser] +#[packrat_parser] +pub(crate) fn library_text_incomplete(s: Span) -> IResult { let (s, a) = many0(white_space)(s)?; let (s, b) = many0(library_description)(s)?; Ok((s, LibraryText { nodes: (a, b) })) diff --git a/sv-parser-parser/src/source_text/system_verilog_source_text.rs b/sv-parser-parser/src/source_text/system_verilog_source_text.rs index bc87d60..b0b45e1 100644 --- a/sv-parser-parser/src/source_text/system_verilog_source_text.rs +++ b/sv-parser-parser/src/source_text/system_verilog_source_text.rs @@ -5,6 +5,15 @@ use crate::*; #[tracable_parser] #[packrat_parser] pub(crate) fn source_text(s: Span) -> IResult { + let (s, a) = many0(white_space)(s)?; + let (s, b) = opt(timeunits_declaration)(s)?; + let (s, (c, _)) = many_till(description, eof)(s)?; + Ok((s, SourceText { nodes: (a, b, c) })) +} + +#[tracable_parser] +#[packrat_parser] +pub(crate) fn source_text_incomplete(s: Span) -> IResult { let (s, a) = many0(white_space)(s)?; let (s, b) = opt(timeunits_declaration)(s)?; let (s, c) = many0(description)(s)?; diff --git a/sv-parser-parser/src/tests.rs b/sv-parser-parser/src/tests.rs index 002d942..09e5935 100644 --- a/sv-parser-parser/src/tests.rs +++ b/sv-parser-parser/src/tests.rs @@ -22,6 +22,26 @@ macro_rules! test { }; } +macro_rules! error_test { + ( $x:expr, $y:expr, $p:expr ) => { + nom_packrat::init!(); + let info = SpanInfo::default(); + #[cfg(feature = "trace")] + let info = info.set_tracable_info( + info.get_tracable_info() + //.fold("white_space") + .fold("number") + .fold("binary_operator") + .fold("unary_operator"), + ); + let ret = all_consuming($x)(Span::new_extra($y, info)); + match ret { + Err(Err::Error(e)) => assert_eq!(nom_greedyerror::error_position(&e), $p), + _ => (), + } + }; +} + mod unit { use super::*; @@ -15893,6 +15913,19 @@ mod spec { } } +mod error { + use super::*; + + #[test] + fn test1() { + error_test!( + source_text, + r##"module A(); parameter A = 1 endmodule"##, + Some(12) + ); + } +} + #[test] fn debug() { test!( diff --git a/sv-parser/src/lib.rs b/sv-parser/src/lib.rs index 9df2cc6..0f7213f 100644 --- a/sv-parser/src/lib.rs +++ b/sv-parser/src/lib.rs @@ -1,13 +1,14 @@ #![recursion_limit = "256"] -use nom::combinator::all_consuming; use nom_greedyerror::error_position; use std::collections::HashMap; use std::fmt; use std::hash::BuildHasher; use std::path::{Path, PathBuf}; pub use sv_parser_error::Error; -use sv_parser_parser::{lib_parser, sv_parser, Span, SpanInfo}; +use sv_parser_parser::{ + lib_parser, lib_parser_incomplete, sv_parser, sv_parser_incomplete, Span, SpanInfo, +}; pub use sv_parser_pp::preprocess::{ preprocess, preprocess_str, Define, DefineText, Defines, PreprocessedText, }; @@ -110,9 +111,9 @@ pub fn parse_sv_pp( ) -> Result<(SyntaxTree, Defines), Error> { let span = Span::new_extra(text.text(), SpanInfo::default()); let result = if allow_incomplete { - sv_parser(span) + sv_parser_incomplete(span) } else { - all_consuming(sv_parser)(span) + sv_parser(span) }; match result { Ok((_, x)) => Ok(( @@ -200,7 +201,7 @@ pub fn parse_lib_pp( ) -> Result<(SyntaxTree, Defines), Error> { let span = Span::new_extra(text.text(), SpanInfo::default()); let result = if allow_incomplete { - all_consuming(lib_parser)(span) + lib_parser_incomplete(span) } else { lib_parser(span) };