Simplify the grammar using extras and add bracket comment

This commit is contained in:
Uy Ha 2021-06-26 17:54:04 +02:00
parent 0988d2ce86
commit 3b2b47dbaa
6 changed files with 16071 additions and 16924 deletions

26
corpus/comment.txt Normal file
View file

@ -0,0 +1,26 @@
=========================
Bracket comment [comment]
=========================
#[[Some comment]]
---
(source_file
(comment (bracket_argument))
)
==========================================================
Command invocation with embedded bracket comment [comment]
==========================================================
message(STATUS #[[Some comment]] "comment is next" #[[Some comment]])
---
(source_file
(message_command
(message)
(comment (bracket_argument))
(argument (quoted_argument (quoted_element)))
(comment (bracket_argument))
)
)

View file

@ -56,3 +56,20 @@ message(STATUS [=[Some argument ]==] ]=] )
(argument (bracket_argument)) (argument (bracket_argument))
) )
) )
=============================================================
Message with STATUS and bracket argument and newline [message]
=============================================================
message(STATUS
[=[Some argument
]==] ]=] )
---
(source_file
(message_command
(message)
(argument (bracket_argument))
)
)

View file

@ -80,6 +80,7 @@ module.exports = grammar({
name: "cmake", name: "cmake",
externals: ($) => [$.bracket_argument], externals: ($) => [$.bracket_argument],
extras: ($) => [/[\s\n\r]/, $.comment],
rules: { rules: {
source_file: ($) => repeat($._command_invocation), source_file: ($) => repeat($._command_invocation),
@ -98,36 +99,36 @@ module.exports = grammar({
argument: ($) => choice($.bracket_argument, $.quoted_argument, $.unquoted_argument), argument: ($) => choice($.bracket_argument, $.quoted_argument, $.unquoted_argument),
quoted_argument: ($) => seq('"', optional($.quoted_element), '"'), quoted_argument: ($) => seq('"', optional($.quoted_element), '"'),
quoted_element: ($) => repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence, seq("\\", newline()))), quoted_element: ($) => repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence)),
unquoted_argument: ($) => repeat1(choice($.variable_ref, /[^ ()#\"\\]/, $.escape_sequence)), unquoted_argument: ($) => prec.right(repeat1(choice($.variable_ref, /[^\s\n\r()#\"\\]/, $.escape_sequence))),
if_command: ($) => command($.if, args(choice($.argument, ...if_args))), if_command: ($) => command($.if, repeat(choice($.argument, ...if_args))),
elseif_command: ($) => command($.elseif, args(choice($.argument, ...if_args))), elseif_command: ($) => command($.elseif, repeat(choice($.argument, ...if_args))),
else_command: ($) => command($.else, optional(choice($.argument, ...if_args))), else_command: ($) => command($.else, optional(choice($.argument, ...if_args))),
endif_command: ($) => command($.endif, optional(choice($.argument, ...if_args))), endif_command: ($) => command($.endif, optional(choice($.argument, ...if_args))),
if_condition: ($) => if_condition: ($) =>
seq($.if_command, repeat(choice($._command_invocation, $.elseif_command, $.else_command)), $.endif_command), seq($.if_command, repeat(choice($._command_invocation, $.elseif_command, $.else_command)), $.endif_command),
foreach_command: ($) => command($.foreach, args(choice($.argument, ...foreach_args))), foreach_command: ($) => command($.foreach, repeat(choice($.argument, ...foreach_args))),
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($._command_invocation), $.endforeach_command),
while_command: ($) => command($.while, args(choice($.argument, ...if_args))), while_command: ($) => command($.while, repeat(choice($.argument, ...if_args))),
endwhile_command: ($) => command($.endwhile, optional(choice($.argument, ...if_args))), endwhile_command: ($) => command($.endwhile, optional(choice($.argument, ...if_args))),
while_loop: ($) => seq($.while_command, repeat($._command_invocation), $.endwhile_command), while_loop: ($) => seq($.while_command, repeat($._command_invocation), $.endwhile_command),
function_command: ($) => command($.function, args($.argument)), function_command: ($) => command($.function, repeat($.argument)),
endfunction_command: ($) => command($.endfunction, args($.argument)), endfunction_command: ($) => command($.endfunction, repeat($.argument)),
function_def: ($) => seq($.function_command, repeat($._command_invocation), $.endfunction_command), function_def: ($) => seq($.function_command, repeat($._command_invocation), $.endfunction_command),
macro_command: ($) => command($.macro, args($.argument)), macro_command: ($) => command($.macro, repeat($.argument)),
endmacro_command: ($) => command($.endmacro, args($.argument)), endmacro_command: ($) => command($.endmacro, repeat($.argument)),
macro_def: ($) => seq($.macro_command, repeat($._command_invocation), $.endmacro_command), macro_def: ($) => seq($.macro_command, repeat($._command_invocation), $.endmacro_command),
message_command: ($) => command($.message, optional(args(choice($.argument, ...message_args)))), message_command: ($) => command($.message, optional(repeat(choice($.argument, ...message_args)))),
normal_command: ($) => command($.identifier, optional(args($.argument))), normal_command: ($) => command($.identifier, repeat($.argument)),
_command_invocation: ($) => _command_invocation: ($) =>
choice( choice(
@ -140,6 +141,8 @@ module.exports = grammar({
$.message_command $.message_command
), ),
comment: ($) => seq("#", choice($.bracket_argument)),
...commandNames(...commands), ...commandNames(...commands),
identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/, identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/,
integer: (_) => /[+-]*\d+/, integer: (_) => /[+-]*\d+/,
@ -158,18 +161,6 @@ function commandNames(...names) {
return Object.assign({}, ...names.map(commandName)); return Object.assign({}, ...names.map(commandName));
} }
function args(rule) {
return seq(rule, repeat(prec.left(seq(repeat1(seperation()), optional(rule)))));
}
function command(name_rule, arg_rule) { function command(name_rule, arg_rule) {
return seq(name_rule, "(", repeat(seperation()), arg_rule, ")"); return seq(name_rule, "(", arg_rule, ")");
}
function seperation() {
return choice(space(), newline());
}
function space() {
return /[ \t]+/;
}
function newline() {
return /\n+/;
} }

File diff suppressed because it is too large Load diff

View file

@ -37,6 +37,21 @@
] ]
} }
}, },
{
"type": "comment",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "bracket_argument",
"named": true
}
]
}
},
{ {
"type": "else_command", "type": "else_command",
"named": true, "named": true,
@ -715,6 +730,10 @@
"type": "\"", "type": "\"",
"named": false "named": false
}, },
{
"type": "#",
"named": false
},
{ {
"type": "$CACHE", "type": "$CACHE",
"named": false "named": false
@ -983,10 +1002,6 @@
"type": "ZIP_LISTS", "type": "ZIP_LISTS",
"named": false "named": false
}, },
{
"type": "\\",
"named": false
},
{ {
"type": "\\n", "type": "\\n",
"named": false "named": false

30300
src/parser.c

File diff suppressed because it is too large Load diff