parser.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import sys
  2. from external.graph_drawing.graph import generate_graph
  3. from external.graph_drawing.line import generate_line
  4. from src.node import negation_to_node
  5. def create_graph(node):
  6. return generate_graph(negation_to_node(node))
  7. class ParserWrapper(object):
  8. def __init__(self, base_class, **kwargs):
  9. self.input_buffer = []
  10. self.last_buffer = ''
  11. self.input_position = 0
  12. self.closed = False
  13. self.verbose = kwargs.get('verbose', False)
  14. self.parser = base_class(file=self, read=self.read, **kwargs)
  15. def readline(self, nbytes=False):
  16. return self.read(nbytes)
  17. def read(self, nbytes=False):
  18. if len(self.last_buffer) >= nbytes:
  19. buf = self.last_buffer[:nbytes]
  20. self.last_buffer = self.last_buffer[nbytes:]
  21. return buf
  22. buf = self.last_buffer
  23. try:
  24. buf += self.input_buffer[self.input_position]
  25. if self.verbose:
  26. print 'read:', buf # pragma: nocover
  27. self.input_position += 1
  28. except IndexError:
  29. self.closed = True
  30. return ''
  31. self.last_buffer = buf[nbytes:]
  32. return buf
  33. def close(self):
  34. self.closed = True
  35. self.input_position = len(self.input_buffer)
  36. def run(self, input_buffer, *args, **kwargs):
  37. map(self.append, input_buffer)
  38. return self.parser.run(*args, **kwargs)
  39. def append(self, input):
  40. self.closed = False
  41. self.input_buffer.append(input + '\n')
  42. def run_expressions(base_class, expressions, fail=True, silent=False,
  43. **kwargs):
  44. """
  45. Run a list of mathematical expression through the term rewriting system and
  46. check if the output matches the expected output. The list of EXPRESSIONS
  47. consists of tuples (expression, output), where expression is the
  48. mathematical expression to evaluate (String) and output is the expected
  49. output of the evaluation (thus, the output can be Float, Int or None).
  50. If KEEPFILES is non-zero or True, the generated Flex and Bison files will
  51. be kept. Otherwise, those temporary files will be deleted. If FAIL is True,
  52. and the output of the expression is not equal to the expected output, an
  53. assertion error is raised. If SILENT is False, and an assertion error is
  54. raised, an error message is printed on stderr. If SILENT is True, no error
  55. message will be printed.
  56. If VERBOSE is non-zero and a positive integer number, verbosity of the term
  57. rewriting system will be increased. This will output debug messages and a
  58. higher value will print more types of debug messages.
  59. """
  60. parser = ParserWrapper(base_class, **kwargs)
  61. for exp, out in expressions:
  62. res = None
  63. try:
  64. res = parser.run([exp])
  65. assert res == out
  66. except: # pragma: nocover
  67. if not silent:
  68. print >>sys.stderr, 'error: %s gives %s, but expected: %s' \
  69. % (exp, str(res), str(out))
  70. if not silent and hasattr(res, 'nodes'):
  71. print >>sys.stderr, 'result graph:'
  72. print >>sys.stderr, create_graph(res)
  73. print >>sys.stderr, 'expected graph:'
  74. print >>sys.stderr, create_graph(out)
  75. if fail:
  76. raise
  77. def apply_expressions(base_class, expressions, fail=True, silent=False,
  78. **kwargs):
  79. parser = ParserWrapper(base_class, **kwargs)
  80. for exp, times, out in expressions:
  81. res = None
  82. try:
  83. res = parser.run([exp] + list('@' * times))
  84. assert res == out
  85. except: # pragma: nocover
  86. if not silent:
  87. print >>sys.stderr, 'error: %s gives %s, but expected: %s' \
  88. % (exp, str(res), str(out))
  89. if not silent and hasattr(res, 'nodes'):
  90. print >>sys.stderr, 'result graph:'
  91. print >>sys.stderr, create_graph(res)
  92. print >>sys.stderr, 'expected graph:'
  93. print >>sys.stderr, create_graph(out)
  94. if fail:
  95. raise
  96. def graph(parser, *exp, **kwargs):
  97. return create_graph(ParserWrapper(parser, **kwargs).run(exp))
  98. def line(parser, *exp, **kwargs):
  99. return generate_line(ParserWrapper(parser, **kwargs).run(exp))