def do_preprocess(defn): """ Run a string through the C preprocessor that ships with pycparser but is weirdly inaccessible? """ from pycparser.ply import lex, cpp lexer = lex.lex(cpp) p = cpp.Preprocessor(lexer) p.parse(defn) return ''.join(tok.value for tok in p.parser if tok.type not in p.ignore)
def lazy_preprocess(source): pp = cpp.Preprocessor(lex.lex(module=cpp)) pp.parse(source) result = "" while True: token = pp.token() if not token: return result result += token.value
def do_preprocess(defn): """ Run a string through the C preprocessor that ships with pycparser but is weirdly inaccessible? """ from pycparser.ply import lex, cpp # pylint:disable=import-outside-toplevel lexer = lex.lex(cpp) p = cpp.Preprocessor(lexer) # p.add_path(dir) will add dir to the include search path p.parse(defn) return ''.join(tok.value for tok in p.parser if tok.type not in p.ignore)
def do_preprocess(defn): """ Run a string through the C preprocessor that ships with pycparser but is weirdly inaccessable? """ import pycparser.ply.lex as lex import pycparser.ply.cpp as cpp lexer = lex.lex(cpp) p = cpp.Preprocessor(lexer) # p.add_path(dir) will add dir to the include search path p.parse(defn) return ''.join(tok.value for tok in p.parser if tok.type not in p.ignore)
def preprocess_defs(defn): from pycparser.ply import lex, cpp lexer = lex.lex(cpp) p = cpp.Preprocessor(lexer) list(p.parsegen(defn)) defs = {x: y.value[0].value for x, y in p.macros.items() if not x.startswith("__")} ret = {} for x, y in defs.items(): y = y.strip("'").strip('"') v = None if y.isnumeric(): v = int(y) elif y.startswith("0x") and all(c in string.hexdigits for c in y[2:]): v = int(y, 16) elif len(y) == 1: v = ord(y) if v is not None: ret[x] = v return ret