Permalink
Browse files

Change more errors to exceptions in cmd_parse.py

Addresses #27 and #103.
  • Loading branch information...
Andy Chu
Andy Chu committed Aug 23, 2018
1 parent 6136bbf commit b1b046f7052f56b94d8e54ef7bf61b33393141d1
Showing with 27 additions and 24 deletions.
  1. +9 −24 osh/cmd_parse.py
  2. +18 −0 test/parse-errors.sh
View
@@ -417,14 +417,12 @@ def _MakeAssignment(self, assign_kw, suffix_words):
# It sets global variables.
ok, static_val, quoted = word.StaticEval(w)
if not ok or quoted:
self.AddErrorContext(
'Variable names must be constant strings, got %s', w, word=w)
return None
p_die("Variable names must be unquoted constants", word=w)
# No value is equivalent to ''
if not match.IsValidVarName(static_val):
self.AddErrorContext('Invalid variable name %r', static_val, word=w)
return None
p_die('Invalid variable name %r', static_val, word=w)
a = (static_val, assign_op_e.Equal, None, left_spid)
assignments.append(a)
@@ -544,9 +542,7 @@ def ParseSimpleCommand(self):
if redirects:
binding1 = prefix_bindings[0]
_, _, _, spid = binding1
self.AddErrorContext('Got redirects in global assignment',
span_id=spid)
return None
p_die("Global assignment shouldn't have redirects", span_id=spid)
pairs = []
for lhs, op, rhs, spid in prefix_bindings:
@@ -582,17 +578,13 @@ def ParseSimpleCommand(self):
if redirects:
# Attach the error location to the keyword. It would be more precise
# to attach it to the
self.AddErrorContext('Got redirects in assignment', token=kw_token)
return None
p_die("Assignments shouldn't have redirects", token=kw_token)
if prefix_bindings: # FOO=bar local spam=eggs not allowed
# Use the location of the first value. TODO: Use the whole word
# before splitting.
_, _, v0, _ = prefix_bindings[0]
self.AddErrorContext(
'Invalid prefix bindings in assignment: %s', prefix_bindings,
word=v0)
return None
p_die("Assignments shouldn't have environment bindings", word=v0)
node = self._MakeAssignment(kw_token.id, suffix_words)
if not node: return None
@@ -605,24 +597,17 @@ def ParseSimpleCommand(self):
return None
if prefix_bindings: # FOO=bar local spam=eggs not allowed
# Use the location of the first value. TODO: Use the whole word before
# splitting.
# TODO: Change location as above
_, _, v0, _ = prefix_bindings[0]
self.AddErrorContext(
'Invalid prefix bindings in control flow: %s', prefix_bindings,
word=v0)
return None
p_die("Control flow shouldn't have environment bindings", word=v0)
# Attach the token for errors. (Assignment may not need it.)
if len(suffix_words) == 1:
arg_word = None
elif len(suffix_words) == 2:
arg_word = suffix_words[1]
else:
# Underline the extra word.
self.AddErrorContext(
'Unexpected argument to %r', kw_token.val, word=suffix_words[2])
return None
p_die('Unexpected argument to %r', kw_token.val, word=suffix_words[2])
return ast.ControlFlow(kw_token, arg_word)
View
@@ -192,8 +192,20 @@ quoted-strings() {
cmd-parse() {
set +o errexit
_error-case 'FOO=1 break'
_error-case 'break 1 2'
}
redirect() {
set +o errexit
_error-case 'echo < <<'
_error-case 'echo $( echo > >> )'
_error-case 'FOO=1 BAR=2 > out'
_error-case '> out FOO=1 BAR=2'
_error-case 'local BAR=2 > out'
}
simple-command() {
@@ -207,8 +219,12 @@ simple-command() {
assign() {
set +o errexit
_error-case 'local name$x'
_error-case 'local "ab"'
_error-case 'local a.b'
_error-case 'FOO=1 local foo=1'
}
# I can't think of any other here doc error conditions except arith/var/command
@@ -239,6 +255,8 @@ cases-in-strings() {
cmd-parse
simple-command
redirect
here-doc
# Word
word-parse

0 comments on commit b1b046f

Please sign in to comment.