public
Description: A Ruby-based parsing DSL based on parsing expression grammars.
Homepage: http://treetop.rubyforge.org
Clone URL: git://github.com/nathansobo/treetop.git
Search Repo:
Refactored registration of results into retain method on results that has 
refcounting
Nathan Sobo (author)
Fri May 02 10:46:01 -0700 2008
commit  c34aac5d33bfa927181b57c392c71ca9f5377880
tree    a61d0eb8a94adc82eace2079ffb35b877a074bdd
parent  2a0d504674aaa1a2250fbd6cf97ae4a6d231c66f
...
7
8
9
10
 
11
12
13
...
7
8
9
 
10
11
12
13
0
@@ -7,7 +7,7 @@ module Treetop
0
         use_vars :result, :start_index
0
         builder.assign "failed_alternatives", "[]"
0
         compile_alternatives(alternatives)
0
- builder << "#{result_var}.dependencies.concat(failed_alternatives)"
0
+ builder << "#{result_var}.add_dependencies(failed_alternatives)"
0
         end_comment(self)
0
       end
0
       
...
63
64
65
66
 
67
68
69
...
63
64
65
 
66
67
68
69
0
@@ -63,7 +63,7 @@ module Treetop
0
       end
0
       
0
       def accumulate_dependency(dependency)
0
- builder.accumulate "#{result_var}.dependencies", dependency
0
+ builder << "#{result_var}.add_dependencies([#{dependency}])"
0
       end
0
     
0
       def assign_result(value_ruby)
...
16
17
18
19
 
20
21
22
...
16
17
18
 
19
20
21
22
0
@@ -16,7 +16,7 @@ module Treetop
0
         builder.else_ do
0
           assign_failure start_index_var
0
           reset_index
0
- builder << "#{result_var}.dependencies.concat(#{accumulator_var})"
0
+ builder << "#{result_var}.add_dependencies(#{accumulator_var})"
0
         end
0
         end_comment(self)
0
       end
...
1
2
3
4
 
5
6
7
...
10
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
14
15
...
1
2
3
 
4
5
6
7
...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
0
@@ -1,7 +1,7 @@
0
 module Treetop
0
   module Runtime
0
     class ParseResult
0
- attr_reader :dependent_results, :dependencies, :child_results, :dependents, :memoizations
0
+ attr_reader :dependent_results, :dependencies, :child_results, :dependents, :memoizations, :refcount
0
       attr_accessor :interval, :parent, :registered, :result_cache
0
       alias :registered? :registered
0
       
0
@@ -10,6 +10,31 @@ module Treetop
0
         @dependencies = []
0
         @dependents = []
0
         @memoizations = []
0
+ @refcount = 0
0
+ end
0
+
0
+ def add_dependencies(new_dependencies)
0
+ new_dependencies.each do |dependency|
0
+ dependency.dependents.push(self)
0
+ end
0
+ dependencies.concat(new_dependencies)
0
+ end
0
+
0
+ def retain(result_cache)
0
+ if refcount == 0
0
+ result_cache.results.push(self)
0
+ @result_cache = result_cache
0
+ end
0
+
0
+ if child_results
0
+ child_results.each do |child_result|
0
+ child_result.retain(result_cache)
0
+ end
0
+ end
0
+ dependencies.each do |dependency|
0
+ dependency.retain(result_cache)
0
+ end
0
+ @refcount += 1
0
       end
0
 
0
       def expire(expire_parent=false)
...
10
11
12
13
 
14
15
16
...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
...
10
11
12
 
13
14
15
16
...
60
61
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
64
65
0
@@ -10,7 +10,7 @@ module Treetop
0
 
0
       def store_result(rule_name, result)
0
         result.memoizations.push(Memoization.new(rule_name, result, result_index))
0
- register_result(result)
0
+ result.retain(self)
0
       end
0
 
0
       def get_result(rule_name, start_index)
0
@@ -60,27 +60,6 @@ module Treetop
0
       end
0
 
0
       protected
0
-
0
- def register_result(result)
0
- return if result.registered?
0
- result.result_cache = self
0
-
0
- if result.child_results
0
- result.child_results.each do |child_result|
0
- child_result.parent = result
0
- register_result(child_result)
0
- end
0
- end
0
-
0
- result.dependencies.each do |subresult|
0
- subresult.dependents.push(result)
0
- register_result(subresult)
0
- end
0
-
0
- result.registered = true
0
- results.push(result)
0
- end
0
-
0
       attr_reader :result_index, :results_to_delete, :memoizations_to_expire
0
     end
0
   end
...
144
145
146
147
 
148
149
150
151
 
152
153
154
...
195
196
197
198
 
199
200
201
...
144
145
146
 
147
148
149
150
 
151
152
153
154
...
195
196
197
 
198
199
200
201
0
@@ -144,11 +144,11 @@ module ResultCacheSpec
0
         @child = SyntaxNode.new(input, 0...5)
0
         @epsilon_node = SyntaxNode.new(input, 5...5)
0
         @predication_result = SyntaxNode.new(input, 5..8)
0
- epsilon_node.dependencies.push(predication_result)
0
+ epsilon_node.add_dependencies([predication_result])
0
         @parent = SyntaxNode.new(input, 0...5, [child, epsilon_node])
0
         @non_local_dependency = TerminalParseFailure.new(5...15, 'x' * 10)
0
 
0
- parent.dependencies.push(non_local_dependency)
0
+ parent.add_dependencies([non_local_dependency])
0
         parent.dependencies.should == [non_local_dependency]
0
 
0
         cache.store_result(:foo, parent)
0
@@ -195,7 +195,7 @@ module ResultCacheSpec
0
       before do
0
         @grandchild = SyntaxNode.new(input, 3..5)
0
         @dependency = SyntaxNode.new(input, 5...8)
0
- grandchild.dependencies.push(dependency)
0
+ grandchild.add_dependencies([dependency])
0
         @child = SyntaxNode.new(input, 0..5, [SyntaxNode.new(input, 0...3), grandchild])
0
         @parent = SyntaxNode.new(input, 0..5, [child])
0
 

Comments

    No one has commented yet.