Skip to content

Commit

Permalink
Drop token info for endless method definition
Browse files Browse the repository at this point in the history
Because it does not have closing `end`.
  • Loading branch information
nobu committed Jun 24, 2020
1 parent 419b059 commit 3d8705d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
23 changes: 23 additions & 0 deletions parse.y
Expand Up @@ -1053,6 +1053,7 @@ static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_
static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);

#define WARN_EOL(tok) \
(looking_at_eol_p(p) ? \
Expand Down Expand Up @@ -2493,6 +2494,7 @@ arg : lhs '=' arg_rhs
}
| defn_head f_paren_args '=' arg
{
token_info_drop(p, "def", @1.beg_pos);
restore_defun(p, $<node>1->nd_defn);
/*%%%*/
$$ = set_defun_body(p, $1, $2, $4, &@$);
Expand All @@ -2502,6 +2504,7 @@ arg : lhs '=' arg_rhs
}
| defn_head f_paren_args '=' arg modifier_rescue arg
{
token_info_drop(p, "def", @1.beg_pos);
restore_defun(p, $<node>1->nd_defn);
/*%%%*/
$4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
Expand Down Expand Up @@ -5766,6 +5769,26 @@ token_info_pop(struct parser_params *p, const char *token, const rb_code_locatio
ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
}

static void
token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
{
token_info *ptinfo_beg = p->token_info;

if (!ptinfo_beg) return;
p->token_info = ptinfo_beg->next;

if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
ptinfo_beg->beg.column != beg_pos.column ||
strcmp(ptinfo_beg->token, token)) {
compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
beg_pos.lineno, beg_pos.column, token,
ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
ptinfo_beg->token);
}

ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
}

static void
token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
{
Expand Down
20 changes: 16 additions & 4 deletions test/ripper/test_parser_events.rb
Expand Up @@ -26,13 +26,21 @@ def compile_error(str)
end

def warning(str)
parse(str, :warning) {|e, *args| return args}
assert(false, "warning expected")
tree = parse(str, :warning) {|e, *args| return args}
if block_given?
yield tree
else
assert(false, "warning expected")
end
end

def warn(str)
parse(str, :warn) {|e, *args| return args}
assert(false, "warning expected")
tree = parse(str, :warn) {|e, *args| return args}
if block_given?
yield tree
else
assert(false, "warning expected")
end
end

def test_program
Expand Down Expand Up @@ -1552,6 +1560,10 @@ def test_warn_mismatched_indentations
fmt, tokend, tokbeg, line = assert_warning("") {break warn("if true\n end\n")}
assert_match(/mismatched indentations/, fmt)
assert_equal(["if", "end", 1], [tokbeg, tokend, line])
result = assert_warning("") {
warn("begin\n" " def f() = nil\n" "end\n") {break :ok}
}
assert_equal(:ok, result)
end

def test_in
Expand Down
11 changes: 6 additions & 5 deletions test/ruby/test_rubyoptions.rb
Expand Up @@ -494,16 +494,17 @@ def test_indentation_check
["case nil; when true", "end"],
["if false;", "end", "if true\nelse ", "end"],
["else", " end", "_ = if true\n"],
["begin\n def f() = nil", "end"],
].each do
|b, e = 'end', pre = nil, post = nil|
src = ["#{pre}#{b}\n", " #{e}\n#{post}"]
k = b[/\A\s*(\S+)/, 1]
e = e[/\A\s*(\S+)/, 1]
n = 2
n += pre.count("\n") if pre
n = 1 + src[0].count("\n")
n1 = 1 + (pre ? pre.count("\n") : 0)

a.for("no directives with #{src}") do
err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n-1}"]
err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n1}"]
t.rewind
t.truncate(0)
t.puts src
Expand All @@ -522,7 +523,7 @@ def test_indentation_check
end

a.for("false and true directives with #{src}") do
err = ["#{t.path}:#{n+2}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n+1}"]
err = ["#{t.path}:#{n+2}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n1+2}"]
t.rewind
t.truncate(0)
t.puts "# -*- warn-indent: false -*-"
Expand All @@ -544,7 +545,7 @@ def test_indentation_check
end

a.for("BOM with #{src}") do
err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n-1}"]
err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n1}"]
t.rewind
t.truncate(0)
t.print "\u{feff}"
Expand Down

0 comments on commit 3d8705d

Please sign in to comment.