Skip to content

Commit 8667c40

Browse files
committed
fix regression in reference pattern
1 parent 884042a commit 8667c40

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ public enum RustGrammar implements GrammarRuleKey {
225225
PATH_PATTERN,
226226
PATTERN,
227227
PATTERN_NO_TOP_ALT,
228+
PATTERN_WITHOUT_RANGE,
228229
PERCENTEQ_EXPRESSION,
229230
PLUSEQ_EXPRESSION,
230231
PREDICATE_LOOP_EXPRESSION,
@@ -732,7 +733,7 @@ private static void implItem(LexerlessGrammarBuilder b) {
732733
b.rule(TRAIT_IMPL).is(
733734
b.optional(RustKeyword.KW_UNSAFE, SPC), RustKeyword.KW_IMPL, SPC,
734735
b.optional(GENERIC_PARAMS, SPC),
735-
b.optional(RustPunctuator.NOT,SPC), TYPE_PATH, SPC, RustKeyword.KW_FOR, SPC, TYPE, SPC,
736+
b.optional(RustPunctuator.NOT, SPC), TYPE_PATH, SPC, RustKeyword.KW_FOR, SPC, TYPE, SPC,
736737
b.optional(WHERE_CLAUSE, SPC), "{", SPC,
737738
b.zeroOrMore(INNER_ATTRIBUTE, SPC),
738739
b.zeroOrMore(ASSOCIATED_ITEM, SPC), "}"
@@ -833,8 +834,8 @@ private static void externcrates(LexerlessGrammarBuilder b) {
833834

834835
private static void modules(LexerlessGrammarBuilder b) {
835836
b.rule(MODULE).is(b.firstOf(
836-
b.sequence(b.optional(RustKeyword.KW_UNSAFE, SPC),RustKeyword.KW_MOD, SPC, IDENTIFIER, SPC, RustPunctuator.SEMI),
837-
b.sequence(b.optional(RustKeyword.KW_UNSAFE, SPC),RustKeyword.KW_MOD, SPC, IDENTIFIER, SPC, "{", SPC,
837+
b.sequence(b.optional(RustKeyword.KW_UNSAFE, SPC), RustKeyword.KW_MOD, SPC, IDENTIFIER, SPC, RustPunctuator.SEMI),
838+
b.sequence(b.optional(RustKeyword.KW_UNSAFE, SPC), RustKeyword.KW_MOD, SPC, IDENTIFIER, SPC, "{", SPC,
838839
b.zeroOrMore(INNER_ATTRIBUTE, SPC),
839840
b.zeroOrMore(ITEM, SPC), "}"
840841
)));
@@ -974,6 +975,10 @@ private static void patterns(LexerlessGrammarBuilder b) {
974975
);
975976
b.rule(PATTERN_NO_TOP_ALT).is(b.firstOf(
976977
RANGE_PATTERN,
978+
PATTERN_WITHOUT_RANGE
979+
));
980+
981+
b.rule(PATTERN_WITHOUT_RANGE).is(b.firstOf(
977982
TUPLE_STRUCT_PATTERN,
978983
STRUCT_PATTERN,
979984
MACRO_INVOCATION,
@@ -1004,7 +1009,10 @@ private static void patterns(LexerlessGrammarBuilder b) {
10041009
TUPLE_PATTERN,
10051010
GROUPED_PATTERN,
10061011
SLICE_PATTERN
1012+
1013+
10071014
));
1015+
10081016
b.rule(LITERAL_PATTERN).is(b.firstOf(
10091017
BOOLEAN_LITERAL,
10101018
CHAR_LITERAL,
@@ -1026,10 +1034,10 @@ private static void patterns(LexerlessGrammarBuilder b) {
10261034
b.rule(WILDCARD_PATTERN).is(RustPunctuator.UNDERSCORE);
10271035
b.rule(REST_PATTERN).is(RustPunctuator.DOTDOT);
10281036

1029-
b.rule(RANGE_PATTERN).is(b.firstOf(OBSOLETE_RANGE_PATTERN,INCLUSIVE_RANGE_PATTERN, HALF_OPEN_RANGE_PATTERN ));
1037+
b.rule(RANGE_PATTERN).is(b.firstOf(OBSOLETE_RANGE_PATTERN, INCLUSIVE_RANGE_PATTERN, HALF_OPEN_RANGE_PATTERN));
10301038
b.rule(INCLUSIVE_RANGE_PATTERN).is(b.sequence(RANGE_PATTERN_BOUND, RustPunctuator.DOTDOTEQ, RANGE_PATTERN_BOUND));
10311039
b.rule(HALF_OPEN_RANGE_PATTERN).is(b.sequence(RANGE_PATTERN_BOUND, RustPunctuator.DOTDOT));
1032-
b.rule(OBSOLETE_RANGE_PATTERN).is(b.sequence(RANGE_PATTERN_BOUND, RustPunctuator.DOTDOTDOT ,RANGE_PATTERN_BOUND));
1040+
b.rule(OBSOLETE_RANGE_PATTERN).is(b.sequence(RANGE_PATTERN_BOUND, RustPunctuator.DOTDOTDOT, RANGE_PATTERN_BOUND));
10331041
b.rule(RANGE_PATTERN_BOUND).is(b.firstOf(
10341042
CHAR_LITERAL, BYTE_LITERAL, b.sequence(b.optional("-"), INTEGER_LITERAL),
10351043
b.sequence(b.optional("-"), FLOAT_LITERAL),
@@ -1039,7 +1047,7 @@ private static void patterns(LexerlessGrammarBuilder b) {
10391047
b.rule(REFERENCE_PATTERN).is(
10401048
b.firstOf(RustPunctuator.ANDAND, RustPunctuator.AND),
10411049
b.optional(RustKeyword.KW_MUT),
1042-
PATTERN
1050+
PATTERN_WITHOUT_RANGE
10431051
);
10441052
b.rule(STRUCT_PATTERN).is(
10451053
PATH_IN_EXPRESSION, SPC, "{", SPC, b.optional(STRUCT_PATTERN_ELEMENTS), SPC, "}"
@@ -1113,7 +1121,7 @@ private static void functionpointer(LexerlessGrammarBuilder b) {
11131121
b.optional(BARE_FUNCTION_RETURN_TYPE)
11141122
);
11151123

1116-
b.rule(FUNCTION_TYPE_QUALIFIERS).is(b.optional(RustKeyword.KW_UNSAFE), b.optional(SPC,RustKeyword.KW_EXTERN, SPC, b.optional(ABI)));
1124+
b.rule(FUNCTION_TYPE_QUALIFIERS).is(b.optional(RustKeyword.KW_UNSAFE), b.optional(SPC, RustKeyword.KW_EXTERN, SPC, b.optional(ABI)));
11171125

11181126
b.rule(BARE_FUNCTION_RETURN_TYPE).is(RustPunctuator.RARROW, SPC, TYPE_NO_BOUNDS);
11191127
b.rule(FUNCTION_PARAMETERS_MAYBE_NAMED_VARIADIC).is(b.firstOf(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void testTokens() {
6868

6969
@Test
7070
public void testParsing() {
71-
String sexpr = "const foo : Self;";
71+
String sexpr = "let foo = &i|i;";
7272

7373
//Print out Ast node content for debugging purpose
7474

community-rust-frontend/src/test/java/org/sonar/rust/parser/expressions/ClosureExpressionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public void testClosureParameter() {
4242
assertThat(RustGrammar.create().build().rule(RustGrammar.CLOSURE_PARAM))
4343
.matches("k:i32")
4444
.matches("j")
45-
.matches("state: Rc<RefCell<OpState>>")
45+
.matches("state: Rc<RefCell< OpState>>")
4646
.matches("bufs : BufVec")
4747
.matches("&i")
48+
.notMatches("&i| i")
4849
;
4950
}
5051

@@ -68,7 +69,7 @@ public void testClosureExpression() {
6869
.matches("|&i|{i==NUM_MSG}")
6970
.matches("|| i == NUM_MSG")
7071
.matches("|i| i == NUM_MSG")
71-
//TODO .matches("| &i | i == NUM_MSG")
72+
.matches("|&i| i == NUM_MSG")
7273

7374
;
7475
}

community-rust-frontend/src/test/java/org/sonar/rust/parser/lexer/IdentifierTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void testNonKeywords() {
6767
.matches("_identifier")
6868
.matches("Москва")
6969
.matches("東京")
70+
.notMatches("i|")
7071
;
7172
}
7273

@@ -121,8 +122,8 @@ public void testIdentifier() {
121122
.matches("foo")
122123
.matches("_identifier")
123124
.matches("r#true")
124-
//.matches("Москва")
125-
//.matches("東京")
125+
.matches("Москва")
126+
.matches("東京")
126127

127128
;
128129

community-rust-frontend/src/test/java/org/sonar/rust/parser/patterns/PatternTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void testLitteralPattern() {
3333
.matches("42")
3434
.matches("'z'")
3535
.matches("b'c'")
36+
.notMatches("&i| i")
3637

3738

3839
;
@@ -120,6 +121,8 @@ public void testReferencePattern() {
120121
.matches("&&42")
121122
.matches("&mut 42")
122123
.matches("&[u8]")
124+
.matches("&i")
125+
.notMatches("&i| i")
123126

124127
;
125128

@@ -254,6 +257,15 @@ public void testPathPattern() {
254257

255258
}
256259

260+
@Test
261+
public void testPatternWithoutRange() {
262+
assertThat(RustGrammar.create().build().rule(RustGrammar.PATTERN_WITHOUT_RANGE))
263+
.matches("i")
264+
.matches("&i")
265+
;
266+
267+
}
268+
257269

258270
@Test
259271
public void testPatternNoTopAlt() {
@@ -315,6 +327,9 @@ public void testPatternNoTopAlt() {
315327
.matches("(b'8', [b'#'])")
316328
.matches("OK(_)")
317329
.matches("Err(true_prior)")
330+
.matches("&i")
331+
.notMatches("&i|")
332+
318333
;
319334
}
320335

0 commit comments

Comments
 (0)