My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/_test/wrong_cache_bug.py
1bc735ea4745 vs. d0c0c1b67cfa Compare: vs.  Format:
Revision d0c0c1b67cfa
Go to: 
Project members, sign in to write a code review
/src/lepl/_test/wrong_cache_bug.py   1bc735ea4745 /src/lepl/_test/wrong_cache_bug.py   d0c0c1b67cfa
1 from lepl.core.rewriters import LeftMemoize
2 1
3 # 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
4 # (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
5 # 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
6 # at http://www.mozilla.org/MPL/ 5 # at http://www.mozilla.org/MPL/
7 # 6 #
8 # Software distributed under the License is distributed on an "AS IS" 7 # Software distributed under the License is distributed on an "AS IS"
9 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
10 # the License for the specific language governing rights and 9 # the License for the specific language governing rights and
11 # limitations under the License. 10 # limitations under the License.
12 # 11 #
13 # The Original Code is LEPL (http://www.acooke.org/lepl) 12 # The Original Code is LEPL (http://www.acooke.org/lepl)
14 # The Initial Developer of the Original Code is Andrew Cooke. 13 # The Initial Developer of the Original Code is Andrew Cooke.
15 # Portions created by the Initial Developer are Copyright (C) 2009-2010 14 # Portions created by the Initial Developer are Copyright (C) 2009-2010
16 # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved.
17 # 16 #
18 # 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
19 # of the LGPL license (the GNU Lesser General Public License, 18 # of the LGPL license (the GNU Lesser General Public License,
20 # 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
21 # of the LGPL License are applicable instead of those above. 20 # of the LGPL License are applicable instead of those above.
22 # 21 #
23 # 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
24 # 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
25 # 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
26 # provisions above and replace them with the notice and other provisions 25 # provisions above and replace them with the notice and other provisions
27 # 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
28 # 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
29 # MPL or the LGPL License. 28 # MPL or the LGPL License.
30 29
31 ''' 30 '''
32 Example returning less results than before. 31 Example returning less results than before.
33 ''' 32 '''
34 33
35 from logging import basicConfig, DEBUG 34 from logging import basicConfig, DEBUG
36 from unittest import TestCase 35 from unittest import TestCase
37 36
38 from lepl import * 37 from lepl import *
39 38
40 class CacheTest(TestCase): 39 class CacheTest(TestCase):
41 40
42 def test_cache(self): 41 def test_cache(self):
43 #basicConfig(level=DEBUG) 42 #basicConfig(level=DEBUG)
44 43
45 with TraceVariables(): 44 with TraceVariables():
46 value = Token(UnsignedReal()) 45 value = Token(UnsignedReal())
47 symbol = Token('[^0-9a-zA-Z \t\r\n]') 46 symbol = Token('[^0-9a-zA-Z \t\r\n]')
48 number = (Optional(symbol('-')) + value) >> float 47 number = (Optional(symbol('-')) + value) >> float
49 group2, group3c = Delayed(), Delayed() 48 group2, group3c = Delayed(), Delayed()
50 49
51 parens = symbol('(') & group3c & symbol(')') 50 parens = symbol('(') & group3c & symbol(')')
52 group1 = parens | number 51 group1 = parens | number
53 52
54 mul = (group2 & symbol('*') & group2) > List # changed 53 mul = (group2 & symbol('*') & group2) > List # changed
55 div = (group2 & symbol('/') & group2) > List # changed 54 div = (group2 & symbol('/') & group2) > List # changed
56 group2 += (mul | div | group1) 55 group2 += (mul | div | group1)
57 56
58 add = (group3c & symbol('+') & group3c) > List # changed 57 add = (group3c & symbol('+') & group3c) > List # changed
59 sub = (group3c & symbol('-') & group3c) > List # changed 58 sub = (group3c & symbol('-') & group3c) > List # changed
60 group3c += (add | sub | group2) 59 group3c += (add | sub | group2)
61 60
62 group3c.config.clear().lexer().auto_memoize().trace_variables() 61 group3c.config.clear().lexer().auto_memoize().trace_variables()
63 p = group3c.get_parse_all() 62 p = group3c.get_parse_all()
64 print(p.matcher.tree()) 63 #print(p.matcher.tree())
65 results = list(p('1+2*(3-4)+5/6+7')) 64 results = list(p('1+2*(3-4)+5/6+7'))
66 for result in results: 65 for result in results:
67 print(result[0]) 66 #print(result[0])
67 pass
68 assert len(results) == 12, results 68 assert len(results) == 12, results
69 69
70 def test_left(self): 70 def test_left(self):
71 #basicConfig(level=DEBUG) 71 #basicConfig(level=DEBUG)
72 a = Delayed() 72 a = Delayed()
73 a += Optional(a) & (a | 'b' | 'c')
74 for (conservative, full, d, n) in [(None, True, 0, 104),
75 (None, True, 1, 38),
76 (False, False, 1, 38)]:
77 a.config.clear().no_full_first_match().auto_memoize(
78 conservative=conservative, full=full, d=d)
79 p = a.get_parse_all()
80 #print(p.matcher.tree())
81 r = list(p('bcb'))
82 assert len(r) == n, (n, len(r), r)
83
84 def test_trace_variables(self):
85 # for comparison
86 a = Delayed()
73 a += Optional(a) & (a | 'b' | 'c') 87 a += Optional(a) & (a | 'b' | 'c')
74 a.config.no_full_first_match().auto_memoize() 88 a.config.no_full_first_match().auto_memoize()
75 p = a.get_parse_all() 89 p = a.get_parse_all()
76 #print(p.matcher.tree()) 90 #print(p.matcher.tree())
91
92 with TraceVariables():
93 a = Delayed()
94 a += Optional(a) & (a | 'b' | 'c')
95 a.config.no_full_first_match().auto_memoize().trace_variables()
96 p = a.get_parse_all()
97 #print(p.matcher.tree())
77 r = list(p('bcb')) 98 r = list(p('bcb'))
78 assert r == [['b', 'c', 'b'], ['b', 'c', 'b'], ['b', 'c', 'b'], ['b', 'c', 'b'], ['b', 'c', 'b'], ['b', 'c', 'b'], ['b', 'c'], ['b', 'c'], ['b']], r 99 assert len(r) == 104, (len(r), r)
79
80 100
Powered by Google Project Hosting