Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions src/asm/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1448,30 +1448,57 @@ static void readString(void)
dbgPrint("Reading string\n");
lexerState->disableMacroArgs = true;
lexerState->disableInterpolation = true;

bool multiline = false;

if (peek(0) == '"') {
shiftChars(1);
if (peek(0) == '"') {
/* """ begins a multi-line string */
shiftChars(1);
multiline = true;
} else {
/* "" is an empty string */
goto finish;
}
}

for (;;) {
int c = peek(0);

if (c == '\r' || c == '\n') {
if (!multiline) {
/* '\r' or '\n' ends a single-line string early */
c = EOF;
} else if (c == '\r' && peek(1) == '\n') {
/* '\r\n' becomes '\n' in multi-line strings */
shiftChars(1);
c = '\n';
}
}

switch (c) {
case '"':
shiftChars(1);
if (multiline) {
/* """ ends a multi-line string */
if (peek(1) != '"' || peek(2) != '"')
break;
shiftChars(3);
} else {
shiftChars(1);
}
if (i == sizeof(yylval.tzString)) {
i--;
warning(WARNING_LONG_STR, "String constant too long\n");
}
yylval.tzString[i] = '\0';
dbgPrint("Read string \"%s\"\n", yylval.tzString);
goto finish;

case '\r':
case '\n': /* Do not shift these! */
case EOF:
if (i == sizeof(yylval.tzString)) {
i--;
warning(WARNING_LONG_STR, "String constant too long\n");
}
yylval.tzString[i] = '\0';
error("Unterminated string\n");
dbgPrint("Read string \"%s\"\n", yylval.tzString);
goto finish;

case '\\': /* Character escape or macro arg */
Expand Down Expand Up @@ -1553,6 +1580,9 @@ static void readString(void)
}

finish:
yylval.tzString[i] = '\0';

dbgPrint("Read string \"%s\"\n", yylval.tzString);
lexerState->disableMacroArgs = false;
lexerState->disableInterpolation = false;
}
Expand Down
8 changes: 8 additions & 0 deletions src/asm/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ There are a number of escape sequences you can use within a string:
.El
(Note that some of those can be used outside of strings, when noted further in this document.)
.Pp
Multi-line strings are contained in triple quotes
.Pq Ql \&"\&"\&"for instance\&"\&"\&" .
Escape sequences work the same way in multi-line strings; however, literal newline
characters will be included as-is, without needing to escape them with
.Ql \[rs]r
or
.Ql \[rs]n .
.Pp
A funky feature is
.Ql {symbol}
within a string, called
Expand Down
32 changes: 32 additions & 0 deletions test/asm/multi-line-strings.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
S EQUS "Hello"

PRINTT "\"\"\"\n"

PRINTT """{S}
world
"""

PRINTT """The multi-line string \ ; line continuations work
can contain:
- "single quotes"
- ""double quotes""
- even escaped \"""triple"\"" ""\"quotes\"\"\"
!"""

PRINTT """\n"""

printarg: MACRO
PRINTT "arg <\1>\n"
PRINTT """arg (\1)\n"""
ENDM

printarg "
printarg """

EMPTY1 EQUS ""
EMPTY2 EQUS "\ ; comment
"
EMPTY3 EQUS """"""
EMPTY4 EQUS """\ ; comment
"""
PRINTT STRCAT("(", "{EMPTY1}", "{EMPTY2}", "{EMPTY3}", "{EMPTY4}", ")\n")
Empty file added test/asm/multi-line-strings.err
Empty file.
13 changes: 13 additions & 0 deletions test/asm/multi-line-strings.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Hello
world
The multi-line string can contain:
- "single quotes"
- ""double quotes""
- even escaped """triple""" """quotes"""
!
arg <">
arg (")
arg <""">
arg (""")
()