Permalink
Browse files

Change bool_parse.py to raise execeptions.

Explore errors for the test builtin, and slightly clean up error
messages.  They still could be better.

Addresses issue #27 and #103.
  • Loading branch information...
Andy Chu
Andy Chu committed Aug 22, 2018
1 parent ec4582f commit c4628578bf0b5bf48e1190c78403f80c3b5ab60d
Showing with 37 additions and 26 deletions.
  1. +6 −12 core/test_builtin.py
  2. +3 −14 osh/bool_parse.py
  3. +19 −0 test/parse-errors.sh
  4. +9 −0 test/runtime-errors.sh
View
@@ -163,19 +163,13 @@ def Test(argv, need_right_bracket):
if bool_node is None:
bool_node = b_parser.ParseForBuiltin()
#log('Bool expr %s', bool_node)
if bool_node is None:
for e in b_parser.Error():
log("test: %s", e.UserErrorString())
# TODO: There should be a nice method to print argv. And some way to
# point to the error.
log("Error parsing test/[ expression: %s", argv)
return 2 # parse error is 2
except util.ParseError as e:
util.error(e.UserErrorString())
return 2
util.error("test: %s", e.UserErrorString())
# TODO: There should be a nice method to print argv. And some way to point
# to the error.
log("Error parsing test expression: %s", argv)
return 2 # parse error is 2
# mem: Don't need it for BASH_REMATCH? Or I guess you could support it
# exec_opts: don't need it, but might need it later
@@ -190,7 +184,7 @@ def Test(argv, need_right_bracket):
except util.FatalRuntimeError as e:
# e.g. [ -t xxx ]
# TODO: Printing the location would be nice.
print('test: %s' % e.UserErrorString(), file=sys.stderr)
util.error('test: %s', e.UserErrorString())
return 2
status = 0 if b else 1
View
@@ -64,13 +64,6 @@ def __init__(self, w_parser):
self.error_stack = []
def Error(self):
return self.error_stack
def AddErrorContext(self, msg, *args, **kwargs):
err = util.ParseError(msg, *args, **kwargs)
self.error_stack.append(err)
def _NextOne(self, lex_mode=lex_mode_e.DBRACKET):
#print('_Next', self.cur_word)
n = len(self.words)
@@ -140,10 +133,8 @@ def ParseForBuiltin(self):
node = self.ParseExpr()
if self.op_id != Id.Eof_Real:
self.AddErrorContext(
'Unexpected trailing word in test expression: %s',
self.cur_word)
return None
p_die('Unexpected trailing word in test expression: %s',
self.cur_word, word=self.cur_word)
return node
@@ -261,9 +252,7 @@ def ParseFactor(self):
if not self._Next(): return None
node = self.ParseExpr()
if self.op_id != Id.Op_RParen:
self.AddErrorContext(
'Expected ), got %s', self.cur_word, word=self.cur_word)
return None
p_die('Expected ), got %s', self.cur_word, word=self.cur_word)
if not self._Next(): return None
return node
View
@@ -151,6 +151,24 @@ bool-expr() {
_error-case '[['
}
# These don't have any location information.
test-builtin() {
set +o errexit
# Extra token
_error-case '[ x -a y f ]'
_error-case 'test x -a y f'
# Missing closing ]
_error-case '[ x '
# Hm some of these errors are wonky. Need positions.
_error-case '[ x x ]'
# -o tests if an option is enabled.
#_error-case '[ -o x ]'
}
quoted-strings() {
set +o errexit
@@ -192,6 +210,7 @@ cases-in-strings() {
arith-expr
bool-expr
test-builtin
}
# Cases in their own file
View
@@ -258,6 +258,15 @@ string_to_intbase() {
echo 'SHOULD NOT GET HERE'
}
#
# Builtins
#
test_builtin() {
# xxx is not a valid file descriptor
[ -t xxx ]
}
#
# BOOLEAN ERRORS
#

0 comments on commit c462857

Please sign in to comment.