From 27797e811836e73a90cf736d65c468e82a5c80a7 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Wed, 25 Sep 2013 06:30:17 -0700 Subject: More cleanups - whitespace, copyrights, etc. --- examples/c-to-c.py | 16 +++++++++------- examples/cdecl.py | 46 +++++++++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 30 deletions(-) (limited to 'examples') diff --git a/examples/c-to-c.py b/examples/c-to-c.py index 35e1c64..2d3484c 100644 --- a/examples/c-to-c.py +++ b/examples/c-to-c.py @@ -4,7 +4,7 @@ # Example of using pycparser.c_generator, serving as a simplistic translator # from C to AST and back to C. # -# Copyright (C) 2008-2012, Eli Bendersky +# Copyright (C) 2008-2013, Eli Bendersky # License: BSD #------------------------------------------------------------------------------ from __future__ import print_function @@ -19,17 +19,19 @@ from pycparser import parse_file, c_parser, c_generator def translate_to_c(filename): + """ Simply use the c_generator module to emit a parsed AST. + """ ast = parse_file(filename, use_cpp=True) generator = c_generator.CGenerator() print(generator.visit(ast)) -def zz_test_translate(): +def _zz_test_translate(): # internal use src = r''' - + void f(char * restrict joe){} - + int main(void) { unsigned int long k = 4; @@ -41,9 +43,9 @@ int main(void) ast = parser.parse(src) ast.show() generator = c_generator.CGenerator() - + print(generator.visit(ast)) - + # tracing the generator for debugging #~ import trace #~ tr = trace.Trace(countcallers=1) @@ -53,7 +55,7 @@ int main(void) #------------------------------------------------------------------------------ if __name__ == "__main__": - #zz_test_translate() + #_zz_test_translate() if len(sys.argv) > 1: translate_to_c(sys.argv[1]) else: diff --git a/examples/cdecl.py b/examples/cdecl.py index 8884591..e885e40 100644 --- a/examples/cdecl.py +++ b/examples/cdecl.py @@ -1,15 +1,14 @@ #----------------------------------------------------------------- # pycparser: cdecl.py # -# Example of the CDECL tool using pycparser. CDECL "explains" -# C type declarations in plain English. +# Example of the CDECL tool using pycparser. CDECL "explains" C type +# declarations in plain English. # -# 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, it isn't -# really used - by pycparser must know which tokens are types. +# 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. # # For example: # @@ -17,7 +16,7 @@ # => # ar is a pointer to array[10] of pointer to const Node # -# Copyright (C) 2008-2011, Eli Bendersky +# Copyright (C) 2008-2013, Eli Bendersky # License: BSD #----------------------------------------------------------------- import sys @@ -31,22 +30,23 @@ from pycparser import c_parser, c_ast def explain_c_declaration(c_decl): - """ Parses the declaration in c_decl and returns a text + """ 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. """ parser = c_parser.CParser() - + try: node = parser.parse(c_decl, filename='') except c_parser.ParseError: e = sys.exc_info()[1] return "Parse error:" + str(e) - if ( not isinstance(node, c_ast.FileAST) or - not isinstance(node.ext[-1], c_ast.Decl)): + if (not isinstance(node, c_ast.FileAST) or + not isinstance(node.ext[-1], c_ast.Decl) + ): return "Not a valid declaration" return _explain_decl_node(node.ext[-1]) @@ -58,10 +58,10 @@ def _explain_decl_node(decl_node): """ #~ print decl_node.show() storage = ' '.join(decl_node.storage) + ' ' if decl_node.storage else '' - - return (decl_node.name + - " is a " + - storage + + + return (decl_node.name + + " is a " + + storage + _explain_type(decl_node.type)) @@ -69,7 +69,7 @@ def _explain_type(decl): """ Recursively explains a type decl node """ typ = type(decl) - + if typ == c_ast.TypeDecl: quals = ' '.join(decl.quals) + ' ' if decl.quals else '' return quals + _explain_type(decl.type) @@ -83,16 +83,16 @@ def _explain_type(decl): elif typ == c_ast.ArrayDecl: arr = 'array' if decl.dim: arr += '[%s]' % decl.dim.value - + return arr + " of " + _explain_type(decl.type) - + elif typ == c_ast.FuncDecl: if decl.args: params = [_explain_type(param) for param in decl.args.params] args = ', '.join(params) else: args = '' - + return ('function(%s) returning ' % (args) + _explain_type(decl.type)) @@ -104,5 +104,5 @@ if __name__ == "__main__": c_decl = "char *(*(**foo[][8])())[];" print("Explaining the declaration: " + c_decl + "\n") - print(explain_c_declaration(c_decl) + "\n") + print(explain_c_declaration(c_decl) + "\n") -- cgit v1.2.3