Skip to content

Commit

Permalink
parser: move duplicated end tag check to BaseParser
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 17, 2024
1 parent 35e1681 commit 2b47b16
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ def pull_event
if @document_status == :in_doctype
raise ParseException.new("Malformed DOCTYPE: unclosed", @source)
end
unless @tags.empty?
path = "/" + @tags.join("/")
raise ParseException.new("Missing end tag for '#{path}'", @source)
end
return [ :end_document ]
end
return @stack.shift if @stack.size > 0
Expand Down
8 changes: 0 additions & 8 deletions lib/rexml/parsers/streamparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class StreamParser
def initialize source, listener
@listener = listener
@parser = BaseParser.new( source )
@tag_stack = []
end

def add_listener( listener )
Expand All @@ -20,21 +19,14 @@ def parse
event = @parser.pull
case event[0]
when :end_document
unless @tag_stack.empty?
tag_path = "/" + @tag_stack.join("/")
raise ParseException.new("Missing end tag for '#{tag_path}'",
@parser.source)
end
return
when :start_element
@tag_stack << event[1]
attrs = event[2].each do |n, v|
event[2][n] = @parser.unnormalize( v )
end
@listener.tag_start( event[1], attrs )
when :end_element
@listener.tag_end( event[1] )
@tag_stack.pop
when :text
unnormalized = @parser.unnormalize( event[1] )
@listener.text( unnormalized )
Expand Down
7 changes: 0 additions & 7 deletions lib/rexml/parsers/treeparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@ def add_listener( listener )
end

def parse
tag_stack = []
entities = nil
begin
while true
event = @parser.pull
#STDERR.puts "TREEPARSER GOT #{event.inspect}"
case event[0]
when :end_document
unless tag_stack.empty?
raise ParseException.new("No close tag for #{@build_context.xpath}",
@parser.source, @parser)
end
return
when :start_element
tag_stack.push(event[1])
el = @build_context = @build_context.add_element( event[1] )
event[2].each do |key, value|
el.attributes[key]=Attribute.new(key,value,self)
end
when :end_element
tag_stack.pop
@build_context = @build_context.parent
when :text
if @build_context[-1].instance_of? Text
Expand Down
2 changes: 1 addition & 1 deletion test/parser/test_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_no_close_tag
parse(xml)
end
assert_equal(<<-MESSAGE, exception.to_s)
No close tag for /root
Missing end tag for '/root'
Line: 1
Position: #{xml.bytesize}
Last 80 unconsumed characters:
Expand Down

0 comments on commit 2b47b16

Please sign in to comment.