Commit Graph

19 Commits

Author SHA1 Message Date
Timothy Sample 118750bb79 Handle here-documents everywhere
This commit fixes a problem with the old method of handling
here-documents.  We used to ignore here-documents in many places where
they are valid (e.g., between Boolean operators).  They are now valid
after any 'NEWLINE' token.  To achieve this, the handling logic was
moved out of the parser and into the lexer.  We now use a hook
mechanism to manage the necessary communication between the parser and
lexer.

* geesh/parser.scm (read-here-docs): Return strings instead of tokens.
(make-lexer): Add here-docs-hook as a keyword argument, and call it
after reading here-documents.
(make-parser): Let command-hooks transform commands, and remove
here-document handling rules.
(parse): Handle merging here-documents into commands.
* tests/parser.scm: Add tests for many less-intuitive here-document
locations.
2018-12-05 16:08:09 -05:00
Timothy Sample 8774eec56a Factor out common code from the read-sh functions
* geesh/parser.scm (make-lexer): Remove the read-sh/bracketed and
read-sh/backquoted parameters.
(parse): New function.
(->command-list): New function.
(read-sh/bracketed, read-sh/backquoted, read-sh, read-sh-all):
Simplify by using the new functions.
2018-12-05 16:08:09 -05:00
Timothy Sample 9d834d0767 Fix parsing of for loops
* geesh/parser.scm (make-parser): When parsing for loops, make all
word-lists actual lists, and make an empty word-list with the 'in'
keyword yield an empty list rather than a reference to '$@'.
* tests/parser.scm: Update tests.
2018-11-28 23:04:30 -05:00
Timothy Sample 48e122c42f Fix AST for redirects and assignments
This should have been in 2b05199562, but
it was overlooked.

* geesh/parser.scm (make-parser): Splice in arguments in the redirect
case (to be consistent with the non-redirect case).
* tests/parser.scm: Add a test for this.
2018-11-28 22:09:34 -05:00
Timothy Sample ff14ea0097 Rename '<sh-define>' to '<sh-defun>' and simplify
This change is already in the syntax document.

* geesh/parser.scm (make-parser): Rename '<sh-define>' to '<sh-defun>'
and use the function name directly instead of making it a singleton
list.
* tests/parser.scm: Update tests.
* .dir-locals.el: Update indentation.
2018-11-02 14:35:34 -04:00
Timothy Sample 2d61e91b27 Flatten AST form for pipelines
* geesh/parser.scm (make-parser): Splice in commands to keep
'<sh-pipeline>' flat.
* tests/parser.scm: Adjust and add tests accordingly.
2018-07-19 01:28:43 -04:00
Timothy Sample c79cea756d Rename '<sh-pipe>' to '<sh-pipeline>'
* doc/syntax.txt: Rename '<sh-pipe>' to '<sh-pipeline>'.
* geesh/parser.scm (make-parser): Ditto.
* tests/parser.scm: Fix test accordingly.
2018-07-19 01:19:00 -04:00
Timothy Sample 12b0354233 Rename '<sh-bang>' to '<sh-not>'
* geesh/parser.scm (make-parser): Rename '<sh-bang>' to '<sh-not>'.
2018-07-19 01:02:08 -04:00
Timothy Sample 2aa8615f9e Update AST for command substitutions
The AST generated by the parser did not match the syntax document.
However, neither were right.  This commit updates the syntax document
to describe a more consistent form, and fixes the code to follow it.

* doc/syntax.txt (word): Make the children of '<sh-cmd-sub>' a
possibly empty list of 'list's.
* geesh/lexer.scm (get-bracketed-command): Splice in children for
'<sh-cmd-sub>'.
(get-backquoted-command): Ditto.
* geesh/parser.scm (read-sh/bracketed): Make sure that result is
always a list.
(read-sh/backquoted): Ditto.
* tests/lexer.scm: Update tests accordingly.
* tests/parser.scm: Ditto.
2018-07-18 14:27:11 -04:00
Timothy Sample da1d561f22 Fix AST to prevent empty '<sh-exec-let>' bindings
Commands with prefix redirects and no arguments or assignments would
generate '<sh-exec-let>' forms with an empty list for bindings.

