Skip to content

Commit a3c4639

Browse files
committed
Fix lexical boundary for compiler directive names
Turns out that numbers and underscores are considered part of the directive name.
1 parent f56f855 commit a3c4639

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Lexing of conditional directive expressions containing compiler directives, comments, or strings.
13+
- Lexing of compiler directives similar to conditional directives (e.g. `{$if_}`).
1314

1415
## [0.3.0] - 2024-05-29
1516

core/src/defaults/lexer.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,14 @@ fn conditional_directive_type(
998998
input: &str,
999999
offset: usize,
10001000
) -> (usize, Option<ConditionalDirectiveKind>) {
1001-
let end_offset = offset + count_matching(input, offset, |b| b.is_ascii_alphabetic());
1001+
let end_offset = offset
1002+
+ count_matching(
1003+
input,
1004+
offset,
1005+
// Compiler directive names are ASCII only.
1006+
// They can't start with '_' or a number, but there's no harm in including them.
1007+
|b| matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'_'),
1008+
);
10021009

10031010
let directive = &input[offset..end_offset];
10041011

@@ -1509,11 +1516,21 @@ mod tests {
15091516
#[test]
15101517
fn lex_compiler_directives() {
15111518
run_test(
1512-
"(*$message*) {$foo *) } (*$bar aa {}*)",
1519+
"
1520+
(*$message*)
1521+
{$foo *) }
1522+
(*$bar aa {}*)
1523+
{$if1}
1524+
{$if9}
1525+
{$if_}
1526+
",
15131527
&[
15141528
("(*$message*)", TT::CompilerDirective),
15151529
("{$foo *) }", TT::CompilerDirective),
15161530
("(*$bar aa {}*)", TT::CompilerDirective),
1531+
("{$if1}", TT::CompilerDirective),
1532+
("{$if9}", TT::CompilerDirective),
1533+
("{$if_}", TT::CompilerDirective),
15171534
],
15181535
);
15191536
[

0 commit comments

Comments
 (0)