Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
409 changes: 185 additions & 224 deletions community-rust-frontend/src/main/java/org/sonar/rust/RustGrammar.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ public void testTokens() {

@Test
public void testParsing() {


String sexpr = "enum Test {}";


String sexpr = "const foo : Self;";

//Print out Ast node content for debugging purpose

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ public void matchingEmpty() {
RustGrammar.COMPILATION_UNIT,
RustGrammar.EOF,
RustGrammar.FUNCTION_QUALIFIERS,
RustGrammar.FUNCTION_TYPE_QUALIFIERS,
RustGrammar.LIFETIME_BOUNDS,
RustGrammar.LIFETIME_PARAMS,
RustGrammar.SPC,
RustGrammar.TYPE_PARAMS
RustGrammar.SPC
));

for (RustGrammar r : rustGrammars) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
* Copyright (C) 2021 Eric Le Goff
* mailto:community-rust AT pm DOT me
* http://github.com/elegoff/sonar-rust
*
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Expand All @@ -35,7 +35,6 @@ public void testAttribute() {
.matches("foo_bar")
.matches("foo_type")
.matches("crate_type")

;
}

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

Expand All @@ -56,6 +56,42 @@ public void testOuterAttribute() {
.matches("#[inline]")
.matches("#[allow(unrooted_must_root)]")
.matches("#[cfg(not(any(target_os = \"macos\", windows)))]")
.matches("#[allow(non_camel_case_types)]")
;
}

@Test
public void testMetaWord() {
assertThat(RustGrammar.create().build().rule(RustGrammar.META_WORD))
.matches("no_std")
;
}

@Test
public void testMetaNameValueStr() {
assertThat(RustGrammar.create().build().rule(RustGrammar.META_NAME_VALUE_STR))
.matches("doc = \"example\"")
;
}

@Test
public void testMetaListPaths() {
assertThat(RustGrammar.create().build().rule(RustGrammar.META_LIST_PATHS))
.matches("allow(unused, clippy::inline_always)")
;
}

@Test
public void testMetaListIdents() {
assertThat(RustGrammar.create().build().rule(RustGrammar.META_LIST_IDENTS))
.matches("macro_use(foo, bar)")
;
}

@Test
public void testMetaListNameValueStr() {
assertThat(RustGrammar.create().build().rule(RustGrammar.META_LIST_NAME_VALUE_STR))
.matches("link(name = \"CoreFoundation\", kind = \"framework\")")
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void testCallParams() {
" node,\n" +
" media_element,\n" +
" }")
.matches("|| 42")
.matches("|i| 42")



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public void testClosureParameters() {
;
}

@Test
public void testClosureParameter() {
assertThat(RustGrammar.create().build().rule(RustGrammar.CLOSURE_PARAM))
.matches("k:i32")
.matches("j")
.matches("state: Rc<RefCell<OpState>>")
.matches("bufs : BufVec")
.matches("&i")
;
}

@Test
public void testClosureExpression() {
assertThat(RustGrammar.create().build().rule(RustGrammar.CLOSURE_EXPRESSION))
Expand All @@ -54,6 +65,10 @@ public void testClosureExpression() {
" a\n" +
" .boxed_local()\n" +
" }")
.matches("|&i|{i==NUM_MSG}")
.matches("|| i == NUM_MSG")
.matches("|i| i == NUM_MSG")
//TODO .matches("| &i | i == NUM_MSG")

;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
public class ExpressionTest {

@Test
public void testExpressionExceptStruct() {
assertThat(RustGrammar.create().build().rule(RustGrammar.EXPRESSION_EXCEPT_STRUCT))
public void testScrutinee() {
assertThat(RustGrammar.create().build().rule(RustGrammar.SCRUTINEE))
.matches("a")
.matches("a || b")
.matches("a() || b")
Expand Down Expand Up @@ -59,6 +59,8 @@ public void testExpressionExceptStruct() {
" }")
.matches("if_ok")
.matches("match_ok")
.matches("next.iter().all(|i| i == 42)")
.matches("i == NUM_MSG")

;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void testGroupedExpression() {
.matches("( 1 + 1 )")
.matches("(foo)")
.matches("(1 +(2+3))")
.matches("( #![crate_type = \"lib\"] 40+2 )")
.matches("(state.get_error_class_fn)")
.matches("(state.borrow().get_error_class_fn)")
.matches("(disk_byte as char)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ public void tesIfExpression() {
" }\n" +
" _ => false,\n" +
" } {}")
.matches("if next.iter().all(|i| i == 42) {\n" +
" \n" +
" break;\n" +
" }")
/*TODO .matches("if next.iter().all(|&i| i == 42) {\n" +
" \n" +
" break;\n" +
" }")

*/


;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ public void testLoopExpression() {
" println!(\"Irrefutable patterns are always true\");\n" +
" break;\n" +
"}")

.matches("while let Some(v @ 1) | Some(v @ 2) = vals.pop() {\n" +
" // Prints 2, 2, then 1\n" +
" println!(\"{}\", v);\n" +
"}")


.matches("for text in v {\n" +
" println!(\"I like {}.\", text);\n" +
"}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ public void tesMatchArmGuard() {
;
}

@Test
public void tesMatchArmPattern() {
assertThat(RustGrammar.create().build().rule(RustGrammar.MATCH_ARM_PATTERNS))
.matches("42")
.matches("foo")
.matches("S(z @ 1, _)")
.matches("(\"Bacon\", b)")


;
}

@Test
public void tesMatchArm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public void testStructExprTuple() {
assertThat(RustGrammar.create().build().rule(RustGrammar.STRUCT_EXPR_TUPLE))
.matches("Vec::<u8>::with_capacity()")
.matches("collect::<Vec<_>>()")
.matches("some_fn::<Cookie>(#![inner])")
.matches("some_fn::<Cookie>()")
.matches("some_fn::<Cookie>(Cookie)")
.matches("some_fn::<Cookie>(#![inner] Cookie)")
;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void testTupleElement() {

@Test
public void testTupleIndexingExpression() {
// assertThat(RustGrammar.create().build().rule(RustGrammar.TUPLE_INDEXING_EXPRESSION))
assertThat(RustGrammar.create().build().rule(RustGrammar.EXPRESSION))
.matches("point.1")
.matches("self.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void testConstant() {
.matches("const STRING: &'static str = \"bitstring\";")
.matches("const WHITE: Color = Color(255, 255, 255);")
.matches("const MAX: Self;")
.matches("const EMPTY: Vec<i32> = Vec::new();")
;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,6 @@ public void testConstParam(){
;
}

@Test
public void testTypeParams(){
assertThat(RustGrammar.create().build().rule(RustGrammar.TYPE_PARAMS))
.matches("")
.matches("AAA")
.matches("AAA,BBB,CCC")
.matches("#[test] AAA")
.matches("#[test] AAA, BBB")
.matches("#[test] AAA : i32, BBB : f64, CCC : bool")
.matches("T")
;
}

@Test
public void testLifetimeParam(){
Expand All @@ -220,28 +208,9 @@ public void testLifetimeParam(){
.notMatches("<'as")
.notMatches("<'as>")
.notMatches("'trait>")
.matches("#[outer]'ABC")
;
}

@Test
public void testLifetimeParams(){
assertThat(RustGrammar.create().build().rule(RustGrammar.LIFETIME_PARAMS))
.matches("'a")
.matches("'a,'b")
.matches("'de")
.matches("'ABC")
.matches("'ABC , 'DEF")
.notMatches("'trait")
.notMatches("'as")
.notMatches("'as>")
.notMatches("<'as")
.notMatches("<'as>")
.matches("#[outer]'ABC")
;
}


@Test
public void testLifetimeBounds(){
assertThat(RustGrammar.create().build().rule(RustGrammar.LIFETIME_BOUNDS))
Expand Down
Loading