Skip to content

Commit

Permalink
fix: regex recognizer anchoring
Browse files Browse the repository at this point in the history
  • Loading branch information
igordejanovic committed Jan 4, 2024
1 parent 4383f5e commit 2de5537
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/src/tutorials/calculator/calculator1/src/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenRecognizer(
TokenKind::Operand,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\d+(\\.\\d+)?")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\d+(\\.\\d+)?", ")")).unwrap() }),
),
),
TokenRecognizer(
TokenKind::Operator,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\+|-|\\*|/")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\+|-|\\*|/", ")")).unwrap() }),
),
),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenRecognizer(
TokenKind::Number,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\d+(\\.\\d+)?")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\d+(\\.\\d+)?", ")")).unwrap() }),
),
),
TokenRecognizer(TokenKind::Plus, Recognizer::StrMatch("+")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenRecognizer(
TokenKind::Number,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\d+(\\.\\d+)?")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\d+(\\.\\d+)?", ")")).unwrap() }),
),
),
TokenRecognizer(TokenKind::Plus, Recognizer::StrMatch("+")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenRecognizer(
TokenKind::Number,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\d+(\\.\\d+)?")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\d+(\\.\\d+)?", ")")).unwrap() }),
),
),
TokenRecognizer(TokenKind::Plus, Recognizer::StrMatch("+")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenRecognizer(
TokenKind::Number,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\d+(\\.\\d+)?")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\d+(\\.\\d+)?", ")")).unwrap() }),
),
),
TokenRecognizer(TokenKind::Plus, Recognizer::StrMatch("+")),
Expand Down
2 changes: 1 addition & 1 deletion rustemo-compiler/src/generator/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ impl<'g, 's> PartGenerator<'g, 's> for BasePartGenerator {
let r = r.as_ref();
parse_quote! {
TokenRecognizer(TokenKind::#token_kind, Recognizer::RegexMatch(Lazy::new(|| {
Regex::new(concat!("^", #r)).unwrap()
Regex::new(concat!("^(", #r, ")")).unwrap()
})))
}
},
Expand Down
25 changes: 14 additions & 11 deletions rustemo-compiler/src/lang/rustemo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16900,35 +16900,37 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenKind::Name,
Recognizer::RegexMatch(
Lazy::new(|| {
Regex::new(concat!("^", "[a-zA-Z_][a-zA-Z0-9_\\.]*")).unwrap()
Regex::new(concat!("^(", "[a-zA-Z_][a-zA-Z0-9_\\.]*", ")")).unwrap()
}),
),
),
TokenRecognizer(
TokenKind::RegexTerm,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "/(\\\\.|[^/\\\\])*/")).unwrap() }),
Lazy::new(|| {
Regex::new(concat!("^(", "/(\\\\.|[^/\\\\])*/", ")")).unwrap()
}),
),
),
TokenRecognizer(
TokenKind::IntConst,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\d+")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\d+", ")")).unwrap() }),
),
),
TokenRecognizer(
TokenKind::FloatConst,
Recognizer::RegexMatch(
Lazy::new(|| {
Regex::new(concat!("^", "[+-]?[0-9]+[.][0-9]*([e][+-]?[0-9]+)?"))
Regex::new(concat!("^(", "[+-]?[0-9]+[.][0-9]*([e][+-]?[0-9]+)?", ")"))
.unwrap()
}),
),
),
TokenRecognizer(
TokenKind::BoolConst,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "true|false")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "true|false", ")")).unwrap() }),
),
),
TokenRecognizer(
Expand All @@ -16937,8 +16939,9 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
Lazy::new(|| {
Regex::new(
concat!(
"^",
"(?s)(^'[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|(^\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")"
"^(",
"(?s)(^'[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|(^\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")",
")"
),
)
.unwrap()
Expand All @@ -16948,26 +16951,26 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenRecognizer(
TokenKind::Annotation,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "@[a-zA-Z0-9_]+")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "@[a-zA-Z0-9_]+", ")")).unwrap() }),
),
),
TokenRecognizer(
TokenKind::WS,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\s+")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\s+", ")")).unwrap() }),
),
),
TokenRecognizer(
TokenKind::CommentLine,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "//.*")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "//.*", ")")).unwrap() }),
),
),
TokenRecognizer(
TokenKind::NotComment,
Recognizer::RegexMatch(
Lazy::new(|| {
Regex::new(concat!("^", "((\\*[^/])|[^\\s*/]|/[^\\*])+")).unwrap()
Regex::new(concat!("^(", "((\\*[^/])|[^\\s*/]|/[^\\*])+", ")")).unwrap()
}),
),
),
Expand Down
2 changes: 1 addition & 1 deletion tests/src/output_dir/output_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ pub(crate) static RECOGNIZERS: [TokenRecognizer; TERMINAL_COUNT] = [
TokenRecognizer(
TokenKind::Num,
Recognizer::RegexMatch(
Lazy::new(|| { Regex::new(concat!("^", "\\d+")).unwrap() }),
Lazy::new(|| { Regex::new(concat!("^(", "\\d+", ")")).unwrap() }),
),
),
];
Expand Down

0 comments on commit 2de5537

Please sign in to comment.