Describe the bug
Defining valid functions (copied from a module that successfully compiled) fails with a syntax error when using triple-quoted strings together with escape sequences and square brackets.
I was plagued with syntax errors in the Erlang shell when copy-pasting functions from a module that successfully compiled. I think I have narrowed down the issue to the content of triple-quoted strings. More specifically, it fails when:
- there is a triple-quoted string
- containing a pair of square brackets and an even number of escape sequence
- the result of the expression containing the triple-quoted string is assigned to a variable.
The sigil being used (or no sigil) does not seem to impact the result.
To Reproduce
Short sample:
9> test_fails_1(String) ->
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
* 14:22: syntax error before: '\\'
Full sample:
The following module compiles without warnings or errors:
-module(test).
-export([
test_ok_1/1, test_ok_2/1, test_ok_3/1, test_ok_4/1, test_ok_5/1, test_ok_6/1,
test_fails_1/1, test_fails_2/1
]).
test_ok_1(String) ->
%% No '_Var' here. See test_fails_1.
re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
test_ok_2(String) ->
%% With _Var, but no square bracket pair in triple-quoted string.
_Var = re:replace(
String,
~B"""
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
""",
~"",
[unicode, global, {return, binary}]
).
test_ok_3(String) ->
%% With _Var, but no opening square bracket in triple-quoted string.
_Var = re:replace(
String,
~B"""
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
test_ok_4(String) ->
%% With _Var, square bracket pair, but uneven number of '\x{...}
%% in the triple-quoted string.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
test_ok_5(String) ->
%% With _Var, square bracket pair, even number of '\x{...}' but
%% a blank line.
%% Note the indentation of the blank line: it's the same
%% as the brackets.
%% Also note that the line above and below have 2 '\x{...}'.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{180B}-\x{180D}
]
""",
~"",
[unicode, global, {return, binary}]
).
test_ok_6(String) ->
%% With _Var, square bracket pair, uneven number of '\x{...}' and
%% a blank line. Note the indentation of the blank line: it's more than
%% the brackets, but the line above has a single '\x{...}'.
_Var = re:replace(
String,
~B"""
[
\x{00AD}
\x{180B}-\x{180D}
]
""",
~"",
[unicode, global, {return, binary}]
).
test_fails_1(String) ->
%% Same as test_ok_1, but with a _Var.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
test_fails_2(String) ->
%% Same as test_ok_5, but the extra blank line is indented more.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{180B}-\x{180D}
]
""",
~"",
[unicode, global, {return, binary}]
).
Eshell V17.0.2 (press Ctrl+G to abort, type help(). for help)
1> c(test).
{ok,test}
2> test:test_ok_1(~"la\x{00AD}la\x{FE05}la\x{180C}la").
<<"lalalala">>
Defining the test_ok_{1-6} functions in the shell succeeds, but defining test_fails_1 and test_fails_2 fails with a syntax error, even though they both compiled.
3> test_ok_1(String) ->
%% No '_Var' here. See test_fails_1.
re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
ok
4> test_ok_2(String) ->
%% With _Var, but no square bracket pair in triple-quoted string.
_Var = re:replace(
String,
~B"""
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
""",
~"",
[unicode, global, {return, binary}]
).
ok
5> test_ok_3(String) ->
%% With _Var, but no opening square bracket in triple-quoted string.
_Var = re:replace(
String,
~B"""
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
ok
6> test_ok_4(String) ->
%% With _Var, square bracket pair, but uneven number of '\x{...}
%% in the triple-quoted string.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
ok
7> test_ok_5(String) ->
%% With _Var, square bracket pair, even number of '\x{...}' but
%% a blank line.
%% Note the indentation of the blank line: it's the same
%% as the brackets.
%% Also note that the line above and below have 2 '\x{...}'.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{180B}-\x{180D}
]
""",
~"",
[unicode, global, {return, binary}]
).
ok
8> test_ok_6(String) ->
%% With _Var, square bracket pair, uneven number of '\x{...}' and
%% a blank line. Note the indentation of the blank line: it's more than
%% the brackets, but the line above has a single '\x{...}'.
_Var = re:replace(
String,
~B"""
[
\x{00AD}
\x{180B}-\x{180D}
]
""",
~"",
[unicode, global, {return, binary}]
).
ok
9> test_fails_1(String) ->
%% Same as test_ok_1, but with a _Var.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{034F}
\x{180B}-\x{180D}
\x{FE00}-\x{FE0F}
\x{FFFC}
]
""",
~"",
[unicode, global, {return, binary}]
).
* 14:22: syntax error before: '\\'
9> test_fails_2(String) ->
%% Same as test_ok_5, but the extra blank line is indented more.
_Var = re:replace(
String,
~B"""
[
\x{00AD}\x{1806}
\x{180B}-\x{180D}
]
""",
~"",
[unicode, global, {return, binary}]
).
* 12:22: syntax error before: '\\'
Expected behavior
Copy-pasting a function from a successfully compiling module to the shell should succeed (assuming that the function being defined does not rely on macros, imports or other external requirements).
Affected versions
Tested on 29.0.2 only.
Additional context
Describe the bug
Defining valid functions (copied from a module that successfully compiled) fails with a syntax error when using triple-quoted strings together with escape sequences and square brackets.
I was plagued with syntax errors in the Erlang shell when copy-pasting functions from a module that successfully compiled. I think I have narrowed down the issue to the content of triple-quoted strings. More specifically, it fails when:
The sigil being used (or no sigil) does not seem to impact the result.
To Reproduce
Short sample:
Full sample:
The following module compiles without warnings or errors:
Defining the
test_ok_{1-6}functions in the shell succeeds, but definingtest_fails_1andtest_fails_2fails with a syntax error, even though they both compiled.Expected behavior
Copy-pasting a function from a successfully compiling module to the shell should succeed (assuming that the function being defined does not rely on macros, imports or other external requirements).
Affected versions
Tested on 29.0.2 only.
Additional context