import functools


def _rule(func):
  """A decorator for defining rules."""
  assert func.__doc__
  assert func.__name__.startswith('p_')
  name = func.__name__[2:]
  if name.endswith('_error'):
    name = name[:-6]
  assert func.__doc__.startswith(name + ' :')

  @functools.wraps(func)
  def wrapper(self, p):
    p[0] = func(self, p)
  return wrapper