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 test_line(self): #basicConfig(level=DEBUG) text = Token('[^\n\r]+') quoted = Regexp("'[^']'") line = Line(text(quoted)) line.config.lines(block_start=0) parser = line.get_parse_string() assert parser("'a'") == ["'a'"]
def test_offset(self): #basicConfig(level=DEBUG) text = Token('[^\n\r]+') line = Line(text(~Literal('aa') & Regexp('.*'))) line.config.lines(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') == ['']
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