def _tokenize(self): rd = CodeReader(self.value) s = rd.consume_until(end='=') t = T_AssignOperator(s) self.tokens.append(t) s = rd.consume_all() t = T_Expression(s) self.tokens.append(t)
def __init__(self, value): super().__init__(value) rd = CodeReader(value) rd.consume_exact('#define') rd.consume_inline_whitespace() # get macro name self.name = rd.consume_identifier() # arraylike flag self.arraylike = False self.functionlike = False # macro arguments self.args = None # which argument is variadic self.vararg_pos = None #print(str(rd.has_bracket())) if rd.has_paren(): tmp = rd.consume_block()[1:-1] # inside the paren self.args = [] for a in tmp.split(','): a = a.strip() if len(a) > 0: if a[-3:] == '...': # a is a variadic argument if self.vararg_pos is not None: rd.error('Macro can have only one variadic argument!') self.vararg_pos = len(self.args) a = a[:-3].strip() self.args.append(a) self.functionlike = True elif rd.has_bracket(): tmp = rd.consume_block()[1:-1].strip() # inside the bracket if not re.match(r'\A[a-zA-Z_][a-zA-Z0-9_]*\Z', tmp): rd.error('Invalid argument format for macro "%s": %s' % (self.name, tmp)) self.args = [tmp] self.arraylike = True rd.consume_inline_whitespace() # macro body self.body = rd.consume_all() # macro body tokens self.tokens = [] self.__parse_body()