Skip to content

Commit 034447b

Browse files
authored
Merge pull request #3394 from Earlopain/parser-translator-lexer-range-helper
2 parents 080304e + f339b76 commit 034447b

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def to_a
243243

244244
type = TYPES.fetch(token.type)
245245
value = token.value
246-
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.end_offset])
246+
location = range(token.location.start_offset, token.location.end_offset)
247247

248248
case type
249249
when :kDO
@@ -266,12 +266,12 @@ def to_a
266266

267267
if start_index != index
268268
value += next_token.value
269-
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[lexed[index][0].location.end_offset])
269+
location = range(token.location.start_offset, lexed[index][0].location.end_offset)
270270
index += 1
271271
end
272272
else
273273
value.chomp!
274-
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.end_offset - 1])
274+
location = range(token.location.start_offset, token.location.end_offset - 1)
275275
end
276276
when :tNL
277277
value = nil
@@ -281,8 +281,8 @@ def to_a
281281
value = parse_complex(value)
282282
when :tINTEGER
283283
if value.start_with?("+")
284-
tokens << [:tUNARY_NUM, ["+", Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1])]]
285-
location = Range.new(source_buffer, offset_cache[token.location.start_offset + 1], offset_cache[token.location.end_offset])
284+
tokens << [:tUNARY_NUM, ["+", range(token.location.start_offset, token.location.start_offset + 1)]]
285+
location = range(token.location.start_offset + 1, token.location.end_offset)
286286
end
287287

288288
value = parse_integer(value)
@@ -311,7 +311,7 @@ def to_a
311311
next_location = token.location.join(next_token.location)
312312
type = :tSTRING
313313
value = ""
314-
location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
314+
location = range(next_location.start_offset, next_location.end_offset)
315315
index += 1
316316
elsif value.start_with?("'", '"', "%")
317317
if next_token&.type == :STRING_CONTENT && next_token.value.lines.count <= 1 && next_next_token&.type == :STRING_END
@@ -320,7 +320,7 @@ def to_a
320320
next_location = token.location.join(next_next_token.location)
321321
value = unescape_string(string_value, value)
322322
type = :tSTRING
323-
location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
323+
location = range(next_location.start_offset, next_location.end_offset)
324324
index += 2
325325
tokens << [type, [value, location]]
326326

@@ -392,7 +392,7 @@ def to_a
392392

393393
if emit
394394
end_offset = start_offset + current_line.bytesize + adjustment
395-
tokens << [:tSTRING_CONTENT, [unescape_string(current_line, quote_stack.last), Range.new(source_buffer, offset_cache[start_offset], offset_cache[end_offset])]]
395+
tokens << [:tSTRING_CONTENT, [unescape_string(current_line, quote_stack.last), range(start_offset, end_offset)]]
396396
start_offset = end_offset
397397
current_line = +""
398398
adjustment = 0
@@ -406,10 +406,10 @@ def to_a
406406
if token.type == :HEREDOC_END && value.end_with?("\n")
407407
newline_length = value.end_with?("\r\n") ? 2 : 1
408408
value = heredoc_stack.pop.identifier
409-
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.end_offset - newline_length])
409+
location = range(token.location.start_offset, token.location.end_offset - newline_length)
410410
elsif token.type == :REGEXP_END
411411
value = value[0]
412-
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1])
412+
location = range(token.location.start_offset, token.location.start_offset + 1)
413413
end
414414

415415
quote_stack.pop
@@ -419,7 +419,7 @@ def to_a
419419
type = :tSYMBOL
420420
value = next_token.value
421421
value = { "~@" => "~", "!@" => "!" }.fetch(value, value)
422-
location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
422+
location = range(next_location.start_offset, next_location.end_offset)
423423
index += 1
424424
else
425425
quote_stack.push(value)
@@ -446,7 +446,7 @@ def to_a
446446
tokens << [type, [value, location]]
447447

448448
if token.type == :REGEXP_END
449-
tokens << [:tREGEXP_OPT, [token.value[1..], Range.new(source_buffer, offset_cache[token.location.start_offset + 1], offset_cache[token.location.end_offset])]]
449+
tokens << [:tREGEXP_OPT, [token.value[1..], range(token.location.start_offset + 1, token.location.end_offset)]]
450450
end
451451
end
452452

@@ -455,6 +455,11 @@ def to_a
455455

456456
private
457457

458+
# Creates a new parser range, taking prisms byte offsets into account
459+
def range(start_offset, end_offset)
460+
Range.new(source_buffer, offset_cache[start_offset], offset_cache[end_offset])
461+
end
462+
458463
# Parse an integer from the string representation.
459464
def parse_integer(value)
460465
Integer(value)

0 commit comments

Comments
 (0)