def test_match_re_rules_with_failures(self): rule0 = easyparse.RegularExpressionRule(r'[a-z]') rule1 = easyparse.RegularExpressionRule(r'[0-9]') buf = easyparse.BufferedIterator('7a') buf.checkpoint() self.assertEqual(False, rule0.parse(buf)[0]) buf.rewind() self.assertEqual((True, '7'), rule1.parse(buf))
def test_print_backtrace_regex(self): rule = easyparse.RegularExpressionRule('[0-9]') ok, backtrace = rule.parse('q') self.assertFalse(ok) backtrace_string = self._print_backtrace_to_string(backtrace) exp_backtrace_string = \ """<Unnamed>: RegularExpression: Failed to match '[0-9]'\n""" self.assertEqual(exp_backtrace_string, backtrace_string)
def test_print_backtrace_named_regex(self): g = easyparse.Grammar() g.num = easyparse.RegularExpressionRule('[0-9]') ok, backtrace = g.num.parse('q') self.assertFalse(ok) backtrace_string = self._print_backtrace_to_string(backtrace) exp_backtrace_string = \ """num: RegularExpression: Failed to match '[0-9]'\n""" self.assertEqual(exp_backtrace_string, backtrace_string)
def test_re_match_with_same_whitespace_preceding(self): rule = easyparse.RegularExpressionRule(r' 0') self.assertEqual((True, ' 0'), rule.parse(' 0', allow_whitespace=True))
def test_match_two_re_rules(self): rule0 = easyparse.RegularExpressionRule(r'[0-9]') rule1 = easyparse.RegularExpressionRule(r'[a-z]') buf = easyparse.BufferedIterator('a7') self.assertEqual((True, 'a'), rule1.parse(buf)) self.assertEqual((True, '7'), rule0.parse(buf))
def test_ignore_regular_expression_rule(self): rule = easyparse.RegularExpressionRule(r'[0-9]', ignore_output=True) self.assertEqual((True, easyparse.Ignored), rule.parse('7'))
def test_match_regular_expression_rule(self): rule = easyparse.RegularExpressionRule(r'[0-9]') self.assertEqual((True, '7'), rule.parse('7'))
def test_re_rule_failure(self): rule = easyparse.RegularExpressionRule('a') ok, backtrace = rule.parse('b') self.assertFalse(ok) self.assertEqual(('', 'RegularExpression', 'a'), backtrace)