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) (unquoted_argument)
) )
) )
(body)
(endfunction_command (endfunction_command
(endfunction) (endfunction)
) )
@ -45,6 +46,7 @@ endfunction()
(unquoted_argument) (unquoted_argument)
) )
) )
(body)
(endfunction_command (endfunction_command
(endfunction) (endfunction)
) )
@ -67,6 +69,7 @@ endmacro()
(unquoted_argument) (unquoted_argument)
) )
) )
(body)
(endmacro_command (endmacro_command
(endmacro) (endmacro)
) )
@ -98,6 +101,7 @@ endmacro()
(unquoted_argument) (unquoted_argument)
) )
) )
(body)
(endmacro_command (endmacro_command
(endmacro) (endmacro)
) )
@ -132,9 +136,46 @@ endblock()
(unquoted_argument) (unquoted_argument)
) )
) )
(body)
(endblock_command (endblock_command
(endblock) (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) (if)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endif_command (endif)) (endif_command (endif))
) )
) )
@ -31,10 +32,12 @@ endif()
(if) (if)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(elseif_command (elseif_command
(elseif) (elseif)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endif_command (endif)) (endif_command (endif))
) )
) )
@ -55,20 +58,24 @@ endif()
(if) (if)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(elseif_command (elseif_command
(elseif) (elseif)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(else_command (else)) (else_command (else))
(body)
(endif_command (endif)) (endif_command (endif))
) )
) )
========================================== ============================================
If with one command invocation [condition] If with many command invocations [condition]
========================================== ============================================
if(cond) if(cond)
message(STATUS) message(STATUS)
message(STATUS)
endif() endif()
--- ---
@ -79,14 +86,85 @@ endif()
(if) (if)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(normal_command (body
(identifier) (normal_command
(argument (unquoted_argument)) (identifier)
(argument (unquoted_argument))
)
(normal_command
(identifier)
(argument (unquoted_argument))
)
) )
(endif_command (endif)) (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] Condition with parentheses [condition]
====================================== ======================================
@ -103,6 +181,7 @@ endif()
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endif_command (endif)) (endif_command (endif))
) )
) )
@ -125,6 +204,7 @@ endif(NOT (A AND B) OR C)
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(else_command (else_command
(else) (else)
(argument (unquoted_argument)) (argument (unquoted_argument))
@ -134,6 +214,7 @@ endif(NOT (A AND B) OR C)
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endif_command (endif_command
(endif) (endif)
(argument (unquoted_argument)) (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) (foreach)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endforeach_command (endforeach)) (endforeach_command (endforeach))
) )
) )
@ -32,6 +33,7 @@ endforeach(var)
(foreach) (foreach)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endforeach_command (endforeach_command
(endforeach) (endforeach)
(argument (unquoted_argument)) (argument (unquoted_argument))
@ -53,6 +55,7 @@ ENDFOREACH()
(foreach) (foreach)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endforeach_command (endforeach)) (endforeach_command (endforeach))
) )
) )
@ -72,6 +75,7 @@ endForEach()
(foreach) (foreach)
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endforeach_command (endforeach)) (endforeach_command (endforeach))
) )
) )
@ -92,6 +96,7 @@ endforeach()
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endforeach_command (endforeach)) (endforeach_command (endforeach))
) )
) )
@ -112,6 +117,7 @@ endforeach()
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
(body)
(endforeach_command (endforeach)) (endforeach_command (endforeach))
) )
) )

View file

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

View file

