| 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 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 return Term('CONTINUE', tree, depth) | 288 return Term('CONTINUE', tree, depth) |
| 289 | 289 |
| 290 @staticmethod | 290 @staticmethod |
| 291 def unique_key(name): | 291 def unique_key(name): |
| 292 return Term('UNIQUE_KEY', name) | 292 return Term('UNIQUE_KEY', name) |
| 293 | 293 |
| 294 @staticmethod | 294 @staticmethod |
| 295 def join_subtree(tree, subtree_name): | 295 def join_subtree(tree, subtree_name): |
| 296 return Term('JOIN', tree, subtree_name) | 296 return Term('JOIN', tree, subtree_name) |
| 297 | 297 |
| 298 @staticmethod | |
| 299 def or_terms(terms): | |
| 300 if len(terms) == 1: return terms[0] | |
| 301 return Term('OR', *terms) if terms else Term.empty() | |
| 302 | |
| 303 @staticmethod | |
| 304 def cat_terms(terms): | |
| 305 if len(terms) == 1: return terms[0] | |
| 306 return Term('CAT', *terms) if terms else Term.empty() | |
| 307 | |
| 308 __modifer_map = { | 298 __modifer_map = { |
| 309 '+': 'ONE_OR_MORE', | 299 '+': 'ONE_OR_MORE', |
| 310 '?': 'ZERO_OR_ONE', | 300 '?': 'ZERO_OR_ONE', |
| 311 '*': 'ZERO_OR_MORE', | 301 '*': 'ZERO_OR_MORE', |
| 312 } | 302 } |
| 313 | 303 |
| 314 @staticmethod | 304 @staticmethod |
| 315 def apply_modifier(modifier, term): | 305 def apply_modifier(modifier, term): |
| 316 return Term(NfaBuilder.__modifer_map[modifier], term) | 306 return Term(NfaBuilder.__modifer_map[modifier], term) |
| 307 |
| 308 @staticmethod |
| 309 def __flatten_terms(terms, name): |
| 310 for term in terms: |
| 311 assert isinstance(term, Term) |
| 312 if not term: |
| 313 continue |
| 314 if term.name() == name: |
| 315 for arg in term.args(): |
| 316 if arg: |
| 317 yield arg |
| 318 else: |
| 319 yield term |
| 320 |
| 321 @staticmethod |
| 322 def __flatten_literals(terms): |
| 323 literal = None |
| 324 for term in terms: |
| 325 assert isinstance(term, Term) |
| 326 if not term: |
| 327 continue |
| 328 if term.name() == 'LITERAL': |
| 329 if literal: |
| 330 literal += term.args()[0] |
| 331 else: |
| 332 literal = term.args()[0] |
| 333 else: |
| 334 if literal: |
| 335 yield Term('LITERAL', literal) |
| 336 literal = None |
| 337 if term: |
| 338 yield term |
| 339 if literal: |
| 340 yield Term('LITERAL', literal) |
| 341 |
| 342 @staticmethod |
| 343 def or_terms(terms): |
| 344 terms = list(NfaBuilder.__flatten_terms(terms, 'OR')) |
| 345 assert terms |
| 346 return terms[0] if len(terms) == 1 else Term('OR', *terms) |
| 347 |
| 348 @staticmethod |
| 349 def cat_terms(terms): |
| 350 terms = NfaBuilder.__flatten_terms(terms, 'CAT') |
| 351 terms = list(NfaBuilder.__flatten_literals(terms)) |
| 352 assert terms |
| 353 return terms[0] if len(terms) == 1 else Term('CAT', *terms) |
| OLD | NEW |