Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/prism/internal/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ static const pm_node_flags_t PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE = ((
/* Dedenting a heredoc folds interpolated string literals into its text. */
static const pm_node_flags_t PM_INTERPOLATED_STRING_NODE_FLAGS_DEDENTED = ((PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 2);

/* Adjacent plain strings contribute one encoding when interpolated. Store the
* result of checking their parts on the concatenation. Zero means it is unknown. */
enum {
PM_STRING_CONCAT_ENCODING_ASCII_ONLY = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 3,
PM_STRING_CONCAT_ENCODING_SOURCE = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 4,
PM_STRING_CONCAT_ENCODING_UTF8 = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 5,
PM_STRING_CONCAT_ENCODING_BINARY = (PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 6,
PM_STRING_CONCAT_ENCODING_MASK = PM_STRING_CONCAT_ENCODING_ASCII_ONLY | PM_STRING_CONCAT_ENCODING_SOURCE |
PM_STRING_CONCAT_ENCODING_UTF8 | PM_STRING_CONCAT_ENCODING_BINARY
};

/*
* A growable list of nodes for bookkeeping that never becomes part of the tree,
* such as the block exits and implicit parameters that are tracked while
Expand Down
232 changes: 114 additions & 118 deletions src/prism.c

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ module Prism
(<%= field.name %>.length == other.<%= field.name %>.length) &&
<%= field.name %>.zip(other.<%= field.name %>).all? { |left, right| left === right }<%= " &&" if index != fields.length - 1 %>
<%- elsif field.is_a?(Prism::Template::Flags) -%>
(flags === other.flags)<%= " &&" if index != fields.length - 1 %>
<%- public_flags_mask = (1 << (Prism::Template::COMMON_FLAGS_COUNT + field.values.length)) - 1 -%>
((flags & <%= public_flags_mask %>) == (other.flags & <%= public_flags_mask %>))<%= " &&" if index != fields.length - 1 %>
<%- else -%>
(<%= field.name %> === other.<%= field.name %>)<%= " &&" if index != fields.length - 1 %>
<%- end -%>
Expand Down
49 changes: 49 additions & 0 deletions test/prism/encoding/string_encoding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@ def test_incompatible_encodings_in_string_interpolation
assert_equal '\u3042', errors.first.location.slice
end

def test_encoding_of_adjacent_string_literals_inside_interpolation
[
['"\xcd#{"\u3042"""}"', "ASCII-8BIT / UTF-8", '#{"\u3042"""}'],
['"#{"" "ascii" "\u3042"}\xcd"', "UTF-8 / ASCII-8BIT", '\xcd'],
['"\xcd#{"a" "b"}\u3042"', "ASCII-8BIT / UTF-8", '\u3042']
].each do |source, encodings, location|
errors = Prism.parse(source, encoding: "US-ASCII").errors
assert_equal ["string literal encodings differ (#{encodings})"], errors.map(&:message), source
assert_equal location, errors.first.location.slice
end

['"\xcd#{"\u3042" "#{value}"}"', '"\xcd#{"" "#{"\u3042"}"}"', '"\xcd#{"ascii" "\xff"}"'].each do |source|
assert_predicate Prism.parse(source, encoding: "US-ASCII"), :success?, source
end

source = '"\xcd#{"\u3042" "\xcd" "\u3042"}"'
errors = Prism.parse(source, encoding: "US-ASCII").errors
assert_equal ['"\xcd"', '#{"\u3042" "\xcd" "\u3042"}'], errors.map { |error| error.location.slice }
end

def test_concatenation_encodings_survive_until_heredoc_dedent
first = '#{"\u3042" ""}'
middle = '#{"a" "b"}' * 32
last = '#{"" "\xcd"}'
source = "<<~H\n #{first}#{middle}#{last}\nH\n"
errors = Prism.parse(source, encoding: "US-ASCII").errors
assert_equal ["string literal encodings differ (UTF-8 / ASCII-8BIT)"], errors.map(&:message)
assert_equal last, errors.first.location.slice
end

def test_encoding_at_heredoc_interpolation
unicode = '#{?\u3042}'
raw = "\x9Da".b
Expand Down Expand Up @@ -176,6 +206,25 @@ def test_encoding_at_heredoc_interpolation
assert_equal unicode, result.errors.first.location.slice
end

def test_encoding_between_a_heredoc_and_an_adjacent_string
encoding = ENV["PRISM_BUILD_MINIMAL"] ? "ASCII-8BIT" : "Windows-31J"
unicode = '#{?\u3042}'
suffix = '"\xA2"'

[["H", unicode], ["-H", unicode], ["~H", unicode], ["~H", " #{unicode}"]].each do |opening, body|
source = "<<#{opening}#{suffix}\n#{body}\nH\n"
result = Prism.parse(source, encoding: encoding)
assert_equal ["string literal encodings differ (UTF-8 / #{encoding})"], result.errors.map(&:message), source
assert_equal suffix, result.errors.first.location.slice
end

source = "<<H#{suffix}\n#{unicode}\#{value}\nH\n"
assert_predicate Prism.parse(source, encoding: encoding), :success?

source = "<<~H#{suffix}\n #{unicode}\ntext\nH\n"
assert_predicate Prism.parse(source, encoding: encoding), :success?
end

def test_interpolation_encoding_errors_in_source_order
errors = Prism.parse('"\u3042#{"\xD4"}#{"\xD4" "\u3042"}"', encoding: "ASCII-8BIT").errors
assert_equal ["string literal encodings differ (UTF-8 / ASCII-8BIT)", "string literal encodings differ (ASCII-8BIT / UTF-8)"], errors.map(&:message)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# shareable_constant_value: literal
X = [*b] and X
X = *b and X
^~~ unexpected 'and', expecting end-of-input
^~~ unexpected 'and', ignoring it
A::X = 1, 2 or X
^~ unexpected 'or', expecting end-of-input
^~ unexpected 'or', ignoring it

45 changes: 45 additions & 0 deletions test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,48 @@ def test_top_level_yield_is_invalid_in_dead_code
assert Prism.parse_success?("def f; yield if false; end")
end

def test_yield_inside_defined_across_scope_boundaries
["class C; yield; end", "module M; yield; end", "class << self; yield; end"].each do |body|
errors = Prism.parse("defined?(#{body})\n#{body}").errors
assert_equal [:invalid_yield], errors.map(&:type), body
assert_equal 2, errors.first.location.start_line, body
end
end

def test_endless_method_rescue_rejects_unattached_do_block
result = Prism.parse("relay def f = 1 rescue 2 do end")

assert_equal [:def_endless_do_block], result.errors.map(&:type)
assert_equal "do end", result.errors.first.location.slice
end

def test_command_blocks_in_conditional_branches_inside_command_arguments
command = "dispatch build () {} do work end"
branches = ["if flag;", "if flag; elsif other;", "if flag; else", "unless flag;", "unless flag; else"]
branches.each do |branch|
source = "outer case value; in Integer; else #{branch} #{command}; end; end"
result = Prism.parse(source)
assert_predicate result, :success?, source

assert_nil result.statement.block
dispatch = result.statement.breadth_first_search { |node| node.is_a?(CallNode) && node.name == :dispatch }
assert_equal "do work end", dispatch.block.slice
assert_equal "{}", dispatch.arguments.arguments.first.block.slice
end

refute Prism.parse_success?("if flag; #{command}; end")
end

def test_plain_do_blocks_in_a_conditional_command_argument
assert Prism.parse_success?("outer :first, if flag; work do end; end")
assert Prism.parse_success?("outer(:first, if flag; items.each do end; end)")

["items.each", "run()", "items[0]"].each do |call|
source = "outer :first, if flag; #{call} do end; end"
refute Prism.parse_success?(source), source
end
end

def test_command_block_in_a_loop_header_inside_command_arguments
["while", "until", "for i in"].each do |header|
source = "outer value = #{header} check def task = inner item {} do work end do loop_body end"
Expand Down Expand Up @@ -395,6 +430,16 @@ def test_pattern_newlines_in_pins_and_guards
end
end

def test_required_keyword_parameter_before_a_newline_in_a_pattern_guard
result = Prism.parse("case x; in [] if def f(b:\n) end; end")
assert_empty result.errors
parameter = result.statement.conditions.first.pattern.predicate.parameters.keywords.fetch(0)
assert_instance_of RequiredKeywordParameterNode, parameter
assert_equal :b, parameter.name

refute Prism.parse_success?("case x; in [] if def f(b:\n1) end; end")
end

def test_range_patterns_require_literal_bounds
%w[.. ... 1.. 1...].each do |range|
result = Prism.parse("case x; in #{range}-2**2; end")
Expand Down
8 changes: 8 additions & 0 deletions test/prism/result/equality_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ def test_equality
complex_source_2 = "class Something; @var = something.else { _2 }; end"
refute_operator Prism.parse_statement(complex_source_1), :===, Prism.parse_statement(complex_source_2)
end

def test_equality_ignores_cached_concatenation_encoding
source = '"a" "b"'
utf8 = Prism.parse_statement(source, encoding: "UTF-8")
ascii = Prism.parse_statement(source, encoding: "US-ASCII")

assert_operator utf8, :===, ascii
end
end
end
23 changes: 23 additions & 0 deletions test/prism/result/heredoc_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@

module Prism
class HeredocTest < TestCase
def test_quoted_hash_label_spanning_a_heredoc
["'", '"'].each do |quote|
result = Prism.parse(<<~RUBY)
p(<<A, {#{quote}first
body
A
it\\'s#{quote}: 1})
RUBY

assert_predicate result, :success?, quote
heredoc, hash = result.statement.arguments.arguments
assert_equal "body\n", heredoc.unescaped

assoc = hash.elements.first
key = assoc.key
assert_instance_of InterpolatedSymbolNode, key
assert_equal ["first\n", "it\\'s"], key.parts.map(&:slice)
assert_equal ["first\n", "it's"], key.parts.map(&:unescaped)
assert_equal "#{quote}:", key.closing
assert_equal 1, assoc.value.value
end
end

def test_word_separators_spanning_a_heredoc
%w[%w %i].each do |literal|
{ "\nA\n " => [], "first\nbody\nA\n second" => %w[first second] }.each do |contents, expected|
Expand Down
Loading