Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: tools/lexer_generator/rule_parser_test.py

Issue 48783004: Experimental lexer generator: fixing the rule parser + adding tests. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/rule_parser.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2013 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are
4 # met:
5 #
6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided
11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 import unittest
29 from rule_parser import RuleParser
30
31 class RuleParserTestCase(unittest.TestCase):
32
33 def setUp(self):
34 self.parser = RuleParser()
35 self.parser.build()
36
37 def test_basic(self):
38 self.parser.parse('alias = regex1;')
39 self.parser.parse('<cond1> regex2 :=> cond2')
40 self.parser.parse('<cond2> regex3 {body}')
41 self.parser.parse('<cond3> regex4 => cond4 {body}')
42
43 self.assertTrue(len(self.parser.aliases), 1)
44 self.assertTrue('alias' in self.parser.aliases)
45 self.assertEquals(self.parser.aliases['alias'], 'regex1')
46
47 self.assertTrue(len(self.parser.transitions), 2)
48 self.assertTrue('cond1' in self.parser.transitions)
49 self.assertEquals(len(self.parser.transitions['cond1']), 1)
50 self.assertTrue('regex2' in self.parser.transitions['cond1'])
51 self.assertEquals(self.parser.transitions['cond1']['regex2'],
52 ('condition', 'cond2'))
53
54 self.assertTrue('cond2' in self.parser.transitions)
55 self.assertEquals(len(self.parser.transitions['cond2']), 1)
56 self.assertTrue('regex3' in self.parser.transitions['cond2'])
57 self.assertEquals(self.parser.transitions['cond2']['regex3'],
58 ('body', 'body'))
59
60 self.assertTrue('cond3' in self.parser.transitions)
61 self.assertEquals(len(self.parser.transitions['cond3']), 1)
62 self.assertTrue('regex4' in self.parser.transitions['cond3'])
63 self.assertEquals(self.parser.transitions['cond3']['regex4'],
64 ('condition_and_body', 'cond4', 'body'))
65
66 def test_more_complicated(self):
67 self.parser.parse('alias = regex;with;semicolon;')
68 self.parser.parse('<cond1> regex2 :=> with :=> arrow :=> cond2')
69 self.parser.parse('<cond1> regex3}with}braces} {body {with} braces } }')
70 self.parser.parse('<cond1> regex4{with{braces} {body {with} braces } }')
71
72 self.assertEquals(self.parser.aliases['alias'], 'regex;with;semicolon')
73
74 self.assertEquals(
75 self.parser.transitions['cond1']['regex2 :=> with :=> arrow'],
76 ('condition', 'cond2'))
77
78 self.assertEquals(
79 self.parser.transitions['cond1']['regex3}with}braces}'],
80 ('body', 'body {with} braces }'))
81
82 self.assertEquals(
83 self.parser.transitions['cond1']['regex4{with{braces}'],
84 ('body', 'body {with} braces }'))
85
86 def test_body_with_if(self):
87 self.parser.parse('<cond> regex { if (foo) { bar } }')
88 self.assertEquals(
89 self.parser.transitions['cond']['regex'],
90 ('body', 'if (foo) { bar }'))
91
92 def test_regexp_with_count(self):
93 self.parser.parse('<cond> regex{1,3} { if (foo) { bar } }')
94 self.assertEquals(
95 self.parser.transitions['cond']['regex{1,3}'],
96 ('body', 'if (foo) { bar }'))
97
98 if __name__ == '__main__':
99 unittest.main()
OLDNEW
« no previous file with comments | « tools/lexer_generator/rule_parser.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698