Modified: trunk/jparsetree/lib/parse_tree.rb (696 => 697)
--- trunk/jparsetree/lib/parse_tree.rb 2007-08-22 15:26:53 UTC (rev 696)
+++ trunk/jparsetree/lib/parse_tree.rb 2007-08-23 01:09:51 UTC (rev 697)
@@ -42,7 +42,7 @@
def initialize(arg=false)
end
-VERSION = '1.0.0.693'
+VERSION = '1.0.0.694'
##
# Main driver for ParseTree. Returns an array of arrays containing
@@ -163,12 +163,10 @@
end
m = Kernel.const_get(name_sym).new.method(methodNameAsSymbol)
# m = klass.instance_method(methodNameAsSymbol)
- #puts "Method Type #{m.java_class}"
method_ast = m.mc
if method_ast.is_a?(Array) && method_ast.first == :alias
alias_method = true
m = Kernel.const_get(klass.name).new.method(method_ast[1].to_sym)
- #puts "Alias Method as: #{m.inspect}"
method_ast = m.mc
end
@@ -201,14 +199,17 @@
def location(obj, node)
obj.extend(JParseTree::SourceLocations)
- location = node.position()
- if location
- obj.source_location = (location.start_offset()..location.end_offset())
- else
- # I'm not sure if this is the best way... it's either this or
- # setting it to nil;
- obj.source_location = (-1..-1)
- abc = 1
+ if node
+ location = node.position()
+ if location
+ obj.source_location = (location.start_offset()..location.end_offset())
+ else
+ # I'm not sure if this is the best way... it's either this or
+ # setting it to nil;
+ obj.source_location = (-1..-1)
+ end
+ else
+ obj.source_location = (-1..-1)
end
obj
end
@@ -233,20 +234,17 @@
def visit(node)
@stackElements ||= []
node.accept(self.to_java_object) if node
- # p ">>>>>>> #{node}"
nil
end
def visitAliasNode(iVisited)
- #puts iVisited.to_s + " #{iVisited.old_name} #{iVisited.new_name}"
@stackElements << location([:alias,[:lit, iVisited.new_name.to_sym], [:lit, iVisited.old_name.to_sym]], iVisited)
nil
end
def visitAndNode(iVisited)
- #puts iVisited.to_s
visit(iVisited.first_node)
first = @stackElements.pop
visit(iVisited.second_node)
@@ -256,7 +254,6 @@
end
def visitArgsNode(iVisited)
-# iVisited.args.child_nodes.each {|n| puts n.name} unless iVisited.args == nil
tempArgs = [:args]
iVisited.args.child_nodes.each {|n| tempArgs << n.name.to_sym} unless iVisited.args == nil
@@ -301,7 +298,6 @@
def visitArgsCatNode(iVisited)
- #puts iVisited.to_s
visit(iVisited.first_node)
first = @stackElements.pop
visit(iVisited.second_node)
@@ -311,7 +307,6 @@
end
def visitArrayNode(iVisited)
- #puts iVisited.to_s
temp = [:array]
iVisited.child_nodes.each {|n|
visit(n)
@@ -355,7 +350,6 @@
end
def visitBackRefNode(iVisited)
-# puts iVisited.to_s + " " + iVisited.type
# HACK: the type is a char/Fixnum ASCII value, so take it and add it to a string
type = ""
type << iVisited.type
@@ -366,7 +360,6 @@
def visitBeginNode(iVisited)
- #puts iVisited.to_s
visit(iVisited.body_node)
bl = JParseTree.filter_nested_arrays(@stackElements.pop)
@stackElements << location([:begin, bl], iVisited)
@@ -375,22 +368,19 @@
def visitBignumNode(iVisited)
# TODO: fix Bigum
- #puts iVisited.to_s + " " + iVisited.value
+ #
+ nil
end
def visitBlockArgNode(iVisited)
- #p "Block"
-# puts iVisited.to_s + " " + iVisited.count
@stackElements << location([:block_arg, iVisited.name.to_sym], iVisited)
nil
end
def visitBlockNode(iVisited)
- # puts iVisited.to_s
tempStack = [:block]
# tempStack = []
- # p "1"
# tempStack = @stackElements.pop unless @stackElements.size < 1
# tempStack ||= []
iVisited.child_nodes.each {|n|
@@ -414,7 +404,6 @@
end
def visitBreakNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.value_node)
temp = @stackElements.pop
if temp == nil
@@ -427,14 +416,12 @@
end
def visitConstDeclNode(iVisited)
- # puts iVisited.to_s + " #{iVisited.name}"
visit(iVisited.value_node)
@stackElements << location([:cdecl, iVisited.name.to_sym,@stackElements.pop], iVisited)
nil
end
def visitClassVarAsgnNode(iVisited)
- # puts iVisited.to_s + " #{iVisited.name}"
visit(iVisited.value_node)
@stackElements << location([:cvasgn, iVisited.name.to_sym, @stackElements.pop], iVisited)
nil
@@ -447,13 +434,11 @@
end
def visitClassVarNode(iVisited)
- # puts iVisited.to_s + " @@#{iVisited.name}"
@stackElements << location([:cvar, iVisited.name.to_sym], iVisited)
nil
end
def visitCallNode(iVisited)
- # puts iVisited.to_s + " #{iVisited.name}"
visit(iVisited.receiver_node)
recv = JParseTree.filter_nested_arrays(@stackElements.pop)
@@ -470,43 +455,27 @@
tmp_elements = visitIterNode_fromCall(iVisited.iter_node, tmp_elements)
end
@stackElements << location(tmp_elements, iVisited)
- # @stackElements << [:call , recv, iVisited.name.to_sym]
nil
end
def visitCaseNode(iVisited)
- puts iVisited.to_s
# Note: this is the _expression_ that returns the value handled in the when expressions;
visit(iVisited.case_node)
node = @stackElements.pop
-
- @stackElements << []
-
-# NOTE: the child nodes, for some reason contain
- if iVisited.child_nodes.size > 1
- # HACK: for some reason iVisited.child_nodes[1..xxx] won't work
- # thus, I just filter out the 0th element, which - for some other odd
- # reason is just a repeat of the case _expression_;
- iVisited.child_nodes.each_with_index{|el, idx|
- if idx > 0
- el.accept(self.to_java_object)
- end
- }
- else
- if iVisited.child_nodes.size == 1
- iVisited.child_nodes[0].accept(self.to_java_object)
- end
+ if iVisited.child_nodes.size == 1
+ iVisited.child_nodes[0].accept(self.to_java_object)
+ else
+ iVisited.child_nodes[1].accept(self.to_java_object)
end
-
+
@stackElements << location([:case, node, *JParseTree.filter_nested_arrays(@stackElements.pop)], iVisited)
nil
end
def visitClassNode(iVisited)
- # puts iVisited.to_s
@sexpr ||= []
# @sexpr << :class
@@ -541,7 +510,6 @@
end
def visitColon2Node(iVisited)
- # puts iVisited.to_s + " #{iVisited.name}"
visit(iVisited.left_node) unless iVisited.left_node.nil?
if iVisited.left_node.nil?
@stackElements << location([:colon2, nil, iVisited.name.to_sym], iVisited)
@@ -552,12 +520,11 @@
end
def visitColon3Node(iVisited)
- # puts iVisited.to_s + " ::#{iVisited.name}"
@stackElements << location([:colon3, iVisited.name.to_sym], iVisited)
+ nil
end
def visitConstNode(iVisited)
- # puts iVisited.to_s + " #{iVisited.name}"
@stackElements << location([:const, iVisited.name.to_sym], iVisited)
nil
end
@@ -606,13 +573,11 @@
end
def visitDStrNode(iVisited)
- # puts iVisited.to_s
# TODO: bug in parsetree? it returns the first string as string literal, instead of [:str, ..]
- @stackElements << location([:dstr], iVisited)
+ retVal = location([:dstr], iVisited)
count = 1
iVisited.child_nodes.each {|n|
- # p "#{n}"
if !n.nil?
n.accept(self.to_java_object)
temp = @stackElements.pop
@@ -621,16 +586,21 @@
# HACK: ParseTree, for some reason, wants the first
# element of the :dstr to be a string literal, and
# not a [:str, val]
- @stackElements.last << temp[1]
+ [EMAIL PROTECTED] << temp[1]
+ retVal << temp[1]
else
- @stackElements.last << JParseTree.filter_nested_arrays(temp)
+ [EMAIL PROTECTED] << JParseTree.filter_nested_arrays(temp)
+ retVal << JParseTree.filter_nested_arrays(temp)
end
else
- @stackElements.last << JParseTree.filter_nested_arrays(temp)
+ [EMAIL PROTECTED] << JParseTree.filter_nested_arrays(temp)
+ retVal << JParseTree.filter_nested_arrays(temp)
end
count = count + 1
end
}
+
+ @stackElements << retVal
nil
end
@@ -663,7 +633,6 @@
end
def visitDVarNode(iVisited)
- # puts iVisited.to_s + " #{iVisited.name}"
# TODO: PROBLEM here?
@stackElements << location([:dvar, iVisited.name.to_sym], iVisited)
nil
@@ -675,7 +644,6 @@
def visitDXStrNode(iVisited)
- # puts iVisited.to_s
# TODO: bug in parsetree? it returns the first string as string literal, instead of [:str, ..]
@stackElements << location([:dxstr], iVisited)
@@ -703,20 +671,9 @@
nil
end
+
-# def visitDXStrNode(iVisited)
-# # puts iVisited.to_s
-## iVisited.child_nodes.each {|n| visit(n)}
-# iVisited.child_nodes.each {|n|
-# visit(n)
-# p "[EMAIL PROTECTED]"
-# }
-# end
-
-
-
def visitDefinedNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.expression_node)
@stackElements << location([:defined, @stackElements.pop], iVisited)
nil
@@ -724,12 +681,10 @@
def visitDefnNode(iVisited)
@sexpr ||= []
- # puts iVisited.to_s + " #{iVisited.name_node.name}"
args = nil
blockarg = nil
if iVisited.args_node
- puts iVisited.args_node.inspect
iVisited.args_node.accept(self.to_java_object)
args = @stackElements.pop
@@ -804,7 +759,6 @@
end
def visitDotNode(iVisited)
- # puts iVisited.to_s + " #{iVisited.exclusive ? '..' : '...'}"
visit(iVisited.begin_node)
beginNode = @stackElements.pop
visit(iVisited.end_node)
@@ -839,7 +793,6 @@
end
def visitEnsureNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.ensure_node)
ensureNode = @stackElements.pop
visit(iVisited.body_node)
@@ -848,13 +801,11 @@
end
def visitEvStrNode(iVisited)
-# puts "EvStr content: #{iVisited.body}"
visit(iVisited.body)
nil
end
def visitFCallNode(iVisited)
- # puts iVisited.to_s + " " + iVisited.name
visit(iVisited.args_node)
args = @stackElements.pop
if args == nil
@@ -870,7 +821,6 @@
end
def visitFalseNode(iVisited)
- # puts iVisited.to_s
@stackElements << [:false]
nil
end
@@ -878,12 +828,10 @@
def visitFixnumNode(iVisited)
@stackElements << location([:lit, iVisited.value],iVisited)
- # puts iVisited.to_s + " " + iVisited.value.to_s
nil
end
def visitFlipNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.begin_node)
begin_node = @stackElements.pop
visit(iVisited.end_node)
@@ -898,13 +846,11 @@
end
def visitFloatNode(iVisited)
- # puts iVisited.to_s + " " + iVisited.value.to_s
@stackElements << location([:lit, iVisited.value], iVisited)
nil
end
def visitForNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.var_node)
var = @stackElements.pop
visit(iVisited.iter_node)
@@ -916,20 +862,17 @@
end
def visitGlobalAsgnNode(iVisited)
- # puts iVisited.to_s + " " + iVisited.name
visit(iVisited.value_node)
@stackElements << location([:gasgn, iVisited.name.to_sym, @stackElements.pop], iVisited)
nil
end
def visitGlobalVarNode(iVisited)
- # puts "GlobalVar " + iVisited.to_s + " " + iVisited.name
@stackElements << location([:gvar, iVisited.name.to_sym ], iVisited)
nil
end
def visitHashNode(iVisited)
- # puts iVisited.to_s
result = location([:hash], iVisited)
visit(iVisited.list_node)
temp = @stackElements.pop
@@ -962,7 +905,6 @@
end
def visitIfNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.condition)
cond = JParseTree.filter_nested_arrays(@stackElements.pop)
if cond.is_a? Symbol
@@ -1005,7 +947,6 @@
end
def visitIterNode_fromCall(iVisited, cond)
-# puts "HHHHHELLLOOOOO #{iVisited.methods.inspect}:#{cond}"
if !iVisited.respond_to? :var_node
visitIterNode_fromBlockPass(iVisited.body_node, cond)
else
@@ -1027,7 +968,6 @@
def visitLocalAsgnNode(iVisited)
- # puts iVisited.to_s + " Local var name " + iVisited.name + " is var index " + iVisited.count.to_s
visit(iVisited.value_node)
temp = @stackElements.pop
if temp == nil
@@ -1045,13 +985,11 @@
def visitLocalVarNode(iVisited)
- @stackElements << location([:lvar, iVisited.name.to_sym], iVisited)
-# @stackElements << iVisited.name.to_sym
+ @stackElements << location([:lvar, iVisited.name.to_sym], iVisited)
nil
end
def visitMultipleAsgnNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.head_node)
head = @stackElements.pop
if iVisited.args_node
@@ -1069,19 +1007,7 @@
# visit(iVisited.args_node)
visit(iVisited.value_node)
val = @stackElements.pop
-# if args
-# if val
-# , args, val]
-# else
-# @stackElements << location([:masgn, head, args], iVisited)
-# end
-# else
-# if val
-# @stackElements << location([:masgn, head, val], iVisited)
-# else
-# @stackElements << location([:masgn, head], iVisited)
-# end
-# end
+
if args
node << args
end
@@ -1094,7 +1020,6 @@
end
def visitMatch2Node(iVisited)
- # puts iVisited.to_s
visit(iVisited.receiver_node)
recv = @stackElements.pop
visit(iVisited.value_node)
@@ -1111,18 +1036,15 @@
end
def visitMatchNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.regexp_node)
@stackElements << location([:match, @stackElements.pop], iVisited)
nil
end
def visitModuleNode(iVisited)
- # puts iVisited.to_s
# TODO: this isn't called!
visit(iVisited.body_node)
body = @stackElements.pop
- #puts "[EMAIL PROTECTED]"
@stackElements << location([:module, iVisited.getCPath.name.to_sym,
location([:scope, JParseTree.filter_nested_arrays(body)], iVisited.body_node)
], iVisited)
@@ -1139,7 +1061,6 @@
end
def visitNextNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.value_node)
temp = @stackElements.pop
if temp == nil
@@ -1151,27 +1072,23 @@
end
def visitNilNode(iVisited)
- # puts iVisited.to_s
# TODO: check this
@stackElements << :nil
nil
end
def visitNotNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.condition_node)
@stackElements << location([:not, JParseTree.filter_nested_arrays([EMAIL PROTECTED])], iVisited)
nil
end
def visitNthRefNode(iVisited)
- # puts iVisited.to_s + " match number " + iVisited.match_number.to_s
@stackElements << location([:nth_ref, iVisited.match_number], iVisited)
nil
end
def visitOpElementAsgnNode(iVisited)
- # puts iVisited.to_s + " " + iVisited.operator_name
visit(iVisited.receiver_node)
recv = @stackElements.pop
visit(iVisited.args_node)
@@ -1204,6 +1121,7 @@
visit(iVisited.second_node)
val = @stackElements.pop
@stackElements << location([op_symbol, recv, val], iVisited)
+ nil
end
def visitOpAsgnAndNode(iVisited)
@@ -1217,12 +1135,11 @@
end
def visitOptNNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.body_node)
+ nil
end
def visitOrNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.first_node)
first = @stackElements.pop
visit(iVisited.second_node)
@@ -1231,7 +1148,6 @@
end
def visitPostExeNode(iVisited)
- # puts iVisited.to_s
@stackElements ||= []
tmp = [:iter, [:postexe], nil]
if iVisited.child_nodes
@@ -1246,7 +1162,6 @@
end
def visitRedoNode(iVisited)
- # puts iVisited.to_s
@stackElements << location([:redo], iVisited)
nil
end
@@ -1258,7 +1173,6 @@
end
def visitRescueBodyNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.exception_nodes)
excNodes = @stackElements.pop
visit(iVisited.body_node)
@@ -1285,14 +1199,12 @@
end
def visitRescueNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.body_node)
body = @stackElements.pop
visit(iVisited.rescue_node)
rescueNode = @stackElements.pop
visit(iVisited.else_node)
else_node = @stackElements.pop
- p "Else: #{else_node} ||| #{body}"
if else_node == nil
if !body
@stackElements << location([:rescue, rescueNode], iVisited)
@@ -1313,13 +1225,11 @@
def visitRetryNode(iVisited)
- # puts iVisited.to_s
@stackElements << location([:retry], iVisited)
nil
end
def visitReturnNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.value_node)
temp = @stackElements.pop
if temp
@@ -1332,7 +1242,6 @@
def visitSClassNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.receiver_node)
recv = @stackElements.pop
@@ -1345,27 +1254,22 @@
def visitSelfNode(iVisited)
- # puts iVisited.to_s
@stackElements << location([:self], iVisited)
nil
end
def visitSplatNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.value)
@stackElements << location([:splat, @stackElements.pop], iVisited)
nil
end
def visitStrNode(iVisited)
- # puts iVisited.to_s + "\"" + iVisited.value + "\""
@stackElements << location([:str, iVisited.value], iVisited)
nil
end
def visitSuperNode(iVisited)
- # puts iVisited.to_s
- # p "FOO"
visit(iVisited.args_node)
args = @stackElements.pop
if args != nil
@@ -1383,40 +1287,34 @@
end
def visitSValueNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.value)
@stackElements << location([:svalue, @stackElements.pop], iVisited)
nil
end
def visitSymbolNode(iVisited)
- # puts iVisited.to_s + " " + iVisited.name
@stackElements << location([:lit, iVisited.name.to_sym], iVisited)
nil
end
def visitToAryNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.value)
@stackElements << location([:to_ary, @stackElements.pop], iVisited)
nil
end
def visitTrueNode(iVisited)
- # puts iVisited.to_s
@stackElements << [:true]
nil
end
def visitUndefNode(iVisited)
- # puts iVisited.to_s + " " + iVisited.name
@stackElements << location([:undef, [:lit, iVisited.name.to_sym]], iVisited)
# p "[EMAIL PROTECTED]"
nil
end
def visitUntilNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.condition_node)
cond = @stackElements.pop
visit(iVisited.body_node)
@@ -1428,53 +1326,57 @@
def visitVAliasNode(iVisited)
- # puts iVisited.to_s + " aliasing " + iVisited.old_name + " as " + iVisited.new_name
@stackElements << location([:valias, iVisited.new_name.to_sym, iVisited.old_name.to_sym], iVisited)
nil
end
def visitRootNode(iVisited)
visit(iVisited.body_node)
+ nil
end
def visitVCallNode(iVisited)
- # puts iVisited.to_s + " " + iVisited.method_name
-# visit(iVisited.args_node)
@stackElements << location([:vcall, iVisited.name.to_sym], iVisited)
nil
end
-
- def visitWhenNode(iVisited)
- visit(iVisited.expression_nodes)
+
+
+
+
+ def visitWhenNode(iVisited)
+ retVal = []
+
+ currNode = iVisited
+ while currNode.class == org.jruby.ast.WhenNode
+ visit(currNode.expression_nodes)
expr = @stackElements.pop
- if iVisited.body_node
- visit(iVisited.body_node)
+ if currNode.body_node
+ visit(currNode.body_node)
body = @stackElements.pop
else
body = nil
end
+ retVal << location([:when, JParseTree.filter_nested_arrays(expr), JParseTree.filter_nested_arrays(body) ], currNode)
+ currNode = currNode.next_case
+ end
- @stackElements.last << location([:when, JParseTree.filter_nested_arrays(expr), JParseTree.filter_nested_arrays(body) ], iVisited)
- # Note: for some reason, the Case node doesn't contain it's WhenNodes in a list;
- # instead, it has a reference to the head of a linked list of WhenNodes + a default _expression_;
- # No idea why that's the case, but it makes for some nice additional complexity...
- if iVisited.next_case.class == org.jruby.ast.WhenNode
- puts iVisited.next_case
- visitWhenNode(iVisited.next_case)
- else
- if iVisited.next_case
- visit(iVisited.next_case)
- tmp = @stackElements.pop
- else
- tmp = nil
- end
- @stackElements.last << JParseTree.filter_nested_arrays(tmp)
- end
- nil
+ # this linked list of WhenNodes is terminated by a non-WhenNode
+ # object, either some other node or nil
+ if currNode
+ visit(currNode)
+ tmp = @stackElements.pop
+ else
+ tmp = nil
+ end
+ retVal << JParseTree.filter_nested_arrays(tmp)
+
+ @stackElements << retVal
+
+ nil
end
+
def visitWhileNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.condition_node)
condNode = JParseTree.filter_nested_arrays(@stackElements.pop)
if condNode == :false || condNode == :true
@@ -1487,13 +1389,11 @@
end
def visitXStrNode(iVisited)
- # puts iVisited.to_s + " \"" + iVisited.value + "\""
@stackElements << location([:xstr, iVisited.value], iVisited)
nil
end
def visitYieldNode(iVisited)
- # puts iVisited.to_s
visit(iVisited.args_node)
temp = @stackElements.pop
if temp == nil
@@ -1505,13 +1405,11 @@
end
def visitZArrayNode(iVisited)
- # puts iVisited.to_s
@stackElements << location([:zarray], iVisited)
nil
end
def visitZSuperNode(iVisited)
- # puts iVisited.to_s
@stackElements << location([:zsuper], iVisited)
nil
end