Skip to content

Commit 6cf2bfb

Browse files
committed
Fix enumeration parsing
1 parent 0a8f2f4 commit 6cf2bfb

File tree

2 files changed

+93
-85
lines changed

2 files changed

+93
-85
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ private static void enumerationsItem(LexerlessGrammarBuilder b) {
563563
SPC, b.optional(ENUM_ITEMS), SPC, "}");
564564
b.rule(ENUM_ITEMS).is(seq(b, ENUM_ITEM, RustPunctuator.COMMA));
565565
b.rule(ENUM_ITEM).is(b.zeroOrMore(OUTER_ATTRIBUTE, SPC), b.optional(VISIBILITY, SPC),
566-
IDENTIFIER, SPC, b.optional(b.firstOf(ENUM_ITEM_TUPLE, ENUM_ITEM_STRUCT), SPC, b.optional(ENUM_ITEM_DISCRIMINANT)));
566+
IDENTIFIER, SPC, b.optional(b.firstOf(ENUM_ITEM_TUPLE, ENUM_ITEM_STRUCT, ENUM_ITEM_DISCRIMINANT)));
567567
b.rule(ENUM_ITEM_TUPLE).is("(", SPC, b.optional(TUPLE_FIELDS), SPC, ")");
568568
b.rule(ENUM_ITEM_STRUCT).is("{", SPC, b.optional(STRUCT_FIELDS), SPC, "}");
569569
b.rule(ENUM_ITEM_DISCRIMINANT).is(RustPunctuator.EQ, SPC, EXPRESSION);

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

