Skip to content

Commit

Permalink
Remove the older form of parse error handling.
Browse files Browse the repository at this point in the history
Simplify parsing of $PS4 as well.

All unit tests and spec tests pass.
  • Loading branch information
Andy Chu committed Aug 30, 2018
1 parent cae0c27 commit fd737dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 50 deletions.
31 changes: 3 additions & 28 deletions bin/oil.py
Expand Up @@ -127,20 +127,7 @@ def InteractiveLoop(opts, ex, c_parser, arena):
c_parser.Reset()
c_parser.ResetInputObjects()
continue
#log('parsed node: %s', node)

# Failed parse.
# TODO: Need an error for an empty command, which we ignore? GetLine
# could do that in the first position?
# ParseSimpleCommand fails with '\n' token?
if not node:
e = c_parser.Error()
# NOTE: This is a bit verbose.
ui.PrintErrorStack(e, arena)

c_parser.Reset()
c_parser.ResetInputObjects()
continue
assert node is not None

if ast_f:
ast_lib.PrettyPrint(node)
Expand Down Expand Up @@ -333,13 +320,7 @@ def OshMain(argv0, argv, login_shell):
except util.ParseError as e:
ui.PrettyPrintError(e, arena, sys.stderr)
return 2
else:
# TODO: Remove this older form of error handling.
if not node:
err = c_parser.Error()
assert err, err # can't be empty
ui.PrintErrorStack(err, arena)
return 2 # parse error is code 2
assert node is not None

do_exec = True
if opts.fix:
Expand Down Expand Up @@ -506,13 +487,7 @@ def OshCommandMain(argv):
except util.ParseError as e:
ui.PrettyPrintError(e, arena, sys.stderr)
return 2
else:
# TODO: Remove this older form of error handling.
if not node:
err = c_parser.Error()
assert err, err # can't be empty
ui.PrintErrorStack(err, arena)
return 2 # parse error is code 2
assert node is not None

f.close()

Expand Down
24 changes: 2 additions & 22 deletions core/cmd_exec.py
Expand Up @@ -218,19 +218,11 @@ def ParseTrapCode(self, code_str):
self.arena.PushSource(source_name)

try:
# TODO: Get rid of duplicate error handling:
err = None
try:
node = c_parser.ParseWholeFile()
except util.ParseError as e:
err = e
else:
if not node:
err = c_parser.Error()

if err:
util.error('Parse error in %r:', source_name)
ui.PrintErrorStack(err, self.arena, sys.stderr)
ui.PrettyPrintError(e, self.arena, sys.stderr)
return None

finally:
Expand Down Expand Up @@ -1482,21 +1474,9 @@ def _EvalPS4(self):
# We have to parse this at runtime. PS4 should usually remain constant.
w_parser = parse_lib.MakeWordParserForPlugin(ps4, self.arena)

# 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.
ok = True
ps4_word = ast.CompoundWord()
try:
w_parser.ReadHereDocBody(ps4_word.parts)
ps4_word = w_parser.ReadPS()
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 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)])
Expand Down
9 changes: 9 additions & 0 deletions osh/word_parse.py
Expand Up @@ -1160,3 +1160,12 @@ def ReadHereDocBody(self, parts):
"""
self._ReadLikeDQ(None, parts)
# Returns nothing

def ReadPS(self):
"""For $PS1, $PS4, etc."""
# 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.
w = ast.CompoundWord()
self._ReadLikeDQ(None, w.parts)
return w

0 comments on commit fd737dd

Please sign in to comment.