6 class MacroParser(object):
8 def __init__ (self
, buf
):
15 A macro with arguments must have its open paren immediately following
16 its name without any whitespace.
20 print "parsing '%s'"%self
.buffer
23 bufSize
= len(self
.buffer)
27 if c
in [' ', "\t"] and len(name
) == 0:
28 # This is a simple macro with no arguments.
31 content
= self
.buffer[i
:]
32 self
.setMacro(name
, vars, content
)
34 elif c
== '(' and len(name
) == 0:
35 # This one has arguments.
38 vars, content
= self
.parseArgs(buf
)
39 self
.setMacro(name
, vars, content
)
45 def parseArgs (self
, buffer):
48 The buffer is expected to be formatted like '(a, b, c)' where the first
49 character is the open paren.
69 raise globals.ParseError ('')
72 elif c
in " \t" and scope
> 0:
80 raise globals.ParseError ('')
82 return vars, buffer[i
+1:]
85 def setMacro (self
, name
, vars, content
):
93 print "content: '%s'"%content
96 self
.macro
= Macro(name
)
97 for i
in xrange(0, len(vars)):
98 self
.macro
.vars[vars[i
]] = i
100 # tokinize it using lexer.
101 mclexer
= srclexer
.SrcLexer(content
)
102 mclexer
.expandHeaders
= False
103 mclexer
.inMacroDefine
= True
105 self
.macro
.tokens
= mclexer
.getTokens()
107 print self
.macro
.tokens
109 if not self
.isValidMacro(self
.macro
):
113 if self
.macro
!= None:
114 print "macro registered!"
116 print "macro not registered"
118 def isValidMacro (self
, macro
):
120 n
= len(macro
.tokens
)
123 elif len(macro
.name
) > 4 and macro
.name
[1:4] == 'ID_':
124 # We don't want to expand macros like HID_, SID_, WID_, etc.