Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
8286e605
Commit
8286e605
authored
Jan 03, 2021
by
Seeker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for escaped newlines in squiggly heredocs
parent
c52efe7b
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
1852 additions
and
1326 deletions
+1852
-1326
mrbgems/mruby-compiler/core/parse.y
mrbgems/mruby-compiler/core/parse.y
+101
-35
mrbgems/mruby-compiler/core/y.tab.c
mrbgems/mruby-compiler/core/y.tab.c
+1741
-1291
test/t/literals.rb
test/t/literals.rb
+10
-0
No files found.
mrbgems/mruby-compiler/core/parse.y
View file @
8286e605
...
...
@@ -4625,6 +4625,93 @@ read_escape(parser_state *p)
}
}
static void
heredoc_count_indent(parser_heredoc_info *hinf, const char *str, size_t len, size_t *indent, size_t *offset)
{
*indent = 0;
*offset = 0;
for (size_t i = 0; i < len; i++) {
size_t size;
if (str[i] == '\n')
break;
else if (str[i] == '\t')
size = 8;
else if (ISSPACE(str[i]))
size = 1;
else
break;
if (*indent + size > hinf->indent)
break;
*indent += size;
*offset += 1;
}
}
static void
heredoc_remove_indent(parser_state *p, parser_heredoc_info *hinf)
{
if (!hinf->remove_indent)
return;
node *indented, *n, *pair, *escaped;
const char *str;
size_t len, indent, offset, start, end;
indented = hinf->indented;
while (indented) {
n = indented->car;
pair = n->car;
str = (char*)pair->car;
len = (size_t)pair->cdr;
escaped = n->cdr;
if (escaped) {
start = 0;
while (start < len) {
end = escaped ? (size_t)escaped->car : len;
heredoc_count_indent(hinf, str + start, end - start, &indent, &offset);
if (indent < hinf->indent)
hinf->indent = indent;
start = end;
if (escaped)
escaped = escaped->cdr;
}
}
indented = indented->cdr;
}
if (hinf->indent == 0)
return;
indented = hinf->indented;
while (indented) {
n = indented->car;
pair = n->car;
str = (char*)pair->car;
len = (size_t)pair->cdr;
escaped = n->cdr;
if (escaped) {
char *newstr = strndup(str, len);
size_t newlen = 0;
start = 0;
while (start < len) {
end = escaped ? (size_t)escaped->car : len;
size_t esclen = end - start;
heredoc_count_indent(hinf, str + start, esclen, &indent, &offset);
esclen -= offset;
memcpy(newstr + newlen, str + start + offset, esclen);
newlen += esclen;
start = end;
if (escaped)
escaped = escaped->cdr;
}
newstr[newlen] = '\0';
pair->car = (node*)newstr;
pair->cdr = (node*)newlen;
} else {
heredoc_count_indent(hinf, str, len, &indent, &offset);
pair->car = (node*)(str + offset);
pair->cdr = (node*)(len - offset);
}
indented = indented->cdr;
}
}
static int
parse_string(parser_state *p)
{
...
...
@@ -4635,14 +4722,18 @@ parse_string(parser_state *p)
int end = intn(p->lex_strterm->cdr->cdr->cdr);
parser_heredoc_info *hinf = (type & STR_FUNC_HEREDOC) ? parsing_heredoc_inf(p) : NULL;
mrb_bool unindent = hinf && hinf->remove_indent;
mrb_bool head = hinf && hinf->line_head;
mrb_bool empty = TRUE;
size_t spaces = 0;
size_t pos = -1;
node *escaped = NULL;
if (beg == 0) beg = -3; /* should never happen */
if (end == 0) end = -3;
newtok(p);
while ((c = nextc(p)) != end || nest_level != 0) {
pos++;
if (hinf && (c == '\n' || c < 0)) {
mrb_bool line_head;
tokadd(p, '\n');
...
...
@@ -4662,36 +4753,7 @@ parse_string(parser_state *p)
}
}
if ((len-1 == hinf->term_len) && (strncmp(s, hinf->term, len-1) == 0)) {
if (hinf->remove_indent && hinf->indent > 0) {
node *tmp = hinf->indented;
while (tmp) {
node *pair = tmp->car;
const char *str = (char*)pair->car;
size_t len = (size_t)pair->cdr;
size_t indent = 0;
size_t offset = 0;
for (size_t i = 0; i < len; i++) {
size_t size;
if (str[i] == '\n')
break;
else if (str[i] == '\t')
size = 8;
else if (ISSPACE(str[i]))
size = 1;
else
break;
if (indent + size > hinf->indent)
break;
indent += size;
++offset;
}
if (indent > 0) {
pair->car = (node*)(str + offset);
pair->cdr = (node*)(len - offset);
}
tmp = tmp->cdr;
}
}
heredoc_remove_indent(p, hinf);
return tHEREDOC_END;
}
}
...
...
@@ -4712,14 +4774,14 @@ parse_string(parser_state *p)
}
node *nd = new_str(p, tok(p), toklen(p));
pylval.nd = nd;
if (head) {
hinf->indented = push(hinf->indented,
nd->cdr
);
if (
unindent &&
head) {
hinf->indented = push(hinf->indented,
cons(nd->cdr, escaped)
);
if ((hinf->indent == -1 || spaces < hinf->indent) && (!empty || !line_head))
hinf->indent = spaces;
}
return tHD_STRING_MID;
}
if (
hinf && hinf->line_
head && empty) {
if (
unindent &&
head && empty) {
if (c == '\t')
spaces += 8;
else if (ISSPACE(c))
...
...
@@ -4748,6 +4810,10 @@ parse_string(parser_state *p)
else if (c == '\n') {
p->lineno++;
p->column = 0;
if (unindent) {
escaped = push(escaped, (node*)pos);
pos--;
}
if (type & STR_FUNC_ARRAY) {
tokadd(p, '\n');
}
...
...
@@ -4800,8 +4866,8 @@ parse_string(parser_state *p)
node *nd = new_str(p, tok(p), toklen(p));
pylval.nd = nd;
if (hinf) {
if (head) {
hinf->indented = push(hinf->indented,
nd->cdr
);
if (
unindent &&
head) {
hinf->indented = push(hinf->indented,
cons(nd->cdr, escaped)
);
if (hinf->indent == -1 || spaces < hinf->indent)
hinf->indent = spaces;
}
...
...
mrbgems/mruby-compiler/core/y.tab.c
View file @
8286e605
This diff is collapsed.
Click to expand it.
test/t/literals.rb
View file @
8286e605
...
...
@@ -194,6 +194,14 @@ QQ2
v v v
vvv
VVV
v4 = <<~VVV
vvv \
vvv
VVV
v5 = <<~VVV
vvv \
vvv
VVV
w = %W( 1
#{
<<
WWW
}
3
www
...
...
@@ -237,6 +245,8 @@ ZZZ
assert_equal "
\n
vvv
\n
vvv
\n
", v1
assert_equal "
\t
vvv
\n
vvv
\n
", v2
assert_equal "v v v
\n
vvv
\n
", v3
assert_equal "vvv vvv
\n
", v4
assert_equal " vvv vvv
\n
", v5
assert_equal ["1", "www
\n
", "3", "4", "5"], w
assert_equal [1, "foo 222 333
\n
444
\n
5
\n
bar
\n
6
\n
", 9], x
assert_equal "", z
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment