Allow compound statements and redirects in more places

Fixes #35
This commit is contained in:
Max Brunsfeld 2018-10-18 11:05:58 -07:00
parent 5203b504db
commit cdbc8863cd
7 changed files with 83752 additions and 76388 deletions

View File

@ -90,13 +90,11 @@ cat a b > /dev/null
--- ---
(program (program
(command (redirected_statement
(command_name (word)) (command (command_name (word)))
(file_redirect (word))) (file_redirect (word)))
(command (redirected_statement
(command_name (word)) (command (command_name (word)) (word) (word))
(word)
(word)
(file_redirect (word))) (file_redirect (word)))
(command (command
(file_redirect (file_descriptor) (word)) (file_redirect (file_descriptor) (word))
@ -117,15 +115,14 @@ JS
--- ---
(program (program
(command (redirected_statement
(command_name (word)) (command (command_name (word)))
(heredoc_redirect (heredoc_start)) (heredoc_redirect (heredoc_start)))
(heredoc_body)) (heredoc_body)
(command (redirected_statement
(command_name (word)) (command (command_name (word)) (word))
(word) (heredoc_redirect (heredoc_start)))
(heredoc_redirect (heredoc_start)) (heredoc_body))
(heredoc_body)))
=============================== ===============================
Heredocs with variables Heredocs with variables
@ -140,12 +137,12 @@ exit
--- ---
(program (program
(command (redirected_statement
(command_name (word)) (command (command_name (word)))
(heredoc_redirect (heredoc_start)) (heredoc_redirect (heredoc_start)))
(heredoc_body (heredoc_body
(simple_expansion (variable_name)) (simple_expansion (variable_name))
(expansion (variable_name)))) (expansion (variable_name)))
(command (command_name (word)))) (command (command_name (word))))
================================= =================================
@ -161,13 +158,13 @@ wc -l $tmpfile
--- ---
(program (program
(command (redirected_statement
(command_name (word)) (command (command_name (word)))
(heredoc_redirect (heredoc_start)) (heredoc_redirect (heredoc_start)))
(heredoc_body (heredoc_body
(simple_expansion (variable_name)) (simple_expansion (variable_name))
(simple_expansion (variable_name)) (simple_expansion (variable_name))
(expansion (variable_name)))) (expansion (variable_name)))
(command (command
(command_name (word)) (command_name (word))
(word) (word)

View File

@ -200,9 +200,10 @@ echo abc > >(wc -c)
(process_substitution (process_substitution
(command (command_name (word)) (word)) (command (command_name (word)) (word))
(command (command_name (word)) (word)))) (command (command_name (word)) (word))))
(command (redirected_statement
(command_name (word)) (command
(word) (command_name (word))
(word))
(file_redirect (process_substitution (file_redirect (process_substitution
(command (command_name (word)) (word)))))) (command (command_name (word)) (word))))))

View File

@ -64,10 +64,11 @@ done < <(cat file)
--- ---
(program (program
(while_statement (redirected_statement
(command (command_name (word)) (word)) (while_statement
(do_group (command (command_name (word)) (word))
(command (command_name (word)) (simple_expansion (variable_name)))) (do_group
(command (command_name (word)) (simple_expansion (variable_name)))))
(file_redirect (process_substitution (file_redirect (process_substitution
(command (command_name (word)) (word)))))) (command (command_name (word)) (word))))))
@ -312,9 +313,9 @@ function do_yet_another_thing {
(function_definition (function_definition
(word) (word)
(compound_statement (command (command_name (word)) (word)))) (compound_statement (command (command_name (word)) (word))))
(function_definition (redirected_statement (function_definition
(word) (word)
(compound_statement (command (command_name (word)) (word))) (compound_statement (command (command_name (word)) (word))))
(file_redirect (file_descriptor) (word)))) (file_redirect (file_descriptor) (word))))
========================================= =========================================
@ -429,3 +430,30 @@ unsetenv -f ONE TWO
(unset_command (variable_name)) (unset_command (variable_name))
(unset_command (string (simple_expansion (variable_name)))) (unset_command (string (simple_expansion (variable_name))))
(unset_command (word) (variable_name) (variable_name))) (unset_command (word) (variable_name) (variable_name)))
===========================================
Compound statements
===========================================
a () {
ls || { echo "b"; return 0; }
echo c
}
{ echo "a"
echo "b"
} >&2
---
(program
(function_definition (word) (compound_statement
(list
(command (command_name (word)))
(compound_statement
(command (command_name (word)) (string))
(command (command_name (word)) (word))))
(command (command_name (word)) (word))))
(redirected_statement
(compound_statement (command (command_name (word)) (string)) (command (command_name (word)) (string)))
(file_redirect (word))))

View File

@ -52,12 +52,14 @@ module.exports = grammar({
_terminated_statement: $ => seq( _terminated_statement: $ => seq(
$._statement, $._statement,
optional($.heredoc_body),
$._terminator $._terminator
), ),
// Statements // Statements
_statement: $ => choice( _statement: $ => choice(
$.redirected_statement,
$.variable_assignment, $.variable_assignment,
$.command, $.command,
$.declaration_command, $.declaration_command,
@ -72,15 +74,26 @@ module.exports = grammar({
$.pipeline, $.pipeline,
$.list, $.list,
$.subshell, $.subshell,
$.compound_statement,
$.function_definition $.function_definition
), ),
_statements: $ => seq( _statements: $ => seq(
repeat($._terminated_statement), repeat($._terminated_statement),
$._statement, $._statement,
optional($.heredoc_body),
optional($._terminator) optional($._terminator)
), ),
redirected_statement: $ => prec(-1, seq(
$._statement,
repeat1(choice(
$.file_redirect,
$.heredoc_redirect,
$.herestring_redirect
))
)),
for_statement: $ => seq( for_statement: $ => seq(
'for', 'for',
$._simple_variable_name, $._simple_variable_name,
@ -111,13 +124,7 @@ module.exports = grammar({
while_statement: $ => seq( while_statement: $ => seq(
'while', 'while',
$._terminated_statement, $._terminated_statement,
$.do_group, $.do_group
repeat(choice(
$.file_redirect,
$.heredoc_redirect,
$.herestring_redirect
)),
optional($.heredoc_body)
), ),
do_group: $ => seq( do_group: $ => seq(
@ -188,8 +195,7 @@ module.exports = grammar({
seq('function', $.word, optional(seq('(', ')'))), seq('function', $.word, optional(seq('(', ')'))),
seq($.word, '(', ')') seq($.word, '(', ')')
), ),
$.compound_statement, $.compound_statement
optional($.file_redirect)
), ),
compound_statement: $ => seq( compound_statement: $ => seq(
@ -232,12 +238,7 @@ module.exports = grammar({
seq('[', $._expression, ']'), seq('[', $._expression, ']'),
seq('[[', $._expression, ']]'), seq('[[', $._expression, ']]'),
seq('((', $._expression, '))') seq('((', $._expression, '))')
), )
repeat(choice(
$.file_redirect,
$.heredoc_redirect,
$.herestring_redirect
))
), ),
declaration_command: $ => prec.left(seq( declaration_command: $ => prec.left(seq(
@ -269,13 +270,7 @@ module.exports = grammar({
choice('=~', '=='), choice('=~', '=='),
choice($._literal, $.regex) choice($._literal, $.regex)
) )
)), ))
repeat(choice(
$.file_redirect,
$.heredoc_redirect,
$.herestring_redirect
)),
optional($.heredoc_body)
)), )),
command_name: $ => $._literal, command_name: $ => $._literal,

View File

@ -1,28 +1,20 @@
examples/bash-it/plugins/available/git.plugin.bash examples/bash-it/plugins/available/git.plugin.bash
examples/bash-it/plugins/available/extract.plugin.bash examples/bash-it/plugins/available/extract.plugin.bash
examples/bash-it/plugins/available/go.plugin.bash examples/bash-it/plugins/available/go.plugin.bash
examples/bash-it/install.sh
examples/bash-it/completion/available/svn.completion.bash examples/bash-it/completion/available/svn.completion.bash
examples/bash-it/completion/available/docker-compose.completion.bash examples/bash-it/completion/available/docker-compose.completion.bash
examples/bash-it/completion/available/gh.completion.bash
examples/bash-it/completion/available/drush.completion.bash examples/bash-it/completion/available/drush.completion.bash
examples/bash-it/completion/available/hub.completion.bash
examples/bash-it/completion/available/docker-machine.completion.bash examples/bash-it/completion/available/docker-machine.completion.bash
examples/bash-it/completion/available/git.completion.bash examples/bash-it/completion/available/git.completion.bash
examples/bash-it/completion/available/defaults.completion.bash examples/bash-it/completion/available/defaults.completion.bash
examples/bash-it/completion/available/packer.completion.bash
examples/bash-it/completion/available/docker.completion.bash examples/bash-it/completion/available/docker.completion.bash
examples/bash-it/completion/available/tmux.completion.bash examples/bash-it/completion/available/tmux.completion.bash
examples/bash-it/lib/preexec.bash examples/bash-it/lib/preexec.bash
examples/bash-it/lib/composure.bash examples/bash-it/lib/composure.bash
examples/bash-it/test_lib/bats-support/src/output.bash
examples/bash-it/test_lib/bats-assert/src/assert.bash
examples/bash-it/themes/hawaii50/hawaii50.theme.bash examples/bash-it/themes/hawaii50/hawaii50.theme.bash
examples/bash-it/themes/dulcie/dulcie.theme.bash
examples/bash-it/themes/colors.theme.bash examples/bash-it/themes/colors.theme.bash
examples/bash-it/themes/morris/morris.theme.bash examples/bash-it/themes/morris/morris.theme.bash
examples/bash-it/themes/powerline/powerline.base.bash examples/bash-it/themes/powerline/powerline.base.bash
examples/bash-it/themes/base.theme.bash
examples/bash-it/themes/brainy/brainy.theme.bash examples/bash-it/themes/brainy/brainy.theme.bash
examples/bash-it/themes/nwinkler_random_colors/nwinkler_random_colors.theme.bash examples/bash-it/themes/nwinkler_random_colors/nwinkler_random_colors.theme.bash
examples/bash-it/themes/kitsune/kitsune.theme.bash examples/bash-it/themes/kitsune/kitsune.theme.bash

161
src/grammar.json vendored
View File

@ -21,6 +21,18 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_statement" "name": "_statement"
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "heredoc_body"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_terminator" "name": "_terminator"
@ -30,6 +42,10 @@
"_statement": { "_statement": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "redirected_statement"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "variable_assignment" "name": "variable_assignment"
@ -86,6 +102,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "subshell" "name": "subshell"
}, },
{
"type": "SYMBOL",
"name": "compound_statement"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "function_definition" "name": "function_definition"
@ -106,6 +126,18 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_statement" "name": "_statement"
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "heredoc_body"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -120,6 +152,39 @@
} }
] ]
}, },
"redirected_statement": {
"type": "PREC",
"value": -1,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_statement"
},
{
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "file_redirect"
},
{
"type": "SYMBOL",
"name": "heredoc_redirect"
},
{
"type": "SYMBOL",
"name": "herestring_redirect"
}
]
}
}
]
}
},
"for_statement": { "for_statement": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -265,38 +330,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "do_group" "name": "do_group"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "file_redirect"
},
{
"type": "SYMBOL",
"name": "heredoc_redirect"
},
{
"type": "SYMBOL",
"name": "herestring_redirect"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "heredoc_body"
},
{
"type": "BLANK"
}
]
} }
] ]
}, },
@ -681,18 +714,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "compound_statement" "name": "compound_statement"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "file_redirect"
},
{
"type": "BLANK"
}
]
} }
] ]
}, },
@ -877,26 +898,6 @@
] ]
} }
] ]
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "file_redirect"
},
{
"type": "SYMBOL",
"name": "heredoc_redirect"
},
{
"type": "SYMBOL",
"name": "herestring_redirect"
}
]
}
} }
] ]
}, },
@ -1060,38 +1061,6 @@
} }
] ]
} }
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "file_redirect"
},
{
"type": "SYMBOL",
"name": "heredoc_redirect"
},
{
"type": "SYMBOL",
"name": "herestring_redirect"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "heredoc_body"
},
{
"type": "BLANK"
}
]
} }
] ]
} }

159832
src/parser.c vendored

File diff suppressed because it is too large Load Diff