Beispiel #1
0
 def test_bad_rule_reference(self):
     """ Refer to a rule that doesn't exist. """
     p = ParserBase()
     p.new_rule('bad', 'notarule')
     with self.assertRaises(KeyError,
                            msg='non-existent rule did not raise error'):
         p.parse('any text')
Beispiel #2
0
 def test_recursion(self):
     """ Test the use of recursion to achieve repetition. """
     p = ParserBase()
     p.new_rule('repeat', '"hello" repeat | "hello"')
     # standard check on a single 'hello'
     self.check(p, *STANDARD)
     # check for repeated 'hello's
     check = 'hello' * 10
     self.check(p, check, check)
Beispiel #3
0
 def test_flatten(self):
     """ Test the flatten method. """
     p = ParserBase()
     string = 'aaaaaa'
     p.new_rule('as', '"a" as | "a"', main=True)
     token = p.parse(string)
     token.flatten()
     self.assertEqual(string,
                      ''.join(c.value() for c in token.children),
                      msg='flatten failed for simple example')
Beispiel #4
0
 def test_empty_string(self):
     """ Check that the parser understands empty strings. """
     p = ParserBase()
     p.new_rule('empty', '""')
     p.new_rule('hello', '"hello"')
     p.new_rule('combined', 'hello empty', main=True)
     # do the standard check
     self.check(p, *STANDARD)
     # check the empty case by itself
     p.main = 'empty'
     self.check(p, "", "")
Beispiel #5
0
 def test_branching_complex(self):
     """ A more complicated branching example. """
     p = ParserBase()
     p.new_rule('end', '"llo"')
     p.new_rule('branch', '"hellp" | "he" end', main=True)
     self.check(p, *STANDARD)
Beispiel #6
0
 def test_branching(self):
     """ Test a simple 'or' example. """
     p = ParserBase()
     p.new_rule('branch', '"null" | "hello"')
     self.check(p, *STANDARD)
Beispiel #7
0
 def test_long_sequence(self):
     """ Test a longer sequence, using literals and rules. """
     p = ParserBase()
     p.new_rule('ls', '"l"')
     p.new_rule('long', '"h" "e" ls ls "o"', main=True)
     self.check(p, *STANDARD)
Beispiel #8
0
 def test_rule_sequence(self):
     """ Test a rule that contains a sequence of tokens. """
     p = ParserBase()
     p.new_rule('hello', '"hel" "lo"')
     self.check(p, *STANDARD)
Beispiel #9
0
 def test_rule_reference(self):
     """ Test a custom rule that references another rule. """
     p = ParserBase()
     p.new_rule('literal', '"hello"')
     p.new_rule('reference', 'literal')
     self.check(p, *STANDARD)
Beispiel #10
0
 def test_literal(self):
     """ Test a custom rule that involves a literal. """
     p = ParserBase()
     p.new_rule('hello', '"hello"', main=True)
     self.check(p, *STANDARD)
Beispiel #11
0
 def test_rule_addition(self):
     """ Check that rules can be added to the rule dictionary. """
     p = ParserBase()
     p.new_rule('hello', '"hello"')
     # check that the p has the new rule
     self.assertEqual(list(p.rules.keys()), ['hello'])
Beispiel #12
0
 def test_backslash_escaping(self):
     """ Try escaping a backslash in a rule. """
     p = ParserBase()
     p.new_rule('escape', '"\\\\"')
     p.parse('\\')
Beispiel #13
0
 def test_quote_escaping(self):
     """ Try using an escaped double-quote in a rule. """
     p = ParserBase()
     p.new_rule('quote', r'"\""')
     p.parse('"')
Beispiel #14
0
 def test_pipe_escaping(self):
     """ Try using an escaped pipe in a rule. """
     p = ParserBase()
     p.new_rule(r'escaped', '"pipe: " "\|"')
     p.parse('pipe: |')