feat: put the body of commands into a body node

This is motivated by helping writing queries for indenting nodes easier
This commit is contained in:
Uy Ha 2023-06-28 13:14:23 +00:00
parent aafadc0d65
commit ef75262109
8 changed files with 20457 additions and 22231 deletions

View file

@ -14,6 +14,7 @@ endfunction()
(unquoted_argument)
)
)
(body)
(endfunction_command
(endfunction)
)
@ -45,6 +46,7 @@ endfunction()
(unquoted_argument)
)
)
(body)
(endfunction_command
(endfunction)
)
@ -67,6 +69,7 @@ endmacro()
(unquoted_argument)
)
)
(body)
(endmacro_command
(endmacro)
)
@ -98,6 +101,7 @@ endmacro()
(unquoted_argument)
)
)
(body)
(endmacro_command
(endmacro)
)
@ -132,9 +136,46 @@ endblock()
(unquoted_argument)
)
)
(body)
(endblock_command
(endblock)
)
)
)
================================
Nested function [block_commands]
================================
function(a)
function(b)
endfunction()
endfunction()
---
(source_file
(function_def
(function_command
(function)
(argument
(unquoted_argument)
)
)
(body
(function_def
(function_command
(function)
(argument
(unquoted_argument)
)
)
(body)
(endfunction_command
(endfunction)
)
)
)
(endfunction_command
(endfunction)
)
)
)

View file

@ -12,6 +12,7 @@ endif()
(if)
(argument (unquoted_argument))
)
(body)
(endif_command (endif))
)
)
@ -31,10 +32,12 @@ endif()
(if)
(argument (unquoted_argument))
)
(body)
(elseif_command
(elseif)
(argument (unquoted_argument))
)
(body)
(endif_command (endif))
)
)
@ -55,20 +58,24 @@ endif()
(if)
(argument (unquoted_argument))
)
(body)
(elseif_command
(elseif)
(argument (unquoted_argument))
)
(body)
(else_command (else))
(body)
(endif_command (endif))
)
)
==========================================
If with one command invocation [condition]
==========================================
============================================
If with many command invocations [condition]
============================================
if(cond)
message(STATUS)
message(STATUS)
endif()
---
@ -79,14 +86,85 @@ endif()
(if)
(argument (unquoted_argument))
)
(body
(normal_command
(identifier)
(argument (unquoted_argument))
)
(normal_command
(identifier)
(argument (unquoted_argument))
)
)
(endif_command (endif))
)
)
==============================================================
If, elseof, and else with many command invocations [condition]
==============================================================
if(cond)
message(STATUS)
message(STATUS)
elseif(cond)
message(STATUS)
message(STATUS)
else(cond)
message(STATUS)
message(STATUS)
endif()
---
(source_file
(if_condition
(if_command
(if)
(argument (unquoted_argument))
)
(body
(normal_command
(identifier)
(argument (unquoted_argument))
)
(normal_command
(identifier)
(argument (unquoted_argument))
)
)
(elseif_command
(elseif)
(argument (unquoted_argument))
)
(body
(normal_command
(identifier)
(argument (unquoted_argument))
)
(normal_command
(identifier)
(argument (unquoted_argument))
)
)
(else_command
(else)
(argument (unquoted_argument))
)
(body
(normal_command
(identifier)
(argument (unquoted_argument))
)
(normal_command
(identifier)
(argument (unquoted_argument))
)
)
(endif_command (endif))
)
)
======================================
Condition with parentheses [condition]
======================================
@ -103,6 +181,7 @@ endif()
(argument (unquoted_argument))
(argument (unquoted_argument))
)
(body)
(endif_command (endif))
)
)
@ -125,6 +204,7 @@ endif(NOT (A AND B) OR C)
(argument (unquoted_argument))
(argument (unquoted_argument))
)
(body)
(else_command
(else)
(argument (unquoted_argument))
@ -134,6 +214,7 @@ endif(NOT (A AND B) OR C)
(argument (unquoted_argument))
(argument (unquoted_argument))
)
(body)
(endif_command
(endif)
(argument (unquoted_argument))
@ -145,3 +226,33 @@ endif(NOT (A AND B) OR C)
)
)
)
============================
Nested condition [condition]
============================
if(A)
if(A)
endif()
endif()
---
(source_file
(if_condition
(if_command
(if)
(argument (unquoted_argument))
)
(body
(if_condition
(if_command
(if)
(argument (unquoted_argument))
)
(body)
(endif_command (endif))
)
)
(endif_command (endif))
)
)

