My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/offside/_example/offside.py
3a4faaa63b87 vs. 67579ff784f3 Compare: vs.  Format:
Revision 67579ff784f3
Go to: 
Project members, sign in to write a code review
/src/lepl/offside/_example/offside.py   3a4faaa63b87 /src/lepl/offside/_example/offside.py   67579ff784f3
1 1
2 # Copyright 2009 Andrew Cooke 2 # Copyright 2009 Andrew Cooke
3 3
4 # This file is part of LEPL. 4 # This file is part of LEPL.
5 # 5 #
6 # LEPL is free software: you can redistribute it and/or modify 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 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 8 # by the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version. 9 # (at your option) any later version.
10 # 10 #
11 # LEPL is distributed in the hope that it will be useful, 11 # LEPL is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Lesser General Public License for more details. 14 # GNU Lesser General Public License for more details.
15 # 15 #
16 # You should have received a copy of the GNU Lesser General Public License 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/>. 17 # along with LEPL. If not, see <http://www.gnu.org/licenses/>.
18 18
19 ''' 19 '''
20 Show how the BLine and Block tokens can be used 20 Show how the BLine and Block tokens can be used
21 ''' 21 '''
22 22
23 # pylint: disable-msg=W0401, W0614, W0621, C0103, C0111, R0201, C0301
24 #@PydevCodeAnalysisIgnore
25
23 26
24 from logging import basicConfig, DEBUG 27 from logging import basicConfig, DEBUG
25 28
26 from lepl import * 29 from lepl import *
27 from lepl._example.support import Example 30 from lepl._example.support import Example
28 31
29 32
30 class OffsideExample(Example): 33 class OffsideExample(Example):
31 34
32 def test_offside(self): 35 def test_offside(self):
33 basicConfig(level=DEBUG) 36 basicConfig(level=DEBUG)
34 introduce = ~Token(':') 37 introduce = ~Token(':')
35 word = Token(Word(Lower())) 38 word = Token(Word(Lower()))
36 scope = Delayed() 39 scope = Delayed()
37 line = (BLine(word[:] | Empty()) > list) | scope 40 line = (BLine(word[:] | Empty()) > list) | scope
38 scope += BLine(word[:] & introduce) & Block(line[:]) > list 41 scope += BLine(word[:] & introduce) & Block(line[:]) > list
39 parser = line[:].string_parser(OffsideConfiguration(policy=2)) 42 parser = line[:].string_parser(OffsideConfiguration(policy=2))
40 self.examples([(lambda: parser(''' 43 self.examples([(lambda: parser('''
41 abc def 44 abc def
42 ghijk: 45 ghijk:
43 mno pqr: 46 mno pqr:
44 stu 47 stuv
45 vwx yz 48 wx yz
46 '''), "[[], ['abc', 'def'], ['ghijk', ['mno', 'pqr', ['stu']], ['vwx', 'yz']]]")]) 49 '''), "[[], ['abc', 'def'], ['ghijk', ['mno', 'pqr', ['stuv']], ['wx', 'yz']]]")])
47 50
48 def test_offside2(self): 51 def test_offside2(self):
49 basicConfig(level=DEBUG) 52 basicConfig(level=DEBUG)
50 introduce = ~Token(':') 53 introduce = ~Token(':')
51 word = Token(Word(Lower())) 54 word = Token(Word(Lower()))
52 statement = Delayed() 55 statement = Delayed()
53 simple = BLine(word[:]) 56 simple = BLine(word[:])
54 empty = BLine(Empty()) 57 empty = BLine(Empty())
55 block = BLine(word[:] & introduce) & Block(statement[:]) 58 block = BLine(word[:] & introduce) & Block(statement[:])
56 statement += (simple | empty | block) > list 59 statement += (simple | empty | block) > list
57 parser = statement[:].string_parser(OffsideConfiguration(policy=2)) 60 parser = statement[:].string_parser(OffsideConfiguration(policy=2))
58 self.examples([(lambda: parser(''' 61 self.examples([(lambda: parser('''
59 abc def 62 abc def
60 ghijk: 63 ghijk:
61 mno pqr: 64 mno pqr:
62 stu 65 stuv
63 vwx yz 66 wx yz
64 '''), 67 '''),
65 "[[], ['abc', 'def'], ['ghijk', ['mno', 'pqr', ['stu']], ['vwx', 'yz']]]")]) 68 "[[], ['abc', 'def'], ['ghijk', ['mno', 'pqr', ['stuv']], ['wx', 'yz']]]")])
66 69
67 70
68 def test_pithon(self): 71 def test_pithon(self):
69 basicConfig(level=DEBUG) 72 basicConfig(level=DEBUG)
70 73
71 word = Token(Word(Lower())) 74 word = Token(Word(Lower()))
72 continuation = Token(r'\\') 75 continuation = Token(r'\\')
73 symbol = Token(Any('()')) 76 symbol = Token(Any('()'))
74 introduce = ~Token(':') 77 introduce = ~Token(':')
75 comma = ~Token(',') 78 comma = ~Token(',')
76 79
77 CLine = CLineFactory(continuation) 80 CLine = CLineFactory(continuation)
78 81
79 statement = Delayed() 82 statement = Delayed()
80 83
81 empty = Line(Empty()) 84 empty = Line(Empty())
82 simple = CLine(word[1:]) 85 simple = CLine(word[1:])
83 ifblock = CLine(word[1:] & introduce) & Block(statement[1:]) 86 ifblock = CLine(word[1:] & introduce) & Block(statement[1:])
84 87
85 args = Extend(word[:, comma]) > tuple 88 args = Extend(word[:, comma]) > tuple
86 fundef = word[1:] & ~symbol('(') & args & ~symbol(')') 89 fundef = word[1:] & ~symbol('(') & args & ~symbol(')')
87 function = CLine(fundef & introduce) & Block(statement[1:]) 90 function = CLine(fundef & introduce) & Block(statement[1:])
88 91
89 statement += (empty | simple | ifblock | function) > list 92 statement += (empty | simple | ifblock | function) > list
90 93
91 parser = statement[:].string_parser(OffsideConfiguration(policy=2)) 94 parser = statement[:].string_parser(OffsideConfiguration(policy=2))
92 95
93 self.examples([(lambda: parser(''' 96 self.examples([(lambda: parser('''
94 this is a grammar with a similar 97 this is a grammar with a similar
95 line structure to python 98 line structure to python
96 99
97 if something: 100 if something:
98 then we indent 101 then we indent
99 else: 102 else:
100 something else 103 something else
101 104
102 def function(a, b, c): 105 def function(a, b, c):
103 we can nest blocks: 106 we can nest blocks:
104 like this 107 like this
105 and we can also \ 108 and we can also \
106 have explicit continuations \ 109 have explicit continuations \
107 with \ 110 with \
108 any \ 111 any \
109 indentation 112 indentation
110 113
111 same for (argument, 114 same for (argument,
112 lists): 115 lists):
113 which do not need the 116 which do not need the
114 continuation marker 117 continuation marker
115 '''), 118 '''),
116 "[[], ['this', 'is', 'a', 'grammar', 'with', 'a', 'similar'], " 119 "[[], ['this', 'is', 'a', 'grammar', 'with', 'a', 'similar'], "
117 "['line', 'structure', 'to', 'python'], [], " 120 "['line', 'structure', 'to', 'python'], [], "
118 "['if', 'something', ['then', 'we', 'indent']], " 121 "['if', 'something', ['then', 'we', 'indent']], "
119 "['else', ['something', 'else'], []], " 122 "['else', ['something', 'else'], []], "
120 "['def', 'function', ('a', 'b', 'c'), " 123 "['def', 'function', ('a', 'b', 'c'), "
121 "['we', 'can', 'nest', 'blocks', ['like', 'this']], " 124 "['we', 'can', 'nest', 'blocks', ['like', 'this']], "
122 "['and', 'we', 'can', 'also', 'have', 'explicit', 'continuations', " 125 "['and', 'we', 'can', 'also', 'have', 'explicit', 'continuations', "
123 "'with', 'any', 'indentation'], []], " 126 "'with', 'any', 'indentation'], []], "
124 "['same', 'for', ('argument', 'lists'), " 127 "['same', 'for', ('argument', 'lists'), "
125 "['which', 'do', 'not', 'need', 'the'], " 128 "['which', 'do', 'not', 'need', 'the'], "
126 "['continuation', 'marker']]]")]) 129 "['continuation', 'marker']]]")])
127 130
Powered by Google Project Hosting