diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2014-03-12 20:42:40 +0200 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2014-03-12 20:42:40 +0200 |
commit | 23f6d4ab0e3f854197eb36c2025a94e70f6a194e (patch) | |
tree | b4e721e146775759af861ac563b396294fb3a200 /parsertest.py | |
parent | fc42ae04507d6723d0331a9f9b71d3328ec56ce4 (diff) | |
download | meson-23f6d4ab0e3f854197eb36c2025a94e70f6a194e.zip meson-23f6d4ab0e3f854197eb36c2025a94e70f6a194e.tar.gz meson-23f6d4ab0e3f854197eb36c2025a94e70f6a194e.tar.bz2 |
Something.
Diffstat (limited to 'parsertest.py')
-rwxr-xr-x | parsertest.py | 68 |
1 files changed, 60 insertions, 8 deletions
diff --git a/parsertest.py b/parsertest.py index 704c188..544aa74 100755 --- a/parsertest.py +++ b/parsertest.py @@ -24,10 +24,13 @@ class ParseException(Exception): self.colno = colno class Token: - def __init__(self, id, lineno, colno): - self.id = id + def __init__(self, tid, lineno, colno): + self.tid = tid self.lineno = lineno self.colno = colno + + def __eq__(self, other): + return self.tid == other.tid class Lexer: def __init__(self): @@ -96,11 +99,60 @@ class Lexer: if not matched: raise ParseException(lineno, col) +class Parser: + def __init__(self, code): + self.stream = Lexer().lex(code) + self.getsym() + + def getsym(self): + self.current = next(self.stream) + + def accept(self, s): + if self.current.tid == s: + self.getsym() + return True + return False + + def expect(self, s): + if self.accept(s): + return True + raise ParseException('Unknown token', s.lineno, s.colno) + + def parse(self): + self.codeblock() + + def statement(self): + if self.accept('('): + self.statement() + self.expect(')') + + + def line(self): + if self.accept('if'): + self.statement() + self.ifelseblock() + self.elseblock() + self.expect('endif') + if self.token == 'eol': + return + self.statement() + + def codeblock(self): + if self.accept('eol'): + return self.codeblock() + cond = True + while cond: + self.line() + cond = self.expect('eol') + if __name__ == '__main__': code = open(sys.argv[1]).read() - lex = Lexer() - try: - for i in lex.lex(code): - print('Token:', i.id, 'Line:', i.lineno, 'Column:', i.colno) - except ParseException as e: - print('Error line', e.lineno, 'column', e.colno)
\ No newline at end of file +# lex = Lexer() +# try: +# for i in lex.lex(code): +# print('Token:', i.tid, 'Line:', i.lineno, 'Column:', i.colno) +# except ParseException as e: +# print('Error line', e.lineno, 'column', e.colno) + parser = Parser(code) + parser.parse() + |