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

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

Issue 155763005: Experimental parser: small cleanup (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 | « no previous file | 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
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 } 302 }
303 303
304 @staticmethod 304 @staticmethod
305 def apply_modifier(modifier, term): 305 def apply_modifier(modifier, term):
306 return Term(NfaBuilder.__modifer_map[modifier], term) 306 return Term(NfaBuilder.__modifer_map[modifier], term)
307 307
308 @staticmethod 308 @staticmethod
309 def __flatten_terms(terms, name): 309 def __flatten_terms(terms, name):
310 for term in terms: 310 for term in terms:
311 assert isinstance(term, Term) 311 assert isinstance(term, Term)
312 if not term:
313 continue
314 if term.name() == name: 312 if term.name() == name:
315 for arg in term.args(): 313 for arg in term.args():
316 if arg: 314 if arg:
317 yield arg 315 yield arg
318 else: 316 else:
319 yield term 317 if term:
318 yield term
320 319
321 @staticmethod 320 @staticmethod
322 def __flatten_literals(terms): 321 def __flatten_literals(terms):
323 acc = () 322 acc = ()
324 for term in terms: 323 for term in terms:
325 assert isinstance(term, Term) 324 assert isinstance(term, Term)
326 if not term:
327 continue
328 if term.name() == 'LITERAL': 325 if term.name() == 'LITERAL':
329 acc += term.args() 326 acc += term.args()
330 else: 327 else:
331 if acc: 328 if acc:
332 yield Term('LITERAL', *acc) 329 yield Term('LITERAL', *acc)
333 acc = () 330 acc = ()
334 if term: 331 if term:
335 yield term 332 yield term
336 if acc: 333 if acc:
337 yield Term('LITERAL', *acc) 334 yield Term('LITERAL', *acc)
338 335
339 @staticmethod 336 @staticmethod
340 def or_terms(terms): 337 def or_terms(terms):
341 terms = list(NfaBuilder.__flatten_terms(terms, 'OR')) 338 terms = list(NfaBuilder.__flatten_terms(terms, 'OR'))
342 assert terms 339 assert terms
343 return terms[0] if len(terms) == 1 else Term('OR', *terms) 340 return terms[0] if len(terms) == 1 else Term('OR', *terms)
344 341
345 @staticmethod 342 @staticmethod
346 def cat_terms(terms): 343 def cat_terms(terms):
347 terms = NfaBuilder.__flatten_terms(terms, 'CAT') 344 terms = NfaBuilder.__flatten_terms(terms, 'CAT')
348 terms = list(NfaBuilder.__flatten_literals(terms)) 345 terms = list(NfaBuilder.__flatten_literals(terms))
349 assert terms 346 assert terms
350 return terms[0] if len(terms) == 1 else Term('CAT', *terms) 347 return terms[0] if len(terms) == 1 else Term('CAT', *terms)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698