diff options
author | Eli Bendersky <eliben@gmail.com> | 2017-02-26 06:03:54 -0800 |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2017-02-26 06:03:54 -0800 |
commit | 69bca70ea145ac81f6b950a741f5e08183d3e0fa (patch) | |
tree | 594afe184e72760d276804c27a0820bf35a63c1b /examples | |
parent | b23e2673242b943b79112e008f5fb395371ff3aa (diff) | |
download | pycparser-69bca70ea145ac81f6b950a741f5e08183d3e0fa.tar.gz |
Clean up cdecl.py a bit
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cdecl.py | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/examples/cdecl.py b/examples/cdecl.py index 92ac715..a510087 100644 --- a/examples/cdecl.py +++ b/examples/cdecl.py @@ -6,9 +6,8 @@ # # The AST generated by pycparser from the given declaration is traversed # recursively to build the explanation. Note that the declaration must be a -# valid external declaration in C. All the types used in it must be defined with -# typedef, or parsing will fail. The definition can be arbitrary - pycparser -# doesn't really care what the type is defined to be, only that it's a type. +# valid external declaration in C. As shown below, typedef can be optionally +# expanded. # # For example: # @@ -17,7 +16,7 @@ # explain_c_declaration(c_decl) # => ar is a pointer to array[10] of pointer to const Node # -# struct and typedef are expanded when according arguments are set: +# struct and typedef can be optionally expanded: # # explain_c_declaration(c_decl, expand_typedef=True) # => ar is a pointer to array[10] of pointer to const int @@ -48,8 +47,11 @@ def explain_c_declaration(c_decl, expand_struct=False, expand_typedef=False): """ Parses the declaration in c_decl and returns a text explanation as a string. - The last external node of the string is used, to allow - earlier typedefs for used types. + The last external node of the string is used, to allow earlier typedefs + for used types. + + expand_struct=True will spell out struct definitions recursively. + expand_typedef=True will expand typedef'd types. """ parser = c_parser.CParser() @@ -125,32 +127,35 @@ def _explain_type(decl): ('containing {%s}' % members if members else '')) -def expand_struct_typedef(cdecl, file_ast, expand_struct=False, expand_typedef=False): - """Expand struct & typedef in context of file_ast and return a new expanded node""" +def expand_struct_typedef(cdecl, file_ast, + expand_struct=False, + expand_typedef=False): + """Expand struct & typedef and return a new expanded node.""" decl_copy = copy.deepcopy(cdecl) _expand_in_place(decl_copy, file_ast, expand_struct, expand_typedef) return decl_copy def _expand_in_place(decl, file_ast, expand_struct=False, expand_typedef=False): - """Recursively expand struct & typedef in place, throw Exception if + """Recursively expand struct & typedef in place, throw RuntimeError if undeclared struct or typedef are used """ typ = type(decl) if typ in (c_ast.Decl, c_ast.TypeDecl, c_ast.PtrDecl, c_ast.ArrayDecl): - decl.type = _expand_in_place(decl.type, file_ast, expand_struct, expand_typedef) + decl.type = _expand_in_place(decl.type, file_ast, expand_struct, + expand_typedef) elif typ == c_ast.Struct: if not decl.decls: struct = _find_struct(decl.name, file_ast) if not struct: - raise Exception('using undeclared struct %s' % decl.name) + raise RuntimeError('using undeclared struct %s' % decl.name) decl.decls = struct.decls for i, mem_decl in enumerate(decl.decls): - decl.decls[i] = _expand_in_place(mem_decl, file_ast, expand_struct, expand_typedef) - + decl.decls[i] = _expand_in_place(mem_decl, file_ast, expand_struct, + expand_typedef) if not expand_struct: decl.decls = [] @@ -158,7 +163,7 @@ def _expand_in_place(decl, file_ast, expand_struct=False, expand_typedef=False): decl.names[0] not in ('int', 'char')): typedef = _find_typedef(decl.names[0], file_ast) if not typedef: - raise Exception('using undeclared type %s' % decl.names[0]) + raise RuntimeError('using undeclared type %s' % decl.names[0]) if expand_typedef: return typedef.type |