Comments
Patch
@@ -16,16 +16,27 @@
class Input(object):
def __init__(self, fp):
self.fp = fp
+ self.filename = fp.name
+ self.lineno = 0
self.line = None
self.offset = 0
self.is_eof = False
self.__fill_buf()
+ def __repr__(self):
+ return "%s:%d" % (str(self.filename), self.lineno)
+
def __fill_buf(self):
if not self.line and not self.is_eof:
self.line = self.fp.readline()
if not self.line:
self.is_eof = True
+ else:
+ self.lineno = self.lineno + 1
+
+ def set_next_line(self, filename, lineno):
+ self.filename = filename
+ self.lineno = lineno - 1
def peek(self):
if self.is_eof:
@@ -101,12 +112,12 @@ class Lexer(object):
def pop_expected(self, type_expected=None, value_expected=None):
self.__ensure_token()
if self.current_type != type_expected:
- raise Exception("expected '%s', got %s %s" %
- (type_expected, self.current_type, self.current_value))
+ raise Exception("%s: expected '%s', got %s %s" %
+ (self.input, type_expected, self.current_type, self.current_value))
if value_expected != None:
if self.current_value != value_expected:
- raise Exception("expected '%s', got %s" %
- (value_expected, self.current_value))
+ raise Exception("%s: expected '%s', got %s" %
+ (self.input, value_expected, self.current_value))
return self.pop()
def check_token(self, type_expected, value_expected=None):
@@ -300,7 +311,11 @@ class CLexer(Lexer):
token += ch
self.input.pop()
ch = self.input.peek()
- return ('directive', token)
+ if token[1] == ' ':
+ tokens = token.split()
+ self.input.set_next_line(tokens[2][1:-1], int(tokens[1]))
+ else:
+ return ('directive', token)
else:
return ('unknown', ch)
return (None, None)