def p_error(p): def find_column(input, lexpos): last_cr = input.rfind('\n', 0, lexpos) if last_cr < 0: last_cr = 0 column = (lexpos - last_cr) + 1 return column if parser.recover_errors: if p is None: e = yacc.YaccSymbol() e.type = 'error' e.value = None e.lineno = 0 e.lexpos = lexer.lexpos parser.errok() return e else: while lexer.lexstatestack: lexer.pop_state() if not p: raise SyntaxError("Parse error") column = find_column(parser.input, p.lexpos) raise SyntaxError("Invalid token '{0}' at line {1}, column {2}".format( p.value, p.lineno, column))
def p_error(p): if parser.recover_errors: if p is None: e = yacc.YaccSymbol() e.type = 'error' e.value = None e.lineno = 0 e.lexpos = lexer.lexpos parser.errok() return e else: if not p: raise SyntaxError("Parse error") raise SyntaxError("Invalid token '{0}' at line {1}, column {2}".format( p.value, p.lineno, p.lexpos))
def parse(script): parser = yacc.yacc() # parser.restart() is broken, so sym = yacc.YaccSymbol() sym.type = '$end' parser.symstack = [sym] parser.statestack = [0] parser.state = None parser.token = None try: parser.parse(script, lexer=lexer) except LexError as e: raise PwsSyntaxError(e) return last_eval
def token(token_type): t = yacc.YaccSymbol() t.type = token_type t.value = None return t