1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
import sys
from globals import *
import srclexer
class MacroParser(object):
def __init__ (self, buf):
self.buffer = buf
self.macro = None
self.debug = False
def parse (self):
"""
A macro with arguments must have its open paren immediately following
its name without any whitespace.
"""
if self.debug:
print "-"*68
print "parsing '%s'"%self.buffer
i = 0
bufSize = len(self.buffer)
name, buf = '', ''
while i < bufSize:
c = self.buffer[i]
if c in [' ', "\t"] and len(name) == 0:
# This is a simple macro with no arguments.
name = buf
vars = []
content = self.buffer[i:]
self.setMacro(name, vars, content)
return
elif c == '(' and len(name) == 0:
# This one has arguments.
name = buf
buf = self.buffer[i:]
vars, content = self.parseArgs(buf)
self.setMacro(name, vars, content)
return
else:
buf += c
i += 1
def parseArgs (self, buffer):
"""Parse arguments.
The buffer is expected to be formatted like '(a, b, c)' where the first
character is the open paren.
"""
scope = 0
buf = ''
vars = []
content = ''
bufSize = len(buffer)
i = 0
while i < bufSize:
c = buffer[i]
if c == '(':
scope += 1
elif c == ')':
scope -= 1
if len(buf) > 0:
vars.append(buf)
if scope == 0:
break
elif c == ',':
if len(buf) == 0:
raise globals.ParseError ('')
vars.append(buf)
buf = ''
elif c in " \t" and scope > 0:
pass
else:
buf += c
i += 1
if scope > 0:
raise globals.ParseError ('')
return vars, buffer[i+1:]
def setMacro (self, name, vars, content):
if self.debug:
print "-"*68
print "name: %s"%name
for var in vars:
print "var: %s"%var
if len(vars) == 0:
print "no vars"
print "content: '%s'"%content
if len(content) > 0:
self.macro = Macro(name)
for i in xrange(0, len(vars)):
self.macro.vars[vars[i]] = i
# tokinize it using lexer.
mclexer = srclexer.SrcLexer(content)
mclexer.expandHeaders = False
mclexer.inMacroDefine = True
mclexer.tokenize()
self.macro.tokens = mclexer.getTokens()
if self.debug:
print self.macro.tokens
if not self.isValidMacro(self.macro):
self.macro = None
if self.debug:
if self.macro != None:
print "macro registered!"
else:
print "macro not registered"
def isValidMacro (self, macro):
n = len(macro.tokens)
if n == 0:
return False
elif len(macro.name) > 4 and macro.name[1:4] == 'ID_':
# We don't want to expand macros like HID_, SID_, WID_, etc.
return False
return True
def getMacro (self):
return self.macro
|