Skip to content

Commit

Permalink
Auto-remove template methods and remove leading underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
natezb committed Feb 20, 2017
1 parent d53e36a commit 79100bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
16 changes: 8 additions & 8 deletions pycparser/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,19 +1015,19 @@ def p_declarator(self, p):
p[0] = p[1]

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
def _p_XXX_declarator_1(self, p):
def p_XXX_declarator_1(self, p):
""" XXX_declarator : direct_XXX_declarator
"""
p[0] = p[1]

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
def _p_XXX_declarator_2(self, p):
def p_XXX_declarator_2(self, p):
""" XXX_declarator : pointer direct_XXX_declarator
"""
p[0] = self._type_modify_decl(p[2], p[1])

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
def _p_direct_XXX_declarator_1(self, p):
def p_direct_XXX_declarator_1(self, p):
""" direct_XXX_declarator : YYY
"""
p[0] = c_ast.TypeDecl(
Expand All @@ -1037,13 +1037,13 @@ def _p_direct_XXX_declarator_1(self, p):
coord=self._coord(p.lineno(1)))

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'))
def _p_direct_XXX_declarator_2(self, p):
def p_direct_XXX_declarator_2(self, p):
""" direct_XXX_declarator : LPAREN XXX_declarator RPAREN
"""
p[0] = p[2]

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
def _p_direct_XXX_declarator_3(self, p):
def p_direct_XXX_declarator_3(self, p):
""" direct_XXX_declarator : direct_XXX_declarator LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET
"""
quals = (p[3] if len(p) > 5 else []) or []
Expand All @@ -1058,7 +1058,7 @@ def _p_direct_XXX_declarator_3(self, p):
p[0] = self._type_modify_decl(decl=p[1], modifier=arr)

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
def _p_direct_XXX_declarator_4(self, p):
def p_direct_XXX_declarator_4(self, p):
""" direct_XXX_declarator : direct_XXX_declarator LBRACKET STATIC type_qualifier_list_opt assignment_expression RBRACKET
| direct_XXX_declarator LBRACKET type_qualifier_list STATIC assignment_expression RBRACKET
"""
Expand All @@ -1080,7 +1080,7 @@ def _p_direct_XXX_declarator_4(self, p):
# Special for VLAs
#
@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
def _p_direct_XXX_declarator_5(self, p):
def p_direct_XXX_declarator_5(self, p):
""" direct_XXX_declarator : direct_XXX_declarator LBRACKET type_qualifier_list_opt TIMES RBRACKET
"""
arr = c_ast.ArrayDecl(
Expand All @@ -1092,7 +1092,7 @@ def _p_direct_XXX_declarator_5(self, p):
p[0] = self._type_modify_decl(decl=p[1], modifier=arr)

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
def _p_direct_XXX_declarator_6(self, p):
def p_direct_XXX_declarator_6(self, p):
""" direct_XXX_declarator : direct_XXX_declarator LPAREN parameter_type_list RPAREN
| direct_XXX_declarator LPAREN identifier_list_opt RPAREN
"""
Expand Down
9 changes: 5 additions & 4 deletions pycparser/plyparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def _parse_error(self, msg, coord):
def parameterized(*params):
""" Decorator to create parameterized rules.
Parameterized rule methods must be named starting with '_p_' and contain
Parameterized rule methods must be named starting with 'p_' and contain
'XXX', and their docstrings may contain 'XXX' and 'YYY'. These will be
replaced by the given parameter tuples. For example, ``_p_XXX_rule()`` with
replaced by the given parameter tuples. For example, ``p_XXX_rule()`` with
docstring 'XXX_rule : YYY' when decorated with
``@parameterized(('id', 'ID'))`` produces ``p_id_rule()`` with the docstring
'id_rule : ID'. Using multiple tuples produces multiple rules.
Expand All @@ -73,9 +73,10 @@ def decorate(rule_func):

def template(cls):
for attr_name in dir(cls):
if attr_name.startswith('_p_'):
if attr_name.startswith('p_'):
method = getattr(cls, attr_name)
if hasattr(method, '_params'):
delattr(cls, attr_name) # Remove template method
_create_param_rules(cls, method)
return cls

Expand All @@ -87,5 +88,5 @@ def param_rule(self, p):
func(self, p)

param_rule.__doc__ = func.__doc__.replace('XXX', xxx).replace('YYY', yyy)
param_rule.__name__ = func.__name__.replace('XXX', xxx)[1:]
param_rule.__name__ = func.__name__.replace('XXX', xxx)
setattr(cls, param_rule.__name__, param_rule)

0 comments on commit 79100bd

Please sign in to comment.