diff --git a/CHANGELOG.md b/CHANGELOG.md index 18d87fb..4a19620 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.10.3...Unreleased) - ReleaseDate +* [Fixed] uncleared internal state + ## [v0.10.3](https://github.com/dalance/sv-parser/compare/v0.10.2...v0.10.3) - 2021-01-08 * [Fixed] empty port list is parsed as non-ANSI style diff --git a/sv-parser-parser/src/lib.rs b/sv-parser-parser/src/lib.rs index 7351bd4..aa86217 100644 --- a/sv-parser-parser/src/lib.rs +++ b/sv-parser-parser/src/lib.rs @@ -91,26 +91,32 @@ impl HasExtraState for SpanInfo { nom_packrat::storage!(AnyNode, bool, 1024); pub fn sv_parser(s: Span) -> IResult { - nom_packrat::init!(); + init(); source_text(s) } pub fn sv_parser_incomplete(s: Span) -> IResult { - nom_packrat::init!(); + init(); source_text_incomplete(s) } pub fn lib_parser(s: Span) -> IResult { - nom_packrat::init!(); + init(); library_text(s) } pub fn lib_parser_incomplete(s: Span) -> IResult { - nom_packrat::init!(); + init(); library_text_incomplete(s) } pub fn pp_parser(s: Span) -> IResult { - nom_packrat::init!(); + init(); preprocessor_text(s) } + +fn init() { + nom_packrat::init!(); + clear_directive(); + clear_version(); +} diff --git a/sv-parser-parser/src/utils.rs b/sv-parser-parser/src/utils.rs index 997a270..824a749 100644 --- a/sv-parser-parser/src/utils.rs +++ b/sv-parser-parser/src/utils.rs @@ -351,9 +351,13 @@ pub(crate) fn end_directive() { IN_DIRECTIVE.with(|x| x.borrow_mut().pop()); } +pub(crate) fn clear_directive() { + IN_DIRECTIVE.with(|x| x.borrow_mut().clear()); +} + // ----------------------------------------------------------------------------- -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub(crate) enum VersionSpecifier { Ieee1364_1995, Ieee1364_2001, @@ -418,6 +422,12 @@ pub(crate) fn current_version() -> Option { }) } +pub(crate) fn clear_version() { + CURRENT_VERSION.with(|current_version| { + current_version.borrow_mut().clear(); + }); +} + // ----------------------------------------------------------------------------- pub(crate) fn concat<'a>(a: Span<'a>, b: Span<'a>) -> Option> { diff --git a/sv-parser/src/lib.rs b/sv-parser/src/lib.rs index 0f7213f..db4811e 100644 --- a/sv-parser/src/lib.rs +++ b/sv-parser/src/lib.rs @@ -277,4 +277,46 @@ mod test { let comment = unwrap_node!(&syntax_tree, Comment); assert!(comment.is_some()); } + + #[test] + fn test_continuous() { + let src = r##"`ifdef A +`endif + +module FetchStage(); + always_comb begin + for (int j = i + 1; j < FETCH_WIDTH; j++) begin + end + break; + end + + AddrPath fetchAddrOut; +endmodule"##; + + let src_broken = r##"`ifdef A +endif + +module FetchStage(); + always_comb begin + for (int j = i + 1; j < FETCH_WIDTH; j++) begin + end + break; + end + + AddrPath fetchAddrOut; +endmodule"##; + + let path = PathBuf::from(""); + let defines = HashMap::new(); + let ret = parse_sv_str(src, &path, &defines, &[""], false, false); + assert!(ret.is_ok()); + let ret = parse_sv_str(src_broken, &path, &defines, &[""], false, false); + assert!(ret.is_err()); + let ret = parse_sv_str(src, &path, &defines, &[""], false, false); + assert!(ret.is_ok()); + let ret = parse_sv_str(src_broken, &path, &defines, &[""], false, false); + assert!(ret.is_err()); + let ret = parse_sv_str(src, &path, &defines, &[""], false, false); + assert!(ret.is_ok()); + } }