Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: tools/lexer_generator/transition_keys.py

Issue 157813004: Experimental parser: store literals as ints (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/transition_key_test.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 from types import IntType
28 from itertools import chain 29 from itertools import chain
29 from action import Term 30 from action import Term
30 from string import printable 31 from string import printable
31 32
32 class KeyEncoding(object): 33 class KeyEncoding(object):
33 34
34 __encodings = {} 35 __encodings = {}
35 36
36 @staticmethod 37 @staticmethod
37 def get(name): 38 def get(name):
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 138
138 @staticmethod 139 @staticmethod
139 def epsilon(): 140 def epsilon():
140 '''Returns a TransitionKey for the epsilon (empty) transition.''' 141 '''Returns a TransitionKey for the epsilon (empty) transition.'''
141 return TransitionKey.__cached_key(None, 'epsilon', 142 return TransitionKey.__cached_key(None, 'epsilon',
142 lambda : Term("EPSILON_KEY")) 143 lambda : Term("EPSILON_KEY"))
143 144
144 @staticmethod 145 @staticmethod
145 def omega(): 146 def omega():
146 '''Always matches.''' 147 '''Always matches.'''
147 return TransitionKey.__cached_key(None, 'omega', 148 return TransitionKey.__cached_key(None, 'omega', lambda : Term("OMEGA_KEY"))
148 lambda : Term("OMEGA_KEY"))
149 149
150 @staticmethod 150 @staticmethod
151 def any(encoding): 151 def any(encoding):
152 '''Returns a TransitionKey which matches any encoded character.''' 152 '''Returns a TransitionKey which matches any encoded character.'''
153 return TransitionKey.__cached_key(encoding, 'any', 153 return TransitionKey.__cached_key(encoding, 'any',
154 lambda : encoding.all_components_iter()) 154 lambda : encoding.all_components_iter())
155 155
156 @staticmethod 156 @staticmethod
157 def single_char(encoding, char): # TODO(dcarney): char should be int 157 def single_char(encoding, char):
158 '''Returns a TransitionKey for a single-character transition.''' 158 '''Returns a TransitionKey for a single-character transition.'''
159 return TransitionKey(encoding, Term("NUMERIC_RANGE_KEY", ord(char), ord(char ))) 159 return TransitionKey.range(encoding, char, char)
160 160
161 @staticmethod 161 @staticmethod
162 def range(encoding, a, b): 162 def range(encoding, a, b):
163 '''Returns a TransitionKey for a single-character transition.''' 163 '''Returns a TransitionKey for a single-character transition.'''
164 assert type(a) == IntType and type(b) == IntType
164 return TransitionKey(encoding, Term("NUMERIC_RANGE_KEY", a, b)) 165 return TransitionKey(encoding, Term("NUMERIC_RANGE_KEY", a, b))
165 166
166 @staticmethod 167 @staticmethod
167 def unique(term): # TODO(dcarney): rename 168 def unique(term): # TODO(dcarney): rename
168 '''Returns a unique TransitionKey for the given name (and creates it if it 169 '''Returns a unique TransitionKey for the given name (and creates it if it
169 doesn't exist yet). The TransitionKey won't have any real character range, 170 doesn't exist yet). The TransitionKey won't have any real character range,
170 but a newly-created "mock" character range which is separate from all other 171 but a newly-created "mock" character range which is separate from all other
171 character ranges.''' 172 character ranges.'''
172 return TransitionKey(None, Term("TERM_KEY", term)) 173 return TransitionKey(None, Term("TERM_KEY", term))
173 174
174 @staticmethod 175 @staticmethod
175 def __process_term(encoding, term, components, key_map): 176 def __process_term(encoding, term, components, key_map):
176 key = term.name() 177 key = term.name()
177 args = term.args() 178 args = term.args()
178 if key == 'RANGE': 179 if key == 'RANGE':
179 components.append(Term('NUMERIC_RANGE_KEY', ord(args[0]), ord(args[1]))) 180 components.append(Term('NUMERIC_RANGE_KEY', args[0], args[1]))
180 elif key == 'LITERAL': 181 elif key == 'LITERAL':
181 for char in args[0]: # TODO(dcarney): don't use strings for literals 182 components += map(lambda x : Term('NUMERIC_RANGE_KEY', x, x), args)
182 components.append(Term('NUMERIC_RANGE_KEY', ord(char), ord(char)))
183 elif key == 'CAT': 183 elif key == 'CAT':
184 for x in args: 184 for x in args:
185 TransitionKey.__process_term(encoding, x, components, key_map) 185 TransitionKey.__process_term(encoding, x, components, key_map)
186 elif key == 'CHARACTER_CLASS': 186 elif key == 'CHARACTER_CLASS':
187 class_name = args[0] 187 class_name = args[0]
188 if encoding.named_range(class_name): 188 if encoding.named_range(class_name):
189 c = encoding.named_range(class_name) 189 c = encoding.named_range(class_name)
190 if class_name in key_map: 190 if class_name in key_map:
191 assert key_map[class_name] == TransitionKey(encoding, c) 191 assert key_map[class_name] == TransitionKey(encoding, c)
192 components.append(c) 192 components.append(c)
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 { 643 {
644 'whitespace': 644 'whitespace':
645 [(9, 9), (11, 12), (32, 32), ('non_primary_whitespace',)], 645 [(9, 9), (11, 12), (32, 32), ('non_primary_whitespace',)],
646 'letter': 646 'letter':
647 [(65, 90), (97, 122), ('non_primary_letter',)], 647 [(65, 90), (97, 122), ('non_primary_letter',)],
648 'line_terminator': 648 'line_terminator':
649 [(10, 10), (13, 13), ('non_primary_line_terminator',)], 649 [(10, 10), (13, 13), ('non_primary_line_terminator',)],
650 'identifier_part_not_letter': 650 'identifier_part_not_letter':
651 [(48, 57), (95, 95), ('non_primary_identifier_part_not_letter',)], 651 [(48, 57), (95, 95), ('non_primary_identifier_part_not_letter',)],
652 }) 652 })
OLDNEW
« no previous file with comments | « tools/lexer_generator/transition_key_test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698