| 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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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 generator import Generator | 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, rules, string, expected_stream): |
| 34 for (ix, item) in enumerate(expected_stream): | 34 expected_stream.append(('terminate', '\0')) |
| 35 self.assertEquals(stream[ix][0], item[0]) | 35 for i, (action, start, stop) in enumerate(Generator(rules).lex(string)): |
| 36 self.assertEquals(stream[ix][4], item[1]) | 36 self.assertEquals(expected_stream[i][0], action) |
| 37 self.assertEquals(expected_stream[i][1], string[start : stop]) |
| 37 | 38 |
| 38 def test_simple(self): | 39 def test_simple(self): |
| 39 rules = ''' | 40 rules = ''' |
| 40 <default> | 41 <default> |
| 41 "(" { LBRACE } <<continue>> | 42 "(" { LBRACE } |
| 42 ")" { RBRACE } <<continue>> | 43 ")" { RBRACE } |
| 43 | 44 |
| 44 "foo" { FOO } <<continue>> | 45 "foo" { FOO } |
| 45 eof <<terminate>> | 46 eof <<terminate>>''' |
| 46 default <<break>>''' | |
| 47 | |
| 48 generator = Generator(rules) | |
| 49 | 47 |
| 50 string = 'foo()\0' | 48 string = 'foo()\0' |
| 51 self.__verify_action_stream( | 49 self.__verify_action_stream(rules, string, |
| 52 generator.lex(string), | |
| 53 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')')]) | 50 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')')]) |
| 54 | 51 |
| 55 def test_maximal_matching(self): | 52 def test_maximal_matching(self): |
| 56 rules = ''' | 53 rules = ''' |
| 57 <default> | 54 <default> |
| 58 "<" { LT } <<continue>> | 55 "<" { LT } |
| 59 "<<" { SHL } <<continue>> | 56 "<<" { SHL } |
| 60 " " { SPACE } <<continue>> | 57 " " { SPACE } |
| 61 eof <<terminate>> | 58 eof <<terminate>>''' |
| 62 default <<break>>''' | |
| 63 | |
| 64 generator = Generator(rules) | |
| 65 | 59 |
| 66 string = '<< <\0' | 60 string = '<< <\0' |
| 67 self.__verify_action_stream( | 61 self.__verify_action_stream(rules, string, |
| 68 generator.lex(string), | |
| 69 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<')]) | 62 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<')]) |
| 70 | 63 |
| 71 | |
| 72 def test_consecutive_epsilon_transitions(self): | 64 def test_consecutive_epsilon_transitions(self): |
| 73 # This rule set will create a NFA where we first have an epsilon transition, | |
| 74 # then following that, an epsilon transition with an action. This will test | |
| 75 # that the action is correctly pulled forward when creating the dfa. | |
| 76 rules = ''' | 65 rules = ''' |
| 77 digit = [0-9]; | 66 digit = [0-9]; |
| 78 number = (digit+ ("." digit+)?); | 67 number = (digit+ ("." digit+)?); |
| 79 <default> | 68 <default> |
| 80 number { NUMBER } <<continue>> | 69 number { NUMBER } |
| 81 eof <<terminate>> | 70 eof <<terminate>>''' |
| 82 default <<break>>''' | |
| 83 | |
| 84 generator = Generator(rules) | |
| 85 | 71 |
| 86 string = '555\0' | 72 string = '555\0' |
| 87 self.__verify_action_stream( | 73 self.__verify_action_stream(rules, string, [('NUMBER', '555')]) |
| 88 generator.lex(string), | |
| 89 [('NUMBER', '555')]) | |
| OLD | NEW |