diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-22 23:32:44 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-22 23:32:44 +0000 |
commit | 4992c02095b53e69cceb925e1487e91228dd6972 (patch) | |
tree | c8bfa32ef6aa2820e404fe0c0327e1bffbfa15b3 /parse.y | |
parent | 200d9c06a15ae8581903457f77b8dc4d6c897395 (diff) | |
download | ruby-4992c02095b53e69cceb925e1487e91228dd6972.tar.gz |
parse.y: warn ? followed by a word
* parse.y (parse_qmark): warn `?` with unbalanced space, which is
interpreted as a conditional operator.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56877 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'parse.y')
-rw-r--r-- | parse.y | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -775,6 +775,7 @@ static VALUE parser_heredoc_dedent(struct parser_params*,VALUE); # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d)) #ifdef RIPPER static ID id_warn, id_warning; +# define WARN_S_L(s,l) STR_NEW(s,l) # define WARN_S(s) STR_NEW2(s) # define WARN_I(i) INT2NUM(i) # define PRIsWARN "s" @@ -788,6 +789,7 @@ static void ripper_compile_error(struct parser_params*, const char *fmt, ...); # define compile_error ripper_compile_error # define PARSER_ARG parser, #else +# define WARN_S_L(s,l) s # define WARN_S(s) s # define WARN_I(i) i # define PRIsWARN PRIsVALUE @@ -7468,7 +7470,7 @@ parse_numeric(struct parser_params *parser, int c) } static int -parse_qmark(struct parser_params *parser) +parse_qmark(struct parser_params *parser, int space_seen) { rb_encoding *enc; register int c; @@ -7521,6 +7523,16 @@ parse_qmark(struct parser_params *parser) } else if ((rb_enc_isalnum(c, current_enc) || c == '_') && lex_p < lex_pend && is_identchar(lex_p, lex_pend, current_enc)) { + if (space_seen) { + const char *start = lex_p - 1, *p = start; + do { + int n = rb_enc_precise_mbclen(p, lex_pend, current_enc); + p += n; + } while (p < lex_pend && is_identchar(p, lex_pend, current_enc)); + rb_warn2("`?' just followed by `%.*s' is interpreted as" \ + " a conditional operator, put a space after `?'", + WARN_I((int)(p - start)), WARN_S_L(start, (p - start))); + } goto ternary; } else if (c == '\\') { @@ -8267,7 +8279,7 @@ parser_yylex(struct parser_params *parser) return tSTRING_BEG; case '?': - return parse_qmark(parser); + return parse_qmark(parser, space_seen); case '&': if ((c = nextc()) == '&') { |