Package PyFoam :: Package Basics :: Module PlyParser
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.PlyParser

 1  #  ICE Revision: $Id: PlyParser.py 8663 2008-03-31 16:26:11Z bgschaid $  
 2  """Base class for all parser classes based on PLY 
 3   
 4  Most of this class was shamelessly stolen from the examples""" 
 5   
 6  import sys 
 7   
 8  if sys.version_info < (2,3): 
 9      raise "PyFoam","Version "+str(sys.version_info)+" is not sufficient for ply (2.3 needed)" 
10   
11  import PyFoam.ThirdParty.ply.lex as lex 
12  import PyFoam.ThirdParty.ply.yacc as yacc 
13   
14  import os 
15   
16 -class PlyParser(object):
17 """ 18 Base class for a lexer/parser that has the rules defined as methods 19 """ 20 tokens = () 21 precedence = () 22 23
24 - def __init__(self, **kw):
25 """Constructs the parser and the lexer""" 26 self.debug = kw.get('debug', 2) 27 self.names = { } 28 try: 29 modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__ 30 except: 31 modname = "parser"+"_"+self.__class__.__name__ 32 self.debugfile = modname + ".dbg" 33 self.tabmodule = modname + "_" + "parsetab" 34 #print self.debugfile, self.tabmodule 35 36 # Build the lexer and parser 37 lex.lex(module=self, debug=self.debug) 38 yacc.yacc(module=self, 39 debug=self.debug, 40 debugfile=self.debugfile, 41 tabmodule=self.tabmodule, 42 check_recursion=self.debug) 43 self.lex=lex 44 self.yacc=yacc
45
46 - def parse(self,content):
47 """Do the actual parsing 48 @param content: String that is to be parsed 49 @return: Result of the parsing""" 50 51 if self.debug: 52 debug=10 53 else: 54 debug=0 55 56 return yacc.parse(content,debug=debug)
57