Skip to content

Commit

Permalink
word_parse.py: Change more error cases to use the exception style.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Aug 30, 2018
1 parent b39592d commit 16559bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
28 changes: 8 additions & 20 deletions osh/word_parse.py
Expand Up @@ -141,10 +141,9 @@ def _ReadVarOpArg(self, arg_lex_mode, eof_type=Id.Undefined_Tok,
self._Next(arg_lex_mode)
self._Peek()

w = self._ReadCompoundWord(
lex_mode=arg_lex_mode, eof_type=eof_type, empty_ok=empty_ok)
if not w:
return None
w = self._ReadCompoundWord(lex_mode=arg_lex_mode, eof_type=eof_type,
empty_ok=empty_ok)
assert w is not None

# This is for "${s:-}", ${s/a//}, etc. It is analogous to
# LooksLikeAssignment where we turn x= into x=''. It has the same
Expand Down Expand Up @@ -895,9 +894,7 @@ def _ReadArrayLiteralPart(self):
words = []
while True:
w = w_parser.ReadWord(lex_mode_e.OUTER)
if not w:
self.error_stack.extend(w_parser.Error())
return None
assert w is not None

if w.tag == word_e.TokenWord:
word_id = word.CommandId(w)
Expand Down Expand Up @@ -959,9 +956,7 @@ def _ReadCompoundWord(self, eof_type=Id.Undefined_Tok,
if t.id == Id.Op_LParen:
self.lexer.PushHint(Id.Op_RParen, Id.Right_ArrayLiteral)
part2 = self._ReadArrayLiteralPart()
if not part2:
self.AddErrorContext('_ReadArrayLiteralPart failed')
return False
assert part2 is not None
word.parts.append(part2)

elif self.token_kind == Kind.VSub:
Expand All @@ -970,15 +965,13 @@ def _ReadCompoundWord(self, eof_type=Id.Undefined_Tok,

elif self.token_kind == Kind.ExtGlob:
part = self._ReadExtGlobPart()
if not part:
return None
assert part is not None
word.parts.append(part)

elif self.token_kind == Kind.Left:
#print('_ReadLeftParts')
part = self._ReadLeftParts()
if not part:
return None
assert part is not None
word.parts.append(part)

# NOT done yet, will advance below
Expand Down Expand Up @@ -1051,8 +1044,6 @@ def _ReadArithWord(self):

elif self.token_kind in (Kind.Lit, Kind.Left):
w = self._ReadCompoundWord(lex_mode=lex_mode_e.ARITH)
if not w:
return None, True
return w, False

elif self.token_kind == Kind.VSub:
Expand Down Expand Up @@ -1125,10 +1116,7 @@ def _ReadWord(self, lex_mode):

else:
w = self._ReadCompoundWord(lex_mode=lex_mode)
if not w:
self.AddErrorContext(
'Error reading command word', token=self.cur_token)
return None, False
assert w is not None
return w, False

else:
Expand Down
1 change: 1 addition & 0 deletions test/parse-errors.sh
Expand Up @@ -93,6 +93,7 @@ array-literal() {
_error-case 'a=(1 & 2)'
_error-case 'a= (1 2)'
_error-case 'a=(1 2'
_error-case 'a=(1 ${2@} )' # error in word inside array literal
}

arith-context() {
Expand Down

0 comments on commit 16559bd

Please sign in to comment.