| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| 11 # with the distribution. | 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its | 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived | 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. | 14 # from this software without specific prior written permission. |
| 15 # | 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 import unittest | 28 import unittest |
| 29 from lexer import Lexer | 29 from generator import Generator |
| 30 | 30 |
| 31 class LexerTestCase(unittest.TestCase): | 31 class LexerTestCase(unittest.TestCase): |
| 32 | 32 |
| 33 def __verify_action_stream(self, stream, expected_stream): | 33 def __verify_action_stream(self, stream, expected_stream): |
| 34 for (ix, item) in enumerate(expected_stream): | 34 for (ix, item) in enumerate(expected_stream): |
| 35 self.assertEquals(stream[ix][0], item[0]) | 35 self.assertEquals(stream[ix][0], item[0]) |
| 36 self.assertEquals(stream[ix][4], item[1]) | 36 self.assertEquals(stream[ix][4], item[1]) |
| 37 | 37 |
| 38 def test_simple(self): | 38 def test_simple(self): |
| 39 rules = ''' | 39 rules = ''' |
| 40 <default> | 40 <default> |
| 41 "(" { LBRACE } | 41 "(" { LBRACE } <<continue>> |
| 42 ")" { RBRACE } | 42 ")" { RBRACE } <<continue>> |
| 43 | 43 |
| 44 "foo" { FOO } | 44 "foo" { FOO } <<continue>> |
| 45 eof <<terminate>>''' | 45 eof <<terminate>> |
| 46 default <<break>>''' |
| 46 | 47 |
| 47 lexer = Lexer(rules) | 48 generator = Generator(rules) |
| 48 | 49 |
| 49 string = 'foo()\0' | 50 string = 'foo()\0' |
| 50 self.__verify_action_stream( | 51 self.__verify_action_stream( |
| 51 lexer.lex(string), | 52 generator.lex(string), |
| 52 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')')]) | 53 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')')]) |
| 53 | 54 |
| 54 def test_maximal_matching(self): | 55 def test_maximal_matching(self): |
| 55 rules = ''' | 56 rules = ''' |
| 56 <default> | 57 <default> |
| 57 "<" { LT } | 58 "<" { LT } <<continue>> |
| 58 "<<" { SHL } | 59 "<<" { SHL } <<continue>> |
| 59 " " { SPACE } | 60 " " { SPACE } <<continue>> |
| 60 eof <<terminate>>''' | 61 eof <<terminate>> |
| 62 default <<break>>''' |
| 61 | 63 |
| 62 lexer = Lexer(rules) | 64 generator = Generator(rules) |
| 63 | 65 |
| 64 string = '<< <\0' | 66 string = '<< <\0' |
| 65 self.__verify_action_stream( | 67 self.__verify_action_stream( |
| 66 lexer.lex(string), | 68 generator.lex(string), |
| 67 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<')]) | 69 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<')]) |
| 68 | 70 |
| 69 | 71 |
| 70 def test_consecutive_epsilon_transitions(self): | 72 def test_consecutive_epsilon_transitions(self): |
| 71 # This rule set will create a NFA where we first have an epsilon transition, | 73 # This rule set will create a NFA where we first have an epsilon transition, |
| 72 # then following that, an epsilon transition with an action. This will test | 74 # then following that, an epsilon transition with an action. This will test |
| 73 # that the action is correctly pulled forward when creating the dfa. | 75 # that the action is correctly pulled forward when creating the dfa. |
| 74 rules = ''' | 76 rules = ''' |
| 75 digit = [0-9]; | 77 digit = [0-9]; |
| 76 number = (digit+ ("." digit+)?); | 78 number = (digit+ ("." digit+)?); |
| 77 <default> | 79 <default> |
| 78 number { NUMBER } | 80 number { NUMBER } <<continue>> |
| 79 eof <<terminate>>''' | 81 eof <<terminate>> |
| 82 default <<break>>''' |
| 80 | 83 |
| 81 lexer = Lexer(rules) | 84 generator = Generator(rules) |
| 82 | 85 |
| 83 string = '555\0' | 86 string = '555\0' |
| 84 self.__verify_action_stream( | 87 self.__verify_action_stream( |
| 85 lexer.lex(string), | 88 generator.lex(string), |
| 86 [('NUMBER', '555')]) | 89 [('NUMBER', '555')]) |
| OLD | NEW |