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')
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')
def test_ignore(self): """ Test the 'ignore' whitespace handling technique. """ # set up parser with new grammar and the digit_run function p = ParserBase(ws_handler=ignore) p.from_function(digit_run, ws_handling=True) p.grammar(GRAMMAR, main='programme') # trial various combinations of whitespace # single spacing p.parse('if 34 > 33 then 44 + 3') # no spacing p.parse('if34>33then44+3') # random spacing, including a tab p.parse('if34 > 33 then 44+\t3')
def test_require_with_ignore(self): """ Test the 'require' whitespace handling technique, using the 'ignore' option. """ # set up parser with new grammar and the digit_run function p = ParserBase(ws_handler=require(' ', ignore=True)) p.from_function(digit_run, ws_handling=True) p.grammar(GRAMMAR, main='programme') # trial various combinations of whitespace # single spacing - should work p.parse('if 34 > 33 then 44 + 3') # double spacing - should now work p.parse('if 34 > 33 then 44 + 3') # no spacing - should fail with self.assertRaises(DelimiterError): p.parse('if34>33then44+3')
def test_ignore_specific(self): """ Test the 'ignore_specific' whitespace handling technique. """ # set up parser with new grammar and the digit_run function p = ParserBase(ws_handler=ignore_specific(' ')) p.from_function(digit_run, ws_handling=True) p.grammar(GRAMMAR, main='programme') # trial various combinations of whitespace # single spacing p.parse('if 34 > 33 then 44 + 3') # no spacing p.parse('if34>33then44+3') # random spacing, including a tab - should fail with self.assertRaises(NotFoundError, msg='should ignore tab'): p.parse('if34 > 33 then 44+\t3')
def test_backslash_escaping(self): """ Try escaping a backslash in a rule. """ p = ParserBase() p.new_rule('escape', '"\\\\"') p.parse('\\')
def test_quote_escaping(self): """ Try using an escaped double-quote in a rule. """ p = ParserBase() p.new_rule('quote', r'"\""') p.parse('"')
def test_pipe_escaping(self): """ Try using an escaped pipe in a rule. """ p = ParserBase() p.new_rule(r'escaped', '"pipe: " "\|"') p.parse('pipe: |')
def test_grammar(self): """ Use the grammar method, also providing an opportunity to check a more complex multi-rule example. """ p = ParserBase() p.grammar(GRAMMAR) # these should all succeed p.parse('https://www.abcabc.com') p.parse('https://www.bcaabc.co.uk') p.parse('http://www.a.fr') # these should all fail with self.assertRaises(NotFoundError): p.parse('www.abc.com') with self.assertRaises(NotFoundError): p.parse('https:/www.abcabc.com') with self.assertRaises(NotFoundError): p.parse('https://www.abcd.co.uk') with self.assertRaises(NotFoundError): p.parse('http://www..com') with self.assertRaises(IncompleteParseError): p.parse('https://www.a.fra')