diff --git a/parse.py b/parse.py index 863625d..b6b2981 100644 --- a/parse.py +++ b/parse.py @@ -4,15 +4,22 @@ import log import ast_types -class ParseContext: - def __init__(self, parent, context, line, column): - self.parent = parent - self.context = context +class ParseLocation: + def __init__(self, line, column): self.line = line self.column = column def __repr__(self): - return "ParseContext(parent %s, context '%s', line %i, column %i)" % (self.parent, self.context, self.line, self.column) + return "ParseLocation(line %i, column %i)" % (self.line, self.column) + +class ParseContext: + def __init__(self, parent, context, location): + self.parent = parent + self.context = context + self.location = location + + def __repr__(self): + return "ParseContext(parent %s, context '%s', location %s)" % (self.parent, self.context, self.location) class ParseError(BaseException): def __init__(self, context, error): @@ -23,14 +30,13 @@ return "ParseError(context %s, error '%s')" % (self.context, self.error) class Token: - def __init__(self, type, value, line, column): + def __init__(self, type, value, location): self.type = type self.value = value - self.line = line - self.column = column + self.location = location def __repr__(self): - return "Token(type %s, value '%s', line %i column %i)" % (self.type, self.value, self.line, self.column) + return "Token(type %s, value '%s', location %s)" % (self.type, self.value, self.location) def is_whitespace(symbol): return symbol == " " or symbol == "\t" or symbol == "\n" @@ -78,7 +84,7 @@ def skip_note(self, line, column): log.log(log.LEXER, log.TRACE, "Skipping tokens until EndNote") - context = ParseContext(None, "reading note", line, column) + context = ParseContext(None, "reading note", ParseLocation(line, column)) (token, _, _) = self.read_token() while token and token != "EndNote": (token, _, _) = self.read_token() @@ -87,7 +93,7 @@ def read_text(self, line, column): log.log(log.LEXER, log.TRACE, "Reading characters until EndText") - context = ParseContext(None, "reading text", line, column) + context = ParseContext(None, "reading text", ParseLocation(line, column)) start = self.pos (token, _, _) = self.read_token() while token and token != "EndText": @@ -124,12 +130,12 @@ else: type = "symbol" value = token - tok = Token(type, value, line, column) + tok = Token(type, value, ParseLocation(line, column)) log.log(log.LEXER, log.DEBUG, "Appending %s" % (tok)) tokens.append(tok) (token, line, column) = self.read_token() log.log(log.LEXER, log.TRACE, "Done tokenizing, adding EOF") - tokens.append(Token("EOF", None, self.pos_line, self.pos_column)) + tokens.append(Token("EOF", None, ParseLocation(self.pos_line, self.pos_column))) log.log(log.LEXER, log.DEBUG, "Tokens are %s" % (tokens)) return tokens @@ -155,7 +161,7 @@ def create_context(self, context, text): token = self.tokens[self.pos] - return ParseContext(context, text, token.line, token.column) + return ParseContext(context, text, token.location) def parse_version(self, context): log.log(log.PARSER, log.TRACE, "Parsing version identifier...") @@ -305,7 +311,9 @@ print("Parse error: %s" % (e.error)) context = e.context while context: - print("While %s at line %i column %i" % (context.context, context.line, context.column)) + line = context.location.line + column = context.location.column + print("While %s at line %i column %i" % (context.context, line, column)) context = context.parent print("While parsing file %s" % (filename)) return None