Skip to content

Commit 35ca72a

Browse files
committed
{feature} add special case for "r" to improve error message
Adding special handling for the "r" allows detecting `r#""#` style raw strings, which are no longer valid in KDL v2. Related to #11
1 parent fa2a9f6 commit 35ca72a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/parser/tokenize/tokenize.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,34 @@ export function handleNumberCharacter(ctx) {
540540
}
541541
}
542542

543+
/** @param {TokenizeContext} ctx */
544+
function handleR(ctx) {
545+
pop(ctx);
546+
547+
if (ctx.current != 0x23 /* # */) {
548+
zerOrMore(ctx, isIdentifierChar);
549+
return mkToken(ctx, T_IDENTIFIER_STRING);
550+
}
551+
552+
const token = handleHashCharacter(ctx);
553+
token.start.offset--;
554+
token.start.column--;
555+
556+
let message;
557+
switch (token.type) {
558+
case T_RAW_STRING:
559+
case T_MULTILINE_RAW_STRING:
560+
message = 'Invalid raw string, the correct syntax is #" "#, not r#" "#';
561+
break;
562+
563+
default:
564+
message = "Invalid token, did you forget a whitespace after this r?";
565+
}
566+
567+
(token.errors ??= []).push(new InvalidKdlError(message, {token}));
568+
return token;
569+
}
570+
543571
/** @param {TokenizeContext} ctx */
544572
export function handleIdentifierCharacter(ctx) {
545573
pop(ctx);
@@ -596,6 +624,7 @@ characterHandlers[0x3d] = createSingleCharacterToken(T_EQUALS); // =
596624
characterHandlers[0x5b] = handleInvalidCharacter; // [
597625
characterHandlers[0x5c] = createSingleCharacterToken(T_ESCLINE); // \
598626
characterHandlers[0x5d] = handleInvalidCharacter; // ]
627+
characterHandlers[0x72] = handleR;
599628
characterHandlers[0x7b] = createSingleCharacterToken(T_OPEN_BRACE); // {
600629
characterHandlers[0x7d] = createSingleCharacterToken(T_CLOSE_BRACE); // }
601630
characterHandlers[0x85] = handleNewlineCharacter; // Next Line

test/issues.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,8 @@ test("issue kdl-org/kdl#502: formatting outputs invalid raw unicode data", () =>
9494
);
9595
});
9696

97+
test("issue #11: v1 style raw strings", () => {
98+
assert.throws(() => parse('/-node {a x=r#"123"#}'), /Invalid raw string/);
99+
});
100+
97101
test.run();

0 commit comments

Comments
 (0)