Skip to content

Commit

Permalink
Remove the last vestiges of error code handling.
Browse files Browse the repository at this point in the history
Get rid of Error() and error_stack.  We're using exceptions now.

- Remove old comments
- Rename back to VirtualLineReader
  • Loading branch information
Andy Chu committed Sep 6, 2018
1 parent f52e2a4 commit c6346a6
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 57 deletions.
8 changes: 6 additions & 2 deletions core/reader.py
Expand Up @@ -95,8 +95,12 @@ def StringLineReader(s, arena):
# internally.


class HereDocLineReader(_Reader):
"""Used for here docs."""
class VirtualLineReader(_Reader):
"""Read from lines we already read from the OS.
Used for here docs and aliases.
"""

def __init__(self, lines, arena):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion core/reader_test.py
Expand Up @@ -35,7 +35,7 @@ def testLineReadersAreEquivalent(self):

a3 = self.pool.NewArena()
lines = [(0, 'one\n', 0), (1, 'two', 0)]
r3 = reader.HereDocLineReader(lines, a3)
r3 = reader.VirtualLineReader(lines, a3)

for a in [a1, a2, a3]:
a.PushSource('reader_test.py')
Expand Down
10 changes: 1 addition & 9 deletions core/tdop.py
Expand Up @@ -221,11 +221,6 @@ def __init__(self, spec, w_parser):
self.cur_word = None # current token
self.op_id = Id.Undefined_Tok

self.error_stack = []

def Error(self):
return self.error_stack

def _Led(self, token):
return self.spec.LookupLed(token)

Expand All @@ -245,10 +240,7 @@ def Eat(self, token_type):
def Next(self):
"""Preferred over Eat()? """
self.cur_word = self.w_parser.ReadWord(lex_mode_e.ARITH)
if self.cur_word is None:
error_stack = self.w_parser.Error()
self.error_stack.extend(error_stack)
p_die('Error reading arith word in ArithParser')
assert self.cur_word is not None
self.op_id = word.ArithId(self.cur_word)
return True

Expand Down
5 changes: 2 additions & 3 deletions osh/cmd_parse.py
Expand Up @@ -99,7 +99,7 @@ def _ParseHereDocBody(parse_ctx, h, line_reader, arena):
# LiteralPart for each line.
h.stdin_parts = _MakeLiteralHereLines(here_lines, arena)
else:
line_reader = reader.HereDocLineReader(here_lines, arena)
line_reader = reader.VirtualLineReader(here_lines, arena)
w_parser = parse_ctx.MakeWordParserForHereDoc(line_reader)
w_parser.ReadHereDocBody(h.stdin_parts) # fills this in

Expand Down Expand Up @@ -523,7 +523,6 @@ def _ExpandAliases(self, words, cur_aliases):
expanded.append(alias_exp)
i += 1


if not alias_exp.endswith(' '):
# alias e='echo [ ' is the same expansion as
# alias e='echo ['
Expand Down Expand Up @@ -571,7 +570,7 @@ def _ExpandAliases(self, words, cur_aliases):
self.arena.PopSource()

# TODO: Change name back to VirtualLineReader?
line_reader = reader.HereDocLineReader(line_info, self.arena)
line_reader = reader.VirtualLineReader(line_info, self.arena)
_, cp = self.parse_ctx.MakeParser(line_reader)

try:
Expand Down
16 changes: 0 additions & 16 deletions osh/parse_lib.py
Expand Up @@ -19,22 +19,6 @@ def InitLexer(s, arena):
return line_reader, lx


# When does line_reader change?
# - here docs
# - aliases
#
# WordParser gets the line_reader from either parse_state OR an explicit
# argument!
#
# self.parse_state.MakeParser(...)
#
# instead of CommandParser and WordParser holding the arena.
# NOTE: arith parser and bool parser never need to instantiate parsers
# only word/command parser have this dependency.
#
# common thing: word parser does not use arena OR aliases. But it needs to
# create a command parser.

class ParseContext(object):
"""Context shared between the mutually recursive Command and Word parsers.
Expand Down
13 changes: 1 addition & 12 deletions osh/word_parse.py
Expand Up @@ -87,8 +87,6 @@ def Reset(self, lex_mode=lex_mode_e.OUTER):
self.cursor = None
self.cursor_was_newline = False

self.error_stack = []

def _Peek(self):
"""Helper method."""
if self.next_lex_mode is not None:
Expand All @@ -107,9 +105,6 @@ def _Next(self, lex_mode):
"""
self.next_lex_mode = lex_mode

def Error(self):
return self.error_stack

def PrevToken(self):
"""Inspect state. Used by completion.
Expand Down Expand Up @@ -1095,13 +1090,7 @@ def ReadWord(self, lex_mode):
if not need_more:
break

# TODO: Change the interface of _ReadArithWord and _ReadWord.
# Error cases should raise ParseError.
# Then they should return either the word, or None to try again?
if not w:
error_stack = self.Error()
p_die('ReadWord: %s', error_stack[-1])

assert w is not None, w
self.cursor = w

# TODO: Do consolidation of newlines in the lexer?
Expand Down
18 changes: 4 additions & 14 deletions osh/word_parse_test.py
Expand Up @@ -46,11 +46,8 @@ def _assertReadWordWithArena(test, word_str):
print('\n---', word_str)
arena, w_parser = _InitWordParserWithArena(word_str)
w = w_parser.ReadWord(lex_mode_e.OUTER)
if w:
ast_lib.PrettyPrint(w)
else:
err = w_parser.Error()
test.fail("Couldn't parse %r: %s" % (word_str, err))
assert w is not None
ast_lib.PrettyPrint(w)

# Next word must be Eof_Real
w2 = w_parser.ReadWord(lex_mode_e.OUTER)
Expand Down Expand Up @@ -353,10 +350,7 @@ def testRead(self):

while True:
w = w_parser.ReadWord(lex_mode_e.OUTER)
if w is None:
e = w_parser.Error()
print('Error in word parser: %s' % e)
self.fail(e)
assert w is not None

ast_lib.PrettyPrint(w)

Expand Down Expand Up @@ -455,11 +449,7 @@ def testReadArith(self):

while True:
w = w_parser.ReadWord(lex_mode_e.ARITH)
if not w:
err = w_parser.Error()
print('ERROR', err)
self.fail(err)
break
assert w is not None
ast_lib.PrettyPrint(w)
if word.CommandId(w) in (Id.Eof_Real, Id.Unknown_Tok):
break
Expand Down

0 comments on commit c6346a6

Please sign in to comment.