Skip to content

Commit

Permalink
Remove Source#string= method
Browse files Browse the repository at this point in the history
## Why?

We want to just change scan pointer.

ruby#114 (comment)
> I want to just change scan pointer (StringScanner#pos=) instead of changing @scanner.string.
  • Loading branch information
naitoh committed Mar 3, 2024
1 parent 19975fe commit e59a2af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
23 changes: 13 additions & 10 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ def pull_event
#STDERR.puts @source.encoding
#STDERR.puts "BUFFER = #{@source.buffer.inspect}"
if @document_status == nil
start_position = @source.position
if @source.match("<?", true)
return process_instruction
return process_instruction(start_position)
elsif @source.match("<!", true)
if @source.match("--", true)
return [ :comment, @source.match(/(.*?)-->/um, true)[1] ]
Expand All @@ -224,7 +225,7 @@ def pull_event
else
message = "#{base_error_message}: invalid name"
end
@source.string = "<!DOCTYPE" + @source.buffer
@source.position = start_position
raise REXML::ParseException.new(message, @source)
end
@nsstack.unshift(curr_ns=Set.new)
Expand Down Expand Up @@ -266,6 +267,7 @@ def pull_event
end
if @document_status == :in_doctype
@source.match(/\s*/um, true) # skip spaces
start_position = @source.position
if @source.match("<!", true)
if @source.match("ELEMENT", true)
md = @source.match(/(.*?)>/um, true)
Expand Down Expand Up @@ -325,7 +327,7 @@ def pull_event
else
message = "#{base_error_message}: invalid name"
end
@source.string = " <!NOTATION" + @source.buffer
@source.position = start_position
raise REXML::ParseException.new(message, @source)
end
name = parse_name(base_error_message)
Expand Down Expand Up @@ -355,6 +357,7 @@ def pull_event
@source.match(/\s*/um, true)
end
begin
start_position = @source.position
if @source.match("<", true)
if @source.match("/", true)
@nsstack.shift
Expand All @@ -367,7 +370,7 @@ def pull_event
if md.nil? or last_tag != md[1]
message = "Missing end tag for '#{last_tag}'"
message += " (got '#{md[1]}')" if md
@source.string = "</" + @source.buffer if md.nil?
@source.position = start_position if md.nil?
raise REXML::ParseException.new(message, @source)
end
return [ :end_element, last_tag ]
Expand All @@ -391,12 +394,12 @@ def pull_event
raise REXML::ParseException.new( "Declarations can only occur "+
"in the doctype declaration.", @source)
elsif @source.match("?", true)
return process_instruction
return process_instruction(start_position)
else
# Get the next tag
md = @source.match(TAG_PATTERN, true)
unless md
@source.string = "<" + @source.buffer
@source.position = start_position
raise REXML::ParseException.new("malformed XML: missing tag start", @source)
end
tag = md[1]
Expand Down Expand Up @@ -578,11 +581,11 @@ def parse_id_invalid_details(accept_external_id:,
end
end

def process_instruction
def process_instruction(start_position)
match_data = @source.match(INSTRUCTION_END, true)
unless match_data
message = "Invalid processing instruction node"
@source.string = "<?" + @source.buffer
@source.position = start_position
raise REXML::ParseException.new(message, @source)
end
if @document_status.nil? and match_data[1] == "xml"
Expand Down Expand Up @@ -625,7 +628,7 @@ def parse_attributes(prefixes, curr_ns)
break if scanner.eos?
end

pos = scanner.pos
start_position = scanner.pos
while true
break if scanner.scan(ATTRIBUTE_PATTERN)
unless scanner.scan(QNAME)
Expand All @@ -648,7 +651,7 @@ def parse_attributes(prefixes, curr_ns)
scanner << "/" if closed
scanner << ">"
scanner << match_data[1]
scanner.pos = pos
scanner.pos = start_position
closed = !match_data[2].nil?
next
end
Expand Down
8 changes: 6 additions & 2 deletions lib/rexml/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ def match(pattern, cons=false)
end
end

def string=(string)
@scanner.string = string
def position
@scanner.pos
end

def position=(pos)
@scanner.pos = pos
end

# @return true if the Source is exhausted
Expand Down
2 changes: 1 addition & 1 deletion test/parse/test_notation_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_no_name
Line: 5
Position: 72
Last 80 unconsumed characters:
<!NOTATION> ]> <r/>
<!NOTATION> ]> <r/>
DETAIL
end

Expand Down

0 comments on commit e59a2af

Please sign in to comment.