Skip to content

Commit 2747808

Browse files
committed
Add potential spacing in macro rules definitions
1 parent c0227cb commit 2747808

File tree

4 files changed

+8558
-12
lines changed

4 files changed

+8558
-12
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ private static void macros(LexerlessGrammarBuilder b) {
825825
/* https://doc.rust-lang.org/reference/macros-by-example.html */
826826
private static void macrosByExample(LexerlessGrammarBuilder b) {
827827
b.rule(MACRO_RULES_DEFINITION).is(
828-
"macro_rules!", SPC, IDENTIFIER, SPC, MACRO_RULES_DEF);
828+
"macro_rules", SPC, RustPunctuator.NOT, SPC, IDENTIFIER, SPC, MACRO_RULES_DEF);
829829
b.rule(MACRO_RULES_DEF).is(b.firstOf(
830830
b.sequence("(", SPC, MACRO_RULES, SPC, ")", SPC, RustPunctuator.SEMI),
831831
b.sequence("[", SPC, MACRO_RULES, SPC, "]", SPC, RustPunctuator.SEMI),
@@ -840,10 +840,10 @@ private static void macrosByExample(LexerlessGrammarBuilder b) {
840840

841841
b.rule(MACRO_MATCH).is(b.firstOf(
842842

843-
b.sequence("$", IDENTIFIER, SPC, RustPunctuator.COLON, SPC, MACRO_FRAG_SPEC),
843+
b.sequence("$", SPC, IDENTIFIER, SPC, RustPunctuator.COLON, SPC, MACRO_FRAG_SPEC),
844844

845-
b.sequence("$", SPC, "(", SPC, b.oneOrMore(MACRO_MATCH, SPC), ")", b.optional(SPC, MACRO_REP_SEP, SPC) // MacroRepSep
846-
, b.firstOf("+", "*", "?")),
845+
b.sequence("$", SPC, "(", SPC, b.oneOrMore(MACRO_MATCH, SPC), ")", b.optional(SPC, MACRO_REP_SEP, SPC), // MacroRepSep
846+
SPC, b.firstOf("+", "*", "?")),
847847
TOKEN_MACRO,
848848
MACRO_MATCHER));
849849

@@ -996,7 +996,7 @@ private static void patterns(LexerlessGrammarBuilder b) {
996996
b.rule(STRUCT_PATTERN_ETCETERA).is(b.zeroOrMore(OUTER_ATTRIBUTE, SPC), RustPunctuator.DOTDOT);
997997

998998
b.rule(TUPLE_STRUCT_PATTERN).is(
999-
PATH_IN_EXPRESSION, "(", SPC, b.optional(RustKeyword.KW_BOX, SPC), b.optional(TUPLE_STRUCT_ITEMS), ")");
999+
PATH_IN_EXPRESSION, SPC, "(", SPC, b.optional(RustKeyword.KW_BOX, SPC), b.optional(TUPLE_STRUCT_ITEMS), ")");
10001000
b.rule(TUPLE_STRUCT_ITEMS).is(seq(b, PATTERN, RustPunctuator.COMMA));
10011001

10021002
b.rule(TUPLE_PATTERN).is("(", SPC, b.optional(TUPLE_PATTERN_ITEMS), SPC, ")");

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ public void testTokens() {
6666

6767
@Test
6868
public void testParsing() {
69-
String sexpr = "for i in 0.. {}";
69+
String sexpr = "fn into(self) -> (String, ::prisma_client_rust::PrismaValue) {\n" +
70+
" match self { SetParam :: SetId(value) => (\"id\" . to_string() , :: prisma_client_rust :: PrismaValue :: String(value)) , " +
71+
"SetParam :: SetUsername(value) => (\"username\" . to_string () , :: prisma_client_rust :: PrismaValue :: String(value)) , " +
72+
"SetParam :: SetEmail(value) => (\"email\" . to_string () , :: prisma_client_rust :: PrismaValue :: String(value)) , " +
73+
"SetParam :: SetFirstName(value) => (\"first_name\" . to_string () , :: prisma_client_rust :: PrismaValue :: String(value)) , " +
74+
"SetParam :: SetLastName(value) => (\"last_name\" . to_string () , :: prisma_client_rust :: PrismaValue :: String(value)) , " +
75+
"SetParam :: SetIsActive(value) => (\"is_active\" . to_string () , :: prisma_client_rust :: PrismaValue :: Boolean(value)) , " +
76+
"SetParam :: SetCreated(value) => (\"created\" . to_string () , :: prisma_client_rust :: PrismaValue :: DateTime(value)) , " +
77+
"SetParam :: SetUpdated(value) => (\"updated\" . to_string() , value . map(| value | :: prisma_client_rust :: PrismaValue :: DateTime(value)) . unwrap_or(:: prisma_client_rust :: PrismaValue :: Null)) , " +
78+
"SetParam :: ConnectAccounts(where_params) =>(\"accounts\" . to_string() , :: prisma_client_rust :: PrismaValue :: Object(vec ! [(\"connect\" . to_string() , :: prisma_client_rust :: PrismaValue :: List(where_params . into_iter() . map(Into :: < super :: account :: WhereParam > :: into) . map(:: prisma_client_rust :: WhereInput :: serialize) . map(:: prisma_client_rust :: SerializedWhereInput :: transform_equals) . map(| v | :: prisma_client_rust :: PrismaValue :: Object(vec ! [v])) . collect()))])) , SetParam :: DisconnectAccounts(where_params) =>(\"accounts\" . to_string() , :: prisma_client_rust :: PrismaValue :: Object(vec ! [(\"disconnect\" . to_string() , :: prisma_client_rust :: PrismaValue :: List(where_params . into_iter() . map(Into :: < super :: account :: WhereParam > :: into) . map(:: prisma_client_rust :: WhereInput :: serialize) . map(:: prisma_client_rust :: SerializedWhereInput :: transform_equals) . map(| v | :: prisma_client_rust :: PrismaValue :: Object(vec ! [v])) . collect()))])) , SetParam :: SetAccounts(where_params) => (\"accounts\" . to_string() , :: prisma_client_rust :: PrismaValue :: Object(vec ! [(\"set\" . to_string() , :: prisma_client_rust :: PrismaValue :: List(where_params . into_iter() . map(Into :: < super :: account :: WhereParam > :: into) . map(:: prisma_client_rust :: WhereInput :: serialize) . map(:: prisma_client_rust :: SerializedWhereInput :: transform_equals) . map(| v | :: prisma_client_rust :: PrismaValue :: Object(vec ! [v])) . collect()))])) }\n" +
79+
" }";
7080

7181
// Print out Ast node content for debugging purpose
7282

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ public void testMacroRules() {
254254
public void testMacroRulesDef() {
255255
assertThat(RustGrammar.create().build().rule(RustGrammar.MACRO_RULES_DEF))
256256
.matches("{($l:tt) => { bar!($l); }}")
257+
.matches("{($ l:tt) => { bar!($ l); }}")
257258
.matches("{($($name:ident($ty:ty, $to:ident, $lt:lifetime);)*) => {\n" +
258259
" $(fn $name(self, v: $ty) -> JsResult<$lt> {\n" +
259260
" self.$to(v as _)\n" +
@@ -271,6 +272,17 @@ public void testMacroRulesDef() {
271272
" }\n" +
272273
" };\n" +
273274
" );")
275+
.matches("(\n" +
276+
" { $ ($key:expr => $ value:expr),+ } => {\n" +
277+
" {\n" +
278+
" let mut m = ::std::collections::HashMap::new();\n" +
279+
" $(\n" +
280+
" m.insert($key, $value);\n" +
281+
" )+\n" +
282+
" m\n" +
283+
" }\n" +
284+
" };\n" +
285+
" );")
274286

275287
;
276288
}
@@ -331,7 +343,15 @@ public void testMacroRulesDefinition() {
331343
" (3) => { a!(4); };\n" +
332344
" (4) => { };\n" +
333345
"}")
346+
.matches("macro_rules ! a {\n" +
347+
" () => { a!(1); };\n" +
348+
" (1) => { a!(2); };\n" +
349+
" (2) => { a!(3); };\n" +
350+
" (3) => { a!(4); };\n" +
351+
" (4) => { };\n" +
352+
"}")
334353
.notMatches("quote!{k}")
354+
.matches("macro_rules! _select_user { ($ (($ ($func_arg : ident : $func_arg_ty : ty) , +) =>)? $module_name : ident { $($ field : ident $ (($ ($ filters : tt) +) $ (. $ arg : ident ($ ($ arg_params : tt) *)) *) ? $ (: $ selection_mode : ident { $ ($ selections : tt) + }) ?) + }) => { # [allow (warnings)] pub mod $ module_name { $ crate :: prisma :: user :: select ! (@ definitions ; $ ($ field $ (($ ($ filters) +) $ (. $ arg ($ ($ arg_params) *)) *) ? $ (: $ selection_mode { $ ($ selections) + }) ?) +) ; pub struct Select (Vec < :: prisma_client_rust :: Selection >) ; impl :: prisma_client_rust :: select :: SelectType for Select { type Data = Data ; type ModelData = $ crate :: prisma :: user :: Data ; fn to_selections (self) -> Vec < :: prisma_client_rust :: Selection > { self . 0 } } use super :: * ; pub fn select ($ ($ ($ func_arg : $ func_arg_ty) , +) ?) -> Select { Select ($ crate :: prisma :: user :: select ! (@ selections_to_select_params ; : select { $ ($ field $ (($ ($ filters) +) $ (. $ arg ($ ($ arg_params) *)) *) ? $ (: $ selection_mode { $ ($ selections) + }) ?) + }) . into_iter () . map (| p | p . to_selection ()) . collect ()) } } } ; ({ $ ($ field : ident $ (($ ($ filters : tt) +) $ (. $ arg : ident ($ ($ arg_params : tt) *)) *) ? $ (: $ selection_mode : ident { $ ($ selections : tt) + }) ?) + }) => { { $ crate :: prisma :: user :: select ! (@ definitions ; $ ($ field $ (($ ($ filters) +) $ (. $ arg ($ ($ arg_params) *)) *) ? $ (: $ selection_mode { $ ($ selections) + }) ?) +) ; pub struct Select (Vec < :: prisma_client_rust :: Selection >) ; impl :: prisma_client_rust :: select :: SelectType for Select { type Data = Data ; type ModelData = $ crate :: prisma :: user :: Data ; fn to_selections (self) -> Vec < :: prisma_client_rust :: Selection > { self . 0 } } Select ($ crate :: prisma :: user :: select ! (@ selections_to_select_params ; : select { $ ($ field $ (($ ($ filters) +) $ (. $ arg ($ ($ arg_params) *)) *) ? $ (: $ selection_mode { $ ($ selections) + }) ?) + }) . into_iter () . map (| p | p . to_selection ()) . collect ()) } } ; (@ definitions ; $ ($ field : ident $ (($ ($ filters : tt) +) $ (. $ arg : ident ($ ($ arg_params : tt) *)) *) ? $ (: $ selection_mode : ident { $ ($ selections : tt) + }) ?) +) => { # [allow (warnings)] enum Fields { id , username , email , first_name , last_name , is_active , created , updated , accounts } # [allow (warnings)] impl Fields { fn selections () { $ (let _ = Fields :: $ field ;) + } } # [allow (warnings)] # [derive (std :: fmt :: Debug , Clone)] pub struct Data { $ (pub $ field : $ crate :: prisma :: user :: select ! (@ field_type ; $ field $ (: $ selection_mode { $ ($ selections) + }) ?) ,) + } impl :: serde :: Serialize for Data { fn serialize < S > (& self , serializer : S) -> Result < S :: Ok , S :: Error > where S : :: serde :: Serializer , { use :: serde :: ser :: SerializeStruct ; let mut state = serializer . serialize_struct (\"Data\" , [$ (stringify ! ($ field) ,) +] . len ()) ? ; $ (state . serialize_field ($ crate :: prisma :: user :: select ! (@ field_serde_name ; $ field) , & self . $ field) ? ;) * state . end () } } impl < 'de > :: serde :: Deserialize < 'de > for Data { fn deserialize < D > (deserializer : D) -> Result < Self , D :: Error > where D : :: serde :: Deserializer < 'de > , { # [allow (warnings)] enum Field { $ ($ field) , + , } impl < 'de > :: serde :: Deserialize < 'de > for Field { fn deserialize < D > (deserializer : D) -> Result < Field , D :: Error > where D : :: serde :: Deserializer < 'de > , { struct FieldVisitor ; impl < 'de > :: serde :: de :: Visitor < 'de > for FieldVisitor { type Value = Field ; fn expecting (& self , formatter : & mut :: std :: fmt :: Formatter) -> :: std :: fmt :: Result { formatter . write_str (concat ! ($ ($ crate :: prisma :: user :: select ! (@ field_serde_name ; $ field) , \", \") , +)) } fn visit_str < E > (self , value : & str) -> Result < Field , E > where E : :: serde :: de :: Error , { match value { $ ($ crate :: prisma :: user :: select ! (@ field_serde_name ; $ field) => Ok (Field :: $ field)) , * , _ => Err (:: serde :: de :: Error :: unknown_field (value , FIELDS)) , } } } deserializer . deserialize_identifier (FieldVisitor) } } struct DataVisitor ; impl < 'de > :: serde :: de :: Visitor < 'de > for DataVisitor { type Value = Data ; fn expecting (& self , formatter : & mut std :: fmt :: Formatter) -> std :: fmt :: Result { formatter . write_str (\"struct Data\") } fn visit_map < V > (self , mut map : V) -> Result < Data , V :: Error > where V : :: serde :: de :: MapAccess < 'de > , { $ (let mut $ field = None ;) * while let Some (key) = map . next_key () ? { match key { $ (Field :: $ field => { if $ field . is_some () { return Err (:: serde :: de :: Error :: duplicate_field ($ crate :: prisma :: user :: select ! (@ field_serde_name ; $ field))) ; } $ field = Some (map . next_value () ?) ; }) * } } $ (let $ field = $ field . ok_or_else (|| serde :: de :: Error :: missing_field ($ crate :: prisma :: user :: select ! (@ field_serde_name ; $ field))) ? ;) * Ok (Data { $ ($ field) , * }) } } const FIELDS : & 'static [& 'static str] = & [\"id\" , \"username\" , \"email\" , \"first_name\" , \"last_name\" , \"is_active\" , \"created\" , \"updated\" , \"accounts\"] ; deserializer . deserialize_struct (\"Data\" , FIELDS , DataVisitor) } } $ ($ (pub mod $ field { $ crate :: prisma :: user :: select ! (@ field_module ; $ field : $ selection_mode { $ ($ selections) + }) ; }) ?) + } ; (@ field_type ; id) => { String } ; (@ field_type ; username) => { String } ; (@ field_type ; email) => { String } ; (@ field_type ; first_name) => { String } ; (@ field_type ; last_name) => { String } ; (@ field_type ; is_active) => { bool } ; (@ field_type ; created) => { :: prisma_client_rust :: chrono :: DateTime < :: prisma_client_rust :: chrono :: FixedOffset , > } ; (@ field_type ; updated) => { Option < :: prisma_client_rust :: chrono :: DateTime < :: prisma_client_rust :: chrono :: FixedOffset , > > } ; (@ field_type ; accounts : $ selection_mode : ident { $ ($ selections : tt) + }) => { Vec < accounts :: Data > } ; (@ field_type ; accounts) => { Vec < crate :: prisma :: account :: Data > } ; (@ field_type ; $ field : ident $ ($ tokens : tt) *) => { compile_error ! (stringify ! (Cannot select field nonexistent field $ field on model \"User\" , available fields are \"id, username, email, first_name, last_name, is_active, created, updated, accounts\")) } ; (@ field_module ; accounts : $ selection_mode : ident { $ ($ selections : tt) + }) => { $ crate :: prisma :: account :: select ! (@ definitions ; $ ($ selections) +) ; } ; (@ field_module ; $ ($ tokens : tt) *) => { } ; (@ selection_field_to_selection_param ; id) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: id :: Select) } ; (@ selection_field_to_selection_param ; username) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: username :: Select) } ; (@ selection_field_to_selection_param ; email) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: email :: Select) } ; (@ selection_field_to_selection_param ; first_name) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: first_name :: Select) } ; (@ selection_field_to_selection_param ; last_name) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: last_name :: Select) } ; (@ selection_field_to_selection_param ; is_active) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: is_active :: Select) } ; (@ selection_field_to_selection_param ; created) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: created :: Select) } ; (@ selection_field_to_selection_param ; updated) => { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: updated :: Select) } ; (@ selection_field_to_selection_param ; accounts $ (($ ($ filters : tt) +) $ (. $ arg : ident ($ ($ arg_params : tt) *)) *) ? : $ selection_mode : ident { $ ($ selections : tt) + }) => { { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: accounts :: Select :: $ selection_mode ($ crate :: prisma :: account :: ManyArgs :: new ($ crate :: prisma :: account :: select ! (@ filters_to_args ; $ ($ ($ filters) +) ?)) $ ($ (. $ arg ($ ($ arg_params) *)) *) ? , $ crate :: prisma :: account :: select ! (@ selections_to_select_params ; : $ selection_mode { $ ($ selections) + }) . into_iter () . collect ())) } } ; (@ selection_field_to_selection_param ; accounts $ (($ ($ filters : tt) +) $ (. $ arg : ident ($ ($ arg_params : tt) *)) *) ?) => { { Into :: < $ crate :: prisma :: user :: SelectParam > :: into ($ crate :: prisma :: user :: accounts :: Select :: Fetch ($ crate :: prisma :: account :: ManyArgs :: new ($ crate :: prisma :: account :: select ! (@ filters_to_args ; $ ($ ($ filters) +) ?)) $ ($ (. $ arg ($ ($ arg_params) *)) *) ?) ,) } } ; (@ selection_field_to_selection_param ; $ ($ tokens : tt) *) => { compile_error ! (stringify ! ($ ($ tokens) *)) } ; (@ selections_to_select_params ; : $ macro_name : ident { $ ($ field : ident $ (($ ($ filters : tt) +) $ (. $ arg : ident ($ ($ arg_params : tt) *)) *) ? $ (: $ selection_mode : ident { $ ($ selections : tt) + }) ?) + }) => { [$ ($ crate :: prisma :: user :: $ macro_name ! (@ selection_field_to_selection_param ; $ field $ (($ ($ filters) +) $ (. $ arg ($ ($ arg_params) *)) *) ? $ (: $ selection_mode { $ ($ selections) + }) ?) ,) +] } ; (@ filters_to_args ;) => { vec ! [] } ; (@ filters_to_args ; $ ($ t : tt) *) => { $ ($ t) * } ; (@ field_serde_name ; id) => { \"id\" } ; (@ field_serde_name ; username) => { \"username\" } ; (@ field_serde_name ; email) => { \"email\" } ; (@ field_serde_name ; first_name) => { \"first_name\" } ; (@ field_serde_name ; last_name) => { \"last_name\" } ; (@ field_serde_name ; is_active) => { \"is_active\" } ; (@ field_serde_name ; created) => { \"created\" } ; (@ field_serde_name ; updated) => { \"updated\" } ; (@ field_serde_name ; accounts) => { \"accounts\" } ; }")
335355

336356
;
337357
}

0 commit comments

Comments
 (0)