Fix grammar for allowing white space to appear everywher

This commit is contained in:
Uy Ha 2021-06-29 20:35:34 +02:00
parent e3dfc2cdca
commit b8c45d2a2b
8 changed files with 15925 additions and 9343 deletions

View file

@ -41,7 +41,8 @@ Line comment [comment]
Message with Line comment [comment] Message with Line comment [comment]
=================================== ===================================
message(STATUS #Some line comment message(STATUS #Some line comment
message #Some other line comment
Second #Some other line comment
) )
--- ---

View file

@ -1,7 +1,7 @@
==================== ====================
Empty if [condition] Empty if [condition]
==================== ====================
if(cond) if ( cond )
endif() endif()
--- ---

View file

@ -90,6 +90,7 @@ endforeach()
(foreach_command (foreach_command
(foreach) (foreach)
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument))
) )
(endforeach_command (endforeach)) (endforeach_command (endforeach))
) )
@ -109,6 +110,7 @@ endforeach()
(foreach_command (foreach_command
(foreach) (foreach)
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument))
) )
(endforeach_command (endforeach)) (endforeach_command (endforeach))
) )

View file

@ -75,3 +75,19 @@ message(STATUS
(argument (bracket_argument)) (argument (bracket_argument))
) )
) )
======================================================
Message with STATUS and an unquoted argument [message]
======================================================
message(STATUS argument)
---
(source_file
(normal_command
(identifier)
(argument (unquoted_argument))
(argument (unquoted_argument))
)
)

View file

@ -25,6 +25,7 @@ message(STATUS Hello)
(normal_command (normal_command
(identifier) (identifier)
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument))
) )
) )

View file

