def test_multiple_input(self): parser = ParserWrapper(Parser) parser.run(['1+2', '?', '3+4', '?']) possibilities = parser.parser.possibilities self.assertEqual('\n'.join([repr(pos) for pos in possibilities]), '<Possibility root="3 + 4" handler=add_numerics' \ ' args=(<Scope of "3 + 4">, 3, 4)>')
def post(self): try: last_line = get_last_line(self) if last_line: parser = ParserWrapper(Parser) response = parser.run([last_line]) response = parser.parser.give_hint() if response: self.write({'hint': str(response)}) return self.write({'hint': 'No further reduction is possible.'}) except Exception as e: self.write(format_exception(e))
def validate(a, b): """ Validate that a => b. """ parser = ParserWrapper(Parser) # Parse both expressions a = parser.run([a]) b = parser.run([b]) if a.equals(b): return VALIDATE_NOPROGRESS # Evaluate a and b, counting the number of steps # Optimization: if b is encountered while evaluating a, return parser.set_root_node(a) A = a a_steps = 0 for i in xrange(MAXIMUM_REWRITE_STEPS): obj = parser.rewrite() if not obj: break # If b is some reduction of a, it will be detected here if obj.equals(b): return VALIDATE_SUCCESS A = obj a_steps += 1 if not A: return VALIDATE_ERROR parser.set_root_node(b) B, b_steps = parser.rewrite_and_count_all() if not B: return VALIDATE_ERROR # Evaluations must be equal if not A.equals(B): return VALIDATE_FAILURE # If evaluation of b took more staps than evaluation of a, the step from a # to b was probably useless or even bad if b_steps >= a_steps: return VALIDATE_NOPROGRESS # Evaluations match and b is evaluated quicker than a => success return VALIDATE_SUCCESS
def post(self): try: last_line = get_last_line(self) if last_line: parser = ParserWrapper(Parser) response = parser.run([last_line]) if response: response = parser.rewrite(include_step=True, check_implicit=True) if response: hint, step = response self.write({'step': str(step), 'hint': str(hint)}) return self.write({'hint': 'No further reduction is possible.'}) except Exception as e: self.write(format_exception(e))
def post(self): try: last_line = get_last_line(self) if last_line: parser = ParserWrapper(Parser) response = parser.run([last_line]) if response: steps = parser.rewrite_all(include_steps=True) if steps: out = [] for h, s in steps: out.append(dict(hint=str(h), step=str(s))) self.write({'steps': out}) return self.write({'hint': 'No further reduction is possible.'}) except Exception as e: self.write(format_exception(e))
def test_multiple_runs(self): parser = ParserWrapper(Parser) parser.run(['1+2', '?']) possibilities = parser.parser.possibilities self.assertEqual('\n'.join([repr(pos) for pos in possibilities]), '<Possibility root="1 + 2" handler=add_numerics' \ ' args=(<Scope of "1 + 2">, 1, 2)>') # Remove previous possibilities after second run() call. parser.run(['', ' ']) possibilities = parser.parser.possibilities self.assertEqual(possibilities, None) # Overwrite previous possibilities with new ones parser.run(['3+4', '?']) possibilities = parser.parser.possibilities self.assertEqual('\n'.join([repr(pos) for pos in possibilities]), '<Possibility root="3 + 4" handler=add_numerics' \ ' args=(<Scope of "3 + 4">, 3, 4)>')
def test_reset_after_failure(self): parser = ParserWrapper(Parser) parser.run(['-(3a+6b)']) parser.parser.find_possibilities() possibilities1 = parser.parser.possibilities self.assertNotEqual(possibilities1, []) parser.run(['5+2*6']) parser.parser.find_possibilities() possibilities2 = parser.parser.possibilities self.assertNotEqual(possibilities2, []) self.assertNotEqual(possibilities1, possibilities2)
def rewrite(exp, **kwargs): wrapper = ParserWrapper(Parser, **kwargs) wrapper.run([exp]) return wrapper.parser.rewrite(check_implicit=False)
def test_constructor(self): node = Node('+', Leaf(1), Leaf(4)) self.assertEqual(ParserWrapper(Parser).run(['1 + 4']), node)
def test_constructor(self): assert ParserWrapper(Parser).run(['1+4']) \ == N('+', L(1), L(4))
def test_raise(self): self.assertRaises(RuntimeError, ParserWrapper(Parser).run, ['raise'])
def tree(exp, **kwargs): return ParserWrapper(Parser, **kwargs).run([exp])
def test_no_expression_error(self): self.assertRaises(RuntimeError, ParserWrapper(Parser).run, ['', '?'])