aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2015-01-05 07:09:20 +0100
committerMatěj Cepl <mcepl@cepl.eu>2015-01-05 12:59:40 +0100
commita9e311030533ac6c175e2289e8928e4aae98b6c3 (patch)
tree61824b7a755d6e1ed942192d0cf7ec99c2be0f17
parente48572bca289eba580b96ca6273c0a3fb4e7d47f (diff)
downloadpygn-a9e311030533ac6c175e2289e8928e4aae98b6c3.tar.gz
Correctly generating abstract tree (as a list) of all tokens.
-rwxr-xr-xtest/test_wlp.py20
-rw-r--r--wlp_yacc.py16
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', '<kame@innocent.com>'),
+ [[Token('VAR', 'From:'),
+ Token('VAL', "'ME'"),
+ [Token('VAR', 'Sender:'), Token('VAL', '"Cosimo"')],
+ [Token('VAR', 'Reply-to'), Token('VAL', '"me"')]]]],
+ [[Token('OWNER', '<alfarano@students.cs.unibo.it>'),
+ [[Token('VAR', 'From:'),
+ Token('VAL', "'Cosimo Alfarano'"),
+ [Token('VAR', 'X-Firstname:'), Token('VAL', "'Cosimo'")]]]]]],
+ [[Token('OWNER', '<kame@innocent.com>'),
+ [[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()