Commit e9e4c133 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2161 from nobu/embedded_document

Embedded document
parents 07b9f2de 8b13efc2
...@@ -3417,15 +3417,15 @@ skip(parser_state *p, char term) ...@@ -3417,15 +3417,15 @@ skip(parser_state *p, char term)
} }
} }
static mrb_bool static int
peek_n(parser_state *p, int c, int n) peekc_n(parser_state *p, int n)
{ {
node *list = 0; node *list = 0;
int c0; int c0;
do { do {
c0 = nextc(p); c0 = nextc(p);
if (c0 < 0) return FALSE; if (c0 < 0) return c0;
list = push(list, (node*)(intptr_t)c0); list = push(list, (node*)(intptr_t)c0);
} while(n--); } while(n--);
if (p->pb) { if (p->pb) {
...@@ -3434,8 +3434,13 @@ peek_n(parser_state *p, int c, int n) ...@@ -3434,8 +3434,13 @@ peek_n(parser_state *p, int c, int n)
else { else {
p->pb = list; p->pb = list;
} }
if (c0 == c) return TRUE; return c0;
return FALSE; }
static mrb_bool
peek_n(parser_state *p, int c, int n)
{
return peekc_n(p, n) == c && c >= 0;
} }
#define peek(p,c) peek_n((p), (c), 0) #define peek(p,c) peek_n((p), (c), 0)
...@@ -3454,7 +3459,7 @@ peeks(parser_state *p, const char *s) ...@@ -3454,7 +3459,7 @@ peeks(parser_state *p, const char *s)
} }
else else
#endif #endif
if (p->s && p->s + len >= p->send) { if (p->s && p->s + len <= p->send) {
if (memcmp(p->s, s, len) == 0) return TRUE; if (memcmp(p->s, s, len) == 0) return TRUE;
} }
return FALSE; return FALSE;
...@@ -3470,6 +3475,10 @@ skips(parser_state *p, const char *s) ...@@ -3470,6 +3475,10 @@ skips(parser_state *p, const char *s)
for (;;) { for (;;) {
c = nextc(p); c = nextc(p);
if (c < 0) return c; if (c < 0) return c;
if (c == '\n') {
p->lineno++;
p->column = 0;
}
if (c == *s) break; if (c == *s) break;
} }
s++; s++;
...@@ -3477,7 +3486,10 @@ skips(parser_state *p, const char *s) ...@@ -3477,7 +3486,10 @@ skips(parser_state *p, const char *s)
int len = strlen(s); int len = strlen(s);
while (len--) { while (len--) {
nextc(p); if (nextc(p) == '\n') {
p->lineno++;
p->column = 0;
}
} }
return TRUE; return TRUE;
} }
...@@ -4189,14 +4201,23 @@ parser_yylex(parser_state *p) ...@@ -4189,14 +4201,23 @@ parser_yylex(parser_state *p)
case '=': case '=':
if (p->column == 1) { if (p->column == 1) {
if (peeks(p, "begin ") || peeks(p, "begin\n")) { static const char begin[] = "begin";
if (skips(p, "\n=end ")) { static const char end[] = "\n=end";
goto retry; if (peeks(p, begin)) {
c = peekc_n(p, sizeof(begin)-1);
if (c < 0 || isspace(c)) {
do {
if (!skips(p, end)) {
yyerror(p, "embedded document meets end of file");
return 0;
} }
if (skips(p, "\n=end\n")) { c = nextc(p);
} while (!(c < 0 || isspace(c)));
if (c != '\n') skip(p, '\n');
p->lineno++;
p->column = 0;
goto retry; goto retry;
} }
goto retry;
} }
} }
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) {
......
...@@ -307,5 +307,9 @@ this is a comment that has =end with spaces after it ...@@ -307,5 +307,9 @@ this is a comment that has =end with spaces after it
=begin this is a comment =begin this is a comment
this is a comment that has extra after =begin and =end with spaces after it this is a comment that has extra after =begin and =end with spaces after it
=end =end
true line = __LINE__
=begin this is a comment
this is a comment that has extra after =begin and =end with tabs after it
=end xxxxxxxxxxxxxxxxxxxxxxxxxx
assert_equal(line + 4, __LINE__)
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment