Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Various fixes found through the prism test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 11, 2024
1 parent 6b201a8 commit e0fc1c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/parser/prism/compare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def self.compare(filepath, source = nil, compare_tokens: true)
end
end

puts "expected:"
pp expected_ast

puts "actual:"
pp actual_ast

return false
end

Expand Down
6 changes: 4 additions & 2 deletions lib/parser/prism/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,8 @@ def visit_multi_target_node(node)
# foo, bar = baz
# ^^^^^^^^^^^^^^
def visit_multi_write_node(node)
node = node.copy(rest: nil) if node.rest.is_a?(ImplicitRestNode)

builder.multi_assign(
builder.multi_lhs(
token(node.lparen_loc),
Expand Down Expand Up @@ -1368,7 +1370,7 @@ def visit_singleton_class_node(node)
token(node.class_keyword_loc),
token(node.operator_loc),
visit(node.expression),
node.body.accept(copy_compiler(locals: node.locals)),
node.body&.accept(copy_compiler(locals: node.locals)),
token(node.end_keyword_loc)
)
end
Expand Down Expand Up @@ -1732,7 +1734,7 @@ def visit_heredoc(node)
end

closing = node.closing
closing_t = [closing.chomp, srange_offsets(node.closing_loc.start_offset, node.closing_loc.end_offset - closing[/\s+$/].length)]
closing_t = [closing.chomp, srange_offsets(node.closing_loc.start_offset, node.closing_loc.end_offset - (closing[/\s+$/]&.length || 0))]

[children, closing_t]
end
Expand Down
36 changes: 30 additions & 6 deletions lib/parser/prism/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,11 @@ def to_a
when :tFLOAT
value = Float(value)
when :tIMAGINARY
value.chomp!("i")
value = Complex(0, value.end_with?("r") ? Rational(value.chomp("r")) : value)
value = parse_complex(value)
when :tINTEGER
if value.start_with?("+")
tokens << [:tUNARY_NUM, ["+", Source::Range.new(buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1])]]
location = Source::Range.new(buffer, token.location.start_offset + 1, token.location.end_offset)
location = Source::Range.new(buffer, offset_cache[token.location.start_offset + 1], offset_cache[token.location.end_offset])
end

value = Integer(value)
Expand All @@ -238,7 +237,7 @@ def to_a
when :tOP_ASGN
value.chomp!("=")
when :tRATIONAL
value = Rational(value.chomp("r"))
value = parse_rational(value)
when :tSPACE
value = nil
when :tSTRING_BEG
Expand Down Expand Up @@ -266,11 +265,12 @@ def to_a
location = Source::Range.new(buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1])
end
when :tSYMBEG
if (next_token = lexed[index]) && next_token.type != :STRING_CONTENT
if (next_token = lexed[index]) && next_token.type != :STRING_CONTENT && next_token.type != :EMBEXPR_BEGIN && next_token.type != :EMBVAR
next_location = token.location.join(next_token.location)
type = :tSYMBOL
value = next_token.value
location = Source::Range.new(buffer, next_location.start_offset, next_location.end_offset)
value = { "~@" => "~", "!@" => "!" }.fetch(value, value)
location = Source::Range.new(buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
index += 1
end
when :tFID
Expand All @@ -288,6 +288,30 @@ def to_a

tokens
end

private

def parse_complex(value)
value.chomp!("i")

if value.end_with?("r")
Complex(0, parse_rational(value))
elsif value.start_with?(/0[BbOoDdXx]/)
Complex(0, Integer(value))
else
Complex(0, value)
end
end

def parse_rational(value)
value.chomp!("r")

if value.start_with?(/0[BbOoDdXx]/)
Rational(Integer(value))
else
Rational(value)
end
end
end
end
end

0 comments on commit e0fc1c4

Please sign in to comment.