mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* parse.y (magic_comment_encoding): warns when ignored.
* parse.y (parser_magic_comment): replaces '-' with '_'. * parse.y (parser_yylex): allows magic comments indented and the second line or later. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b4ae9ed9b0
commit
a77d088cf6
2 changed files with 37 additions and 9 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
Fri Oct 10 19:21:50 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* parse.y (magic_comment_encoding): warns when ignored.
|
||||||
|
|
||||||
|
* parse.y (parser_magic_comment): replaces '-' with '_'.
|
||||||
|
|
||||||
|
* parse.y (parser_yylex): allows magic comments indented and the
|
||||||
|
second line or later.
|
||||||
|
|
||||||
Fri Oct 10 17:26:50 2008 NARUSE, Yui <naruse@ruby-lang.org>
|
Fri Oct 10 17:26:50 2008 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* ext/json/ext/parser/parser.c (JSON_parse_string):
|
* ext/json/ext/parser/parser.c (JSON_parse_string):
|
||||||
|
|
37
parse.y
37
parse.y
|
@ -5166,6 +5166,7 @@ parser_str_new(const char *p, long n, rb_encoding *enc, int func, rb_encoding *e
|
||||||
}
|
}
|
||||||
|
|
||||||
#define lex_goto_eol(parser) (parser->parser_lex_p = parser->parser_lex_pend)
|
#define lex_goto_eol(parser) (parser->parser_lex_p = parser->parser_lex_pend)
|
||||||
|
#define peek(c) (lex_p < lex_pend && (c) == *lex_p)
|
||||||
|
|
||||||
static inline int
|
static inline int
|
||||||
parser_nextc(struct parser_params *parser)
|
parser_nextc(struct parser_params *parser)
|
||||||
|
@ -5216,7 +5217,7 @@ parser_nextc(struct parser_params *parser)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c = (unsigned char)*lex_p++;
|
c = (unsigned char)*lex_p++;
|
||||||
if (c == '\r' && lex_p < lex_pend && *lex_p == '\n') {
|
if (c == '\r' && peek('\n')) {
|
||||||
lex_p++;
|
lex_p++;
|
||||||
c = '\n';
|
c = '\n';
|
||||||
}
|
}
|
||||||
|
@ -5235,7 +5236,6 @@ parser_pushback(struct parser_params *parser, int c)
|
||||||
}
|
}
|
||||||
|
|
||||||
#define was_bol() (lex_p == lex_pbeg + 1)
|
#define was_bol() (lex_p == lex_pbeg + 1)
|
||||||
#define peek(c) (lex_p != lex_pend && (c) == *lex_p)
|
|
||||||
|
|
||||||
#define tokfix() (tokenbuf[tokidx]='\0')
|
#define tokfix() (tokenbuf[tokidx]='\0')
|
||||||
#define tok() tokenbuf
|
#define tok() tokenbuf
|
||||||
|
@ -6069,11 +6069,26 @@ parser_set_encode(struct parser_params *parser, const char *name)
|
||||||
typedef int (*rb_magic_comment_length_t)(struct parser_params *parser, const char *name, int len);
|
typedef int (*rb_magic_comment_length_t)(struct parser_params *parser, const char *name, int len);
|
||||||
typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val);
|
typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val);
|
||||||
|
|
||||||
|
static int
|
||||||
|
comment_at_top(struct parser_params *parser)
|
||||||
|
{
|
||||||
|
const char *p = lex_pbeg, *pend = lex_p - 1;
|
||||||
|
if (parser->line_count != (parser->has_shebang ? 2 : 1)) return 0;
|
||||||
|
while (p < pend) {
|
||||||
|
if (!ISSPACE(*p)) return 0;
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
magic_comment_encoding(struct parser_params *parser, const char *name, const char *val)
|
magic_comment_encoding(struct parser_params *parser, const char *name, const char *val)
|
||||||
{
|
{
|
||||||
if (parser->line_count != (parser->has_shebang ? 2 : 1))
|
if (!comment_at_top(parser)) {
|
||||||
|
rb_warning("encoding '%s' is ignored, valid only in the first line except for shebang line.",
|
||||||
|
val);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
parser_set_encode(parser, val);
|
parser_set_encode(parser, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6143,7 +6158,8 @@ parser_magic_comment(struct parser_params *parser, const char *str, int len)
|
||||||
#ifndef RIPPER
|
#ifndef RIPPER
|
||||||
const struct magic_comment *p = magic_comments;
|
const struct magic_comment *p = magic_comments;
|
||||||
#endif
|
#endif
|
||||||
int n = 0;
|
char *s;
|
||||||
|
int i, n = 0;
|
||||||
|
|
||||||
for (; len > 0 && *str; str++, --len) {
|
for (; len > 0 && *str; str++, --len) {
|
||||||
switch (*str) {
|
switch (*str) {
|
||||||
|
@ -6189,15 +6205,19 @@ parser_magic_comment(struct parser_params *parser, const char *str, int len)
|
||||||
|
|
||||||
n = end - beg;
|
n = end - beg;
|
||||||
str_copy(name, beg, n);
|
str_copy(name, beg, n);
|
||||||
|
s = RSTRING_PTR(name);
|
||||||
|
for (i = 0; i < n; ++i) {
|
||||||
|
if (*s == '-') *s = '_';
|
||||||
|
}
|
||||||
#ifndef RIPPER
|
#ifndef RIPPER
|
||||||
do {
|
do {
|
||||||
if (STRNCASECMP(p->name, RSTRING_PTR(name), n) == 0) {
|
if (STRNCASECMP(p->name, s, n) == 0) {
|
||||||
n = vend - vbeg;
|
n = vend - vbeg;
|
||||||
if (p->length) {
|
if (p->length) {
|
||||||
n = (*p->length)(parser, vbeg, n);
|
n = (*p->length)(parser, vbeg, n);
|
||||||
}
|
}
|
||||||
str_copy(val, vbeg, n);
|
str_copy(val, vbeg, n);
|
||||||
(*p->func)(parser, RSTRING_PTR(name), RSTRING_PTR(val));
|
(*p->func)(parser, s, RSTRING_PTR(val));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (++p < magic_comments + sizeof(magic_comments) / sizeof(*p));
|
} while (++p < magic_comments + sizeof(magic_comments) / sizeof(*p));
|
||||||
|
@ -6344,9 +6364,8 @@ parser_yylex(struct parser_params *parser)
|
||||||
|
|
||||||
case '#': /* it's a comment */
|
case '#': /* it's a comment */
|
||||||
/* no magic_comment in shebang line */
|
/* no magic_comment in shebang line */
|
||||||
if (parser->line_count == (parser->has_shebang ? 2 : 1)
|
if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) {
|
||||||
&& (lex_p - lex_pbeg) == 1) {
|
if (comment_at_top(parser)) {
|
||||||
if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) {
|
|
||||||
set_file_encoding(parser, lex_p, lex_pend);
|
set_file_encoding(parser, lex_p, lex_pend);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue