plyparser.py 4.8 KB

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