You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.9 KiB

7 years ago
  1. #-----------------------------------------------------------------
  2. # plyparser.py
  3. #
  4. # PLYParser class and other utilites for simplifying programming
  5. # parsers with PLY
  6. #
  7. # Eli Bendersky [http://eli.thegreenplace.net]
  8. # License: BSD
  9. #-----------------------------------------------------------------
  10. class Coord(object):
  11. """ Coordinates of a syntactic element. Consists of:
  12. - File name
  13. - Line number
  14. - (optional) column number, for the Lexer
  15. """
  16. __slots__ = ('file', 'line', 'column', '__weakref__')
  17. def __init__(self, file, line, column=None):
  18. self.file = file
  19. self.line = line
  20. self.column = column
  21. def __str__(self):
  22. str = "%s:%s" % (self.file, self.line)
  23. if self.column: str += ":%s" % self.column
  24. return str
  25. class ParseError(Exception): pass
  26. class PLYParser(object):
  27. def _create_opt_rule(self, rulename):
  28. """ Given a rule name, creates an optional ply.yacc rule
  29. for it. The name of the optional rule is
  30. <rulename>_opt
  31. """
  32. optname = rulename + '_opt'
  33. def optrule(self, p):
  34. p[0] = p[1]
  35. optrule.__doc__ = '%s : empty\n| %s' % (optname, rulename)
  36. optrule.__name__ = 'p_%s' % optname
  37. setattr(self.__class__, optrule.__name__, optrule)
  38. def _coord(self, lineno, column=None):
  39. return Coord(
  40. file=self.clex.filename,
  41. line=lineno,
  42. column=column)
  43. def _token_coord(self, p, token_idx):
  44. """ Returns the coordinates for the YaccProduction objet 'p' indexed
  45. with 'token_idx'. The coordinate includes the 'lineno' and
  46. 'column'. Both follow the lex semantic, starting from 1.
  47. """
  48. last_cr = p.lexer.lexer.lexdata.rfind('\n', 0, p.lexpos(token_idx))
  49. if last_cr < 0:
  50. last_cr = -1
  51. column = (p.lexpos(token_idx) - (last_cr))
  52. return self._coord(p.lineno(token_idx), column)
  53. def _parse_error(self, msg, coord):
  54. raise ParseError("%s: %s" % (coord, msg))
  55. def parameterized(*params):
  56. """ Decorator to create parameterized rules.
  57. Parameterized rule methods must be named starting with 'p_' and contain
  58. 'xxx', and their docstrings may contain 'xxx' and 'yyy'. These will be
  59. replaced by the given parameter tuples. For example, ``p_xxx_rule()`` with
  60. docstring 'xxx_rule : yyy' when decorated with
  61. ``@parameterized(('id', 'ID'))`` produces ``p_id_rule()`` with the docstring
  62. 'id_rule : ID'. Using multiple tuples produces multiple rules.
  63. """
  64. def decorate(rule_func):
  65. rule_func._params = params
  66. return rule_func
  67. return decorate
  68. def template(cls):
  69. """ Class decorator to generate rules from parameterized rule templates.
  70. See `parameterized` for more information on parameterized rules.
  71. """
  72. for attr_name in dir(cls):
  73. if attr_name.startswith('p_'):
  74. method = getattr(cls, attr_name)
  75. if hasattr(method, '_params'):
  76. delattr(cls, attr_name) # Remove template method
  77. _create_param_rules(cls, method)
  78. return cls
  79. def _create_param_rules(cls, func):
  80. """ Create ply.yacc rules based on a parameterized rule function
  81. Generates new methods (one per each pair of parameters) based on the
  82. template rule function `func`, and attaches them to `cls`. The rule
  83. function's parameters must be accessible via its `_params` attribute.
  84. """
  85. for xxx, yyy in func._params:
  86. # Use the template method's body for each new method
  87. def param_rule(self, p):
  88. func(self, p)
  89. # Substitute in the params for the grammar rule and function name
  90. param_rule.__doc__ = func.__doc__.replace('xxx', xxx).replace('yyy', yyy)
  91. param_rule.__name__ = func.__name__.replace('xxx', xxx)
  92. # Attach the new method to the class
  93. setattr(cls, param_rule.__name__, param_rule)

Powered by TurnKey Linux.