| 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 15 matching lines...) Expand all Loading... |
| 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(None, (action, None)), s), expec
ted) | 35 expected = map(lambda (action, s) : (Action(None, (action, None)), s), expec
ted) |
| 36 expected.append((Action(None, ('terminate', None)), '\0')) | |
| 37 automata = RuleProcessor.parse(rules).default_automata() | 36 automata = RuleProcessor.parse(rules).default_automata() |
| 38 for automaton in [automata.nfa(), automata.dfa(), automata.minimal_dfa()]: | 37 for automaton in [automata.nfa(), automata.dfa(), automata.minimal_dfa()]: |
| 39 for i, (action, start, stop) in enumerate(automaton.lex(string)): | 38 for i, (action, start, stop) in enumerate(automaton.lex(string)): |
| 40 self.assertEquals(expected[i][0], action) | 39 self.assertEquals(expected[i][0], action) |
| 41 self.assertEquals(expected[i][1], string[start : stop]) | 40 self.assertEquals(expected[i][1], string[start : stop]) |
| 42 | 41 |
| 42 def __terminate(self): |
| 43 return (Action(None, ('terminate', None)), '\0') |
| 44 |
| 43 def test_simple(self): | 45 def test_simple(self): |
| 44 rules = ''' | 46 rules = ''' |
| 45 eos = [:eos:]; | 47 eos = [:eos:]; |
| 46 <<default>> | 48 <<default>> |
| 47 "(" <|LBRACE|> | 49 "(" <|LBRACE|> |
| 48 ")" <|RBRACE|> | 50 ")" <|RBRACE|> |
| 49 | 51 |
| 50 "foo" <|FOO|> | 52 "foo" <|FOO|> |
| 51 eos <|terminate|>''' | 53 eos <|terminate|>''' |
| 52 | 54 |
| 53 string = 'foo()' | 55 string = 'foo()' |
| 54 self.__verify_action_stream(rules, string, | 56 self.__verify_action_stream(rules, string, |
| 55 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')')]) | 57 [('FOO', 'foo'), ('LBRACE', '('), ('RBRACE', ')'), self.__terminate()]) |
| 56 | 58 |
| 57 def test_maximal_matching(self): | 59 def test_maximal_matching(self): |
| 58 rules = ''' | 60 rules = ''' |
| 59 eos = [:eos:]; | 61 eos = [:eos:]; |
| 60 <<default>> | 62 <<default>> |
| 61 "<" <|LT|> | 63 "<" <|LT|> |
| 62 "<<" <|SHL|> | 64 "<<" <|SHL|> |
| 63 " " <|SPACE|> | 65 " " <|SPACE|> |
| 64 eos <|terminate|>''' | 66 eos <|terminate|>''' |
| 65 | 67 |
| 66 string = '<< <' | 68 string = '<< <' |
| 67 self.__verify_action_stream(rules, string, | 69 self.__verify_action_stream(rules, string, |
| 68 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<')]) | 70 [('SHL', '<<'), ('SPACE', ' '), ('LT', '<'), self.__terminate()]) |
| 69 | 71 |
| 70 def test_consecutive_epsilon_transitions(self): | 72 def test_consecutive_epsilon_transitions(self): |
| 71 rules = ''' | 73 rules = ''' |
| 72 eos = [:eos:]; | 74 eos = [:eos:]; |
| 73 digit = [0-9]; | 75 digit = [0-9]; |
| 74 number = (digit+ ("." digit+)?); | 76 number = (digit+ ("." digit+)?); |
| 75 <<default>> | 77 <<default>> |
| 76 number <|NUMBER|> | 78 number <|NUMBER|>''' |
| 77 eos <|terminate|>''' | |
| 78 | 79 |
| 79 string = '555' | 80 string = '555' |
| 80 self.__verify_action_stream(rules, string, [('NUMBER', '555')]) | 81 self.__verify_action_stream(rules, string, [('NUMBER', '555')]) |
| 82 |
| 83 def test_action_precedence(self): |
| 84 rules = ''' |
| 85 <<default>> |
| 86 "key" <|KEYWORD|> |
| 87 /[a-z]+/ <|ID|>''' |
| 88 |
| 89 self.__verify_action_stream(rules, 'ke', [('ID', 'ke')]) |
| 90 self.__verify_action_stream(rules, 'key', [('KEYWORD', 'key')]) |
| 91 self.__verify_action_stream(rules, 'keys', [('ID', 'keys')]) |
| 92 |
| 93 def test_wrong_action_precedence(self): |
| 94 rules = ''' |
| 95 <<default>> |
| 96 /[a-z]+/ <|ID|> |
| 97 "key" <|KEYWORD|>''' |
| 98 |
| 99 # The keyword is not recognized because of the rule preference order (ID |
| 100 # is preferred over KEYWORD). |
| 101 self.__verify_action_stream(rules, 'ke', [('ID', 'ke')]) |
| 102 self.__verify_action_stream(rules, 'key', [('ID', 'key')]) |
| 103 self.__verify_action_stream(rules, 'keys', [('ID', 'keys')]) |
| OLD | NEW |