Skip to content

Commit

Permalink
cleaned up error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Nov 17, 2016
1 parent bb5c21c commit a7188fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
26 changes: 11 additions & 15 deletions smop/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,9 @@ def t_STRING(t):
@TOKEN(r"(\.%s)?%s" % (ws0, id))
def t_IDENT(t):
if t.value == "classdef":
column = t.lexer.lexpos - t.lexer.lexdata.rfind("\n", 0,
t.lexer.lexpos)
raise NotImplementedError("classdef",
(options.filename,
t.lexer.lineno,
column,
t.value))
raise_exception(SyntaxError,
"Not implemented: %s" % t.value,
t.lexer)
t.lexer.lineno += t.value.count("\n")
if t.value[0] == ".":
# Reserved words are not reserved
Expand Down Expand Up @@ -319,13 +315,7 @@ def t_SPACES(t):
pass

def t_error(t):
column = t.lexer.lexpos - t.lexer.lexdata.rfind("\n", 0,
t.lexer.lexpos)
raise SyntaxError("invalid character",
(options.filename,
t.lexer.lineno,
column,
t.value[0]))
raise_exception(SyntaxError, ('Unexpected "%s"' % t.value), t.lexer)

lexer = lex.lex(reflags=re.MULTILINE)
lexer.brackets = 0 # count open square brackets
Expand All @@ -334,7 +324,13 @@ def t_error(t):
lexer.stack = []
return lexer


def raise_exception(error_type, message, my_lexer):
startpos = 1 + my_lexer.lexdata.rfind("\n", 0, my_lexer.lexpos)
endpos = my_lexer.lexdata.find("\n", startpos)
raise error_type(message, (options.filename,
my_lexer.lineno,
1 + my_lexer.lexpos - startpos,
my_lexer.lexdata[startpos:endpos]))
def main():
lexer = new()
line = ""
Expand Down
1 change: 1 addition & 0 deletions smop/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def main():
fp.write(s)

except:
print 40*"="
traceback.print_exc()
if not options.ignore_errors:
return
Expand Down
33 changes: 14 additions & 19 deletions smop/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import ply.yacc as yacc
import lexer
from lexer import tokens
from lexer import tokens, raise_exception
import node
from node import exceptions
import options
Expand Down Expand Up @@ -317,10 +317,9 @@ def p_expr2(p):
"""
if p[2] == "=":
if p[1].__class__ is node.let:
raise NotImplementedError("assignment as expression",
(options.filename,
p.lineno(2)))

raise_exception(SyntaxError,
"Not implemented assignment as expression",
new_lexer)
# The algorithm, which decides if an
# expression F(X)
# is arrayref or funcall, is implemented in
Expand Down Expand Up @@ -361,9 +360,9 @@ def p_expr2(p):
# TBD: mark idents as "P" - persistent
if p[3].__class__ not in (node.ident, node.funcall
): #, p[3].__class__
raise NotImplementedError("multi-assignment",
(options.filename,
p.lineno(2)))
raise_exception(SyntaxError,
"multi-assignment",
new_lexer)
if p[3].__class__ is node.ident:
# [A1(B1) A2(B2) ...] = F implied F()
# import pdb; pdb.set_trace()
Expand All @@ -380,7 +379,7 @@ def p_expr2(p):

# elif p[2] == "." and isinstance(p[3],node.expr) and p[3].op=="parens":
# p[0] = node.getfield(p[1],p[3].args[0])
# raise NotImplementedError(p[3],p.lineno(3),p.lexpos(3))
# raise SyntaxError(p[3],p.lineno(3),p.lexpos(3))
elif p[2] == ":" and isinstance(p[1], node.expr) and p[1].op == ":":
# Colon expression means different things depending on the
# context. As an array subscript, it is a slice; otherwise,
Expand Down Expand Up @@ -519,7 +518,7 @@ def p_for_stmt(p):
"""
if len(p) == 8:
if not isinstance(p[2], node.ident):
raise NotImplementedError("for loop", (options.filename, p.lineno(0)))
raise_exception(SyntaxError, "Not implemented: for loop", new_lexer)
p[2].props = "I" # I= for-loop iteration variable
p[0] = node.for_stmt(ident=p[2], expr=p[4], stmt_list=p[6])

Expand Down Expand Up @@ -825,19 +824,15 @@ def p_while_stmt(p):

@exceptions
def p_error(p):
if p is None:
raise_exception(SyntaxError, "Unexpected EOF", new_lexer)
if p.type == "COMMENT":
# print "Discarded comment", p.value
parser.errok()
return
if p.type == "END_UNEXPECTED":
raise SyntaxError('Unexpected "end"',
options.filename)
raise SyntaxError(p.type,
(options.filename,
p.lineno,
p.lexpos))
#p.value))

raise_exception(SyntaxError,
('Unexpected "%s"' % p.value),
new_lexer)
parser = yacc.yacc(start="top")


Expand Down

0 comments on commit a7188fa

Please sign in to comment.