def _parse_literal_cursor(self, c: Cursor, warn_failed: bool = False) \ -> Tuple[Optional[str], Optional[Union[str, float, int]]]: """ used to parse variable :return: literal, value """ tokens: List[Token] = list(c.get_tokens()) has_assign = False for i, t in enumerate(tokens): if has_assign: if t.kind == TokenKind.IDENTIFIER: if t.cursor.kind == CursorKind.MACRO_INSTANTIATION: return self._parse_macro_literal_cursor(c) if t.kind == TokenKind.LITERAL: spelling = t.spelling val = self._try_parse_literal(t.cursor.kind, t.spelling) last_t = tokens[i - 1] if last_t.kind == TokenKind.PUNCTUATION and last_t.spelling == '-': return spelling, -val return spelling, val elif t.spelling == '=': has_assign = True if has_assign: if warn_failed: logger.warning("unknown literal, kind:%s, spelling:%s, %s", c.kind, c.spelling, c.extent) return None, None
def _process_macro_definition(c: Cursor): name = c.spelling tokens = list(c.get_tokens()) length = len(tokens) m = Macro(name=name, parent=None, # macro has no parent location=location_from_cursor(c), definition="", brief_comment=c.brief_comment, ) if length == 1: return m m.definition = " ".join([i.spelling for i in tokens[1:]]) return m