From b77d7967e9cf00002c6bc9fd1bbcf891a197115e Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 19 Feb 2016 17:36:43 -0500 Subject: Add error handling to lexer --- (limited to 'parsing/parse.sh') diff --git a/parsing/parse.sh b/parsing/parse.sh index 2ac4215..d85a42e 100644 --- a/parsing/parse.sh +++ b/parsing/parse.sh @@ -3,6 +3,15 @@ LF="$(printf '\n.')"; LF="${LF%.}" RS="$(printf '\036.')"; RS="${RS%.}" US="$(printf '\037.')"; US="${US%.}" +dbg() +{ + dbg=true + dbg=false + if ${dbg}; then + printf 'DEBUG: %s\n' "${@}" >&2 + fi +} + . ./tokens.sh . ./lexer.sh @@ -109,25 +118,43 @@ parse() shift 1 init_lexer "${fn}" + + # If this returns (does not exit), there are no errors. while complete_command; do :; done - if :; then # TODO: Test for EOF or errors - get_tokens - return 0 + + get_tokens + + return 0 +} + +try() +{ + local tokens= + local t= + + printf 'Trying script:\n' + printf '\t%s\n' "${@}" + if tokens="$(printf '%s\n' "${@}" | parse -)"; then + IFS="${RS}" + for t in ${tokens}; do + printf 'Token: %s\n' "$(tokname "${t}")" + case "${t%${US}*}" in T_WORD) + printf ' "%s"\n' "${t#T_WORD${US}}" + ;; + esac + done + unset IFS + else + printf 'FAIL\n' fi - return 1 + printf '\n\n' } -if tokens="$(printf '%s\n' '"foo bar" && $baz || qux' '${quux%uux } quuux' | \ - parse -)"; then - IFS="${RS}" - for t in ${tokens}; do - printf 'Token: %s\n' "$(tokname "${t}")" - case "${t%${US}*}" in T_WORD) - printf ' "%s"\n' "${t#T_WORD${US}}" - ;; - esac - done - unset IFS -else - echo FAIL -fi +try '"foo bar" && $baz || qux' '${quux%uux quuux' +try '"foo bar" && $baz || qux' '${quux%uux } quuux' +try 'foo ${bar}' +try 'foo ${#bar}' +try 'foo ${bar#baz}' +try 'foo ${#bar#}' +try 'foo ${^}' +try 'foo `bar`' -- cgit v0.9.1