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

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

Issue 180213003: Experimental parser: cleanup logging (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 9 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/generator.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 2014 the V8 project authors. All rights reserved. 1 # Copyright 2014 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 StringType, IntType 28 import logging
29 import ply.lex as lex
30 import ply.yacc as yacc
29 31
30 class Term(object): 32 class ParserBuilder:
31 '''An immutable class representing a function and its arguments.
32 f(a,b,c) would be represented as ('f', a, b, c) where
33 a, b, and c are strings, integers or Terms.'''
34 33
35 __empty_term = None 34 class Logger(object):
35 def debug(self,msg,*args,**kwargs):
36 logging.debug(msg % args)
36 37
38 def info(self,msg,*args,**kwargs):
39 logging.debug(msg % args)
40
41 def warning(self,msg,*args,**kwargs):
42 raise Exception("warning: "+ (msg % args) + "\n")
43
44 def error(self,msg,*args,**kwargs):
45 raise Exception("error: "+ (msg % args) + "\n")
46
47 __static_instances = {}
37 @staticmethod 48 @staticmethod
38 def empty_term(): 49 def parse(
39 if Term.__empty_term == None: 50 string, name, new_lexer, new_parser, preparse = None, postparse = None):
40 Term.__empty_term = Term('') 51 if not name in ParserBuilder.__static_instances:
41 return Term.__empty_term 52 logger = ParserBuilder.Logger()
42 53 lexer_instance = new_lexer()
43 def __init__(self, name, *args): 54 lexer_instance.lex = lex.lex(module=lexer_instance)
44 assert type(name) == StringType 55 instance = new_parser()
45 assert name or not args, 'empty term must not have args' 56 instance.yacc = yacc.yacc(
46 for v in args: 57 module=instance, debug=True, write_tables=0,
47 assert type(v) == IntType or type(v) == StringType or isinstance(v, Term) 58 debuglog=logger, errorlog=logger)
48 self.__tuple = tuple([name] + list(args)) 59 ParserBuilder.__static_instances[name] = (lexer_instance, instance)
49 self.__str = None 60 (lexer_instance, instance) = ParserBuilder.__static_instances[name]
50 61 if preparse:
51 def name(self): 62 preparse(instance)
52 return self.__tuple[0] 63 try:
53 64 return_value = instance.yacc.parse(string, lexer=lexer_instance.lex)
54 def args(self): 65 except Exception:
55 return self.__tuple[1:] 66 del ParserBuilder.__static_instances[name]
56 67 raise
57 def __hash__(self): 68 if postparse:
58 return hash(self.__tuple) 69 postparse(instance)
59 70 return return_value
60 def __nonzero__(self):
61 'true <==> self == empty_term'
62 return bool(self.__tuple[0])
63
64 def __eq__(self, other):
65 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple)
66
67 # TODO(dcarney): escape '(', ')' and ',' in strings
68 def __str__(self):
69 if self.__str == None:
70 if not self:
71 self.__str = ''
72 else:
73 self.__str = '%s(%s)' % (self.name(), ','.join(map(str, self.args())))
74 return self.__str
OLDNEW
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | tools/lexer_generator/regex_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698