@ -17,10 +17,10 @@ module.exports = grammar({
name: "cmake", name: "cmake",
externals: ($) => [$.bracket_argument, $.bracket_comment, $.line_comment], externals: ($) => [$.bracket_argument, $.bracket_comment, $.line_comment],
extras: ($) => [/[\s\n\r]/, $.bracket_comment, $.line_comment], extras: ($) => [$.bracket_comment, $.line_comment],
rules: { rules: {
source_file: ($) => repeat($._command_invocation), source_file: ($) => repeat($._untrimmed_command_invocation),
escape_sequence: ($) => choice($._escape_identity, $._escape_encoded, $._escape_semicolon), escape_sequence: ($) => choice($._escape_identity, $._escape_encoded, $._escape_semicolon),
_escape_identity: (_) => /\\[^A-Za-z0-9;]/, _escape_identity: (_) => /\\[^A-Za-z0-9;]/,
@ -34,39 +34,45 @@ module.exports = grammar({
cache_var: ($) => seq("$CACHE", "{", $.variable, "}"), cache_var: ($) => seq("$CACHE", "{", $.variable, "}"),
argument: ($) => choice($.bracket_argument, $.quoted_argument, $.unquoted_argument), argument: ($) => choice($.bracket_argument, $.quoted_argument, $.unquoted_argument),
_untrimmed_argument: ($) => choice(/\s/, $.argument),
quoted_argument: ($) => seq('"', optional($.quoted_element), '"'), quoted_argument: ($) => seq('"', optional($.quoted_element), '"'),
quoted_element: ($) => repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence)), quoted_element: ($) => repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence)),
unquoted_argument: ($) => prec.right(repeat1(choice($.variable_ref, /[^\s\n\r()#\"\\]/, $.escape_sequence))), unquoted_argument: ($) => prec.right(repeat1(choice($.variable_ref, /[^\s()#\"\\]/, $.escape_sequence))),
if_command: ($) => command($.if, repeat(choice($.argument))), if_command: ($) => command($.if, repeat($._untrimmed_argument)),
elseif_command: ($) => command($.elseif, repeat(choice($.argument))), elseif_command: ($) => command($.elseif, repeat($._untrimmed_argument)),
else_command: ($) => command($.else, optional(choice($.argument))), else_command: ($) => command($.else, optional(seq(/\s*/, $.argument, /\s*/))),
endif_command: ($) => command($.endif, optional(choice($.argument))), endif_command: ($) => command($.endif, optional(seq(/\s*/, $.argument, /\s*/))),
if_condition: ($) => if_condition: ($) =>
seq($.if_command, repeat(choice($._command_invocation, $.elseif_command, $.else_command)), $.endif_command), seq(
$.if_command,
repeat(choice($._untrimmed_command_invocation, $.elseif_command, $.else_command)),
$.endif_command
),
foreach_command: ($) => command($.foreach, repeat(choice($.argument))), foreach_command: ($) => command($.foreach, repeat($._untrimmed_argument)),
endforeach_command: ($) => command($.endforeach, optional($.argument)), endforeach_command: ($) => command($.endforeach, optional($.argument)),
foreach_loop: ($) => seq($.foreach_command, repeat($._command_invocation), $.endforeach_command), foreach_loop: ($) => seq($.foreach_command, repeat($._untrimmed_command_invocation), $.endforeach_command),
while_command: ($) => command($.while, repeat(choice($.argument))), while_command: ($) => command($.while, repeat($._untrimmed_argument)),
endwhile_command: ($) => command($.endwhile, optional(choice($.argument))), endwhile_command: ($) => command($.endwhile, optional(seq(/\s*/, $.argument, /\s*/))),
while_loop: ($) => seq($.while_command, repeat($._command_invocation), $.endwhile_command), while_loop: ($) => seq($.while_command, repeat($._untrimmed_command_invocation), $.endwhile_command),
function_command: ($) => command($.function, repeat($.argument)), function_command: ($) => command($.function, repeat($._untrimmed_argument)),
endfunction_command: ($) => command($.endfunction, repeat($.argument)), endfunction_command: ($) => command($.endfunction, repeat($._untrimmed_argument)),
function_def: ($) => seq($.function_command, repeat($._command_invocation), $.endfunction_command), function_def: ($) => seq($.function_command, repeat($._untrimmed_command_invocation), $.endfunction_command),
macro_command: ($) => command($.macro, repeat($.argument)), macro_command: ($) => command($.macro, repeat($._untrimmed_argument)),
endmacro_command: ($) => command($.endmacro, repeat($.argument)), endmacro_command: ($) => command($.endmacro, repeat($._untrimmed_argument)),
macro_def: ($) => seq($.macro_command, repeat($._command_invocation), $.endmacro_command), macro_def: ($) => seq($.macro_command, repeat($._untrimmed_command_invocation), $.endmacro_command),
normal_command: ($) => command($.identifier, repeat($.argument)), normal_command: ($) => command($.identifier, repeat($._untrimmed_argument)),
_command_invocation: ($) => _command_invocation: ($) =>
choice($.normal_command, $.if_condition, $.foreach_loop, $.while_loop, $.function_def, $.macro_def), choice($.normal_command, $.if_condition, $.foreach_loop, $.while_loop, $.function_def, $.macro_def),
_untrimmed_command_invocation: ($) => choice(/\s/, $._command_invocation),
...commandNames(...commands), ...commandNames(...commands),
identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/, identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/,
@ -87,5 +93,5 @@ function commandNames(...names) {
} }
function command(name_rule, arg_rule) { function command(name_rule, arg_rule) {
return seq(name_rule, "(", arg_rule, ")"); return seq(name_rule, repeat(/[\t ]/), "(", arg_rule, ")");
} }

View file

@ -5,7 +5,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_command_invocation" "name": "_untrimmed_command_invocation"
} }
}, },
"escape_sequence": { "escape_sequence": {
@ -163,6 +163,19 @@
} }
] ]
}, },
"_untrimmed_argument": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "\\s"
},
{
"type": "SYMBOL",
"name": "argument"
}
]
},
"quoted_argument": { "quoted_argument": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -222,7 +235,7 @@
}, },
{ {
"type": "PATTERN", "type": "PATTERN",
"value": "[^\\s\\n\\r()#\\\"\\\\]" "value": "[^\\s()#\\\"\\\\]"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -239,6 +252,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "if" "name": "if"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -246,13 +266,8 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
}
]
} }
}, },
{ {
@ -268,6 +283,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "elseif" "name": "elseif"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -275,13 +297,8 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
}
]
} }
}, },
{ {
@ -297,6 +314,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "else" "name": "else"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -305,11 +329,19 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{
"type": "PATTERN",
"value": "\\s*"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "argument"
},
{
"type": "PATTERN",
"value": "\\s*"
} }
] ]
}, },
@ -331,6 +363,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "endif" "name": "endif"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -339,11 +378,19 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{
"type": "PATTERN",
"value": "\\s*"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "argument"
},
{
"type": "PATTERN",
"value": "\\s*"
} }
] ]
}, },
@ -372,7 +419,7 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_command_invocation" "name": "_untrimmed_command_invocation"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -398,6 +445,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "foreach" "name": "foreach"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -405,13 +459,8 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
}
]
} }
}, },
{ {
@ -427,6 +476,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "endforeach" "name": "endforeach"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -460,7 +516,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_command_invocation" "name": "_untrimmed_command_invocation"
} }
}, },
{ {
@ -476,6 +532,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "while" "name": "while"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -483,13 +546,8 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
}
]
} }
}, },
{ {
@ -505,6 +563,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "endwhile" "name": "endwhile"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -513,11 +578,19 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{
"type": "PATTERN",
"value": "\\s*"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "argument"
},
{
"type": "PATTERN",
"value": "\\s*"
} }
] ]
}, },
@ -543,7 +616,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_command_invocation" "name": "_untrimmed_command_invocation"
} }
}, },
{ {
@ -559,6 +632,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "function" "name": "function"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -567,7 +647,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
} }
}, },
{ {
@ -583,6 +663,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "endfunction" "name": "endfunction"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -591,7 +678,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
} }
}, },
{ {
@ -611,7 +698,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_command_invocation" "name": "_untrimmed_command_invocation"
} }
}, },
{ {
@ -627,6 +714,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "macro" "name": "macro"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -635,7 +729,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
} }
}, },
{ {
@ -651,6 +745,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "endmacro" "name": "endmacro"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -659,7 +760,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
} }
}, },
{ {
@ -679,7 +780,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_command_invocation" "name": "_untrimmed_command_invocation"
} }
}, },
{ {
@ -695,6 +796,13 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "identifier"
}, },
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -703,7 +811,7 @@
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "argument" "name": "_untrimmed_argument"
} }
}, },
{ {
@ -741,6 +849,19 @@
} }
] ]
}, },
"_untrimmed_command_invocation": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "\\s"
},
{
"type": "SYMBOL",
"name": "_command_invocation"
}
]
},
"if": { "if": {
"type": "PATTERN", "type": "PATTERN",
"value": "[iI][fF]" "value": "[iI][fF]"
@ -799,10 +920,6 @@
} }
}, },
"extras": [ "extras": [
{
"type": "PATTERN",
"value": "[\\s\\n\\r]"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "bracket_comment" "name": "bracket_comment"

24985
src/parser.c

File diff suppressed because it is too large Load diff