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

Unified 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, 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | tools/lexer_generator/regex_parser.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/lexer_generator/ply_utilities.py
diff --git a/tools/lexer_generator/term.py b/tools/lexer_generator/ply_utilities.py
similarity index 54%
copy from tools/lexer_generator/term.py
copy to tools/lexer_generator/ply_utilities.py
index 9cd045b5ce75958e974a73245552f81815155dfc..a2826f3a31af31d0ec59318428afc109969ec611 100644
--- a/tools/lexer_generator/term.py
+++ b/tools/lexer_generator/ply_utilities.py
@@ -25,50 +25,46 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-from types import StringType, IntType
+import logging
+import ply.lex as lex
+import ply.yacc as yacc
-class Term(object):
- '''An immutable class representing a function and its arguments.
- f(a,b,c) would be represented as ('f', a, b, c) where
- a, b, and c are strings, integers or Terms.'''
+class ParserBuilder:
- __empty_term = None
+ class Logger(object):
+ def debug(self,msg,*args,**kwargs):
+ logging.debug(msg % args)
- @staticmethod
- def empty_term():
- if Term.__empty_term == None:
- Term.__empty_term = Term('')
- return Term.__empty_term
-
- def __init__(self, name, *args):
- assert type(name) == StringType
- assert name or not args, 'empty term must not have args'
- for v in args:
- assert type(v) == IntType or type(v) == StringType or isinstance(v, Term)
- self.__tuple = tuple([name] + list(args))
- self.__str = None
-
- def name(self):
- return self.__tuple[0]
+ def info(self,msg,*args,**kwargs):
+ logging.debug(msg % args)
- def args(self):
- return self.__tuple[1:]
+ def warning(self,msg,*args,**kwargs):
+ raise Exception("warning: "+ (msg % args) + "\n")
- def __hash__(self):
- return hash(self.__tuple)
+ def error(self,msg,*args,**kwargs):
+ raise Exception("error: "+ (msg % args) + "\n")
- def __nonzero__(self):
- 'true <==> self == empty_term'
- return bool(self.__tuple[0])
-
- def __eq__(self, other):
- return (isinstance(other, self.__class__) and self.__tuple == other.__tuple)
-
- # TODO(dcarney): escape '(', ')' and ',' in strings
- def __str__(self):
- if self.__str == None:
- if not self:
- self.__str = ''
- else:
- self.__str = '%s(%s)' % (self.name(), ','.join(map(str, self.args())))
- return self.__str
+ __static_instances = {}
+ @staticmethod
+ def parse(
+ string, name, new_lexer, new_parser, preparse = None, postparse = None):
+ if not name in ParserBuilder.__static_instances:
+ logger = ParserBuilder.Logger()
+ lexer_instance = new_lexer()
+ lexer_instance.lex = lex.lex(module=lexer_instance)
+ instance = new_parser()
+ instance.yacc = yacc.yacc(
+ module=instance, debug=True, write_tables=0,
+ debuglog=logger, errorlog=logger)
+ ParserBuilder.__static_instances[name] = (lexer_instance, instance)
+ (lexer_instance, instance) = ParserBuilder.__static_instances[name]
+ if preparse:
+ preparse(instance)
+ try:
+ return_value = instance.yacc.parse(string, lexer=lexer_instance.lex)
+ except Exception:
+ del ParserBuilder.__static_instances[name]
+ raise
+ if postparse:
+ postparse(instance)
+ return return_value
« 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