Start adding fields

This commit is contained in:
Max Brunsfeld 2019-12-10 13:18:29 -08:00
parent 2a3aec5635
commit de690d849a
5 changed files with 81663 additions and 79644 deletions

View File

@ -9,11 +9,18 @@ cat foo | grep -v bar
(program (program
(pipeline (pipeline
(command (command_name (word))) (command
(command (command_name (word)))) name: (command_name (word)))
(command
name: (command_name (word))))
(pipeline (pipeline
(command (command_name (word)) (word)) (command
(command (command_name (word)) (word) (word)))) name: (command_name (word))
argument: (word))
(command
name: (command_name (word))
argument: (word)
argument: (word))))
=================================== ===================================
Lists Lists
@ -48,10 +55,12 @@ done
(program (program
(while_statement (while_statement
(command (command_name (word)) (word)) condition: (command
(do_group name: (command_name (word))
(command (command_name (word)) (word)) argument: (word))
(command (command_name (word)) (word))))) body: (do_group
(command name: (command_name (word)) argument: (word))
(command name: (command_name (word)) argument: (word)))))
==================================== ====================================
While statements with IO redirects While statements with IO redirects
@ -65,12 +74,18 @@ done < <(cat file)
(program (program
(redirected_statement (redirected_statement
(while_statement body: (while_statement
(command (command_name (word)) (word)) condition: (command
(do_group name: (command_name (word))
(command (command_name (word)) (simple_expansion (variable_name))))) argument: (word))
(file_redirect (process_substitution body: (do_group
(command (command_name (word)) (word)))))) (command
name: (command_name (word))
argument: (simple_expansion (variable_name)))))
redirect: (file_redirect
destination: (process_substitution (command
name: (command_name (word))
argument: (word))))))
==================================== ====================================
For statements For statements
@ -89,17 +104,26 @@ done
(program (program
(for_statement (for_statement
(variable_name) variable: (variable_name)
(word) value: (word)
(word) value: (word)
(command_substitution (command (command_name (word)) (word) (word))) value: (command_substitution (command
(do_group name: (command_name (word))
(command (command_name (word)) (simple_expansion (variable_name))))) argument: (word)
argument: (word)))
body: (do_group
(command
name: (command_name (word))
argument: (simple_expansion (variable_name)))))
(for_statement (for_statement
(variable_name) variable: (variable_name)
(do_group body: (do_group
(command (command_name (word)) (simple_expansion (variable_name))) (command
(variable_assignment (variable_name) (raw_string))))) name: (command_name (word))
argument: (simple_expansion (variable_name)))
(variable_assignment
name: (variable_name)
value: (raw_string)))))
==================================== ====================================
C-style for statements C-style for statements

View File

