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
def test_continued_explicit(self): number = Token(Digit()) letter = Token(Letter()) block = Delayed() bline = ContinuedLineFactory(r'x') line = Or(bline(number), bline(letter) & block) > list block += Block(line[1:]) program = Trace(line[1:]) text = '''1 2 a 3 b 4 5 6 ''' program.config.lines(block_policy=explicit) parser = program.get_parse_string() result = parser(text) assert result == [['1'], ['2'], ['a', ['3'], ['b', ['4'], ['5']], ['6']]], result
def test_explicit(self): #basicConfig(level=DEBUG) number = Token(Digit()) letter = Token(Letter()) block = Delayed() line = Or(Line(number), Line(letter) & block) > list block += Block(line[1:]) program = Trace(line[1:]) text = '''1 2 a 3 b 4 5 6 ''' program.config.lines(block_policy=explicit) parser = program.get_parse_string() result = parser(text) assert result == [['1'], ['2'], ['a', ['3'], ['b', ['4'], ['5']], ['6']]], result
def simple_grammar(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(Line(number), Line(letter) & block) > list # and a block is simply a collection of lines, as above block += Block(line[1:]) program = Trace(line[1:]) program.config.lines(block_policy=1) return program
def test_next_line(self): #basicConfig(level=DEBUG) self.assert_direct('''abc d''', Trace(~SkipTo(Newline()) + 'd'), [['d']])