From b807b72a7c745921d289e7248e2fd143e094c729 Mon Sep 17 00:00:00 2001 From: Timothy Sample Date: Mon, 16 Jul 2018 10:18:59 -0400 Subject: [PATCH] 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. --- geesh/parser.scm | 15 ++++++++++++--- tests/parser.scm | 7 +++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/geesh/parser.scm b/geesh/parser.scm index fd8186a..b5c6fd9 100644 --- a/geesh/parser.scm +++ b/geesh/parser.scm @@ -24,7 +24,8 @@ #:use-module (srfi srfi-11) #:use-module (srfi srfi-41) #:use-module (system base lalr) - #:export (read-sh)) + #:export (read-sh + read-sh-all)) ;;; Commentary: ;;; @@ -229,9 +230,9 @@ the same number of times.)" (program (linebreak complete-commands linebreak) - : (if (null? (cdr $2)) (car $2) (reverse! $2)) + : (reverse! $2) (linebreak) - : (eof-object)) + : '()) (complete-commands (complete-commands newline-list complete-command) @@ -786,3 +787,11 @@ port if @var{port} is unspecified)." (lex (lambda () (if stop? '*eoi* (pre-lex)))) (parse (make-parser #:command-hook stop!))) (parse lex syntax-error))) + +(define* (read-sh-all #:optional (port #f)) + "Read all complete Shell commands from @var{port} (or the current +input port if @var{port} is unspecified)." + (let* ((port (or port (current-input-port))) + (lex (make-lexer port read-sh/bracketed read-sh/backquoted)) + (parse (make-parser))) + (parse lex syntax-error))) diff --git a/tests/parser.scm b/tests/parser.scm index 7283954..3e151cc 100644 --- a/tests/parser.scm +++ b/tests/parser.scm @@ -354,4 +354,11 @@ (list (read-sh port) (read-sh port))))) +(test-equal "Reads all commands" + '(( "echo" "foo") + ( "echo" "bar")) + (call-with-input-string "echo foo + echo bar" + read-sh-all)) + (test-end)