本文整理汇总了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]
示例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
示例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
示例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
示例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
示例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)
示例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)