Ejemplo n.º 1
0
    def test_bline(self):
        '''
        Test a simple example: letters introduce numbers in an indented block.
        '''
        #basicConfig(level=DEBUG)

        number = Token(Digit())
        letter = Token(Letter())

        # the simplest whitespace grammar i can think of - lines are either
        # numbers (which are single, simple statements) or letters (which
        # mark the start of a new, indented block).
        block = Delayed()
        line = Or(BLine(number), BLine(letter) & block) > list
        # and a block is simply a collection of lines, as above
        block += Block(line[1:])

        program = Trace(line[1:])

        text = '''1
2
a
 3
 b
  4
  5
 6
'''
        program.config.default_line_aware(block_policy=1)
        parser = program.get_parse_string()
        result = parser(text)
        assert result == [['1'], ['2'],
                          ['a', ['3'], ['b', ['4'], ['5']], ['6']]], result
Ejemplo n.º 2
0
 def test_invert_bug_4(self):
     #basicConfig(level=DEBUG)
     bad = BLine(Token('[^a]*'))
     bad.config.line_aware(block_policy=2).left_memoize()
     parser = bad.get_parse_string()
     result = parser('123')
     assert result == ['123'], result
Ejemplo n.º 3
0
 def test_invert_bug_4(self):
     #basicConfig(level=DEBUG)
     bad = BLine(Token('[^a]*'))
     bad.config.line_aware(block_policy=2).left_memoize()
     parser = bad.get_parse_string()
     result = parser('123')
     assert result == ['123'], result
Ejemplo n.º 4
0
 def test_line(self):
     #basicConfig(level=DEBUG)
     text = Token('[^\n\r]+')
     quoted = Regexp("'[^']'")
     line = BLine(text(quoted))
     line.config.default_line_aware(block_start=0)
     parser = line.get_parse_string()
     assert parser("'a'") == ["'a'"]
Ejemplo n.º 5
0
 def test_invert_bug_6(self):
     #basicConfig(level=DEBUG)
     bad = BLine(Token(str('[^(*SOL)(*EOL)a]*')))
     bad.config.default_line_aware(block_policy=2,
                                   parser_factory=make_str_parser)
     bad.config.trace(True)
     parser = bad.get_parse_string() 
     result = parser(str('123'))
     assert result == [str('123')], result
Ejemplo n.º 6
0
 def test_invert_bug_6(self):
     #basicConfig(level=DEBUG)
     bad = BLine(Token(str('[^(*SOL)(*EOL)a]*')))
     bad.config.default_line_aware(block_policy=2,
                                   parser_factory=make_str_parser)
     bad.config.trace(True)
     parser = bad.get_parse_string()
     result = parser(str('123'))
     assert result == [str('123')], result
Ejemplo n.º 7
0
 def test_offset(self):
     #basicConfig(level=DEBUG)
     text = Token('[^\n\r]+')
     line = BLine(text(~Literal('aa') & Regexp('.*')))
     line.config.default_line_aware(block_start=0)
     parser = line.get_parse_string()
     assert parser('aabc') == ['bc']
     # what happens with an empty match?
     check = ~Literal('aa') & Regexp('.*')
     check.config.no_full_first_match()
     assert check.parse('aa') == ['']
     assert parser('aa') == ['']
Ejemplo n.º 8
0
 def test_bad_config(self):
     #basicConfig(level=DEBUG)
     text = Token('[^\n\r]+')
     quoted = Regexp("'[^']'")
     line = BLine(text(quoted))
     line.config.default_line_aware()
     parser = line.get_parse_string()
     try:
         parser("'a'")
         assert False, 'Expected error'
     except OffsideError as error:
         assert str(error).startswith('No initial indentation has been set.')