Lines changed: 92 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -28,89 +28,97 @@
2828
public class EnumerationTest {
2929

3030

31-
@Test
32-
public void testEnumerationItem() {
33-
assertThat(RustGrammar.create().build().rule(RustGrammar.ENUM_ITEM))
34-
.matches("Dog")
35-
36-
.matches("#[allow(missing_docs)]\n" +
37-
" CubicBezier {\n" +
38-
" x1: Number,\n" +
39-
" y1: Number,\n" +
40-
" x2: Number,\n" +
41-
" y2: Number,\n" +
42-
" }")
43-
.matches("#[allow(missing_docs)]#[css(comma, function)]\n" +
44-
" CubicBezier {\n" +
45-
" x1: Number,\n" +
46-
" y1: Number,\n" +
47-
" x2: Number,\n" +
48-
" y2: Number,\n" +
49-
" }")
50-
.matches("#[allow(missing_docs)]\n" +
51-
" #[css(comma, function)]\n" +
52-
" CubicBezier {\n" +
53-
" x1: Number,\n" +
54-
" y1: Number,\n" +
55-
" x2: Number,\n" +
56-
" y2: Number,\n" +
57-
" }")
58-
59-
60-
61-
;
62-
}
63-
64-
@Test
65-
public void testEnumeration() {
66-
assertThat(RustGrammar.create().build().rule(RustGrammar.ENUMERATION))
67-
.matches("enum Empty {}")
68-
.matches("enum Animal {\n" +
69-
" Dog,\n" +
70-
" Cat,\n" +
71-
"}")
72-
.matches("enum TimingFunction<Integer, Number> {\n" +
73-
" /// `linear | ease | ease-in | ease-out | ease-in-out`\n" +
74-
" Keyword(TimingKeyword),\n" +
75-
" /// `cubic-bezier(<number>, <number>, <number>, <number>)`\n" +
76-
" #[allow(missing_docs)]\n" +
77-
" #[css(comma, function)]\n" +
78-
" CubicBezier {\n" +
79-
" x1: Number,\n" +
80-
" y1: Number,\n" +
81-
" x2: Number,\n" +
82-
" y2: Number,\n" +
83-
" },\n" +
84-
" /// `step-start | step-end | steps(<integer>, [ <step-position> ]?)`\n" +
85-
" /// `<step-position> = jump-start | jump-end | jump-none | jump-both | start | end`\n" +
86-
" #[css(comma, function)]\n" +
87-
" #[value_info(other_values = \"step-start,step-end\")]\n" +
88-
" Steps(Integer, #[css(skip_if = \"is_end\")] StepPosition),\n" +
89-
"}")
90-
91-
92-
.matches("enum TimingFunction<Integer, Number> {\n" +
93-
" /// `linear | ease | ease-in | ease-out | ease-in-out`\n" +
94-
" Keyword(TimingKeyword),\n" +
95-
" /// `cubic-bezier(<number>, <number>, <number>, <number>)`\n" +
96-
" #[allow(missing_docs)]\n" +
97-
" #[css(comma, function)]\n" +
98-
" CubicBezier {\n" +
99-
" x1: Number,\n" +
100-
" y1: Number,\n" +
101-
" x2: Number,\n" +
102-
" y2: Number,\n" +
103-
" },\n" +
104-
" /// `step-start | step-end | steps(<integer>, [ <step-position> ]?)`\n" +
105-
" /// `<step-position> = jump-start | jump-end | jump-none | jump-both | start | end`\n" +
106-
" #[css(comma, function)]\n" +
107-
" #[value_info(other_values = \"step-start,step-end\")]\n" +
108-
" Steps(Integer, #[css(skip_if = \"is_end\")] StepPosition),\n" +
109-
"}")
110-
111-
112-
;
113-
114-
}
31+
@Test
32+
public void testEnumerationItem() {
33+
assertThat(RustGrammar.create().build().rule(RustGrammar.ENUM_ITEM))
34+
.matches("Dog")
35+
36+
.matches("#[allow(missing_docs)]\n" +
37+
" CubicBezier {\n" +
38+
" x1: Number,\n" +
39+
" y1: Number,\n" +
40+
" x2: Number,\n" +
41+
" y2: Number,\n" +
42+
" }")
43+
.matches("#[allow(missing_docs)]#[css(comma, function)]\n" +
44+
" CubicBezier {\n" +
45+
" x1: Number,\n" +
46+
" y1: Number,\n" +
47+
" x2: Number,\n" +
48+
" y2: Number,\n" +
49+
" }")
50+
.matches("#[allow(missing_docs)]\n" +
51+
" #[css(comma, function)]\n" +
52+
" CubicBezier {\n" +
53+
" x1: Number,\n" +
54+
" y1: Number,\n" +
55+
" x2: Number,\n" +
56+
" y2: Number,\n" +
57+
" }")
58+
59+
60+
;
61+
}
62+
63+
@Test
64+
public void testEnumeration() {
65+
assertThat(RustGrammar.create().build().rule(RustGrammar.ENUMERATION))
66+
.matches("enum Empty {}")
67+
.matches("enum Animal {\n" +
68+
" Dog,\n" +
69+
" Cat,\n" +
70+
"}")
71+
.matches("enum TimingFunction<Integer, Number> {\n" +
72+
" /// `linear | ease | ease-in | ease-out | ease-in-out`\n" +
73+
" Keyword(TimingKeyword),\n" +
74+
" /// `cubic-bezier(<number>, <number>, <number>, <number>)`\n" +
75+
" #[allow(missing_docs)]\n" +
76+
" #[css(comma, function)]\n" +
77+
" CubicBezier {\n" +
78+
" x1: Number,\n" +
79+
" y1: Number,\n" +
80+
" x2: Number,\n" +
81+
" y2: Number,\n" +
82+
" },\n" +
83+
" /// `step-start | step-end | steps(<integer>, [ <step-position> ]?)`\n" +
84+
" /// `<step-position> = jump-start | jump-end | jump-none | jump-both | start | end`\n" +
85+
" #[css(comma, function)]\n" +
86+
" #[value_info(other_values = \"step-start,step-end\")]\n" +
87+
" Steps(Integer, #[css(skip_if = \"is_end\")] StepPosition),\n" +
88+
"}")
89+
90+
91+
.matches("enum TimingFunction<Integer, Number> {\n" +
92+
" /// `linear | ease | ease-in | ease-out | ease-in-out`\n" +
93+
" Keyword(TimingKeyword),\n" +
94+
" /// `cubic-bezier(<number>, <number>, <number>, <number>)`\n" +
95+
" #[allow(missing_docs)]\n" +
96+
" #[css(comma, function)]\n" +
97+
" CubicBezier {\n" +
98+
" x1: Number,\n" +
99+
" y1: Number,\n" +
100+
" x2: Number,\n" +
101+
" y2: Number,\n" +
102+
" },\n" +
103+
" /// `step-start | step-end | steps(<integer>, [ <step-position> ]?)`\n" +
104+
" /// `<step-position> = jump-start | jump-end | jump-none | jump-both | start | end`\n" +
105+
" #[css(comma, function)]\n" +
106+
" #[value_info(other_values = \"step-start,step-end\")]\n" +
107+
" Steps(Integer, #[css(skip_if = \"is_end\")] StepPosition),\n" +
108+
"}")
109+
110+
.matches("enum qos_class_t {\n" +
111+
" QOS_CLASS_USER_INTERACTIVE = 0x21,\n" +
112+
" QOS_CLASS_USER_INITIATED = 0x19,\n" +
113+
" QOS_CLASS_DEFAULT = 0x15,\n" +
114+
" QOS_CLASS_UTILITY = 0x11,\n" +
115+
" QOS_CLASS_BACKGROUND = 0x09,\n" +
116+
" QOS_CLASS_UNSPECIFIED = 0x00,\n" +
117+
"}")
118+
119+
120+
;
121+
122+
}
115123

116124
}

0 commit comments

Comments
 (0)