def test_trace(self): buffer = StringIO() with TraceVariables(out=buffer): word = ~Lookahead('OR') & Word() phrase = String() with DroppedSpace(): text = (phrase | word)[1:] > list query = text[:, Drop('OR')] expected = ''' phrase failed stream = 'spicy meatballs OR... word = ['spicy'] stream = ' meatballs OR "el ... phrase failed stream = 'meatballs OR "el b... word = ['meatballs'] stream = ' OR "el bulli rest... phrase failed stream = 'OR "el bulli resta... word failed stream = 'OR "el bulli resta... phrase failed stream = ' OR "el bulli rest... word failed stream = ' OR "el bulli rest... text = [['spicy', 'meatballs']] stream = ' OR "el bulli rest... phrase = ['el bulli restaurant'] stream = '' phrase failed stream = '' word failed stream = '' text = [['el bulli restaurant']] stream = '' query = [['spicy', 'meatballs'], ['el... stream = '' ''' query.config.auto_memoize(full=True) query.parse('spicy meatballs OR "el bulli restaurant"') trace = buffer.getvalue() assert trace == expected, '"""' + trace + '"""' # check caching works query.parse('spicy meatballs OR "el bulli restaurant"') trace = buffer.getvalue() assert trace == expected, '"""' + trace + '"""'
def test_indent(self): ''' Test simple matches against leading spaces. ''' #basicConfig(level=DEBUG) text = ''' left four''' word = Token(Word(Letter())) indent = LineStart() line1 = indent('') + LineEnd() line2 = indent('') & word('left') + LineEnd() line3 = indent(' ') & word('four') + LineEnd() expr = (line1 & line2 & line3) expr.config.lines(block_start=NO_BLOCKS) parser = expr.get_parse_string() result = parser(text) assert result == ['', '', 'left', ' ', 'four'], result
def test_indent(self): ''' Test simple matches against leading spaces. ''' #basicConfig(level=DEBUG) text = ''' left four''' word = Token(Word(Letter())) indent = Indent() line1 = indent('') + Eol() line2 = indent('') & word('left') + Eol() line3 = indent(' ') & word('four') + Eol() expr = (line1 & line2 & line3) expr.config.default_line_aware() parser = expr.get_parse_string() result = parser(text) assert result == ['', '', 'left', ' ', 'four'], result
def test_indent(self): ''' Test simple matches against leading spaces. ''' #basicConfig(level=DEBUG) text = ''' onespace \tspaceandtab''' word = Token(Word(Letter())) indent = LineStart() line1 = indent('') & ~LineEnd() line2 = indent(' ') & word('onespace') & ~LineEnd() line3 = indent(' ') & word('spaceandtab') & ~LineEnd() expr = line1 & line2 & line3 expr.config.lines(tabsize=4, block_start=NO_BLOCKS).trace_stack(True) parser = expr.get_parse_string() result = parser(text) #print(result) assert result == ['', ' ', 'onespace', ' ', 'spaceandtab'], result
def test_indent(self): ''' Test simple matches against leading spaces. ''' #basicConfig(level=DEBUG) text = ''' onespace \tspaceandtab''' word = Token(Word(Letter())) indent = Indent() line1 = indent('') & ~Eol() line2 = indent(' ') & word('onespace') & ~Eol() line3 = indent(' ') & word('spaceandtab') & ~Eol() expr = line1 & line2 & line3 expr.config.default_line_aware(tabsize=4).trace(True) parser = expr.get_parse_string() result = parser(text) #print(result) assert result == ['', ' ', 'onespace', ' ', 'spaceandtab'], result
def test_phone(self): self.assert_direct('andrew, 3333253', Word() / ',' / Integer() / Eof(), [['andrew', ',', ' ', '3333253']])
def test_bug(self): #basicConfig(level=DEBUG) t = Token(Word())(Any()[2] & Eos()) t.match("ab cd")