diff options
author | Eli Bendersky <eliben@gmail.com> | 2013-09-24 06:43:04 -0700 |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2013-09-24 06:43:04 -0700 |
commit | 36332196f7c9b5d8501b496f3ee4d44350e71edc (patch) | |
tree | 5b56e2646a36a1d79ad37a43dfab5d6046d98742 /pycparser | |
parent | 2ecc87c62e5fa58d37df20147da1c6015a15b38a (diff) | |
download | pycparser-36332196f7c9b5d8501b496f3ee4d44350e71edc.tar.gz |
Cosmetic whitespace fixes
Diffstat (limited to 'pycparser')
-rw-r--r-- | pycparser/_ast_gen.py | 86 | ||||
-rw-r--r-- | pycparser/_c_ast.cfg | 376 |
2 files changed, 231 insertions, 231 deletions
diff --git a/pycparser/_ast_gen.py b/pycparser/_ast_gen.py index 5e76456..25c40b7 100644 --- a/pycparser/_ast_gen.py +++ b/pycparser/_ast_gen.py @@ -1,7 +1,7 @@ #----------------------------------------------------------------- # _ast_gen.py # -# Generates the AST Node classes from a specification given in +# Generates the AST Node classes from a specification given in # a .yaml file # # The design of this module was inspired by astgen.py from the @@ -20,7 +20,7 @@ class ASTCodeGenerator(object): file. """ self.cfg_filename = cfg_filename - self.node_cfg = [NodeCfg(name, contents) + self.node_cfg = [NodeCfg(name, contents) for (name, contents) in self.parse_cfgfile(cfg_filename)] def generate(self, file=None): @@ -28,11 +28,11 @@ class ASTCodeGenerator(object): """ src = Template(_PROLOGUE_COMMENT).substitute( cfg_filename=self.cfg_filename) - + src += _PROLOGUE_CODE for node_cfg in self.node_cfg: src += node_cfg.generate_source() + '\n\n' - + file.write(src) def parse_cfgfile(self, filename): @@ -57,10 +57,10 @@ class ASTCodeGenerator(object): class NodeCfg(object): - """ Node configuration. + """ Node configuration. name: node name - contents: a list of contents - attributes and child nodes + contents: a list of contents - attributes and child nodes See comment at the top of the configuration file for details. """ def __init__(self, name, contents): @@ -73,7 +73,7 @@ class NodeCfg(object): for entry in contents: clean_entry = entry.rstrip('*') self.all_entries.append(clean_entry) - + if entry.endswith('**'): self.seq_child.append(clean_entry) elif entry.endswith('*'): @@ -86,7 +86,7 @@ class NodeCfg(object): src += '\n' + self._gen_children() src += '\n' + self._gen_attr_names() return src - + def _gen_init(self): src = "class %s(Node):\n" % self.name @@ -95,17 +95,17 @@ class NodeCfg(object): arglist = '(self, %s, coord=None)' % args else: arglist = '(self, coord=None)' - + src += " def __init__%s:\n" % arglist - + for name in self.all_entries + ['coord']: src += " self.%s = %s\n" % (name, name) - + return src def _gen_children(self): src = ' def children(self):\n' - + if self.all_entries: src += ' nodelist = []\n' @@ -114,21 +114,21 @@ class NodeCfg(object): ' if self.%(child)s is not None:' + ' nodelist.append(("%(child)s", self.%(child)s))\n') % ( dict(child=child)) - + for seq_child in self.seq_child: src += ( ' for i, child in enumerate(self.%(child)s or []):\n' ' nodelist.append(("%(child)s[%%d]" %% i, child))\n') % ( dict(child=seq_child)) - + src += ' return tuple(nodelist)\n' else: src += ' return ()\n' - - return src + + return src def _gen_attr_names(self): - src = " attr_names = (" + ''.join("%r," % nm for nm in self.attr) + ')' + src = " attr_names = (" + ''.join("%r," % nm for nm in self.attr) + ')' return src @@ -136,7 +136,7 @@ _PROLOGUE_COMMENT = \ r'''#----------------------------------------------------------------- # ** ATTENTION ** # This code was automatically generated from the file: -# $cfg_filename +# $cfg_filename # # Do not modify it directly. Modify the configuration file and # run the generator again. @@ -167,21 +167,21 @@ class Node(object): def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showcoord=False, _my_node_name=None): """ Pretty print the Node and all its attributes and children (recursively) to a buffer. - - buf: + + buf: Open IO buffer into which the Node is printed. - - offset: - Initial offset (amount of leading spaces) - + + offset: + Initial offset (amount of leading spaces) + attrnames: True if you want to see the attribute names in name=value pairs. False to only see the values. - + nodenames: - True if you want to see the actual node names + True if you want to see the actual node names within their parents. - + showcoord: Do you want the coordinates of each Node to be displayed. @@ -216,47 +216,47 @@ class Node(object): class NodeVisitor(object): - """ A base NodeVisitor class for visiting c_ast nodes. + """ A base NodeVisitor class for visiting c_ast nodes. Subclass it and define your own visit_XXX methods, where - XXX is the class name you want to visit with these + XXX is the class name you want to visit with these methods. - + For example: - + class ConstantVisitor(NodeVisitor): def __init__(self): self.values = [] - + def visit_Constant(self, node): self.values.append(node.value) - Creates a list of values of all the constant nodes + Creates a list of values of all the constant nodes encountered below the given node. To use it: - + cv = ConstantVisitor() cv.visit(node) - + Notes: - - * generic_visit() will be called for AST nodes for which - no visit_XXX method was defined. - * The children of nodes for which a visit_XXX was + + * generic_visit() will be called for AST nodes for which + no visit_XXX method was defined. + * The children of nodes for which a visit_XXX was defined will not be visited - if you need this, call - generic_visit() on the node. + generic_visit() on the node. You can use: NodeVisitor.generic_visit(self, node) * Modeled after Python's own AST visiting facilities (the ast module of Python 3.0) """ def visit(self, node): - """ Visit a node. + """ Visit a node. """ method = 'visit_' + node.__class__.__name__ visitor = getattr(self, method, self.generic_visit) return visitor(node) - + def generic_visit(self, node): - """ Called if no explicit visitor function exists for a + """ Called if no explicit visitor function exists for a node. Implements preorder visiting of the node. """ for c_name, c in node.children(): diff --git a/pycparser/_c_ast.cfg b/pycparser/_c_ast.cfg index 1c468a2..a8e33ff 100644 --- a/pycparser/_c_ast.cfg +++ b/pycparser/_c_ast.cfg @@ -1,188 +1,188 @@ -#-----------------------------------------------------------------
-# pycparser: _c_ast_gen.cfg
-#
-# Defines the AST Node classes used in pycparser.
-#
-# Each entry is a Node sub-class name, listing the attributes
-# and child nodes of the class:
-# <name>* - a child node
-# <name>** - a sequence of child nodes
-# <name> - an attribute
-#
-# Copyright (C) 2008-2012, Eli Bendersky
-# License: BSD
-#-----------------------------------------------------------------
-
-ArrayDecl: [type*, dim*]
-
-ArrayRef: [name*, subscript*]
-
-# op: =, +=, /= etc.
-#
-Assignment: [op, lvalue*, rvalue*]
-
-BinaryOp: [op, left*, right*]
-
-Break: []
-
-Case: [expr*, stmts**]
-
-Cast: [to_type*, expr*]
-
-# Compound statement in C99 is a list of block items (declarations or
-# statements).
-#
-Compound: [block_items**]
-
-# Compound literal (anonymous aggregate) for C99.
-# (type-name) {initializer_list}
-# type: the typename
-# init: InitList for the initializer list
-#
-CompoundLiteral: [type*, init*]
-
-# type: int, char, float, etc. see CLexer for constant token types
-#
-Constant: [type, value]
-
-Continue: []
-
-# name: the variable being declared
-# quals: list of qualifiers (const, volatile)
-# funcspec: list function specifiers (i.e. inline in C99)
-# storage: list of storage specifiers (extern, register, etc.)
-# type: declaration type (probably nested with all the modifiers)
-# init: initialization value, or None
-# bitsize: bit field size, or None
-#
-Decl: [name, quals, storage, funcspec, type*, init*, bitsize*]
-
-DeclList: [decls**]
-
-Default: [stmts**]
-
-DoWhile: [cond*, stmt*]
-
-# Represents the ellipsis (...) parameter in a function
-# declaration
-#
-EllipsisParam: []
-
-# An empty statement (a semicolon ';' on its own)
-#
-EmptyStatement: []
-
-# Enumeration type specifier
-# name: an optional ID
-# values: an EnumeratorList
-#
-Enum: [name, values*]
-
-# A name/value pair for enumeration values
-#
-Enumerator: [name, value*]
-
-# A list of enumerators
-#
-EnumeratorList: [enumerators**]
-
-# A list of expressions separated by the comma operator.
-#
-ExprList: [exprs**]
-
-# This is the top of the AST, representing a single C file (a
-# translation unit in K&R jargon). It contains a list of
-# "external-declaration"s, which is either declarations (Decl),
-# Typedef or function definitions (FuncDef).
-#
-FileAST: [ext**]
-
-# for (init; cond; next) stmt
-#
-For: [init*, cond*, next*, stmt*]
-
-# name: Id
-# args: ExprList
-#
-FuncCall: [name*, args*]
-
-# type <decl>(args)
-#
-FuncDecl: [args*, type*]
-
-# Function definition: a declarator for the function name and
-# a body, which is a compound statement.
-# There's an optional list of parameter declarations for old
-# K&R-style definitions
-#
-FuncDef: [decl*, param_decls**, body*]
-
-Goto: [name]
-
-ID: [name]
-
-# Holder for types that are a simple identifier (e.g. the built
-# ins void, char etc. and typedef-defined types)
-#
-IdentifierType: [names]
-
-If: [cond*, iftrue*, iffalse*]
-
-# An initialization list used for compound literals.
-#
-InitList: [exprs**]
-
-Label: [name, stmt*]
-
-# A named initializer for C99.
-# The name of a NamedInitializer is a sequence of Nodes, because
-# names can be hierarchical and contain constant expressions.
-#
-NamedInitializer: [name**, expr*]
-
-# a list of comma separated function parameter declarations
-#
-ParamList: [params**]
-
-PtrDecl: [quals, type*]
-
-Return: [expr*]
-
-# name: struct tag name
-# decls: declaration of members
-#
-Struct: [name, decls**]
-
-# type: . or ->
-# name.field or name->field
-#
-StructRef: [name*, type, field*]
-
-Switch: [cond*, stmt*]
-
-# cond ? iftrue : iffalse
-#
-TernaryOp: [cond*, iftrue*, iffalse*]
-
-# A base type declaration
-#
-TypeDecl: [declname, quals, type*]
-
-# A typedef declaration.
-# Very similar to Decl, but without some attributes
-#
-Typedef: [name, quals, storage, type*]
-
-Typename: [quals, type*]
-
-UnaryOp: [op, expr*]
-
-# name: union tag name
-# decls: declaration of members
-#
-Union: [name, decls**]
-
-While: [cond*, stmt*]
-
-
-
+#----------------------------------------------------------------- +# pycparser: _c_ast.cfg +# +# Defines the AST Node classes used in pycparser. +# +# Each entry is a Node sub-class name, listing the attributes +# and child nodes of the class: +# <name>* - a child node +# <name>** - a sequence of child nodes +# <name> - an attribute +# +# Copyright (C) 2008-2012, Eli Bendersky +# License: BSD +#----------------------------------------------------------------- + +ArrayDecl: [type*, dim*] + +ArrayRef: [name*, subscript*] + +# op: =, +=, /= etc. +# +Assignment: [op, lvalue*, rvalue*] + +BinaryOp: [op, left*, right*] + +Break: [] + +Case: [expr*, stmts**] + +Cast: [to_type*, expr*] + +# Compound statement in C99 is a list of block items (declarations or +# statements). +# +Compound: [block_items**] + +# Compound literal (anonymous aggregate) for C99. +# (type-name) {initializer_list} +# type: the typename +# init: InitList for the initializer list +# +CompoundLiteral: [type*, init*] + +# type: int, char, float, etc. see CLexer for constant token types +# +Constant: [type, value] + +Continue: [] + +# name: the variable being declared +# quals: list of qualifiers (const, volatile) +# funcspec: list function specifiers (i.e. inline in C99) +# storage: list of storage specifiers (extern, register, etc.) +# type: declaration type (probably nested with all the modifiers) +# init: initialization value, or None +# bitsize: bit field size, or None +# +Decl: [name, quals, storage, funcspec, type*, init*, bitsize*] + +DeclList: [decls**] + +Default: [stmts**] + +DoWhile: [cond*, stmt*] + +# Represents the ellipsis (...) parameter in a function +# declaration +# +EllipsisParam: [] + +# An empty statement (a semicolon ';' on its own) +# +EmptyStatement: [] + +# Enumeration type specifier +# name: an optional ID +# values: an EnumeratorList +# +Enum: [name, values*] + +# A name/value pair for enumeration values +# +Enumerator: [name, value*] + +# A list of enumerators +# +EnumeratorList: [enumerators**] + +# A list of expressions separated by the comma operator. +# +ExprList: [exprs**] + +# This is the top of the AST, representing a single C file (a +# translation unit in K&R jargon). It contains a list of +# "external-declaration"s, which is either declarations (Decl), +# Typedef or function definitions (FuncDef). +# +FileAST: [ext**] + +# for (init; cond; next) stmt +# +For: [init*, cond*, next*, stmt*] + +# name: Id +# args: ExprList +# +FuncCall: [name*, args*] + +# type <decl>(args) +# +FuncDecl: [args*, type*] + +# Function definition: a declarator for the function name and +# a body, which is a compound statement. +# There's an optional list of parameter declarations for old +# K&R-style definitions +# +FuncDef: [decl*, param_decls**, body*] + +Goto: [name] + +ID: [name] + +# Holder for types that are a simple identifier (e.g. the built +# ins void, char etc. and typedef-defined types) +# +IdentifierType: [names] + +If: [cond*, iftrue*, iffalse*] + +# An initialization list used for compound literals. +# +InitList: [exprs**] + +Label: [name, stmt*] + +# A named initializer for C99. +# The name of a NamedInitializer is a sequence of Nodes, because +# names can be hierarchical and contain constant expressions. +# +NamedInitializer: [name**, expr*] + +# a list of comma separated function parameter declarations +# +ParamList: [params**] + +PtrDecl: [quals, type*] + +Return: [expr*] + +# name: struct tag name +# decls: declaration of members +# +Struct: [name, decls**] + +# type: . or -> +# name.field or name->field +# +StructRef: [name*, type, field*] + +Switch: [cond*, stmt*] + +# cond ? iftrue : iffalse +# +TernaryOp: [cond*, iftrue*, iffalse*] + +# A base type declaration +# +TypeDecl: [declname, quals, type*] + +# A typedef declaration. +# Very similar to Decl, but without some attributes +# +Typedef: [name, quals, storage, type*] + +Typename: [quals, type*] + +UnaryOp: [op, expr*] + +# name: union tag name +# decls: declaration of members +# +Union: [name, decls**] + +While: [cond*, stmt*] + + + |