Skip to content

Commit ea4d242

Browse files
authored
update grammar to latest reference changes (#34)
* meta attributes * Remove enum variant expression * Array expression * Closure expression and patterns * Scrutinee and if let expressions * Match expression * WIP XID_Start * wip identifiers * identifiers * functions * generic params * implementations * range patterns * bare function type * unsafe modules * add a couple of tests * closure parameter * remove dead code
1 parent 94d9f00 commit ea4d242

22 files changed

+426
-439
lines changed

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

Lines changed: 185 additions & 224 deletions
Large diffs are not rendered by default.

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

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

6969
@Test
7070
public void testParsing() {
71-
72-
73-
String sexpr = "enum Test {}";
74-
75-
71+
String sexpr = "const foo : Self;";
7672

7773
//Print out Ast node content for debugging purpose
7874

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ public void matchingEmpty() {
4141
RustGrammar.COMPILATION_UNIT,
4242
RustGrammar.EOF,
4343
RustGrammar.FUNCTION_QUALIFIERS,
44+
RustGrammar.FUNCTION_TYPE_QUALIFIERS,
4445
RustGrammar.LIFETIME_BOUNDS,
45-
RustGrammar.LIFETIME_PARAMS,
46-
RustGrammar.SPC,
47-
RustGrammar.TYPE_PARAMS
46+
RustGrammar.SPC
4847
));
4948

5049
for (RustGrammar r : rustGrammars) {

community-rust-frontend/src/test/java/org/sonar/rust/parser/attributes/AttributeTest.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
* Copyright (C) 2021 Eric Le Goff
44
* mailto:community-rust AT pm DOT me
55
* http://github.com/elegoff/sonar-rust
6-
*
6+
* <p>
77
* This program is free software; you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public
99
* License as published by the Free Software Foundation; either
1010
* version 3 of the License, or (at your option) any later version.
11-
*
11+
* <p>
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1515
* Lesser General Public License for more details.
16-
*
16+
* <p>
1717
* You should have received a copy of the GNU Lesser General Public License
1818
* along with this program; if not, write to the Free Software Foundation,
1919
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@@ -35,7 +35,6 @@ public void testAttribute() {
3535
.matches("foo_bar")
3636
.matches("foo_type")
3737
.matches("crate_type")
38-
3938
;
4039
}
4140

@@ -45,6 +44,7 @@ public void testInnerAttribute() {
4544
assertThat(RustGrammar.create().build().rule(RustGrammar.INNER_ATTRIBUTE))
4645
.matches("#![crate_type = \"lib\"]")
4746
.matches("#![feature(const_fn_fn_ptr_basics)]")
47+
.matches("#![allow(unused_variables)]")
4848
;
4949
}
5050

@@ -56,6 +56,42 @@ public void testOuterAttribute() {
5656
.matches("#[inline]")
5757
.matches("#[allow(unrooted_must_root)]")
5858
.matches("#[cfg(not(any(target_os = \"macos\", windows)))]")
59+
.matches("#[allow(non_camel_case_types)]")
60+
;
61+
}
62+
63+
@Test
64+
public void testMetaWord() {
65+
assertThat(RustGrammar.create().build().rule(RustGrammar.META_WORD))
66+
.matches("no_std")
67+
;
68+
}
69+
70+
@Test
71+
public void testMetaNameValueStr() {
72+
assertThat(RustGrammar.create().build().rule(RustGrammar.META_NAME_VALUE_STR))
73+
.matches("doc = \"example\"")
74+
;
75+
}
76+
77+
@Test
78+
public void testMetaListPaths() {
79+
assertThat(RustGrammar.create().build().rule(RustGrammar.META_LIST_PATHS))
80+
.matches("allow(unused, clippy::inline_always)")
81+
;
82+
}
83+
84+
@Test
85+
public void testMetaListIdents() {
86+
assertThat(RustGrammar.create().build().rule(RustGrammar.META_LIST_IDENTS))
87+
.matches("macro_use(foo, bar)")
88+
;
89+
}
90+
91+
@Test
92+
public void testMetaListNameValueStr() {
93+
assertThat(RustGrammar.create().build().rule(RustGrammar.META_LIST_NAME_VALUE_STR))
94+
.matches("link(name = \"CoreFoundation\", kind = \"framework\")")
5995
;
6096
}
6197
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public void testCallParams() {
4747
" node,\n" +
4848
" media_element,\n" +
4949
" }")
50+
.matches("|| 42")
51+
.matches("|i| 42")
5052

5153

5254

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public void testClosureParameters() {
3737
;
3838
}
3939

40+
@Test
41+
public void testClosureParameter() {
42+
assertThat(RustGrammar.create().build().rule(RustGrammar.CLOSURE_PARAM))
43+
.matches("k:i32")
44+
.matches("j")
45+
.matches("state: Rc<RefCell<OpState>>")
46+
.matches("bufs : BufVec")
47+
.matches("&i")
48+
;
49+
}
50+
4051
@Test
4152
public void testClosureExpression() {
4253
assertThat(RustGrammar.create().build().rule(RustGrammar.CLOSURE_EXPRESSION))
@@ -54,6 +65,10 @@ public void testClosureExpression() {
5465
" a\n" +
5566
" .boxed_local()\n" +
5667
" }")
68+
.matches("|&i|{i==NUM_MSG}")
69+
.matches("|| i == NUM_MSG")
70+
.matches("|i| i == NUM_MSG")
71+
//TODO .matches("| &i | i == NUM_MSG")
5772

5873
;
5974
}

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

Lines changed: 0 additions & 84 deletions
This file was deleted.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
public class ExpressionTest {
2929

3030
@Test
31-
public void testExpressionExceptStruct() {
32-
assertThat(RustGrammar.create().build().rule(RustGrammar.EXPRESSION_EXCEPT_STRUCT))
31+
public void testScrutinee() {
32+
assertThat(RustGrammar.create().build().rule(RustGrammar.SCRUTINEE))
3333
.matches("a")
3434
.matches("a || b")
3535
.matches("a() || b")
@@ -59,6 +59,8 @@ public void testExpressionExceptStruct() {
5959
" }")
6060
.matches("if_ok")
6161
.matches("match_ok")
62+
.matches("next.iter().all(|i| i == 42)")
63+
.matches("i == NUM_MSG")
6264

6365
;
6466
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public void testGroupedExpression() {
3434
.matches("( 1 + 1 )")
3535
.matches("(foo)")
3636
.matches("(1 +(2+3))")
37-
.matches("( #![crate_type = \"lib\"] 40+2 )")
3837
.matches("(state.get_error_class_fn)")
3938
.matches("(state.borrow().get_error_class_fn)")
4039
.matches("(disk_byte as char)")

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ public void tesIfExpression() {
126126
" }\n" +
127127
" _ => false,\n" +
128128
" } {}")
129+
.matches("if next.iter().all(|i| i == 42) {\n" +
130+
" \n" +
131+
" break;\n" +
132+
" }")
133+
/*TODO .matches("if next.iter().all(|&i| i == 42) {\n" +
134+
" \n" +
135+
" break;\n" +
136+
" }")
137+
138+
*/
129139

130140

131141
;

0 commit comments

Comments
 (0)