@ -12,7 +12,7 @@ commands = [
"macro", "macro",
"endmacro", "endmacro",
"block", "block",
"endblock" "endblock",
]; ];
module.exports = grammar({ module.exports = grammar({
@ -27,7 +27,7 @@ module.exports = grammar({
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;]/,
_escape_encoded: (_) => choice("\\t", "\\r", "\\n"), _escape_encoded: (_) => choice("\\t", "\\r", "\\n"),
_escape_semicolon: (_) => choice(";","\\;"), _escape_semicolon: (_) => choice(";", "\\;"),
variable: ($) => prec.left(repeat1(choice(/[a-zA-Z0-9/_.+-]/, $.escape_sequence, $.variable_ref))), variable: ($) => prec.left(repeat1(choice(/[a-zA-Z0-9/_.+-]/, $.escape_sequence, $.variable_ref))),
variable_ref: ($) => choice($.normal_var, $.env_var, $.cache_var), variable_ref: ($) => choice($.normal_var, $.env_var, $.cache_var),
@ -45,41 +45,39 @@ module.exports = grammar({
quoted_argument: ($) => seq('"', optional($.quoted_element), '"'), quoted_argument: ($) => seq('"', optional($.quoted_element), '"'),
quoted_element: ($) => repeat1(choice($.variable_ref, $.gen_exp, $._quoted_text, $.escape_sequence)), 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_argument: ($) =>
_unquoted_text: ($) => prec.left(repeat1(choice('$', /[^()#"\\']/))), 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)), if_command: ($) => command($.if, repeat($._untrimmed_argument)),
elseif_command: ($) => command($.elseif, repeat($._untrimmed_argument)), elseif_command: ($) => command($.elseif, repeat($._untrimmed_argument)),
else_command: ($) => command($.else, repeat($._untrimmed_argument)), else_command: ($) => command($.else, repeat($._untrimmed_argument)),
endif_command: ($) => command($.endif, repeat($._untrimmed_argument)), endif_command: ($) => command($.endif, repeat($._untrimmed_argument)),
if_condition: ($) => if_condition: ($) => seq($.if_command, repeat(choice($.body, $.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($._untrimmed_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($._untrimmed_command_invocation), $.endforeach_command), foreach_loop: ($) => seq($.foreach_command, $.body, $.endforeach_command),
while_command: ($) => command($.while, repeat($._untrimmed_argument)), while_command: ($) => command($.while, repeat($._untrimmed_argument)),
endwhile_command: ($) => command($.endwhile, optional(seq(/\s*/, $.argument, /\s*/))), 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)), function_command: ($) => command($.function, repeat($._untrimmed_argument)),
endfunction_command: ($) => command($.endfunction, 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)), macro_command: ($) => command($.macro, repeat($._untrimmed_argument)),
endmacro_command: ($) => command($.endmacro, 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)), block_command: ($) => command($.block, repeat($._untrimmed_argument)),
endblock_command: ($) => command($.endblock, 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)), 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": { "if_command": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -571,7 +582,7 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_untrimmed_command_invocation" "name": "body"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -665,11 +676,8 @@
"name": "foreach_command" "name": "foreach_command"
}, },
{ {
"type": "REPEAT", "type": "SYMBOL",
"content": { "name": "body"
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -765,11 +773,8 @@
"name": "while_command" "name": "while_command"
}, },
{ {
"type": "REPEAT", "type": "SYMBOL",
"content": { "name": "body"
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -847,11 +852,8 @@
"name": "function_command" "name": "function_command"
}, },
{ {
"type": "REPEAT", "type": "SYMBOL",
"content": { "name": "body"
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -929,11 +931,8 @@
"name": "macro_command" "name": "macro_command"
}, },
{ {
"type": "REPEAT", "type": "SYMBOL",
"content": { "name": "body"
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -1011,11 +1010,8 @@
"name": "block_command" "name": "block_command"
}, },
{ {
"type": "REPEAT", "type": "SYMBOL",
"content": { "name": "body"
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",

View file

@ -61,6 +61,25 @@
"type": "block_command", "type": "block_command",
"named": true "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", "type": "block_def",
"named": true "named": true
@ -69,10 +88,6 @@
"type": "bracket_comment", "type": "bracket_comment",
"named": true "named": true
}, },
{
"type": "endblock_command",
"named": true
},
{ {
"type": "foreach_loop", "type": "foreach_loop",
"named": true "named": true
@ -375,11 +390,7 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "block_def", "type": "body",
"named": true
},
{
"type": "bracket_comment",
"named": true "named": true
}, },
{ {
@ -389,34 +400,6 @@
{ {
"type": "foreach_command", "type": "foreach_command",
"named": true "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, "required": true,
"types": [ "types": [
{ {
"type": "block_def", "type": "body",
"named": true
},
{
"type": "bracket_comment",
"named": true "named": true
}, },
{ {
"type": "endfunction_command", "type": "endfunction_command",
"named": true "named": true
}, },
{
"type": "foreach_loop",
"named": true
},
{ {
"type": "function_command", "type": "function_command",
"named": true "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, "required": true,
"types": [ "types": [
{ {
"type": "block_def", "type": "body",
"named": true
},
{
"type": "bracket_comment",
"named": true "named": true
}, },
{ {
@ -573,37 +520,9 @@
"type": "endif_command", "type": "endif_command",
"named": true "named": true
}, },
{
"type": "foreach_loop",
"named": true
},
{
"type": "function_def",
"named": true
},
{ {
"type": "if_command", "type": "if_command",
"named": true "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, "required": true,
"types": [ "types": [
{ {
"type": "block_def", "type": "body",
"named": true
},
{
"type": "bracket_comment",
"named": true "named": true
}, },
{ {
"type": "endmacro_command", "type": "endmacro_command",
"named": true "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", "type": "macro_command",
"named": true "named": true
},
{
"type": "macro_def",
"named": true
},
{
"type": "normal_command",
"named": true
},
{
"type": "while_loop",
"named": true
} }
] ]
} }
@ -918,48 +805,16 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "block_def", "type": "body",
"named": true
},
{
"type": "bracket_comment",
"named": true "named": true
}, },
{ {
"type": "endwhile_command", "type": "endwhile_command",
"named": true "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", "type": "while_command",
"named": true "named": true
},
{
"type": "while_loop",
"named": true
} }
] ]
} }

42245
src/parser.c

File diff suppressed because it is too large Load diff