Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Start work on heredocs
  • Loading branch information
maxbrunsfeld committed Jul 14, 2017
1 parent d2ac184 commit 6be8857
Show file tree
Hide file tree
Showing 6 changed files with 1,499 additions and 1,079 deletions.
3 changes: 2 additions & 1 deletion binding.gyp
Expand Up @@ -8,7 +8,8 @@
],
"sources": [
"src/parser.c",
"src/binding.cc"
"src/binding.cc",
"src/scanner.cc",
],
"cflags_c": [
"-std=c99",
Expand Down
23 changes: 23 additions & 0 deletions corpus/commands.txt
Expand Up @@ -101,6 +101,29 @@ cat a b 2> /dev/null
(file_redirect (file_descriptor) (file_descriptor))
(command_name))))

===============================
Heredoc redirects
===============================

node <<JS
console.log("hi")
JS

bash -c <<JS
echo hi
JS

---

(program
(command (simple_command
(command_name)
(heredoc_redirect (heredoc))))
(command (simple_command
(command_name)
(argument)
(heredoc_redirect (heredoc)))))

===============================
Variable expansions
===============================
Expand Down
16 changes: 13 additions & 3 deletions grammar.js
Expand Up @@ -3,6 +3,10 @@ module.exports = grammar({

inline: $ => [$.control_operator],

externals: $ => [
$.heredoc
],

rules: {
program: $ => repeat($.command),

Expand All @@ -29,9 +33,10 @@ module.exports = grammar({
$.operator_expansion
))
)),
repeat(
$.file_redirect
)
repeat(choice(
$.file_redirect,
$.heredoc_redirect
))
),

pipeline: $ => prec.left(seq(
Expand Down Expand Up @@ -83,6 +88,11 @@ module.exports = grammar({
)
),

heredoc_redirect: $ => seq(
choice('<<', '<<-'),
$.heredoc
),

file_descriptor: $ => token(prec(1, /\d+/)),

leading_word: $ => /[^\s=|;:{}]+/,
Expand Down
42 changes: 39 additions & 3 deletions src/grammar.json
Expand Up @@ -105,8 +105,17 @@
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "file_redirect"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "file_redirect"
},
{
"type": "SYMBOL",
"name": "heredoc_redirect"
}
]
}
}
]
Expand Down Expand Up @@ -349,6 +358,28 @@
}
]
},
"heredoc_redirect": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "<<"
},
{
"type": "STRING",
"value": "<<-"
}
]
},
{
"type": "SYMBOL",
"name": "heredoc"
}
]
},
"file_descriptor": {
"type": "TOKEN",
"content": {
Expand Down Expand Up @@ -389,7 +420,12 @@
}
],
"conflicts": [],
"externals": [],
"externals": [
{
"type": "SYMBOL",
"name": "heredoc"
}
],
"inline": [
"control_operator"
]
Expand Down

0 comments on commit 6be8857

Please sign in to comment.