Example #1
0
 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')
Example #2
0
 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')
Example #3
0
 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')
Example #4
0
 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')