View file

@ -13,6 +13,7 @@ endforeach()
(foreach)
(argument (unquoted_argument))
)
(body)
(endforeach_command (endforeach))
)
)
@ -32,6 +33,7 @@ endforeach(var)
(foreach)
(argument (unquoted_argument))
)
(body)
(endforeach_command
(endforeach)
(argument (unquoted_argument))
@ -53,6 +55,7 @@ ENDFOREACH()
(foreach)
(argument (unquoted_argument))
)
(body)
(endforeach_command (endforeach))
)
)
@ -72,6 +75,7 @@ endForEach()
(foreach)
(argument (unquoted_argument))
)
(body)
(endforeach_command (endforeach))
)
)
@ -92,6 +96,7 @@ endforeach()
(argument (unquoted_argument))
(argument (unquoted_argument))
)
(body)
(endforeach_command (endforeach))
)
)
@ -112,6 +117,7 @@ endforeach()
(argument (unquoted_argument))
(argument (unquoted_argument))
)
(body)
(endforeach_command (endforeach))
)
)

View file

@ -13,6 +13,7 @@ endwhile()
(while)
(argument (unquoted_argument))
)
(body)
(endwhile_command (endwhile))
)
)
@ -32,6 +33,7 @@ endwhile(cond)
(while)
(argument (unquoted_argument))
)
(body)
(endwhile_command
(endwhile)
(argument (unquoted_argument))

View file

@ -12,7 +12,7 @@ commands = [
"macro",
"endmacro",
"block",
"endblock"
"endblock",
];
module.exports = grammar({
@ -45,41 +45,39 @@ module.exports = grammar({
quoted_argument: ($) => seq('"', optional($.quoted_element), '"'),
quoted_element: ($) => repeat1(choice($.variable_ref, $.gen_exp, $._quoted_text, $.escape_sequence)),
_quoted_text: ($) => prec.left(repeat1(choice('$', /[^\\"]/))),
_quoted_text: (_) => prec.left(repeat1(choice("$", /[^\\"]/))),
unquoted_argument: ($) => prec.right(repeat1(choice($.variable_ref, $.gen_exp, $._unquoted_text, $.escape_sequence))),
_unquoted_text: ($) => prec.left(repeat1(choice('$', /[^()#"\\']/))),
unquoted_argument: ($) =>
prec.right(repeat1(choice($.variable_ref, $.gen_exp, $._unquoted_text, $.escape_sequence))),
_unquoted_text: (_) => prec.left(repeat1(choice("$", /[^()#"\\']/))),
body: ($) => prec.right(repeat1($._untrimmed_command_invocation)),
if_command: ($) => command($.if, repeat($._untrimmed_argument)),
elseif_command: ($) => command($.elseif, repeat($._untrimmed_argument)),
else_command: ($) => command($.else, repeat($._untrimmed_argument)),
endif_command: ($) => command($.endif, repeat($._untrimmed_argument)),
if_condition: ($) =>
seq(
$.if_command,
repeat(choice($._untrimmed_command_invocation, $.elseif_command, $.else_command)),
$.endif_command
),
if_condition: ($) => seq($.if_command, repeat(choice($.body, $.elseif_command, $.else_command)), $.endif_command),
foreach_command: ($) => command($.foreach, repeat($._untrimmed_argument)),
endforeach_command: ($) => command($.endforeach, optional($.argument)),
foreach_loop: ($) => seq($.foreach_command, repeat($._untrimmed_command_invocation), $.endforeach_command),
foreach_loop: ($) => seq($.foreach_command, $.body, $.endforeach_command),
while_command: ($) => command($.while, repeat($._untrimmed_argument)),
endwhile_command: ($) => command($.endwhile, optional(seq(/\s*/, $.argument, /\s*/))),
while_loop: ($) => seq($.while_command, repeat($._untrimmed_command_invocation), $.endwhile_command),
while_loop: ($) => seq($.while_command, $.body, $.endwhile_command),
function_command: ($) => command($.function, repeat($._untrimmed_argument)),
endfunction_command: ($) => command($.endfunction, repeat($._untrimmed_argument)),
function_def: ($) => seq($.function_command, repeat($._untrimmed_command_invocation), $.endfunction_command),
function_def: ($) => seq($.function_command, $.body, $.endfunction_command),
macro_command: ($) => command($.macro, repeat($._untrimmed_argument)),
endmacro_command: ($) => command($.endmacro, repeat($._untrimmed_argument)),
macro_def: ($) => seq($.macro_command, repeat($._untrimmed_command_invocation), $.endmacro_command),
macro_def: ($) => seq($.macro_command, $.body, $.endmacro_command),
block_command: ($) => command($.block, repeat($._untrimmed_argument)),
endblock_command: ($) => command($.endblock, repeat($._untrimmed_argument)),
block_def: ($) => seq($.block_command, repeat($._untrimmed_command_invocation), $.endblock_command),
block_def: ($) => seq($.block_command, $.body, $.endblock_command),
normal_command: ($) => command($.identifier, repeat($._untrimmed_argument)),

View file

@ -433,6 +433,17 @@
}
}
},
"body": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
}
},
"if_command": {
"type": "SEQ",
"members": [
@ -571,7 +582,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
"name": "body"
},
{
"type": "SYMBOL",
@ -665,11 +676,8 @@
"name": "foreach_command"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
"name": "body"
},
{
"type": "SYMBOL",
@ -765,11 +773,8 @@
"name": "while_command"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
"name": "body"
},
{
"type": "SYMBOL",
@ -847,11 +852,8 @@
"name": "function_command"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
"name": "body"
},
{
"type": "SYMBOL",
@ -929,11 +931,8 @@
"name": "macro_command"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
"name": "body"
},
{
"type": "SYMBOL",
@ -1011,11 +1010,8 @@
"name": "block_command"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
"name": "body"
},
{
"type": "SYMBOL",

View file

@ -61,6 +61,25 @@
"type": "block_command",
"named": true
},
{
"type": "body",
"named": true
},
{
"type": "endblock_command",
"named": true
}
]
}
},
{
"type": "body",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "block_def",
"named": true
@ -69,10 +88,6 @@
"type": "bracket_comment",
"named": true
},
{
"type": "endblock_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
@ -375,11 +390,7 @@
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"type": "body",
"named": true
},
{
@ -389,34 +400,6 @@
{
"type": "foreach_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
},
{
"type": "function_def",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "line_comment",
"named": true
},
{
"type": "macro_def",
"named": true
},
{
"type": "normal_command",
"named": true
},
{
"type": "while_loop",
"named": true
}
]
}
@ -457,48 +440,16 @@
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"type": "body",
"named": true
},
{
"type": "endfunction_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
},
{
"type": "function_command",
"named": true
},
{
"type": "function_def",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "line_comment",
"named": true
},
{
"type": "macro_def",
"named": true
},
{
"type": "normal_command",
"named": true
},
{
"type": "while_loop",
"named": true
}
]
}
@ -554,11 +505,7 @@
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"type": "body",
"named": true
},
{
@ -573,37 +520,9 @@
"type": "endif_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
},
{
"type": "function_def",
"named": true
},
{
"type": "if_command",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "line_comment",
"named": true
},
{
"type": "macro_def",
"named": true
},
{
"type": "normal_command",
"named": true
},
{
"type": "while_loop",
"named": true
}
]
}
@ -644,48 +563,16 @@
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"type": "body",
"named": true
},
{
"type": "endmacro_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
},
{
"type": "function_def",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "line_comment",
"named": true
},
{
"type": "macro_command",
"named": true
},
{
"type": "macro_def",
"named": true
},
{
"type": "normal_command",
"named": true
},
{
"type": "while_loop",
"named": true
}
]
}
@ -918,48 +805,16 @@
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"type": "body",
"named": true
},
{
"type": "endwhile_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
},
{
"type": "function_def",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "line_comment",
"named": true
},
{
"type": "macro_def",
"named": true
},
{
"type": "normal_command",
"named": true
},
{
"type": "while_command",
"named": true
},
{
"type": "while_loop",
"named": true
}
]
}

42245
src/parser.c

File diff suppressed because it is too large Load diff