def test_rule_from_function(self): """ Check the operation of custom rules. """ def parse_hello(string): """ A rule that grabs the literal 'hello'. """ w = STANDARD[0] if string.startswith(w): return w, string[len(w):] return None, string p = ParserBase() p.from_function(parse_hello, main=True) self.check(p, *STANDARD)
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')