;; 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 ;") ;; $(...). (expect "$(...)" (parsed "echo $(ls -l)") "echo $(ls -l ;) ;") (expect "$(...) in a word" (parsed "echo a$(b)c") "echo a$(b ;)c ;") (expect "$(...) in double quotes" (parsed "echo \"x $(ls)\"") "echo \"x \"\"$(ls ;)\" ;") (expect "a pipe and a list in $(...)" (parsed "echo $(a | b; c && d)") "echo $(a | b ; c && d ;) ;") (expect "$(...) in $(...)" (parsed "echo $(echo $(ls))") "echo $(echo $(ls ;) ;) ;") (expect "quotes in $(...)" (parsed "echo $(echo ')')") "echo $(echo \")\" ;) ;") (expect "line breaks in $(...)" (parsed "echo $(\na\nb\n)") "echo $(a ; b ;) ;") (expect "an empty $(...)" (parsed "echo $()") "echo $() ;") (expect "$(...) in an assignment" (parsed "X=$(ls)") "X=$(ls ;) ;") ;; 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 out >>log") "sort out >>log ;") (expect "a space after the operator" (parsed "sort < in > out") "sort 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 out") "a 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 "an open $(" (parsed "echo $(ls") "error: Expecting: ')' to close $( / Unexpected: end of input") (expect "a ) without (" (parsed "echo )") "error: ')' without '('") (expect "`...`" (parsed "echo `ls`") "error: `...` is not supported") (expect "here-documents" (parsed "cat <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) ;; One pipe, for an alias. (expect "one pipe" (match (parse-pipe "ls -l | wc") ((Ok cs) (vec-length cs)) ((Err _) 0)) 2) (expect "a list is not one pipe" (parse-pipe "a; b") (Err "not one pipe: ;, &&, || and & are not allowed")) (expect "&& is not one pipe" (parse-pipe "a && b") (Err "not one pipe: ;, &&, || and & are not allowed")) (expect "& is not one pipe" (parse-pipe "a &") (Err "not one pipe: ;, &&, || and & are not allowed")) (expect "nothing is no pipe" (parse-pipe " ") (Err "no command")) ;; Text as in double quotes, for the prompt. (expect "text" (match (parse-text "a \"b\" $X $(c)") ((Ok ps) (word->string ps)) ((Err e) e)) "\"a \"\"\"\"\"b\"\"\"\"\" \"\"${X}\"\" \"\"$(c ;)\"") 0)