Start matching {braces} and nested braces

This commit is contained in:
Tangent Wantwight 2023-07-29 13:41:47 -04:00
parent 88497965b3
commit 9333357e44

View file

@ -21,8 +21,40 @@ var Notcl = (() => {
text: word,
}));
// WIP, need to be able to escape braces correctly
// WIP, error if anything after closing brace
const BracePattern = Peg.Map(
Peg.Sequence(
Peg.Regex(/\{/y),
Peg.AtLeast(
0,
Peg.Choose(
Peg.Map(Brace, (text) => `{${text}}`),
Peg.Map(Peg.Regex(/[^{}]+/y), ([text]) => text)
)
),
Peg.Regex(/\}/y)
),
([_left, fragments, _right]) => fragments.join("")
);
/**
* @type {Peg.Pattern<string>}
*/
function Brace(source, index) {
// Thunk to allow Brace to recurse
return BracePattern(source, index);
}
const Word = Peg.Map(
Peg.Sequence(PreWordWhitespace, BasicWord),
Peg.Sequence(
PreWordWhitespace,
Peg.Choose(
Peg.Map(BracePattern, (text) => ({
text,
})),
BasicWord
)
),
([_, word]) => word
);