Skip to content

Commit

Permalink
Improved exception handling.
Browse files Browse the repository at this point in the history
Parse no longer prints the errors, unless in debug. Parse now gathers all
the errors and throw them when it's done parsing (returning as many errors
as possible at once, without returning silently).
  • Loading branch information
erezsh committed Oct 10, 2012
1 parent 2783667 commit b236a2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion plyplus/__init__.py
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,2 @@
from plyplus import Grammar, SVisitor, STransformer, is_stree from plyplus import Grammar, SVisitor, STransformer, is_stree, PlyplusException


30 changes: 22 additions & 8 deletions plyplus/plyplus.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ def get_token_name(token, default):
']' : 'RSQB', ']' : 'RSQB',
}.get( token, default) }.get( token, default)


class GrammarException(Exception): pass class PlyplusException(Exception): pass

class GrammarException(PlyplusException): pass

class TokenizeError(PlyplusException): pass

class ParseError(PlyplusException): pass


class GetTokenDefs_Visitor(SVisitor): class GetTokenDefs_Visitor(SVisitor):
def __init__(self, dict_to_populate): def __init__(self, dict_to_populate):
Expand Down Expand Up @@ -424,7 +430,7 @@ def __init__(self, grammar, **options):


grammar_tree = grammar_parser.parse(grammar) #, debug=options.get('debug',False)) grammar_tree = grammar_parser.parse(grammar) #, debug=options.get('debug',False))
if not grammar_tree: if not grammar_tree:
raise GrammarException("Parse Error") raise GrammarException("Parse Error: Could not create grammar")


self._grammar = _Grammar(grammar_tree, source, tab_filename, **options) self._grammar = _Grammar(grammar_tree, source, tab_filename, **options)


Expand Down Expand Up @@ -502,9 +508,12 @@ def lex(self, text):
return toks return toks


def parse(self, text): def parse(self, text):
self.errors = []
tree = self.parser.parse(text, lexer=self.lexer, debug=self.debug) tree = self.parser.parse(text, lexer=self.lexer, debug=self.debug)
if not tree: if not tree:
raise Exception("Parse error!") self.errors.append("Could not create parse tree!")
if self.errors:
raise ParseError('\n'.join(self.errors))


# Apply subgrammars # Apply subgrammars
if self.subgrammars: if self.subgrammars:
Expand All @@ -521,7 +530,7 @@ def _add_option(self, name, defin):
if name == '%newline_char': if name == '%newline_char':
self._newline_value = eval(defin) # XXX BAD BAD! I have TODO it differently self._newline_value = eval(defin) # XXX BAD BAD! I have TODO it differently
else: else:
print "Unknown option:", name raise GrammarException( "Unknown option: %s " % name )


@staticmethod @staticmethod
def _unescape_token_def(token_def): def _unescape_token_def(token_def):
Expand Down Expand Up @@ -609,16 +618,21 @@ def _add_rule(self, rule_name, rule_def):


@staticmethod @staticmethod
def t_error(t): def t_error(t):
raise Exception("Illegal character in input: '%s', line: %s, %s" % (t.value[:32], t.lineno, t.type)) raise TokenizeError("Illegal character in input: '%s', line: %s, %s" % (t.value[:32], t.lineno, t.type))


def p_error(self, p): def p_error(self, p):
if p: if p:
if isinstance(p.value, TokValue): if isinstance(p.value, TokValue):
print "Syntax error in input at '%s' (type %s)" % (p.value,p.type), 'line',p.value.line, 'col', p.value.column msg = "Syntax error in input at '%s' (type %s) line %s col %s" % (p.value, p.type, p.value.line, p.value.column)
else: else:
print "Syntax error in input at '%s' (type %s)" % (p.value,p.type), 'line',p.lineno msg = "Syntax error in input at '%s' (type %s) line %s" % (p.value, p.type, p.lineno)
else: else:
print "Syntax error in input (details unknown)", p msg = "Syntax error in input (details unknown)", p

if self.debug:
print msg

self.errors.append(msg)


start = "start" start = "start"


Expand Down

0 comments on commit b236a2c

Please sign in to comment.