| 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 14 matching lines...) Expand all Loading... |
| 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 automaton import Action | 29 from automaton import Action |
| 30 from rule_parser import RuleProcessor | 30 from rule_parser import RuleProcessor |
| 31 | 31 |
| 32 class LexerTestCase(unittest.TestCase): | 32 class LexerTestCase(unittest.TestCase): |
| 33 | 33 |
| 34 def __verify_action_stream(self, rules, string, expected): | 34 def __verify_action_stream(self, rules, string, expected): |
| 35 expected = map(lambda (action, s) : (Action('code', action), s), expected) | 35 expected = map(lambda (action, s) : (Action(None, (action, None)), s), expec
ted) |
| 36 expected.append((Action('terminate'), '\0')) | 36 expected.append((Action(None, ('terminate', None)), '\0')) |
| 37 automata = RuleProcessor.parse(rules).default_automata() | 37 automata = RuleProcessor.parse(rules).default_automata() |
| 38 for automaton in [automata.dfa(), automata.minimal_dfa()]: | 38 for automaton in [automata.dfa(), automata.minimal_dfa()]: |
| 39 for i, (action, start, stop) in enumerate(automaton.lex(string)): | 39 for i, (action, start, stop) in enumerate(automaton.lex(string)): |
| 40 self.assertEquals(expected[i][0], action) | 40 self.assertEquals(expected[i][0], action) |
| 41 self.assertEquals(expected[i][1], string[start : stop]) | 41 self.assertEquals(expected[i][1], string[start : stop]) |
| 42 | 42 |
| 43 def test_simple(self): | 43 def test_simple(self): |
| 44 rules = ''' | 44 rules = ''' |
| 45 <default> | 45 <<default>> |
| 46 "(" { LBRACE } | 46 "(" <|LBRACE|> |
| 47 ")" { RBRACE } | 47 ")" <|RBRACE|> |
| 48 | 48 |
| 49 "foo" { FOO } | 49 "foo" <|FOO|> |
| 50 eof <<terminate>>''' | 50 eof <|terminate|>''' |
| 51 | 51 |
| 52 string = 'foo()\0' | 52 string = 'foo()\0' |
| 53 self.__verify_action_stream(rules, string, | 53 self.__verify_action_stream(rules, string, |
| 54 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')')]) | 54 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')')]) |
| 55 | 55 |
| 56 def test_maximal_matching(self): | 56 def test_maximal_matching(self): |
| 57 rules = ''' | 57 rules = ''' |
| 58 <default> | 58 <<default>> |
| 59 "<" { LT } | 59 "<" <|LT|> |
| 60 "<<" { SHL } | 60 "<<" <|SHL|> |
| 61 " " { SPACE } | 61 " " <|SPACE|> |
| 62 eof <<terminate>>''' | 62 eof <|terminate|>''' |
| 63 | 63 |
| 64 string = '<< <\0' | 64 string = '<< <\0' |
| 65 self.__verify_action_stream(rules, string, | 65 self.__verify_action_stream(rules, string, |
| 66 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<')]) | 66 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<')]) |
| 67 | 67 |
| 68 def test_consecutive_epsilon_transitions(self): | 68 def test_consecutive_epsilon_transitions(self): |
| 69 rules = ''' | 69 rules = ''' |
| 70 digit = [0-9]; | 70 digit = [0-9]; |
| 71 number = (digit+ ("." digit+)?); | 71 number = (digit+ ("." digit+)?); |
| 72 <default> | 72 <<default>> |
| 73 number { NUMBER } | 73 number <|NUMBER|> |
| 74 eof <<terminate>>''' | 74 eof <|terminate|>''' |
| 75 | 75 |
| 76 string = '555\0' | 76 string = '555\0' |
| 77 self.__verify_action_stream(rules, string, [('NUMBER', '555')]) | 77 self.__verify_action_stream(rules, string, [('NUMBER', '555')]) |
| OLD | NEW |