Package lepl :: Package lexer :: Package lines :: Package _test :: Module pithon
[hide private]
[frames] | no frames]

Source Code for Module lepl.lexer.lines._test.pithon

  1  from lepl.matchers.variables import TraceVariables 
  2   
  3  # The contents of this file are subject to the Mozilla Public License 
  4  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  5  # in compliance with the License. You may obtain a copy of the License 
  6  # at http://www.mozilla.org/MPL/ 
  7  # 
  8  # Software distributed under the License is distributed on an "AS IS" 
  9  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 10  # the License for the specific language governing rights and 
 11  # limitations under the License. 
 12  # 
 13  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 14  # The Initial Developer of the Original Code is Andrew Cooke. 
 15  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 16  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 17  # 
 18  # Alternatively, the contents of this file may be used under the terms 
 19  # of the LGPL license (the GNU Lesser General Public License, 
 20  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 21  # of the LGPL License are applicable instead of those above. 
 22  # 
 23  # If you wish to allow use of your version of this file only under the 
 24  # terms of the LGPL License and not to allow others to use your version 
 25  # of this file under the MPL, indicate your decision by deleting the 
 26  # provisions above and replace them with the notice and other provisions 
 27  # required by the LGPL License.  If you do not delete the provisions 
 28  # above, a recipient may use your version of this file under either the 
 29  # MPL or the LGPL License. 
 30   
 31  ''' 
 32  Test a Python-like grammar. 
 33  ''' 
 34   
 35  # pylint: disable-msg=W0614, W0401, W0621, C0103, C0111, R0201, R0904 
 36  #@PydevCodeAnalysisIgnore 
 37   
 38  from logging import basicConfig, DEBUG 
 39  from unittest import TestCase 
 40   
 41  from lepl import * 
42 43 44 -class PithonTest(TestCase):
45 46 @property
47 - def parser(self):
48 49 word = Token(Word(Lower())) 50 continuation = Token(r'\\') 51 symbol = Token(Any('()')) 52 introduce = ~Token(':') 53 comma = ~Token(',') 54 55 CLine = ContinuedLineFactory(continuation) 56 57 statement = word[1:] 58 args = Extend(word[:, comma]) > tuple 59 function = word[1:] & ~symbol('(') & args & ~symbol(')') 60 61 block = Delayed() 62 blank = ~Line(Empty(), indent=False) 63 comment = ~Line(Token('#.*'), indent=False) 64 line = (CLine(statement) | block | blank | comment) > list 65 block += CLine((function | statement) & introduce) & Block(line[1:]) 66 67 program = (line[:] & Eos()) 68 program.config.lines(block_policy=explicit).trace_stack(True) 69 # program.config.clear().blocks(block_policy=explicit) 70 return program.get_parse_string()
71
72 - def test_blocks(self):
73 #basicConfig(level=DEBUG) 74 program1 = ''' 75 kopo fjire ifejfr 76 ogptkr jgitr gtr 77 ffjireofr(kfkorp, freopk): 78 jifr fireo 79 frefre jifoji 80 jio frefre: 81 jiforejifre fiorej 82 jfore fire 83 jioj 84 jioj 85 jiojio 86 ''' 87 result = self.parser(program1) 88 assert result == [[], 89 ['kopo', 'fjire', 'ifejfr'], 90 ['ogptkr', 'jgitr', 'gtr'], 91 ['ffjireofr', ('kfkorp', 'freopk'), 92 ['jifr', 'fireo'], 93 ['frefre', 'jifoji'], 94 ['jio', 'frefre', 95 ['jiforejifre', 'fiorej'], 96 ['jfore', 'fire'], 97 ['jioj']], 98 ['jioj']], 99 ['jiojio']], result
100
101 - def test_no_lexer(self):
102 #basicConfig(level=DEBUG) 103 try: 104 self.parser('123') 105 assert False, 'expected exception' 106 except LexerError as error: 107 assert str(error) == 'No token for \'123\' at ' \ 108 'line 1, character 1 of \'123\'.', str(error)
109
110 - def test_extend(self):
111 #basicConfig(level=DEBUG) 112 result = self.parser(''' 113 def foo(abc, def, 114 ghi): 115 jgiog 116 ''') 117 assert result == [[], 118 ['def', 'foo', ('abc', 'def', 'ghi'), 119 ['jgiog']]], result
120
121 - def test_cline(self):
122 #basicConfig(level=DEBUG) 123 result = self.parser(''' 124 this is a single \ 125 line spread over \ 126 many actual \ 127 lines 128 and this is another 129 ''') 130 assert result == [[], 131 ['this', 'is', 'a', 'single', 'line', 'spread', 132 'over', 'many', 'actual', 'lines'], 133 ['and', 'this', 'is', 'another']], result
134
135 - def test_blanks(self):
136 #basicConfig(level=DEBUG) 137 result = self.parser(''' 138 def foo(): 139 a blank line can be 140 141 inside a block 142 143 or can separate blocks 144 ''') 145 assert result == [[], 146 ['def', 'foo', (), 147 ['a', 'blank', 'line', 'can', 'be'], 148 [], 149 ['inside', 'a', 'block'], 150 [] 151 ], 152 ['or', 'can', 'separate', 'blocks'] 153 ], result
154
155 - def test_comments(self):
156 #basicConfig(level=DEBUG) 157 result = self.parser(''' 158 # a comment here 159 def foo(): 160 # one here 161 contents 162 # wrong indentation! 163 more content''') 164 assert result == [[], [], 165 ['def', 'foo', (), 166 [], 167 ['contents'], 168 [], 169 ['more', 'content']]], result
170
171 - def test_all(self):
172 #basicConfig(level=DEBUG) 173 result = self.parser(''' 174 this is a grammar with a similar 175 line structure to python 176 177 # it supports comments 178 if something: 179 then we indent 180 else: 181 something else 182 183 def function(a, b, c): 184 we can nest blocks: 185 like this 186 and we can also \ 187 have explicit continuations \ 188 with \ 189 any \ 190 indentation 191 192 same for (argument, 193 lists): 194 which do not need the 195 continuation marker 196 # and we can have blank lines inside a block: 197 198 like this 199 ''') 200 assert result == \ 201 [ [], 202 ['this', 'is', 'a', 'grammar', 'with', 'a', 'similar'], 203 ['line', 'structure', 'to', 'python'], 204 [], 205 [], 206 ['if', 'something', 207 ['then', 'we', 'indent']], 208 ['else', 209 ['something', 'else'], 210 []], 211 ['def', 'function', ('a', 'b', 'c'), 212 ['we', 'can', 'nest', 'blocks', 213 ['like', 'this']], 214 ['and', 'we', 'can', 'also', 'have', 'explicit', 215 'continuations', 'with', 'any', 'indentation'], 216 []], 217 ['same', 'for', ('argument', 'lists'), 218 ['which', 'do', 'not', 'need', 'the'], 219 ['continuation', 'marker'], 220 [], 221 [], 222 ['like', 'this']]], result
223