From a9e311030533ac6c175e2289e8928e4aae98b6c3 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Mon, 5 Jan 2015 07:09:20 +0100 Subject: Correctly generating abstract tree (as a list) of all tokens. --- test/test_wlp.py | 20 +++++++++++++++++--- wlp_yacc.py | 16 +++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/test/test_wlp.py b/test/test_wlp.py index db6085f..9825e44 100755 --- a/test/test_wlp.py +++ b/test/test_wlp.py @@ -53,11 +53,25 @@ class TestWLP(unittest.TestCase): self.assertEqual(tokens, expected_stream) def test_wlp_parser(self): + expected_tree = [ + [[Token('OWNER', ''), + [[Token('VAR', 'From:'), + Token('VAL', "'ME'"), + [Token('VAR', 'Sender:'), Token('VAL', '"Cosimo"')], + [Token('VAR', 'Reply-to'), Token('VAL', '"me"')]]]], + [[Token('OWNER', ''), + [[Token('VAR', 'From:'), + Token('VAL', "'Cosimo Alfarano'"), + [Token('VAR', 'X-Firstname:'), Token('VAL', "'Cosimo'")]]]]]], + [[Token('OWNER', ''), + [[Token('VAR', 'From:'), + Token('VAL', "'kame@inwind.it'"), + [Token('VAR', 'Reply-to:'), Token('VAL', '"KA"')], + [Token('VAR', 'Sender:'), Token('VAL', '"Kalfa"')]]]]] + ] lex_stream = wlp_lex.lexer.lex(self.test_input) tree = wlp_yacc.parser.parse(lex_stream) - logging.debug('tree = %s', tree) - logging.debug('tree = dir %s', dir(tree)) - self.assertEqual(tree, []) + self.assertEqual(tree, expected_tree) def test_wlp_C_parser(self): wlp.setfilebyname('examples/whitelist.example') diff --git a/wlp_yacc.py b/wlp_yacc.py index a16f530..910ed1f 100644 --- a/wlp_yacc.py +++ b/wlp_yacc.py @@ -68,9 +68,15 @@ def main(p): # blockstatement # | block blockstatement @pg.production('block : blockstatement') -@pg.production('block : block blockstatement') def block(p): logging.debug('block p = %s', p) + return p[0] + + +@pg.production('block : block blockstatement') +def block_blockstatement(p): + logging.debug('block p = %s', p) + return [p[0], [p[1]]] # blockstatement: @@ -78,21 +84,25 @@ def block(p): @pg.production('blockstatement : OWNER commandline') def blockstatement(p): logging.debug('blockstatement p = %s', p) + return [p[0], [p[1]]] # commandline: # command | commandline command @pg.production('commandline : command') -@pg.production('commandline : commandline command') def commandline(p): logging.debug('commandline p = %s', p) + return p[0] +@pg.production('commandline : commandline command') +def commandline_command(p): + return p[0] + [p[1]] # command: # varpart '=' valpart { found(left,right,owner); } @pg.production('command : VAR VAL') def command(p): logging.debug('command p = %s', p) - return (p[0], p[1]) + return [p[0], p[1]] parser = pg.build() -- cgit