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

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

  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  An example that avoids using tokens with the line aware parsing (you'd have 
 32  to be crazy to want to do this, but it's possible). 
 33  ''' 
 34   
 35  #from logging import basicConfig, DEBUG 
 36  from unittest import TestCase 
 37   
 38  # pylint: disable-msg=W0614 
 39  from lepl import * 
 40   
 41   
42 -class TextTest(TestCase):
43
44 - def parser(self, regexp):
45 ''' 46 Construct a parser that uses "offside rule" parsing, but which 47 avoids using tokens in the grammar. 48 ''' 49 50 # we still need one token, which matches "all the text" 51 Text = Token(regexp) 52 53 def TLine(contents): 54 ''' 55 A version of Line() that takes text-based matchers. 56 ''' 57 return Line(Text(contents))
58 59 # from here on we use TLine instead of BLine and don't worry about 60 # tokens. 61 62 # first define the basic grammar 63 with Separator(~Space()[:]): 64 name = Word() 65 args = name[:, ','] 66 fundef = 'def' & name & '(' & args & ')' & ':' 67 # in reality we would have more expressions! 68 expression = Literal('pass') 69 70 # then define the block structure 71 statement = Delayed() 72 simple = TLine(expression) 73 empty = TLine(Empty()) 74 block = TLine(fundef) & Block(statement[:]) 75 statement += (simple | empty | block) > list 76 program = statement[:] 77 78 program.config.lines(block_policy=2) 79 return program.get_parse_string()
80
81 - def do_parse(self, parser):
82 return parser('''pass 83 def foo(): 84 pass 85 def bar(): 86 pass 87 ''')
88
89 - def test_plus(self):
90 parser = self.parser('[^\n]+') 91 result = self.do_parse(parser) 92 assert result == [['pass'], 93 ['def', 'foo', '(', ')', ':', 94 ['pass'], 95 ['def', 'bar', '(', ')', ':', 96 ['pass']]]], result
97
98 - def test_star(self):
99 ''' 100 I have no idea why this fails, but this test was here before I forgot 101 so I assume it is correct behaviour! 102 103 (I think matching the empty string for a token is probably not a good 104 idea) 105 ''' 106 #basicConfig(level=DEBUG) 107 parser = self.parser('[^\n]*') 108 try: 109 self.do_parse(parser) 110 assert False, 'Expected error' 111 # error changed in Lepl 5 112 # except RuntimeLexerError: 113 except FullFirstMatchException: 114 pass
115