From 44b2422433997d6894f72b2aa502da5ee26eb6b9 Mon Sep 17 00:00:00 2001 From: dalance Date: Wed, 22 Jan 2020 19:25:29 +0900 Subject: [PATCH] Impl __LINE__ and __FILE__ --- CHANGELOG.md | 1 + sv-parser-pp/src/preprocess.rs | 32 ++++++++++++++++++++++++++++++++ sv-parser-pp/testcases/test11.sv | 6 ++++++ 3 files changed, 39 insertions(+) create mode 100644 sv-parser-pp/testcases/test11.sv diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cfa2c2..e7c8d9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.4.19...Unreleased) - ReleaseDate +* [Added] `` `__LINE__`` and `` `__FILE__`` are preprocessed * [Fixed] parser priority about specify * [Fixed] escaped_ideitifier including `` ` `` * [Fixed] time_literal spacing diff --git a/sv-parser-pp/src/preprocess.rs b/sv-parser-pp/src/preprocess.rs index af0e1d8..3090a11 100644 --- a/sv-parser-pp/src/preprocess.rs +++ b/sv-parser-pp/src/preprocess.rs @@ -425,6 +425,22 @@ pub fn preprocess_str, U: AsRef>( defines = new_defines; } } + NodeEvent::Enter(RefNode::PositionCompilerDirective(x)) if !skip => { + let (_, ref x) = x.nodes; + let locate: Locate = x.try_into().unwrap(); + let x = locate.str(s); + if x.starts_with("__FILE__") { + ret.push::( + &x.replace( + "__FILE__", + &format!("\"{}\"", path.as_ref().to_string_lossy()), + ), + None, + ); + } else if x.starts_with("__LINE__") { + ret.push::(&x.replace("__LINE__", &format!("{}", locate.line)), None); + } + } _ => (), } } @@ -793,6 +809,22 @@ endmodule ); } + #[test] + fn test11() { + let (ret, _) = + preprocess(get_testcase("test11.sv"), &HashMap::new(), &[] as &[String]).unwrap(); + assert_eq!( + ret.text(), + r##"module a; +initial begin + if (3 == 0) begin + end +end +endmodule +"## + ); + } + #[test] fn test12() { let (ret, _) = diff --git a/sv-parser-pp/testcases/test11.sv b/sv-parser-pp/testcases/test11.sv new file mode 100644 index 0000000..911ec70 --- /dev/null +++ b/sv-parser-pp/testcases/test11.sv @@ -0,0 +1,6 @@ +module a; +initial begin + if (`__LINE__ == 0) begin + end +end +endmodule