| 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 |
| 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 import logging |
| 28 import ply.lex as lex | 29 import ply.lex as lex |
| 29 import ply.yacc as yacc | 30 import ply.yacc as yacc |
| 30 from term import Term | 31 from term import Term |
| 31 from nfa_builder import NfaBuilder | 32 from nfa_builder import NfaBuilder |
| 32 | 33 from ply_utilities import ParserBuilder |
| 33 class ParserBuilder: | |
| 34 | |
| 35 class Logger(object): | |
| 36 def debug(self,msg,*args,**kwargs): | |
| 37 pass | |
| 38 | |
| 39 def info(self,msg,*args,**kwargs): | |
| 40 pass | |
| 41 | |
| 42 def warning(self,msg,*args,**kwargs): | |
| 43 raise Exception("warning: "+ (msg % args) + "\n") | |
| 44 | |
| 45 def error(self,msg,*args,**kwargs): | |
| 46 raise Exception("error: "+ (msg % args) + "\n") | |
| 47 | |
| 48 __static_instances = {} | |
| 49 @staticmethod | |
| 50 def parse( | |
| 51 string, name, new_lexer, new_parser, preparse = None, postparse = None): | |
| 52 if not name in ParserBuilder.__static_instances: | |
| 53 logger = ParserBuilder.Logger() | |
| 54 lexer_instance = new_lexer() | |
| 55 lexer_instance.lex = lex.lex(module=lexer_instance) | |
| 56 instance = new_parser() | |
| 57 instance.yacc = yacc.yacc( | |
| 58 module=instance, debug=True, write_tables=0, | |
| 59 debuglog=logger, errorlog=logger) | |
| 60 ParserBuilder.__static_instances[name] = (lexer_instance, instance) | |
| 61 (lexer_instance, instance) = ParserBuilder.__static_instances[name] | |
| 62 if preparse: | |
| 63 preparse(instance) | |
| 64 try: | |
| 65 return_value = instance.yacc.parse(string, lexer=lexer_instance.lex) | |
| 66 except Exception: | |
| 67 del ParserBuilder.__static_instances[name] | |
| 68 raise | |
| 69 if postparse: | |
| 70 postparse(instance) | |
| 71 return return_value | |
| 72 | 34 |
| 73 def build_escape_map(chars): | 35 def build_escape_map(chars): |
| 74 def add_escape(d, char): | 36 def add_escape(d, char): |
| 75 d['\\' + char] = char | 37 d['\\' + char] = char |
| 76 return d | 38 return d |
| 77 return reduce(add_escape, chars, | 39 return reduce(add_escape, chars, |
| 78 {'\\t' : '\t', '\\r' : '\r', '\\n' : '\n', '\\v' : '\v', '\\f' : '\f'}) | 40 {'\\t' : '\t', '\\r' : '\r', '\\n' : '\n', '\\v' : '\v', '\\f' : '\f'}) |
| 79 | 41 |
| 80 class RegexLexer: | 42 class RegexLexer: |
| 81 | 43 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 @staticmethod | 246 @staticmethod |
| 285 def __cat(left, right): | 247 def __cat(left, right): |
| 286 assert left | 248 assert left |
| 287 return NfaBuilder.cat_terms([left] if not right else [left, right]) | 249 return NfaBuilder.cat_terms([left] if not right else [left, right]) |
| 288 | 250 |
| 289 @staticmethod | 251 @staticmethod |
| 290 def parse(string): | 252 def parse(string): |
| 291 new_lexer = lambda: RegexLexer() | 253 new_lexer = lambda: RegexLexer() |
| 292 new_parser = lambda: RegexParser() | 254 new_parser = lambda: RegexParser() |
| 293 return ParserBuilder.parse(string, "RegexParser", new_lexer, new_parser) | 255 return ParserBuilder.parse(string, "RegexParser", new_lexer, new_parser) |
| OLD | NEW |