Permalink
Browse files

Fix arithmetic parse error, and PS4 ParseError handling.

The arithmetic error was caused by ; being miscategorized.  It would
become part of an invalid arithmetic expression tree, and then the
runtime would crash.

The PS4 error was introduced in previous commits.
  • Loading branch information...
Andy Chu
Andy Chu committed Aug 22, 2018
1 parent 9766004 commit 0381507d19a6840ac1d70eed2154e6780295ce34
Showing with 18 additions and 6 deletions.
  1. +10 −2 core/cmd_exec.py
  2. +4 −4 osh/arith_parse.py
  3. +4 −0 osh/arith_parse_test.py
View
@@ -1482,9 +1482,17 @@ def _EvalPS4(self):
# NOTE: Reading PS4 is just like reading a here doc line. "\n" is
# allowed too. The OUTER mode would stop at spaces, and ReadWord
# doesn't allow lex_mode_e.DQ.
ps4_word = w_parser.ReadHereDocBody()
ok = True
try:
ps4_word = w_parser.ReadHereDocBody()
except util.ParseError as e:
ok = False
else:
# TODO: Get rid of duplicate forms of error handling.
if not ps4_word:
ok = False
if not ps4_word:
if not ok:
error_str = '<ERROR: cannot parse PS4>'
t = ast.token(Id.Lit_Chars, error_str, const.NO_INTEGER)
ps4_word = ast.CompoundWord([ast.LiteralPart(t)])
View
@@ -122,14 +122,14 @@ def MakeShellSpec():
# -1 precedence -- doesn't matter
spec.Null(-1, tdop.NullConstant, [
Id.Word_Compound,
Id.Arith_Semi, # for loop
])
spec.Null(-1, tdop.NullError, [
Id.Arith_RParen, Id.Arith_RBracket, Id.Arith_Colon,
Id.Eof_Real, Id.Eof_RParen, Id.Eof_Backtick,
# Not in the arithmetic language, but is a common terminator, e.g.
# ${foo:1}
Id.Arith_RBrace,
# Not in the arithmetic language, but useful to define here.
Id.Arith_Semi, # terminates loops like for (( i = 0 ; ... ))
Id.Arith_RBrace, # terminates slices like ${foo:1}
])
# 0 precedence -- doesn't bind until )
View
@@ -159,6 +159,10 @@ def testErrors(self):
testSyntaxError('( 1')
testSyntaxError('(1 + (3 * 4)')
testSyntaxError('(1 + (3 * 4) 5') # Not valid, expr also fails.
testSyntaxError(';')
testSyntaxError('- ;')
#testSyntaxError('1 1')
#testSyntaxError('( 1 ) ( 2 )')

0 comments on commit 0381507

Please sign in to comment.