Skip to content

Commit

Permalink
Add raw identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Jul 1, 2024
1 parent e71cdac commit b119a34
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 8 deletions.
10 changes: 10 additions & 0 deletions crates/analyzer/src/handlers/check_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ impl<'a> CheckIdentifier<'a> {
))
}

if let Some(x) = identifier.strip_prefix("r#") {
if is_sv_keyword(x) {
self.errors.push(AnalyzerError::sv_keyword_usage(
&identifier,
self.text,
&token.into(),
))
}
}

if let Some(prefix) = prefix {
if !identifier.starts_with(prefix) {
self.errors.push(AnalyzerError::invalid_identifier(
Expand Down
23 changes: 23 additions & 0 deletions crates/analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,3 +2047,26 @@ fn detect_recursive() {
let errors = analyze(code);
assert!(matches!(errors[0], AnalyzerError::MismatchType { .. }));
}

#[test]
fn sv_keyword_usage() {
let code = r#"
module ModuleA {
var always: logic;
assign always = 1;
}
"#;

let errors = analyze(code);
assert!(matches!(errors[0], AnalyzerError::SvKeywordUsage { .. }));

let code = r#"
module ModuleA {
var r#always: logic;
assign r#always = 1;
}
"#;

let errors = analyze(code);
assert!(matches!(errors[0], AnalyzerError::SvKeywordUsage { .. }));
}
6 changes: 4 additions & 2 deletions crates/emitter/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,12 @@ impl Emitter {
suffix: &Option<String>,
) {
if prefix.is_some() || suffix.is_some() {
let token = &identifier.identifier_token.append(prefix, suffix);
let token = &identifier.identifier_token.strip_prefix("r#");
let token = &token.append(prefix, suffix);
self.veryl_token(token);
} else {
self.veryl_token(&identifier.identifier_token);
let token = &identifier.identifier_token.strip_prefix("r#");
self.veryl_token(token);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/generated/veryl-exp.par
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
/* 107 */ UnsafeTerm: <INITIAL, Generic>/(?-u:\b)unsafe(?-u:\b)/ : Token;
/* 108 */ VarTerm: <INITIAL, Generic>/(?-u:\b)var(?-u:\b)/ : Token;
/* 109 */ DollarIdentifierTerm: <INITIAL, Generic>/\$[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;
/* 110 */ IdentifierTerm: <INITIAL, Generic>/[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;
/* 110 */ IdentifierTerm: <INITIAL, Generic>/(?:r#)?[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;
/* 111 */ AnyTerm: <Embed>/[^{}]*/ : Token;
/* 112 */ Comments: CommentsOpt /* Option */;
/* 113 */ CommentsOpt /* Option<T>::Some */: CommentsTerm;
Expand Down
4 changes: 2 additions & 2 deletions crates/parser/src/generated/veryl_grammar_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7343,7 +7343,7 @@ pub enum IdentifierStatementGroup {
#[derive(Builder, Debug, Clone)]
#[builder(crate = "parol_runtime::derive_builder")]
pub struct IdentifierTerm {
pub identifier_term: crate::veryl_token::Token, /* [a-zA-Z_][0-9a-zA-Z_$]* */
pub identifier_term: crate::veryl_token::Token, /* (?:r#)?[a-zA-Z_][0-9a-zA-Z_$]* */
}

///
Expand Down Expand Up @@ -14860,7 +14860,7 @@ impl<'t, 'u> VerylGrammarAuto<'t, 'u> {

/// Semantic action for production 110:
///
/// `IdentifierTerm: <INITIAL, Generic>/[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;`
/// `IdentifierTerm: <INITIAL, Generic>/(?:r#)?[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;`
///
#[parol_runtime::function_name::named]
fn identifier_term(&mut self, identifier_term: &ParseTreeType<'t>) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions crates/parser/src/generated/veryl_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub const TERMINALS: &[&str; 118] = &[
/* 112 */ r"(?-u:\b)unsafe(?-u:\b)",
/* 113 */ r"(?-u:\b)var(?-u:\b)",
/* 114 */ r"\$[a-zA-Z_][0-9a-zA-Z_$]*",
/* 115 */ r"[a-zA-Z_][0-9a-zA-Z_$]*",
/* 115 */ r"(?:r#)?[a-zA-Z_][0-9a-zA-Z_$]*",
/* 116 */ r"[^{}]*",
/* 117 */ ERROR_TOKEN,
];
Expand Down Expand Up @@ -18002,7 +18002,7 @@ pub const PRODUCTIONS: &[Production; 959] = &[
lhs: 118,
production: &[ParseType::T(114)],
},
// 110 - IdentifierTerm: /[a-zA-Z_][0-9a-zA-Z_$]*/;
// 110 - IdentifierTerm: /(?:r#)?[a-zA-Z_][0-9a-zA-Z_$]*/;
Production {
lhs: 248,
production: &[ParseType::T(115)],
Expand Down
14 changes: 14 additions & 0 deletions crates/parser/src/veryl_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,20 @@ impl VerylToken {
ret.token.length = length as u32;
ret
}

pub fn strip_prefix(&self, prefix: &str) -> Self {
let text = self.token.text.to_string();
if let Some(text) = text.strip_prefix(prefix) {
let length = text.len();
let text = resource_table::insert_str(text);
let mut ret = self.clone();
ret.token.text = text;
ret.token.length = length as u32;
ret
} else {
self.clone()
}
}
}

impl fmt::Display for VerylToken {
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/veryl.par
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ UnionTerm : <INITIAL, Generic >/(?-u:\b)union(?-u:\b)/
UnsafeTerm : <INITIAL, Generic >/(?-u:\b)unsafe(?-u:\b)/ : Token;
VarTerm : <INITIAL, Generic >/(?-u:\b)var(?-u:\b)/ : Token;
DollarIdentifierTerm : <INITIAL, Generic >/\$[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;
IdentifierTerm : <INITIAL, Generic >/[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;
IdentifierTerm : <INITIAL, Generic >/(?:r#)?[a-zA-Z_][0-9a-zA-Z_$]*/ : Token;
AnyTerm : < Embed>/[^{}]*/ : Token;

// ----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions testcases/map/testcases/sv/62_raw_identifier.sv.map

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

7 changes: 7 additions & 0 deletions testcases/sv/62_raw_identifier.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module veryl_testcase_Module62;
logic clock;
logic reset;
always_comb clock = 1;
always_comb reset = 1;
endmodule
//# sourceMappingURL=../map/testcases/sv/62_raw_identifier.sv.map
6 changes: 6 additions & 0 deletions testcases/veryl/62_raw_identifier.veryl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Module62 {
var r#clock: logic;
var r#reset: logic;
assign r#clock = 1;
assign r#reset = 1;
}

0 comments on commit b119a34

Please sign in to comment.