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

Side by Side Diff: tools/lexer_generator/nfa_builder.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/automata_test.py ('k') | tools/lexer_generator/regex_parser.py » ('j') | 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
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 assert sub_ends, "this creates unreachable nodes" 121 assert sub_ends, "this creates unreachable nodes"
122 self.__patch_ends(ends, sub_start) 122 self.__patch_ends(ends, sub_start)
123 ends = sub_ends 123 ends = sub_ends
124 return (start, ends) 124 return (start, ends)
125 125
126 def __key_state(self, key): 126 def __key_state(self, key):
127 state = self.__new_state() 127 state = self.__new_state()
128 state.add_unclosed_transition(key) 128 state.add_unclosed_transition(key)
129 return (state, [state]) 129 return (state, [state])
130 130
131 def __literal(self, chars): 131 def __literal(self, *chars):
132 terms = map(lambda c : Term('SINGLE_CHAR', c), chars) 132 terms = map(lambda c : Term('SINGLE_CHAR', c), chars)
133 return self.__process(self.cat_terms(terms)) 133 return self.__process(self.cat_terms(terms))
134 134
135 def __single_char(self, char): 135 def __single_char(self, char):
136 return self.__key_state( 136 return self.__key_state(
137 TransitionKey.single_char(self.__encoding, char)) 137 TransitionKey.single_char(self.__encoding, char))
138 138
139 def __class(self, subtree): 139 def __class(self, subtree):
140 return self.__key_state(TransitionKey.character_class( 140 return self.__key_state(TransitionKey.character_class(
141 self.__encoding, Term('CLASS', subtree), self.__character_classes)) 141 self.__encoding, Term('CLASS', subtree), self.__character_classes))
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 continue 313 continue
314 if term.name() == name: 314 if term.name() == name:
315 for arg in term.args(): 315 for arg in term.args():
316 if arg: 316 if arg:
317 yield arg 317 yield arg
318 else: 318 else:
319 yield term 319 yield term
320 320
321 @staticmethod 321 @staticmethod
322 def __flatten_literals(terms): 322 def __flatten_literals(terms):
323 literal = None 323 acc = ()
324 for term in terms: 324 for term in terms:
325 assert isinstance(term, Term) 325 assert isinstance(term, Term)
326 if not term: 326 if not term:
327 continue 327 continue
328 if term.name() == 'LITERAL': 328 if term.name() == 'LITERAL':
329 if literal: 329 acc += term.args()
330 literal += term.args()[0]
331 else:
332 literal = term.args()[0]
333 else: 330 else:
334 if literal: 331 if acc:
335 yield Term('LITERAL', literal) 332 yield Term('LITERAL', *acc)
336 literal = None 333 acc = ()
337 if term: 334 if term:
338 yield term 335 yield term
339 if literal: 336 if acc:
340 yield Term('LITERAL', literal) 337 yield Term('LITERAL', *acc)
341 338
342 @staticmethod 339 @staticmethod
343 def or_terms(terms): 340 def or_terms(terms):
344 terms = list(NfaBuilder.__flatten_terms(terms, 'OR')) 341 terms = list(NfaBuilder.__flatten_terms(terms, 'OR'))
345 assert terms 342 assert terms
346 return terms[0] if len(terms) == 1 else Term('OR', *terms) 343 return terms[0] if len(terms) == 1 else Term('OR', *terms)
347 344
348 @staticmethod 345 @staticmethod
349 def cat_terms(terms): 346 def cat_terms(terms):
350 terms = NfaBuilder.__flatten_terms(terms, 'CAT') 347 terms = NfaBuilder.__flatten_terms(terms, 'CAT')
351 terms = list(NfaBuilder.__flatten_literals(terms)) 348 terms = list(NfaBuilder.__flatten_literals(terms))
352 assert terms 349 assert terms
353 return terms[0] if len(terms) == 1 else Term('CAT', *terms) 350 return terms[0] if len(terms) == 1 else Term('CAT', *terms)
OLDNEW
« no previous file with comments | « tools/lexer_generator/automata_test.py ('k') | tools/lexer_generator/regex_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698