| 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(None, (action, None)), s), expec
ted) | 35 expected = map(lambda (action, s) : (Action(None, (action, None)), s), |
| 36 automata = RuleProcessor.parse(rules, 'latin1').default_automata() | 36 expected) |
| 37 rule_processor = RuleProcessor.parse(rules, 'latin1') |
| 38 automata = rule_processor.default_automata() |
| 37 for automaton in [automata.nfa(), automata.dfa(), automata.minimal_dfa()]: | 39 for automaton in [automata.nfa(), automata.dfa(), automata.minimal_dfa()]: |
| 38 for i, (action, start, stop) in enumerate(automaton.lex(string)): | 40 for i, (action, start, stop) in enumerate( |
| 41 automaton.lex(string, rule_processor.default_action)): |
| 39 self.assertEquals(expected[i][0], action) | 42 self.assertEquals(expected[i][0], action) |
| 40 self.assertEquals(expected[i][1], string[start : stop]) | 43 self.assertEquals(expected[i][1], string[start : stop]) |
| 41 | 44 |
| 42 @staticmethod | 45 @staticmethod |
| 43 def __terminate(): | 46 def __terminate(): |
| 44 return (Action(None, ('terminate', None)), '\0') | 47 return (Action(None, ('terminate', None)), '\0') |
| 45 | 48 |
| 46 def test_simple(self): | 49 def test_simple(self): |
| 47 rules = ''' | 50 rules = ''' |
| 48 eos = [:eos:]; | 51 eos = [:eos:]; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 rules = ''' | 98 rules = ''' |
| 96 <<default>> | 99 <<default>> |
| 97 /[a-z]+/ <|ID|> | 100 /[a-z]+/ <|ID|> |
| 98 "key" <|KEYWORD|>''' | 101 "key" <|KEYWORD|>''' |
| 99 | 102 |
| 100 # The keyword is not recognized because of the rule preference order (ID | 103 # The keyword is not recognized because of the rule preference order (ID |
| 101 # is preferred over KEYWORD). | 104 # is preferred over KEYWORD). |
| 102 self.__verify_action_stream(rules, 'ke', [('ID', 'ke')]) | 105 self.__verify_action_stream(rules, 'ke', [('ID', 'ke')]) |
| 103 self.__verify_action_stream(rules, 'key', [('ID', 'key')]) | 106 self.__verify_action_stream(rules, 'key', [('ID', 'key')]) |
| 104 self.__verify_action_stream(rules, 'keys', [('ID', 'keys')]) | 107 self.__verify_action_stream(rules, 'keys', [('ID', 'keys')]) |
| 108 |
| 109 def test_simple_subgraph(self): |
| 110 rules = ''' |
| 111 <<default>> |
| 112 /[a-z]/ <|ID|Identifier> |
| 113 " " <|SPACE|> |
| 114 <<Identifier>> |
| 115 /[a-z]/ <|ID|continue> |
| 116 ''' |
| 117 self.__verify_action_stream(rules, 'a bc def', |
| 118 [('ID', 'a'), ('SPACE', ' '), ('ID', 'bc'), |
| 119 ('SPACE', ' '), ('ID', 'def')]) |
| 120 |
| 121 def test_entering_subgraph_without_match_action(self): |
| 122 # Note: there is no match action for entering the subgraph. It means that |
| 123 # one char identifiers are not accepted. |
| 124 rules = ''' |
| 125 <<default>> |
| 126 /[a-z]/ <||Identifier> |
| 127 " " <|SPACE|> |
| 128 default_action <ILLEGAL> |
| 129 <<Identifier>> |
| 130 /[a-z]/ <|ID|continue> |
| 131 ''' |
| 132 self.__verify_action_stream(rules, 'bc a def', |
| 133 [('ID', 'bc'), ('SPACE', ' '), ('ILLEGAL', 'a'), |
| 134 ('SPACE', ' '), ('ID', 'def')]) |
| 135 |
| 136 def test_subgraph_with_noncontinue(self): |
| 137 # In the "Identifier" subgraph, we have rules which don't have "continue". |
| 138 rules = ''' |
| 139 <<default>> |
| 140 /[b-z]/ <|ID|Identifier> |
| 141 " " <|SPACE|> |
| 142 <<Identifier>> |
| 143 /[b-z]/ <|ID|continue> |
| 144 /[a]/ <|INVALID|> |
| 145 ''' |
| 146 self.__verify_action_stream(rules, 'bc ba de', |
| 147 [('ID', 'bc'), ('SPACE', ' '), |
| 148 ('INVALID', 'ba'), ('SPACE', ' '), |
| 149 ('ID', 'de')]) |
| OLD | NEW |