bjosh/tests/syntax.bjo
Linus Björnstam 3518b7711d Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
  parser-combinators). Syntax that is not supported raises an error that
  names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
  NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
  export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
  completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
  control under script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00

110 lines
5.4 KiB
Text

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; (bjosh syntax): lines to trees, and the errors.
;;
;; bjo test tests/syntax.bjo
(import (std prelude))
(import (std simpletest))
(import (text parser-combinators))
(import (bjosh syntax))
;; The tree of `text` as `line->string` writes it, or "error: " and the
;; first line of the message.
(: (-> string string))
(defun (parsed text)
(match (parse-line/message text)
((Ok items) (line->string items))
((Err e) (str "error: " (error-reason e)))))
;; The lines of a message after "Expecting:" or the message's own text.
(: (-> string string))
(defun (error-reason message)
(def lines (string-split message "\n"))
(string-join (vec-slice lines 3 (- (vec-length lines) 3)) " / "))
;; The column of the error for `text`, or 0.
(: (-> string int))
(defun (error-column text)
(match (parse-line text)
((Ok _) 0)
((Err e) (record-ref (parse-error-position e) column))))
(defun (main args)
;; Words and quotes.
(expect "a command" (parsed "ls -l /tmp") "ls -l /tmp ;")
(expect "spaces and tabs between words" (parsed " ls \t -l ") "ls -l ;")
(expect "single quotes" (parsed "echo 'a b' 'it''s'") "echo \"a b\" \"it\"\"s\" ;")
(expect "double quotes" (parsed "echo \"a b\"") "echo \"a b\" ;")
(expect "quotes inside a word" (parsed "echo a'b c'd") "echo a\"b c\"d ;")
(expect "\\ makes a character plain" (parsed "echo a\\ b") "echo a\" \"b ;")
(expect "\\ in double quotes escapes only some" (parsed "echo \"\\$a \\x\"")
"echo \"$\"\"a \"\"\\\"\"x\" ;")
(expect "\\ and a line break is nothing" (parsed "echo a\\\nb") "echo ab ;")
(expect "an empty quoted word" (parsed "echo ''") "echo \"\" ;")
;; Parameters and ~.
(expect "$NAME" (parsed "echo $HOME/x") "echo ${HOME}/x ;")
(expect "${NAME}" (parsed "echo ${HOME}x") "echo ${HOME}x ;")
(expect "$NAME in double quotes" (parsed "echo \"$HOME\"") "echo \"${HOME}\" ;")
(expect "$? and $$" (parsed "echo $? $$") "echo ${?} ${$} ;")
(expect "a $ before something else" (parsed "echo a$ $.") "echo a$ $. ;")
(expect "~ at the start" (parsed "cd ~/src ~bob") "cd ~/src ~bob ;")
(expect "~ elsewhere is text" (parsed "echo a~b") "echo a~b ;")
;; Operators.
(expect "a pipe" (parsed "ls | wc -l") "ls | wc -l ;")
(expect "a pipe without spaces" (parsed "ls|wc") "ls | wc ;")
(expect "&& and ||" (parsed "make && echo ok || echo bad") "make && echo ok || echo bad ;")
(expect "; between items" (parsed "a; b ;c") "a ; b ; c ;")
(expect "& runs in the background" (parsed "sleep 9 & ls") "sleep 9 & ls ;")
(expect "line breaks end items" (parsed "a\nb\n\n") "a ; b ;")
(expect "a line break after | continues it" (parsed "ls |\n wc") "ls | wc ;")
(expect "a line break after && continues it" (parsed "a &&\nb") "a && b ;")
;; Redirections.
(expect "< > >>" (parsed "sort <in >out >>log") "sort <in >out >>log ;")
(expect "a space after the operator" (parsed "sort < in > out") "sort <in >out ;")
(expect "2> and 2>>" (parsed "make 2>err 2>>log") "make 2>err 2>>log ;")
(expect "2>&1 and >&2" (parsed "make 2>&1; echo x >&2") "make 2>&1 ; echo x >&2 ;")
(expect "1> is >" (parsed "ls 1>out") "ls >out ;")
(expect "a 2 that is a word" (parsed "echo 2 > x") "echo 2 >x ;")
(expect "a redirection first" (parsed ">out ls") "ls >out ;")
(expect "redirections in a pipe" (parsed "a <in | b >out") "a <in | b >out ;")
;; Assignments.
(expect "an assignment alone" (parsed "X=1") "X=1 ;")
(expect "assignments before a command" (parsed "A=1 B=\"x y\" env") "A=1 B=\"x y\" env ;")
(expect "after a word it is a word" (parsed "echo X=1") "echo X=1 ;")
(expect "not a name" (parsed "1X=2") "1X=2 ;")
;; Comments, and empty lines.
(expect "a comment" (parsed "ls # list") "ls ;")
(expect "# in a word" (parsed "echo a#b") "echo a#b ;")
(expect "only a comment" (parsed "# nothing") "")
(expect "an empty line" (parsed " ") "")
;; Errors.
(expect "an open quote" (parsed "echo 'abc")
"error: Expecting: a closing ' / Unexpected: end of input")
(expect "an open double quote" (parsed "echo \"abc")
"error: Expecting: a closing \" / Unexpected: end of input")
(expect "| at the end" (string-starts-with? (parsed "ls |") "error:") #t)
(expect "|| at the end" (string-starts-with? (parsed "ls ||") "error:") #t)
(expect "a ; first" (string-starts-with? (parsed "; ls") "error:") #t)
(expect "if" (parsed "if true; then ls; fi") "error: `if` is not supported")
(expect "for" (parsed "for x in a; do ls; done") "error: `for` is not supported")
(expect "a quoted if is a word" (parsed "'if' x") "\"if\" x ;")
(expect "( )" (parsed "(ls)") "error: subshells ( ) are not supported")
(expect "$(...)" (parsed "echo $(ls)") "error: $(...) is not supported yet")
(expect "`...`" (parsed "echo `ls`") "error: `...` is not supported")
(expect "here-documents" (parsed "cat <<EOF") "error: here-documents are not supported")
(expect "${NAME:-x}" (parsed "echo ${A:-x}")
"error: Expecting: '}' (only ${NAME} is supported) / Unexpected: ':'")
(expect "descriptors above 2" (parsed "ls 3>x")
"error: only <, >, >>, 2>, 2>>, 2>&1 and >&2 are supported")
(expect "a number before a space is a word" (parsed "echo 3 >x") "echo 3 >x ;")
(expect "the column of an open quote" (error-column "echo 'abc") 10)
0)