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 from PyFoam.Error import PyFoamException
8
9 if sys.version_info < (2,3):
10 raise PyFoamException("Version "+str(sys.version_info)+" is not sufficient for ply (2.3 needed)")
11
12 import PyFoam.ThirdParty.ply.lex as lex
13 import PyFoam.ThirdParty.ply.yacc as yacc
14
15 import os
16
18 """
19 Base class for a lexer/parser that has the rules defined as methods
20 """
21 tokens = ()
22 precedence = ()
23
24
26 """Constructs the parser and the lexer"""
27 self.debug = kw.get('debug', 2)
28 self.names = { }
29 try:
30 modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
31 except:
32 modname = "parser"+"_"+self.__class__.__name__
33 self.debugfile = modname + ".dbg"
34 self.tabmodule = modname + "_" + "parsetab"
35
36
37
38 lex.lex(module=self, debug=self.debug)
39 yacc.yacc(module=self,
40 debug=self.debug,
41 debugfile=self.debugfile,
42 tabmodule=self.tabmodule,
43 check_recursion=self.debug)
44 self.lex=lex
45 self.yacc=yacc
46
48 """Do the actual parsing
49 @param content: String that is to be parsed
50 @return: Result of the parsing"""
51
52 if self.debug:
53 debug=10
54 else:
55 debug=0
56
57 return yacc.parse(content,debug=debug)
58