* geesh/parser.scm (make-parser): Check for and handle an empty
assignment list.
* tests/parser.scm: Add test.
2018-07-17 22:27:10 -04:00
Timothy Sample a711daefa8 Fix an accidental AST format change
Commit b807b72a7c changed the AST format
when reading single commands by mistake.  It caused 'read-sh' to
return a singleton list.

* geesh/parser.scm (make-parser): Restore old behaviour.
(read-sh-all): Convert the output from the parser into a list.
* tests/parser.scm: Add tests to make sure 'read-sh-all' always
returns a list.
2018-07-16 13:09:51 -04:00
Timothy Sample b807b72a7c Add function for reading whole files
* geesh/parser.scm (read-sh-all): New public function for reading all
commands from a port.
(make-parser): Simplify AST forms when reading multiple commands.
* tests/parser.scm: Test it.
2018-07-16 10:18:59 -04:00
Timothy Sample 3ec520596c Fix 'read-sh' to default to current input port
* geesh/parser.scm (read-sh): Make the 'port' argument optional and
default to using 'current-input-port'.
2018-07-16 10:03:58 -04:00
Timothy Sample aabfd76beb Add parser support for here-documents
* geesh/parser.scm (map+fold): New function.
(merge-here-docs): New function.
(remove-quotes): New function.
(read-here-docs): New function.
(make-lexer): Handle newlines and here-document operators specially,
and emit special 'HERE-DOC' and 'HERE-DOC-SEP' tokens.
(make-parser): Use new tokens to support here-documents.
* tests/parser.scm: Add tests for here-documents.
2018-07-15 20:28:04 -04:00
Timothy Sample faf89ad173 Delay processing of dup redirect words
The right-hand side of a dup redirect ("<&" and ">&") may contain
expansions, so we cannot process it during parsing.

* geesh/parser.scm (process-dup-or-close-word): Remove function.
(make-parser): Return the raw word from a dup redirect instead of
trying to convert it into a number or a symbol.
* tests/parser.scm: Fix tests accordingly.
2018-07-15 19:47:10 -04:00
Timothy Sample 7cf2c3d806 Fix handling of reserved words in commands
A reserved word should not be special when it is an argument to a
command.  This commit makes the parser treat reserved words as normal
arguments.  Note that this change exposed problems in many of the
parser tests, which relied on reserved words delimiting commands where
they should not.  Those are now fixed.

* geesh/parser.scm (make-parser): Add reserved words to the default
'WORD*' rule, and use a new rule without reserved words for command
names.
* tests/parser.scm: Add a test for reserved words as arguments, and
fix old tests that relied on the old, incorrect behaviour.
2018-07-15 19:46:14 -04:00
Timothy Sample 7d27433a32 Use '<sh-begin>' for lists of commands
* geesh/parser.scm (make-parser): Put '<sh-begin>' at the beginning of
lists of commands.
* tests/parser.scm: Update tests accordingly.
2018-07-15 19:46:14 -04:00
Timothy Sample 2b05199562 Flatten AST form for multiple assignments
* geesh/parser.scm (make-parser): Instead of using a nested list for
assignments, use a flat list.
* tests/parser.scm: Adjust existing test for this (which checks a
single assignment) and add a second test which checks multiple
assignments.
2018-07-15 19:45:31 -04:00
Timothy Sample 69c3f9e6ad Add parser
* geesh/parser.scm: New file.
* tests/parser.scm: New file.
* Makefile.am: Add them.
* .dir-locals.el: New file. Include indenting rules for Shell AST
forms and 'call-with-backquoted-input-port'.
2018-02-01 00:14:12 -05:00