ruby-changes:50880
From: kou <ko1@a...>
Date: Wed, 4 Apr 2018 15:51:12 +0900 (JST)
Subject: [ruby-changes:50880] kou:r63087 (trunk): rexml: Add codes for debugging XPath logic
kou 2018-04-04 15:51:07 +0900 (Wed, 04 Apr 2018) New Revision: 63087 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63087 Log: rexml: Add codes for debugging XPath logic Modified files: trunk/lib/rexml/xpath_parser.rb Index: lib/rexml/xpath_parser.rb =================================================================== --- lib/rexml/xpath_parser.rb (revision 63086) +++ lib/rexml/xpath_parser.rb (revision 63087) @@ -51,6 +51,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L51 @parser = REXML::Parsers::XPathParser.new @namespaces = nil @variables = {} + @nest = 0 end def namespaces=( namespaces={} ) @@ -151,9 +152,11 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L152 ALL = [ :attribute, :element, :text, :processing_instruction, :comment ] ELEMENTS = [ :element ] def expr( path_stack, nodeset, context=nil ) + # enter(:expr, path_stack, nodeset) node_types = ELEMENTS return nodeset if path_stack.length == 0 || nodeset.length == 0 while path_stack.length > 0 + # trace(:while, path_stack, nodeset) if nodeset.length == 0 path_stack.clear return [] @@ -165,6 +168,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L168 when :qname prefix = path_stack.shift name = path_stack.shift + # enter(:qname, path_stack, prefix, name, nodeset) nodeset.delete_if do |node| # FIXME: This DOUBLES the time XPath searches take ns = get_namespace( node, prefix ) @@ -176,6 +180,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L180 node.name == name and node.namespace == ns ) end + # leave(:qname, path_stack, nodeset) node_types = ELEMENTS when :any @@ -206,12 +211,14 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L211 nt = nil nodeset.each do |node| nt = node.node_type + # trace(:child, nt, node) new_nodeset += node.children if nt == :element or nt == :document end nodeset = new_nodeset node_types = ELEMENTS when :literal + # trace(:literal, path_stack, nodeset) return path_stack.shift when :attribute @@ -277,6 +284,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L284 new_nodeset = [] subcontext = { :size => nodeset.size } pred = path_stack.shift + # enter(:predicate, pred, nodeset) nodeset.each_with_index { |node, index| subcontext[ :node ] = node subcontext[ :index ] = index+1 @@ -294,6 +302,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L302 end } nodeset = new_nodeset + # leave(:predicate_return, nodeset) =begin predicate = path_stack.shift ns = nodeset.clone @@ -393,6 +402,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L402 left = expr( path_stack.shift, nodeset.dup, context ) right = expr( path_stack.shift, nodeset.dup, context ) res = equality_relational_compare( left, op, right ) + # trace(op, left, right, res) return res when :and @@ -465,8 +475,24 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L475 end end # while return nodeset + # ensure + # leave(:expr, path_stack, nodeset) end + def trace(*args) + indent = " " * @nest + puts("#{indent}#{args.inspect}") + end + + def enter(tag, *args) + trace(:enter, tag, *args) + @nest += 1 + end + + def leave(tag, *args) + @nest -= 1 + trace(:leave, tag, *args) + end ########################################################## # FIXME -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/