My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/lexer/lines/_example/line_aware.py
13a59b816b7c vs. d93e5d662c5d Compare: 13a59b816b7c vs. d93e5d662c5d  Format:
Revision d93e5d662c5d
Go to: 
Project members, sign in to write a code review
/src/lepl/lexer/offside/_example/line_aware.py   13a59b816b7c /src/lepl/lexer/lines/_example/line_aware.py   d93e5d662c5d
1 1
2 # The contents of this file are subject to the Mozilla Public License 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 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 4 # in compliance with the License. You may obtain a copy of the License
5 # at http://www.mozilla.org/MPL/ 5 # at http://www.mozilla.org/MPL/
6 # 6 #
7 # Software distributed under the License is distributed on an "AS IS" 7 # Software distributed under the License is distributed on an "AS IS"
8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 # the License for the specific language governing rights and 9 # the License for the specific language governing rights and
10 # limitations under the License. 10 # limitations under the License.
11 # 11 #
12 # The Original Code is LEPL (http://www.acooke.org/lepl) 12 # The Original Code is LEPL (http://www.acooke.org/lepl)
13 # The Initial Developer of the Original Code is Andrew Cooke. 13 # The Initial Developer of the Original Code is Andrew Cooke.
14 # Portions created by the Initial Developer are Copyright (C) 2009-2010 14 # Portions created by the Initial Developer are Copyright (C) 2009-2010
15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved.
16 # 16 #
17 # Alternatively, the contents of this file may be used under the terms 17 # Alternatively, the contents of this file may be used under the terms
18 # of the LGPL license (the GNU Lesser General Public License, 18 # of the LGPL license (the GNU Lesser General Public License,
19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions
20 # of the LGPL License are applicable instead of those above. 20 # of the LGPL License are applicable instead of those above.
21 # 21 #
22 # If you wish to allow use of your version of this file only under the 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 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 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 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 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 27 # above, a recipient may use your version of this file under either the
28 # MPL or the LGPL License. 28 # MPL or the LGPL License.
29 29
30 ''' 30 '''
31 Show how line aware parsing can be used. 31 Show how line aware parsing can be used.
32 ''' 32 '''
33 33
34 from unittest import TestCase 34 from unittest import TestCase
35 from logging import basicConfig, DEBUG 35 from logging import basicConfig, DEBUG
36 36
37 from lepl import * 37 from lepl import *
38 from lepl._example.support import Example 38 from lepl._example.support import Example
39 39
40 40
41 class LineAwareTest(TestCase): 41 class LineAwareTest(TestCase):
42 42
43 def test_explicit_start_end(self): 43 def test_explicit_start_end(self):
44 contents = Token(Any()[:,...]) > list 44 contents = Token(Any()[:,...]) > list
45 line = LineStart() & contents & LineEnd() 45 line = LineStart() & contents & LineEnd()
46 lines = line[:] 46 lines = line[:]
47 lines.config.offside() 47 lines.config.lines()
48 result = lines.parse('line one\nline two\nline three') 48 result = lines.parse('line one\nline two\nline three')
49 assert result == [['line one\n'], ['line two\n'], ['line three']], result 49 assert result == [['line one\n'], ['line two\n'], ['line three']], result
50 50
51 def test_line(self): 51 def test_line(self):
52 contents = Token(Any()[:,...]) > list 52 contents = Token(Any()[:,...]) > list
53 line = Line(contents) 53 line = Line(contents)
54 lines = line[:] 54 lines = line[:]
55 lines.config.offside() 55 lines.config.lines()
56 result = lines.parse('line one\nline two\nline three') 56 result = lines.parse('line one\nline two\nline three')
57 assert result == [['line one\n'], ['line two\n'], ['line three']], result 57 assert result == [['line one\n'], ['line two\n'], ['line three']], result
58 58
59 def test_continued_line(self): 59 def test_continued_line(self):
60 contents = Token('[a-z]+')[:] > list 60 contents = Token('[a-z]+')[:] > list
61 CLine = ContinuedLineFactory(r'\\') 61 CLine = ContinuedLineFactory(r'\\')
62 line = CLine(contents) 62 line = CLine(contents)
63 lines = line[:] 63 lines = line[:]
64 lines.config.offside() 64 lines.config.lines()
65 result = lines.parse('line one \\\nline two\nline three') 65 result = lines.parse('line one \\\nline two\nline three')
66 assert result == [['line', 'one', 'line', 'two'], ['line', 'three']], result 66 assert result == [['line', 'one', 'line', 'two'], ['line', 'three']], result
67 67
68 def test_extend(self): 68 def test_extend(self):
69 #basicConfig(level=DEBUG) 69 #basicConfig(level=DEBUG)
70 contents = Token('[a-z]+')[:] > list 70 contents = Token('[a-z]+')[:] > list
71 parens = Token('\(') & contents & Token('\)') > list 71 parens = Token('\(') & contents & Token('\)') > list
72 line = Line(contents & Optional(Extend(parens))) 72 line = Line(contents & Optional(Extend(parens)))
73 lines = line[:] 73 lines = line[:]
74 lines.config.offside() 74 lines.config.lines()
75 result = lines.parse('line one (this\n extends to line two)\nline three') 75 result = lines.parse('line one (this\n extends to line two)\nline three')
76 assert result == [['line', 'one'], ['(', ['this', 'extends', 'to', 'line', 'two'], ')'], ['line', 'three']], result 76 assert result == [['line', 'one'], ['(', ['this', 'extends', 'to', 'line', 'two'], ')'], ['line', 'three']], result
77 77
78 def test_extend_deepest(self): 78 def test_extend_deepest(self):
79 ''' 79 '''
80 This returned None. 80 This returned None.
81 ''' 81 '''
82 #basicConfig(level=DEBUG) 82 #basicConfig(level=DEBUG)
83 contents = Token('[a-z]+')[:] > list 83 contents = Token('[a-z]+')[:] > list
84 parens = Token('\(') & contents & Token('\)') > list 84 parens = Token('\(') & contents & Token('\)') > list
85 line = Line(contents & Optional(Extend(parens))) 85 line = Line(contents & Optional(Extend(parens)))
86 lines = line[:] 86 lines = line[:]
87 lines.config.offside().record_deepest() 87 lines.config.lines().record_deepest()
88 result = lines.parse('line one (this\n extends to line two)\nline three') 88 result = lines.parse('line one (this\n extends to line two)\nline three')
89 assert result == [['line', 'one'], ['(', ['this', 'extends', 'to', 'line', 'two'], ')'], ['line', 'three']], result 89 assert result == [['line', 'one'], ['(', ['this', 'extends', 'to', 'line', 'two'], ')'], ['line', 'three']], result
90 90
Powered by Google Project Hosting