1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* parse.y (parser_set_token_info): turn on/off with directives.

[ruby-core:25442]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29782 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2010-11-14 07:49:06 +00:00
parent f2dd4eb3cc
commit b5c3fb2465
3 changed files with 63 additions and 4 deletions

View file

@ -1,3 +1,8 @@
Sun Nov 14 16:48:56 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (parser_set_token_info): turn on/off with directives.
[ruby-core:25442]
Sun Nov 14 12:05:24 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (argf_readlines): forward to current_file for arguments

37
parse.y
View file

@ -247,6 +247,7 @@ struct parser_params {
VALUE coverage;
int nerr;
int parser_token_info_enabled;
token_info *parser_token_info;
#else
/* Ripper only */
@ -4966,7 +4967,7 @@ token_info_push(struct parser_params *parser, const char *token)
{
token_info *ptinfo;
if (compile_for_eval) return;
if (!parser->parser_token_info_enabled) return;
ptinfo = ALLOC(token_info);
ptinfo->token = token;
ptinfo->linenum = ruby_sourceline;
@ -4996,9 +4997,11 @@ token_info_pop(struct parser_params *parser, const char *token)
if (token_info_has_nonspaces(parser, token) || ptinfo->nonspc) { /* SKIP */
goto finish;
}
rb_compile_warning(ruby_sourcefile, linenum,
"mismatched indentations at '%s' with '%s' at %d",
token, ptinfo->token, ptinfo->linenum);
if (parser->parser_token_info_enabled) {
rb_compile_warn(ruby_sourcefile, linenum,
"mismatched indentations at '%s' with '%s' at %d",
token, ptinfo->token, ptinfo->linenum);
}
finish:
xfree(ptinfo);
@ -5137,6 +5140,9 @@ yycompile0(VALUE arg, int tracing)
parser_prepare(parser);
deferred_nodes = 0;
#ifndef RIPPER
parser->parser_token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
#endif
n = yyparse((void*)parser);
ruby_debug_lines = 0;
ruby_coverage = 0;
@ -6258,6 +6264,28 @@ magic_comment_encoding(struct parser_params *parser, const char *name, const cha
parser_set_encode(parser, val);
}
static void
parser_set_token_info(struct parser_params *parser, const char *name, const char *val)
{
int *p = &parser->parser_token_info_enabled;
switch (*val) {
case 't': case 'T':
if (strcasecmp(val, "true") == 0) {
*p = TRUE;
return;
}
break;
case 'f': case 'F':
if (strcasecmp(val, "false") == 0) {
*p = FALSE;
return;
}
break;
}
rb_compile_warning(ruby_sourcefile, ruby_sourceline, "invalid value for %s: %s", name, val);
}
struct magic_comment {
const char *name;
rb_magic_comment_setter_t func;
@ -6267,6 +6295,7 @@ struct magic_comment {
static const struct magic_comment magic_comments[] = {
{"coding", magic_comment_encoding, parser_encode_length},
{"encoding", magic_comment_encoding, parser_encode_length},
{"warn_indent", parser_set_token_info},
};
#endif

View file

@ -313,6 +313,31 @@ class TestRubyOptions < Test::Unit::TestCase
err = ["#{t.path}:2: warning: mismatched indentations at 'end' with 'begin' at 1"]
assert_in_out_err(["-w", t.path], "", [], err)
assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err)
t.open
t.puts "# -*- warn-indent: false -*-"
t.puts "begin"
t.puts " end"
t.close
assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 3"]
t.open
t.puts "# -*- warn-indent: false -*-"
t.puts "# -*- warn-indent: true -*-"
t.puts "begin"
t.puts " end"
t.close
assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]')
err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 2"]
t.open
t.puts "# -*- warn-indent: true -*-"
t.puts "begin"
t.puts "# -*- warn-indent: false -*-"
t.puts " end"
t.close
assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
ensure
t.close(true) if t
end