Package lepl :: Package offside :: Package _test :: Module text
[hide private]
[frames] | no frames]

Source Code for Module lepl.offside._test.text

 1   
 2  # Copyright 2009 Andrew Cooke 
 3   
 4  # This file is part of LEPL. 
 5  #  
 6  #     LEPL is free software: you can redistribute it and/or modify 
 7  #     it under the terms of the GNU Lesser General Public License as published 
 8  #     by the Free Software Foundation, either version 3 of the License, or 
 9  #     (at your option) any later version. 
10  #  
11  #     LEPL is distributed in the hope that it will be useful, 
12  #     but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  #     GNU Lesser General Public License for more details. 
15  #  
16  #     You should have received a copy of the GNU Lesser General Public License 
17  #     along with LEPL.  If not, see <http://www.gnu.org/licenses/>. 
18   
19  ''' 
20  An example that avoids using tokens (in a sense) with the line aware 
21  parsing. 
22  ''' 
23   
24  #from logging import basicConfig, DEBUG 
25  from unittest import TestCase 
26   
27  from lepl import * 
28   
29   
30 -class TextTest(TestCase):
31
32 - def parser(self, regexp):
33 ''' 34 Construct a parser that uses "offside rule" parsing, but which 35 avoids using tokens in the grammar. 36 ''' 37 38 # we still need one token, which matches "all the text" 39 Text = Token(regexp) 40 41 def TLine(contents): 42 ''' 43 A version of BLine() that takes text-based matchers. 44 ''' 45 return BLine(Text(contents))
46 47 # from here on we use TLine instead of BLine and don't worry about 48 # tokens. 49 50 # first define the basic grammar 51 with Separator(~Space()[:]): 52 name = Word() 53 args = name[:, ','] 54 fundef = 'def' & name & '(' & args & ')' & ':' 55 # in reality we would have more expressions! 56 expression = Literal('pass') 57 58 # then define the block structure 59 statement = Delayed() 60 simple = TLine(expression) 61 empty = TLine(Empty()) 62 block = TLine(fundef) & Block(statement[:]) 63 statement += (simple | empty | block) > list 64 65 return statement[:].string_parser( 66 LineAwareConfiguration(block_policy=2))
67
68 - def do_parse(self, parser):
69 return parser('''pass 70 def foo(): 71 pass 72 def bar(): 73 pass 74 ''')
75
76 - def test_plus(self):
77 parser = self.parser('[^\n]+') 78 result = self.do_parse(parser) 79 assert result == [['pass'], 80 ['def', 'foo', '(', ')', ':', 81 ['pass'], 82 ['def', 'bar', '(', ')', ':', 83 ['pass']]]], result
84
85 - def test_star(self):
86 #basicConfig(level=DEBUG) 87 parser = self.parser('[^\n]*') 88 try: 89 self.do_parse(parser) 90 assert False, 'Expected error' 91 except RuntimeLexerError: 92 pass
93