My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/lexer/blocks/_test/left_bug.py
249af3234ee4 vs. 1bc735ea4745 Compare: vs.  Format:
Revision 1bc735ea4745
Go to: 
Project members, sign in to write a code review
/src/lepl/lexer/blocks/_test/left_bug.py   249af3234ee4 /src/lepl/lexer/blocks/_test/left_bug.py   1bc735ea4745
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 See http://groups.google.com/group/lepl/browse_thread/thread/79e39e03a03718cc?hl=en_US 31 See http://groups.google.com/group/lepl/browse_thread/thread/79e39e03a03718cc?hl=en_US
32 32
33 The different tree structures seen here seem to be related to the how the 33 The different tree structures seen here seem to be related to the how the
34 left-recursive memoisation fails. In the case without lexer the string is 34 left-recursive memoisation fails. In the case without lexer the string is
35 shorter, which causes failure earlier. I am not at all sure about this... 35 shorter, which causes failure earlier. I am not at all sure about this...
36 ''' 36 '''
37 37
38 from unittest import TestCase 38 from unittest import TestCase
39 from logging import basicConfig, DEBUG 39 from logging import basicConfig, DEBUG
40 40
41 from lepl import * 41 from lepl import *
42 from lepl._test.base import assert_str 42 from lepl._test.base import assert_str
43 43
44 44
45 class LeftBugTest(TestCase): 45 class LeftBugTest(TestCase):
46 46
47 def test_right_no_lexer(self): 47 def test_right_no_lexer(self):
48 #basicConfig(level=DEBUG) 48 #basicConfig(level=DEBUG)
49 word = Any() 49 word = Any()
50 expr1 = Delayed() 50 expr1 = Delayed()
51 call = (expr1 & word) > List 51 call = (expr1 & word) > List
52 expr1 += (call | Empty() | word) 52 expr1 += (call | Empty() | word)
53 program = expr1 & Eos() 53 program = expr1 & Eos()
54 #program.config.trace_stack() 54 program.config.trace_stack().auto_memoize()
55 parser = program.get_parse() 55 parser = program.get_parse()
56 print(parser.matcher.tree()) 56 print(parser.matcher.tree())
57 parsed = parser("abc") 57 parsed = parser("abc")
58 assert_str(parsed[0], 58 assert_str(parsed[0],
59 """List 59 """List
60 +- List 60 +- List
61 | +- 'a' 61 | +- 'a'
62 | `- 'b' 62 | `- 'b'
63 `- 'c'""") 63 `- 'c'""")
64 64
65 def test_right(self): 65 def test_right(self):
66 #basicConfig(level=DEBUG) 66 #basicConfig(level=DEBUG)
67 #CLine = ContinuedBLineFactory(Token(r'\\')) 67 #CLine = ContinuedBLineFactory(Token(r'\\'))
68 word = Token("[A-Za-z_][A-Za-z0-9_]*") 68 word = Token("[A-Za-z_][A-Za-z0-9_]*")
69 expr1 = Delayed() 69 expr1 = Delayed()
70 call = (expr1 & word) > List 70 call = (expr1 & word) > List
71 expr1 += (call | Empty() | word) 71 expr1 += (call | Empty() | word)
72 program = Trace(expr1 & Eos()) 72 program = Trace(expr1 & Eos())
73 program.config.trace_stack() 73 program.config.trace_stack().auto_memoize()
74 parser = program.get_parse() 74 parser = program.get_parse()
75 #print(parser.matcher.tree()) 75 #print(parser.matcher.tree())
76 parsed = parser("a b c") 76 parsed = parser("a b c")
77 assert_str(parsed[0], 77 assert_str(parsed[0],
78 """List 78 """List
79 +- List 79 +- List
80 | +- List 80 | +- 'a'
81 | | `- 'a'
82 | `- 'b' 81 | `- 'b'
83 `- 'c'""") 82 `- 'c'""")
84 83
85 def test_left(self): 84 def test_left(self):
86 CLine = ContinuedBLineFactory(r'\\') 85 CLine = ContinuedBLineFactory(r'\\')
87 expr0 = Token("[A-Za-z_][A-Za-z0-9_]*") 86 expr0 = Token("[A-Za-z_][A-Za-z0-9_]*")
88 expr1 = Delayed() 87 expr1 = Delayed()
89 call = (expr1 & expr0) > List # Deliberately not expr0 & expr1 88 call = (expr1 & expr0) > List # Deliberately not expr0 & expr1
90 expr1 += (call | Empty () | expr0) 89 expr1 += (call | Empty () | expr0)
91 program = (CLine(expr1) & Eos()) 90 program = (CLine(expr1) & Eos())
92 program.config.blocks(block_policy=rightmost) 91 program.config.blocks(block_policy=rightmost).auto_memoize()
93 parsed = program.parse("a b c") 92 parsed = program.parse("a b c")
94 assert_str(parsed[0], 93 assert_str(parsed[0],
95 """List 94 """List
96 +- List 95 +- List
97 | +- List 96 | +- 'a'
98 | | `- 'a'
99 | `- 'b' 97 | `- 'b'
100 `- 'c'""") 98 `- 'c'""")
101 99
Powered by Google Project Hosting