summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2014-12-02 05:29:12 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2014-12-02 05:29:12 (GMT)
commit52a063b131c39f01f3f7b414f958d6a77e11eefd (patch)
tree2d8af050a700fbd333c841be32040e29b4c62120
parent0ecba9c66917841b3761795bfd48f29e7950dfb7 (diff)
downloadagnostic-52a063b131c39f01f3f7b414f958d6a77e11eefd.zip
agnostic-52a063b131c39f01f3f7b414f958d6a77e11eefd.tar.gz
agnostic-52a063b131c39f01f3f7b414f958d6a77e11eefd.tar.bz2
shell parser: initial 'for' implementation.
The following can be parsed (though not yet executed): ./parse 'for a in $(seq 10); do echo hello ; echo "a=$a" ; done'
-rw-r--r--src/shell_parser/posix_shell.pegjs24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/shell_parser/posix_shell.pegjs b/src/shell_parser/posix_shell.pegjs
index 47bfee4..05d6015 100644
--- a/src/shell_parser/posix_shell.pegjs
+++ b/src/shell_parser/posix_shell.pegjs
@@ -394,15 +394,35 @@ SimpleCommand =
Command, as per Section "2.9 Shell Commands"
***************************/
+Compound_List = cmd:List { return cmd ; }
+
/* Compound Commands - Section "2.9.4" */
Compound_Command_Subshell =
- "(" EmptyDelimiter* cmd:List EmptyDelimiter* ")" { return { "compound_subshell" : cmd } ; }
+ "(" EmptyDelimiter* cmd:Compound_List EmptyDelimiter* ")" { return { "compound_subshell" : cmd } ; }
Compound_Command_Currentshell =
"{" EmptyDelimiter* cmd:TerminatedList EmptyDelimiter* "}" { return { "compound_currentshell" : cmd } ; }
Command =
- EmptyDelimiter* cmd:(SimpleCommand / Compound_Command_Subshell / Compound_Command_Currentshell ) EmptyDelimiter* { return cmd; }
+ EmptyDelimiter* cmd:(SimpleCommand / Compound_Command_Subshell / Compound_Command_Currentshell / For_clause ) EmptyDelimiter* { return cmd; }
+
+Wordlist =
+ first:Token_NoDelimiter rest:(EmptyDelimiter Token_NoDelimiter)* {
+ var results = first ;
+ rest.forEach(function(item){
+ var delim = item[0];
+ var tokens = item[1];
+ results.push(delim); // Emptydelimiter is one item
+ results = results.concat(tokens); // Tokens is an array of items, add each one
+ });
+ return results;
+ }
+
+For_clause =
+ "for" EmptyDelimiter* name:VariableName EmptyDelimiter*
+ "in" EmptyDelimiter* wordlist:Wordlist EmptyDelimiter* ";"
+ EmptyDelimiter* "do" EmptyDelimiter* cmd:Compound_List EmptyDelimiter* "done"
+ { return { "for_clause": { "varname" : name, "wordlist" : wordlist, "command":cmd } }; }
/***************************
Pipeline, as per Section 2.9.2