@ -97,45 +97,45 @@ module.exports = grammar({
), ),
redirected_statement: $ => prec(-1, seq( redirected_statement: $ => prec(-1, seq(
$._statement, field('body', $._statement),
repeat1(choice( field('redirect', repeat1(choice(
$.file_redirect, $.file_redirect,
$.heredoc_redirect, $.heredoc_redirect,
$.herestring_redirect $.herestring_redirect
)) )))
)), )),
for_statement: $ => seq( for_statement: $ => seq(
'for', 'for',
$._simple_variable_name, field('variable', $._simple_variable_name),
optional(seq( optional(seq(
'in', 'in',
repeat1($._literal) field('value', repeat1($._literal))
)), )),
$._terminator, $._terminator,
$.do_group field('body', $.do_group)
), ),
c_style_for_statement: $ => seq( c_style_for_statement: $ => seq(
'for', 'for',
'((', '((',
optional($._expression), field('initializer', optional($._expression)),
$._terminator, $._terminator,
optional($._expression), field('condition', optional($._expression)),
$._terminator, $._terminator,
optional($._expression), field('update', optional($._expression)),
'))', '))',
optional(';'), optional(';'),
choice( field('body', choice(
$.do_group, $.do_group,
$.compound_statement $.compound_statement
) ))
), ),
while_statement: $ => seq( while_statement: $ => seq(
'while', 'while',
$._terminated_statement, field('condition', $._terminated_statement),
$.do_group field('body', $.do_group)
), ),
do_group: $ => seq( do_group: $ => seq(
@ -146,7 +146,7 @@ module.exports = grammar({
if_statement: $ => seq( if_statement: $ => seq(
'if', 'if',
$._terminated_statement, field('condition', $._terminated_statement),
'then', 'then',
optional($._statements2), optional($._statements2),
repeat($.elif_clause), repeat($.elif_clause),
@ -168,7 +168,7 @@ module.exports = grammar({
case_statement: $ => seq( case_statement: $ => seq(
'case', 'case',
$._literal, field('value', $._literal),
optional($._terminator), optional($._terminator),
'in', 'in',
$._terminator, $._terminator,
@ -180,16 +180,16 @@ module.exports = grammar({
), ),
case_item: $ => seq( case_item: $ => seq(
$._literal, field('value', $._literal),
repeat(seq('|', $._literal)), repeat(seq('|', field('value', $._literal))),
')', ')',
optional($._statements), optional($._statements),
prec(1, ';;') prec(1, ';;')
), ),
last_case_item: $ => seq( last_case_item: $ => seq(
$._literal, field('value', $._literal),
repeat(seq('|', $._literal)), repeat(seq('|', field('value', $._literal))),
')', ')',
optional($._statements), optional($._statements),
optional(prec(1, ';;')) optional(prec(1, ';;'))
@ -197,19 +197,22 @@ module.exports = grammar({
function_definition: $ => seq( function_definition: $ => seq(
choice( choice(
seq('function', $.word, optional(seq('(', ')'))), seq(
seq($.word, '(', ')') 'function',
field('name', $.word),
optional(seq('(', ')'))
),
seq(
field('name', $.word),
'(', ')'
)
), ),
$.compound_statement field('body', $.compound_statement)
), ),
compound_statement: $ => seq( compound_statement: $ => seq(
'{', '{',
repeat(seq( optional($._statements2),
$._statement,
optional(seq('\n', $.heredoc_body)),
$._terminator
)),
'}' '}'
), ),
@ -272,47 +275,47 @@ module.exports = grammar({
$.variable_assignment, $.variable_assignment,
$.file_redirect $.file_redirect
)), )),
$.command_name, field('name', $.command_name),
repeat(choice( repeat(field('argument', choice(
$._literal, $._literal,
seq( seq(
choice('=~', '=='), choice('=~', '=='),
choice($._literal, $.regex) choice($._literal, $.regex)
) )
)) )))
)), )),
command_name: $ => $._literal, command_name: $ => $._literal,
variable_assignment: $ => seq( variable_assignment: $ => seq(
choice( field('name', choice(
$.variable_name, $.variable_name,
$.subscript $.subscript
), )),
choice( choice(
'=', '=',
'+=' '+='
), ),
choice( field('value', choice(
$._literal, $._literal,
$.array, $.array,
$._empty_value $._empty_value
) ))
), ),
subscript: $ => seq( subscript: $ => seq(
$.variable_name, field('name', $.variable_name),
'[', '[',
$._literal, field('index', $._literal),
optional($._concat), optional($._concat),
']', ']',
optional($._concat) optional($._concat)
), ),
file_redirect: $ => prec.left(seq( file_redirect: $ => prec.left(seq(
optional($.file_descriptor), field('descriptor', optional($.file_descriptor)),
choice('<', '>', '>>', '&>', '&>>', '<&', '>&'), choice('<', '>', '>>', '&>', '&>>', '<&', '>&'),
$._literal field('destination', $._literal)
)), )),
heredoc_redirect: $ => seq( heredoc_redirect: $ => seq(
@ -351,20 +354,20 @@ module.exports = grammar({
binary_expression: $ => prec.left(choice( binary_expression: $ => prec.left(choice(
seq( seq(
$._expression, field('left', $._expression),
choice( field('operator', choice(
'=', '==', '=~', '!=', '=', '==', '=~', '!=',
'+', '-', '+=', '-=', '+', '-', '+=', '-=',
'<', '>', '<=', '>=', '<', '>', '<=', '>=',
'||', '&&', '||', '&&',
$.test_operator $.test_operator
), )),
$._expression field('right', $._expression)
), ),
seq( seq(
$._expression, field('left', $._expression),
choice('==', '=~'), field('operator', choice('==', '=~')),
$.regex field('right', $.regex)
) )
)), )),

685
src/grammar.json vendored
View File

@ -227,27 +227,35 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_statement" "name": "body",
"content": {
"type": "SYMBOL",
"name": "_statement"
}
}, },
{ {
"type": "REPEAT1", "type": "FIELD",
"name": "redirect",
"content": { "content": {
"type": "CHOICE", "type": "REPEAT1",
"members": [ "content": {
{ "type": "CHOICE",
"type": "SYMBOL", "members": [
"name": "file_redirect" {
}, "type": "SYMBOL",
{ "name": "file_redirect"
"type": "SYMBOL", },
"name": "heredoc_redirect" {
}, "type": "SYMBOL",
{ "name": "heredoc_redirect"
"type": "SYMBOL", },
"name": "herestring_redirect" {
} "type": "SYMBOL",
] "name": "herestring_redirect"
}
]
}
} }
} }
] ]
@ -261,8 +269,12 @@
"value": "for" "value": "for"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_simple_variable_name" "name": "variable",
"content": {
"type": "SYMBOL",
"name": "_simple_variable_name"
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -275,10 +287,14 @@
"value": "in" "value": "in"
}, },
{ {
"type": "REPEAT1", "type": "FIELD",
"name": "value",
"content": { "content": {
"type": "SYMBOL", "type": "REPEAT1",
"name": "_literal" "content": {
"type": "SYMBOL",
"name": "_literal"
}
} }
} }
] ]
@ -293,8 +309,12 @@
"name": "_terminator" "name": "_terminator"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "do_group" "name": "body",
"content": {
"type": "SYMBOL",
"name": "do_group"
}
} }
] ]
}, },
@ -310,48 +330,60 @@
"value": "((" "value": "(("
}, },
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "initializer",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_expression" "members": [
}, {
{ "type": "SYMBOL",
"type": "BLANK" "name": "_expression"
} },
] {
"type": "BLANK"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_terminator" "name": "_terminator"
}, },
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "condition",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_expression" "members": [
}, {
{ "type": "SYMBOL",
"type": "BLANK" "name": "_expression"
} },
] {
"type": "BLANK"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_terminator" "name": "_terminator"
}, },
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "update",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_expression" "members": [
}, {
{ "type": "SYMBOL",
"type": "BLANK" "name": "_expression"
} },
] {
"type": "BLANK"
}
]
}
}, },
{ {
"type": "STRING", "type": "STRING",
@ -370,17 +402,21 @@
] ]
}, },
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "body",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "do_group" "members": [
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "do_group"
"name": "compound_statement" },
} {
] "type": "SYMBOL",
"name": "compound_statement"
}
]
}
} }
] ]
}, },
@ -392,12 +428,20 @@
"value": "while" "value": "while"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_terminated_statement" "name": "condition",
"content": {
"type": "SYMBOL",
"name": "_terminated_statement"
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "do_group" "name": "body",
"content": {
"type": "SYMBOL",
"name": "do_group"
}
} }
] ]
}, },
@ -434,8 +478,12 @@
"value": "if" "value": "if"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_terminated_statement" "name": "condition",
"content": {
"type": "SYMBOL",
"name": "_terminated_statement"
}
}, },
{ {
"type": "STRING", "type": "STRING",
@ -536,8 +584,12 @@
"value": "case" "value": "case"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_literal" "name": "value",
"content": {
"type": "SYMBOL",
"name": "_literal"
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -598,8 +650,12 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_literal" "name": "value",
"content": {
"type": "SYMBOL",
"name": "_literal"
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -611,8 +667,12 @@
"value": "|" "value": "|"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_literal" "name": "value",
"content": {
"type": "SYMBOL",
"name": "_literal"
}
} }
] ]
} }
@ -647,8 +707,12 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_literal" "name": "value",
"content": {
"type": "SYMBOL",
"name": "_literal"
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -660,8 +724,12 @@
"value": "|" "value": "|"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_literal" "name": "value",
"content": {
"type": "SYMBOL",
"name": "_literal"
}
} }
] ]
} }
@ -714,8 +782,12 @@
"value": "function" "value": "function"
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "word" "name": "name",
"content": {
"type": "SYMBOL",
"name": "word"
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -744,8 +816,12 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "word" "name": "name",
"content": {
"type": "SYMBOL",
"name": "word"
}
}, },
{ {
"type": "STRING", "type": "STRING",
@ -760,8 +836,12 @@
] ]
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "compound_statement" "name": "body",
"content": {
"type": "SYMBOL",
"name": "compound_statement"
}
} }
] ]
}, },
@ -773,41 +853,16 @@
"value": "{" "value": "{"
}, },
{ {
"type": "REPEAT", "type": "CHOICE",
"content": { "members": [
"type": "SEQ", {
"members": [ "type": "SYMBOL",
{ "name": "_statements2"
"type": "SYMBOL", },
"name": "_statement" {
}, "type": "BLANK"
{ }
"type": "CHOICE", ]
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\n"
},
{
"type": "SYMBOL",
"name": "heredoc_body"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_terminator"
}
]
}
}, },
{ {
"type": "STRING", "type": "STRING",
@ -1094,50 +1149,58 @@
} }
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "command_name" "name": "name",
"content": {
"type": "SYMBOL",
"name": "command_name"
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "argument",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_literal" "members": [
}, {
{ "type": "SYMBOL",
"type": "SEQ", "name": "_literal"
"members": [ },
{ {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING", "type": "CHOICE",
"value": "=~" "members": [
}, {
{ "type": "STRING",
"type": "STRING", "value": "=~"
"value": "==" },
} {
] "type": "STRING",
}, "value": "=="
{ }
"type": "CHOICE", ]
"members": [ },
{ {
"type": "SYMBOL", "type": "CHOICE",
"name": "_literal" "members": [
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "_literal"
"name": "regex" },
} {
] "type": "SYMBOL",
} "name": "regex"
] }
} ]
] }
]
}
]
}
} }
} }
] ]
@ -1151,17 +1214,21 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "name",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "variable_name" "members": [
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "variable_name"
"name": "subscript" },
} {
] "type": "SYMBOL",
"name": "subscript"
}
]
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -1177,21 +1244,25 @@
] ]
}, },
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "value",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_literal" "members": [
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "_literal"
"name": "array" },
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "array"
"name": "_empty_value" },
} {
] "type": "SYMBOL",
"name": "_empty_value"
}
]
}
} }
] ]
}, },
@ -1199,16 +1270,24 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "variable_name" "name": "name",
"content": {
"type": "SYMBOL",
"name": "variable_name"
}
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "[" "value": "["
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_literal" "name": "index",
"content": {
"type": "SYMBOL",
"name": "_literal"
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -1247,16 +1326,20 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "descriptor",
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "file_descriptor" "members": [
}, {
{ "type": "SYMBOL",
"type": "BLANK" "name": "file_descriptor"
} },
] {
"type": "BLANK"
}
]
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -1292,8 +1375,12 @@
] ]
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_literal" "name": "destination",
"content": {
"type": "SYMBOL",
"name": "_literal"
}
} }
] ]
} }
@ -1414,77 +1501,89 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}, },
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "operator",
{ "content": {
"type": "STRING", "type": "CHOICE",
"value": "=" "members": [
}, {
{ "type": "STRING",
"type": "STRING", "value": "="
"value": "==" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "=="
"value": "=~" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "=~"
"value": "!=" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "!="
"value": "+" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "+"
"value": "-" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "-"
"value": "+=" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "+="
"value": "-=" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "-="
"value": "<" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "<"
"value": ">" },
}, {
{ "type": "STRING",
"type": "STRING", "value": ">"
"value": "<=" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "<="
"value": ">=" },
}, {
{ "type": "STRING",
"type": "STRING", "value": ">="
"value": "||" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "||"
"value": "&&" },
}, {
{ "type": "STRING",
"type": "SYMBOL", "value": "&&"
"name": "test_operator" },
} {
] "type": "SYMBOL",
"name": "test_operator"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
} }
] ]
}, },
@ -1492,25 +1591,37 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}, },
{ {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "operator",
{ "content": {
"type": "STRING", "type": "CHOICE",
"value": "==" "members": [
}, {
{ "type": "STRING",
"type": "STRING", "value": "=="
"value": "=~" },
} {
] "type": "STRING",
"value": "=~"
}
]
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "regex" "name": "right",
"content": {
"type": "SYMBOL",
"name": "regex"
}
} }
] ]
} }

1785
src/node-types.json vendored

File diff suppressed because it is too large Load Diff

158672
src/parser.c vendored

File diff suppressed because it is too large Load Diff