当前位置: 首页>>代码示例>>Python>>正文


Python pycparser.CParser方法代码示例

本文整理汇总了Python中pycparser.CParser方法的典型用法代码示例。如果您正苦于以下问题:Python pycparser.CParser方法的具体用法?Python pycparser.CParser怎么用?Python pycparser.CParser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pycparser的用法示例。


在下文中一共展示了pycparser.CParser方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _make_scope

# 需要导入模块: import pycparser [as 别名]
# 或者: from pycparser import CParser [as 别名]
def _make_scope():
    """
    Generate CParser scope_stack argument to parse method
    """
    scope = dict()
    for ty in ALL_TYPES:
        if ty in BASIC_TYPES:
            continue
        if ' ' in ty:
            continue

        typ = ALL_TYPES[ty]
        if isinstance(typ, (SimTypeFunction,SimTypeString, SimTypeWString)):
            continue

        scope[ty] = True
    return [scope] 
开发者ID:angr,项目名称:angr,代码行数:19,代码来源:sim_type.py

示例2: _get_parser

# 需要导入模块: import pycparser [as 别名]
# 或者: from pycparser import CParser [as 别名]
def _get_parser():
    global _parser_cache
    if _parser_cache is None:
        _parser_cache = pycparser.CParser()
    return _parser_cache 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:7,代码来源:cparser.py

示例3: _parse

# 需要导入模块: import pycparser [as 别名]
# 或者: from pycparser import CParser [as 别名]
def _parse(self, csource):
        csource, macros = _preprocess(csource)
        # XXX: for more efficiency we would need to poke into the
        # internals of CParser...  the following registers the
        # typedefs, because their presence or absence influences the
        # parsing itself (but what they are typedef'ed to plays no role)
        ctn = _common_type_names(csource)
        typenames = []
        for name in sorted(self._declarations):
            if name.startswith('typedef '):
                name = name[8:]
                typenames.append(name)
                ctn.discard(name)
        typenames += sorted(ctn)
        #
        csourcelines = []
        csourcelines.append('# 1 "<cdef automatic initialization code>"')
        for typename in typenames:
            csourcelines.append('typedef int %s;' % typename)
        csourcelines.append('typedef int __dotdotdotint__, __dotdotdotfloat__,'
                            ' __dotdotdot__;')
        # this forces pycparser to consider the following in the file
        # called <cdef source string> from line 1
        csourcelines.append('# 1 "%s"' % (CDEF_SOURCE_STRING,))
        csourcelines.append(csource)
        fullcsource = '\n'.join(csourcelines)
        if lock is not None:
            lock.acquire()     # pycparser is not thread-safe...
        try:
            ast = _get_parser().parse(fullcsource)
        except pycparser.c_parser.ParseError as e:
            self.convert_pycparser_error(e, csource)
        finally:
            if lock is not None:
                lock.release()
        # csource will be used to find buggy source text
        return ast, macros, csource 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:39,代码来源:cparser.py

示例4: _parse

# 需要导入模块: import pycparser [as 别名]
# 或者: from pycparser import CParser [as 别名]
def _parse(self, csource):
        csource, macros = _preprocess(csource)
        # XXX: for more efficiency we would need to poke into the
        # internals of CParser...  the following registers the
        # typedefs, because their presence or absence influences the
        # parsing itself (but what they are typedef'ed to plays no role)
        ctn = _common_type_names(csource)
        typenames = []
        for name in sorted(self._declarations):
            if name.startswith('typedef '):
                name = name[8:]
                typenames.append(name)
                ctn.discard(name)
        typenames += sorted(ctn)
        #
        csourcelines = ['typedef int %s;' % typename for typename in typenames]
        csourcelines.append('typedef int __dotdotdot__;')
        csourcelines.append(csource)
        csource = '\n'.join(csourcelines)
        if lock is not None:
            lock.acquire()     # pycparser is not thread-safe...
        try:
            ast = _get_parser().parse(csource)
        except pycparser.c_parser.ParseError as e:
            self.convert_pycparser_error(e, csource)
        finally:
            if lock is not None:
                lock.release()
        # csource will be used to find buggy source text
        return ast, macros, csource 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:32,代码来源:cparser.py

示例5: parse_file

# 需要导入模块: import pycparser [as 别名]
# 或者: from pycparser import CParser [as 别名]
def parse_file(defn, preprocess=True):
    """
    Parse a series of C definitions, returns a tuple of two type mappings, one for variable
    definitions and one for type definitions.
    """
    if pycparser is None:
        raise ImportError("Please install pycparser in order to parse C definitions")

    defn = '\n'.join(x for x in defn.split('\n') if _include_re.match(x) is None)

    if preprocess:
        defn = do_preprocess(defn)

    preamble, ignoreme = make_preamble()
    node = pycparser.c_parser.CParser().parse(preamble + defn)
    if not isinstance(node, pycparser.c_ast.FileAST):
        raise ValueError("Something went horribly wrong using pycparser")
    out = {}
    extra_types = {}
    for piece in node.ext:
        if isinstance(piece, pycparser.c_ast.FuncDef):
            out[piece.decl.name] = _decl_to_type(piece.decl.type, extra_types)
        elif isinstance(piece, pycparser.c_ast.Decl):
            ty = _decl_to_type(piece.type, extra_types)
            if piece.name is not None:
                out[piece.name] = ty
        elif isinstance(piece, pycparser.c_ast.Typedef):
            extra_types[piece.name] = copy.copy(_decl_to_type(piece.type, extra_types))
            extra_types[piece.name].label = piece.name

    for ty in ignoreme:
        del extra_types[ty]
    return out, extra_types 
开发者ID:angr,项目名称:angr,代码行数:35,代码来源:sim_type.py

示例6: parse_type

# 需要导入模块: import pycparser [as 别名]
# 或者: from pycparser import CParser [as 别名]
def parse_type(defn, preprocess=True):  # pylint:disable=unused-argument
    """
    Parse a simple type expression into a SimType

    >>> parse_type('int *')
    """
    if pycparser is None:
        raise ImportError("Please install pycparser in order to parse C definitions")

    defn = re.sub(r"/\*.*?\*/", r"", defn)

    parser = pycparser.CParser()

    parser.cparser = pycparser.ply.yacc.yacc(module=parser,
                                             start='parameter_declaration',
                                             debug=False,
                                             optimize=False,
                                             errorlog=errorlog)

    node = parser.parse(text=defn, scope_stack=_make_scope())
    if not isinstance(node, pycparser.c_ast.Typename) and \
            not isinstance(node, pycparser.c_ast.Decl):
        raise ValueError("Something went horribly wrong using pycparser")

    decl = node.type
    return _decl_to_type(decl) 
开发者ID:angr,项目名称:angr,代码行数:28,代码来源:sim_type.py

示例7: _accepts_scope_stack

# 需要导入模块: import pycparser [as 别名]
# 或者: from pycparser import CParser [as 别名]
def _accepts_scope_stack():
    """
    pycparser hack to include scope_stack as parameter in CParser parse method
    """
    def parse(self, text, scope_stack=None, filename='', debuglevel=0):
        self.clex.filename = filename
        self.clex.reset_lineno()
        self._scope_stack = [dict()] if scope_stack is None else scope_stack
        self._last_yielded_token = None
        return self.cparser.parse(
            input=text,
            lexer=self.clex,
            debug=debuglevel)
    setattr(pycparser.CParser, 'parse', parse) 
开发者ID:angr,项目名称:angr,代码行数:16,代码来源:sim_type.py


注:本文中的pycparser.CParser方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。