Skip to content

Commit 3eba61f

Browse files
committed
Fix issue with self in lexer
1 parent 4eb9eba commit 3eba61f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

community-rust-frontend/src/main/java/org/sonar/rust/RustGrammar.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,8 @@ public static void lexicalpath(LexerlessGrammarBuilder b) {
18521852
b.zeroOrMore(b.sequence(SPC, RustPunctuator.PATHSEP, SPC, SIMPLE_PATH_SEGMENT)));
18531853
b.rule(SIMPLE_PATH_SEGMENT).is(b.firstOf(
18541854
b.sequence(RustKeyword.KW_SUPER, b.nextNot(IDENTIFIER)),
1855-
RustKeyword.KW_SELF_VALUE, b.regexp("^crate$"), b.regexp(DOLLAR_CRATE_REGEX), IDENTIFIER));
1855+
b.sequence(RustKeyword.KW_SELF_VALUE, b.nextNot(b.regexp("[a-zA-Z0-9_]"))),
1856+
b.regexp("^crate$"), b.regexp(DOLLAR_CRATE_REGEX), IDENTIFIER));
18561857

18571858
b.rule(PATH_IN_EXPRESSION).is(
18581859
b.optional(SPC, RustPunctuator.PATHSEP, SPC),

community-rust-frontend/src/test/java/org/sonar/rust/RustLexerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,23 @@ void testParsing() {
8787
System.out.println(AstXmlPrinter.print(astNode));
8888

8989
}
90+
91+
@Test
92+
void testSelfInsideImportTokens() {
93+
testImportParsing("selfish");
94+
testImportParsing("self_api");
95+
}
96+
97+
private void testImportParsing(String filename) {
98+
String sexpr = "\n" +
99+
"use api::" + filename + "::MyService; \n" +
100+
" fn update_rates(){" +
101+
" todo!()\n" +
102+
" }";
103+
104+
ParserAdapter<LexerlessGrammar> parser = new ParserAdapter<>(
105+
StandardCharsets.UTF_8, RustGrammar.create().build());
106+
107+
parser.parse(sexpr);
108+
}
90109
}

0 commit comments

Comments
 (0)