1
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
17 """
18 Base class for a lexer/parser that has the rules defined as methods
19 """
20 tokens = ()
21 precedence = ()
22
23
25 """Constructs the parser and the lexer"""
26 self.debug = kw.get('debug', 1)
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
35
36
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 self.lex=lex
43 self.yacc=yacc
44
46 """Do the actual parsing
47 @param content: String that is to be parsed
48 @return: Result of the parsing"""
49
50 return yacc.parse(content)
51