|
|
@ -127,18 +127,21 @@ class Parser: |
|
|
|
self.getsym() |
|
|
|
self.getsym() |
|
|
|
|
|
|
|
|
|
|
|
def getsym(self): |
|
|
|
def getsym(self): |
|
|
|
self.current = next(self.stream) |
|
|
|
try: |
|
|
|
|
|
|
|
self.current = next(self.stream) |
|
|
|
|
|
|
|
except StopIteration: |
|
|
|
|
|
|
|
self.current = Token('eof', 0, 0) |
|
|
|
|
|
|
|
|
|
|
|
def accept(self, s): |
|
|
|
def accept(self, s): |
|
|
|
if self.current.tid == s: |
|
|
|
if self.current.tid == s: |
|
|
|
self.getsym() |
|
|
|
self.getsym() |
|
|
|
return True |
|
|
|
return True |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def expect(self, s): |
|
|
|
def expect(self, s): |
|
|
|
if self.accept(s): |
|
|
|
if self.accept(s): |
|
|
|
return True |
|
|
|
return True |
|
|
|
raise ParseException('Unknown token ' + self.current.tid, self.current.lineno, self.current.colno) |
|
|
|
raise ParseException('Expecting %s got %s.' % (s, self.current.tid), self.current.lineno, self.current.colno) |
|
|
|
|
|
|
|
|
|
|
|
def parse(self): |
|
|
|
def parse(self): |
|
|
|
self.codeblock() |
|
|
|
self.codeblock() |
|
|
@ -210,10 +213,6 @@ class Parser: |
|
|
|
if self.accept('comma'): |
|
|
|
if self.accept('comma'): |
|
|
|
self.args() |
|
|
|
self.args() |
|
|
|
|
|
|
|
|
|
|
|
def rest_statement(self): |
|
|
|
|
|
|
|
if self.accept('.'): |
|
|
|
|
|
|
|
self.method_call() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def method_call(self): |
|
|
|
def method_call(self): |
|
|
|
self.e8() |
|
|
|
self.e8() |
|
|
|
self.expect('lparen') |
|
|
|
self.expect('lparen') |
|
|
@ -243,7 +242,7 @@ class Parser: |
|
|
|
self.expect('rparen') |
|
|
|
self.expect('rparen') |
|
|
|
|
|
|
|
|
|
|
|
def ifelseblock(self): |
|
|
|
def ifelseblock(self): |
|
|
|
if self.accept('elif'): |
|
|
|
while self.accept('elif'): |
|
|
|
self.statement() |
|
|
|
self.statement() |
|
|
|
self.expect('eol') |
|
|
|
self.expect('eol') |
|
|
|
self.codeblock() |
|
|
|
self.codeblock() |
|
|
@ -268,7 +267,9 @@ class Parser: |
|
|
|
cond = True |
|
|
|
cond = True |
|
|
|
while cond: |
|
|
|
while cond: |
|
|
|
self.line() |
|
|
|
self.line() |
|
|
|
cond = self.expect('eol') or self.current == 'elif' or self.current == 'else' |
|
|
|
cond = self.accept('eol') |
|
|
|
|
|
|
|
if self.current == 'elif' or self.current == 'else': |
|
|
|
|
|
|
|
cond = False |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
if __name__ == '__main__': |
|
|
|
code = open(sys.argv[1]).read() |
|
|
|
code = open(sys.argv[1]).read() |
|
|
|