| Home | Trees | Indices | Help |
|---|
|
|
1 2 # The contents of this file are subject to the Mozilla Public License 3 # (MPL) Version 1.1 (the "License"); you may not use this file except 4 # in compliance with the License. You may obtain a copy of the License 5 # at http://www.mozilla.org/MPL/ 6 # 7 # Software distributed under the License is distributed on an "AS IS" 8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 9 # the License for the specific language governing rights and 10 # limitations under the License. 11 # 12 # The Original Code is LEPL (http://www.acooke.org/lepl) 13 # The Initial Developer of the Original Code is Andrew Cooke. 14 # Portions created by the Initial Developer are Copyright (C) 2009-2010 15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 16 # 17 # Alternatively, the contents of this file may be used under the terms 18 # of the LGPL license (the GNU Lesser General Public License, 19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 20 # of the LGPL License are applicable instead of those above. 21 # 22 # If you wish to allow use of your version of this file only under the 23 # terms of the LGPL License and not to allow others to use your version 24 # of this file under the MPL, indicate your decision by deleting the 25 # provisions above and replace them with the notice and other provisions 26 # required by the LGPL License. If you do not delete the provisions 27 # above, a recipient may use your version of this file under either the 28 # MPL or the LGPL License. 29 30 ''' 31 Tests for offside parsing. 32 ''' 33 34 from logging import basicConfig, DEBUG 35 from unittest import TestCase 36 37 from lepl.lexer.matchers import Token 38 from lepl.matchers.combine import Or 39 from lepl.matchers.core import Delayed 40 from lepl.matchers.derived import Letter, Digit 41 from lepl.matchers.monitor import Trace 42 from lepl.lexer.lines.matchers import Block, Line, explicit, \ 43 ContinuedLineFactory 44 45 46 # pylint: disable-msg=R0201 47 # unittest convention49 ''' 50 Test lines and blocks. 51 ''' 5218354 ''' 55 Test a simple example: letters introduce numbers in an indented block. 56 ''' 57 #basicConfig(level=DEBUG) 58 59 number = Token(Digit()) 60 letter = Token(Letter()) 61 62 # the simplest whitespace grammar i can think of - lines are either 63 # numbers (which are single, simple statements) or letters (which 64 # mark the start of a new, indented block). 65 block = Delayed() 66 line = Or(Line(number), 67 Line(letter) & block) > list 68 # and a block is simply a collection of lines, as above 69 block += Block(line[1:]) 70 71 program = Trace(line[1:]) 72 program.config.lines(block_policy=1) 73 return program7476 program = self.simple_grammar() 77 text = '''1''' 78 parser = program.get_parse_string() 79 result = parser(text) 80 assert result == [['1']], result8183 program = self.simple_grammar() 84 text = '''1 85 2 86 ''' 87 parser = program.get_parse_string() 88 result = parser(text) 89 assert result == [['1'], 90 ['2']], result9193 #basicConfig(level=DEBUG) 94 program = self.simple_grammar() 95 text = '''a 96 3 97 ''' 98 parser = program.get_parse_string() 99 result = parser(text) 100 assert result == [['a', ['3']]], result101103 program = self.simple_grammar() 104 text = '''1 105 2 106 a 107 3 108 b 109 4 110 5 111 6 112 ''' 113 parser = program.get_parse_string() 114 result = parser(text) 115 assert result == [['1'], 116 ['2'], 117 ['a', ['3'], 118 ['b', ['4'], 119 ['5']], 120 ['6']]], result121123 #basicConfig(level=DEBUG) 124 number = Token(Digit()) 125 letter = Token(Letter()) 126 127 block = Delayed() 128 line = Or(Line(number), 129 Line(letter) & block) > list 130 block += Block(line[1:]) 131 132 program = Trace(line[1:]) 133 134 text = '''1 135 2 136 a 137 3 138 b 139 4 140 5 141 6 142 ''' 143 program.config.lines(block_policy=explicit) 144 parser = program.get_parse_string() 145 result = parser(text) 146 assert result == [['1'], 147 ['2'], 148 ['a', ['3'], 149 ['b', ['4'], 150 ['5']], 151 ['6']]], result152154 number = Token(Digit()) 155 letter = Token(Letter()) 156 157 block = Delayed() 158 bline = ContinuedLineFactory(r'x') 159 line = Or(bline(number), 160 bline(letter) & block) > list 161 block += Block(line[1:]) 162 163 program = Trace(line[1:]) 164 165 text = '''1 166 2 167 a 168 3 169 b 170 4 171 5 172 6 173 ''' 174 program.config.lines(block_policy=explicit) 175 parser = program.get_parse_string() 176 result = parser(text) 177 assert result == [['1'], 178 ['2'], 179 ['a', ['3'], 180 ['b', ['4'], 181 ['5']], 182 ['6']]], result
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Sat Jun 9 21:51:02 2012 | http://epydoc.sourceforge.net |