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
(pipeline
(command (command_name (word)))
(command (command_name (word))))
(command
name: (command_name (word)))
(command
name: (command_name (word))))
(pipeline
(command (command_name (word)) (word))
(command (command_name (word)) (word) (word))))
(command
name: (command_name (word))
argument: (word))
(command
name: (command_name (word))
argument: (word)
argument: (word))))
===================================
Lists
@ -48,10 +55,12 @@ done
(program
(while_statement
(command (command_name (word)) (word))
(do_group
(command (command_name (word)) (word))
(command (command_name (word)) (word)))))
condition: (command
name: (command_name (word))
argument: (word))
body: (do_group
(command name: (command_name (word)) argument: (word))
(command name: (command_name (word)) argument: (word)))))
====================================
While statements with IO redirects
@ -65,12 +74,18 @@ done < <(cat file)
(program
(redirected_statement
(while_statement
(command (command_name (word)) (word))
(do_group
(command (command_name (word)) (simple_expansion (variable_name)))))
(file_redirect (process_substitution
(command (command_name (word)) (word))))))
body: (while_statement
condition: (command
name: (command_name (word))
argument: (word))
body: (do_group
(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
@ -89,17 +104,26 @@ done
(program
(for_statement
(variable_name)
(word)
(word)
(command_substitution (command (command_name (word)) (word) (word)))
(do_group
(command (command_name (word)) (simple_expansion (variable_name)))))
variable: (variable_name)
value: (word)
value: (word)
value: (command_substitution (command
name: (command_name (word))
argument: (word)
argument: (word)))
body: (do_group
(command
name: (command_name (word))
argument: (simple_expansion (variable_name)))))
(for_statement
(variable_name)
(do_group
(command (command_name (word)) (simple_expansion (variable_name)))
(variable_assignment (variable_name) (raw_string)))))
variable: (variable_name)
body: (do_group
(command
name: (command_name (word))
argument: (simple_expansion (variable_name)))
(variable_assignment
name: (variable_name)
value: (raw_string)))))
====================================
C-style for statements

View File

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

685
src/grammar.json vendored
View File

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