Fix newline folding test, and newline folding replacement (not replaced yet but brace tests will cover it)

This commit is contained in:
Tangent Wantwight 2023-08-06 20:18:38 -04:00
parent b8d48ef834
commit f3eb9dee6d
2 changed files with 8 additions and 10 deletions

View file

@ -24,12 +24,16 @@ describe("Parsing Notcl", () => {
parse(String.raw`a\
b`)
).toEqual([true, [[{ text: "a" }, { text: "b" }]]]));
it.failing("does split commands on newlines with escaped backslashes", () =>
it("does split words on folded newlines", () =>
expect(
parse(String.raw`a\
b`)
).toEqual([true, [[{ text: "a" }, { text: "b" }]]]));
it("does split commands on newlines with escaped backslashes", () =>
expect(
parse(String.raw`a\\
b`)
).toEqual([true, [[{ text: "a" }], [{ text: "b" }]]])
);
).toEqual([true, [[{ text: "a\\\\" }], [{ text: "b" }]]]));
it("does not split commands on folded newlines with escaped backslashes", () =>
expect(
parse(String.raw`a\\\

View file

@ -7,22 +7,16 @@ export type Word = {
export type Command = Word[];
export type Script = Command[];
const InterCommandWhitespace = Regex(/\s+/y).expects("whitespace");
const Comment = Regex(/#[^\n]*/y)
.expects("#")
.map(() => []);
const PreCommand = AtLeast(0, InterCommandWhitespace);
const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace");
const BasicWord = Regex(/(?!\{)[^\s;]+/y)
.map(([word]) => ({ text: word }))
.expects("BASIC_WORD");
// WIP, need to be able to escape braces correctly
const Brace: Pattern<string> = Sequence(
Regex(/\{/y).expects("{"),
AtLeast(
@ -84,7 +78,7 @@ const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;
export function parse(code: string): [true, Script] | [false, string] {
/* Preprocess */
// fold line endings
code = code.replace(/(?<!\\)((\\\\)*)\\\n/g, "$1");
code = code.replace(/(?<!\\)((\\\\)*)\\\n[ \t]*/g, "$1 ");
/* Parse */
const [commands, errorPos, expected] = Script(code, 0);