commit 8efcd857154b25a82a1f3be300a01944206493c9
parent c00565ef073690cf14fe1aa89bd8565abe521749
Author: Sebastian <sebastian@sebsite.pw>
Date: Sat, 19 Feb 2022 19:16:20 -0500
lex: add support for `raw strings`
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/lex.c b/src/lex.c
@@ -544,13 +544,15 @@ static enum lexical_token
lex_string(struct lexer *lexer, struct token *out)
{
uint32_t c = next(lexer, &out->loc, false);
+ uint32_t delim;
assert(c != UTF8_INVALID);
switch (c) {
case '"':
+ case '`':
+ delim = c;
while ((c = next(lexer, NULL, false)) != UTF8_INVALID) {
- switch (c) {
- case '"':;
+ if (c == delim) {
char *buf = xcalloc(lexer->buflen, 1);
memcpy(buf, lexer->buf, lexer->buflen);
out->token = T_LITERAL;
@@ -559,9 +561,11 @@ lex_string(struct lexer *lexer, struct token *out)
out->string.value = buf;
consume(lexer, -1);
return out->token;
- default:
+ } else {
push(lexer, c, false);
- push(lexer, lex_rune(lexer), false);
+ if (delim == '"') {
+ push(lexer, lex_rune(lexer), false);
+ }
next(lexer, NULL, true);
}
}
@@ -899,6 +903,7 @@ _lex(struct lexer *lexer, struct token *out)
char p[5];
switch (c) {
case '"':
+ case '`':
case '\'':
push(lexer, c, false);
return lex_string(lexer, out);
diff --git a/tests/04-strings.ha b/tests/04-strings.ha
@@ -57,9 +57,14 @@ fn equality() void = {
assert("foo\0bar" != "foo\0foo");
};
+fn raw() void = {
+ assert(`hello \" world` == "hello \\\" world");
+};
+
export fn main() void = {
measurements();
storage();
concat();
equality();
+ raw();
};