@compmd/lezer 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -2
- package/dist/cel-parse.d.ts +16 -0
- package/dist/cel-parse.js +235 -0
- package/dist/cel-parser.d.ts +15 -0
- package/dist/cel-parser.js +46 -0
- package/dist/cel-tokens.d.ts +24 -0
- package/dist/cel-tokens.js +78 -0
- package/dist/compmd.d.ts +14 -0
- package/dist/compmd.js +42 -0
- package/dist/component-line.d.ts +17 -0
- package/dist/component-line.js +167 -0
- package/dist/component-parts.d.ts +11 -0
- package/dist/component-parts.js +51 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.js +3 -0
- package/dist/definition.d.ts +3 -0
- package/dist/definition.js +118 -0
- package/dist/fallback.d.ts +10 -0
- package/dist/fallback.js +31 -0
- package/dist/frontmatter.d.ts +3 -0
- package/dist/frontmatter.js +38 -0
- package/dist/highlighting.d.ts +12 -0
- package/dist/highlighting.js +84 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/inline-component.d.ts +3 -0
- package/dist/inline-component.js +36 -0
- package/dist/interpolation.d.ts +6 -0
- package/dist/interpolation.js +44 -0
- package/dist/math.d.ts +10 -0
- package/dist/math.js +59 -0
- package/dist/nodes.d.ts +20 -0
- package/dist/nodes.js +101 -0
- package/dist/params.d.ts +33 -0
- package/dist/params.js +151 -0
- package/dist/syntax.d.ts +52 -0
- package/dist/syntax.js +52 -0
- package/dist/wiki-link.d.ts +3 -0
- package/dist/wiki-link.js +25 -0
- package/package.json +38 -13
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Interpolation, `{{path}}` (spec §8.1): in text, in a fallback and in a quoted param string. The form is
|
|
2
|
+
// `syntax/patterns.js`'s.
|
|
3
|
+
import { PATTERNS } from "./syntax.js";
|
|
4
|
+
const BRACE = 123;
|
|
5
|
+
/**
|
|
6
|
+
* An interpolation's form: `{{`, a path of CEL identifiers with white space around it allowed, `}}`. Anything else
|
|
7
|
+
* stays text (W106).
|
|
8
|
+
*/
|
|
9
|
+
const INTERPOLATION = new RegExp(`^${PATTERNS.interpolation}`);
|
|
10
|
+
/** The interpolation at the start of `text`: its length, and where its path is. */
|
|
11
|
+
function interpolationAt(text) {
|
|
12
|
+
const match = INTERPOLATION.exec(text);
|
|
13
|
+
if (!match)
|
|
14
|
+
return undefined;
|
|
15
|
+
const [, open, before, path] = match;
|
|
16
|
+
const pathStart = open.length + before.length;
|
|
17
|
+
return { length: match[0].length, path: [pathStart, pathStart + path.length] };
|
|
18
|
+
}
|
|
19
|
+
/** The `Interpolation` element for the interpolation at `from`. */
|
|
20
|
+
function interpolationElement(cx, from, found) {
|
|
21
|
+
const to = from + found.length;
|
|
22
|
+
return cx.elt("Interpolation", from, to, [
|
|
23
|
+
cx.elt("InterpolationMark", from, from + 2),
|
|
24
|
+
cx.elt("InterpolationExpression", from + found.path[0], from + found.path[1]),
|
|
25
|
+
cx.elt("InterpolationMark", to - 2, to),
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
/** The interpolations in a quoted param string `text`, which starts at document position `base`. `\{{` isn't one. */
|
|
29
|
+
export function interpolationsIn(cx, text, base) {
|
|
30
|
+
const found = [];
|
|
31
|
+
for (const match of text.matchAll(/(?<!\\)\{\{/g)) {
|
|
32
|
+
const interpolation = interpolationAt(text.slice(match.index));
|
|
33
|
+
if (interpolation)
|
|
34
|
+
found.push(interpolationElement(cx, base + match.index, interpolation));
|
|
35
|
+
}
|
|
36
|
+
return found;
|
|
37
|
+
}
|
|
38
|
+
/** The inline parser: `{{path}}` in text and fallbacks. An escaped `\{{` is left to Markdown's escapes. */
|
|
39
|
+
export function parseInterpolation(cx, next, pos) {
|
|
40
|
+
if (next !== BRACE || cx.char(pos + 1) !== BRACE)
|
|
41
|
+
return -1;
|
|
42
|
+
const interpolation = interpolationAt(cx.slice(pos, cx.end));
|
|
43
|
+
return interpolation ? cx.addElement(interpolationElement(cx, pos, interpolation)) : -1;
|
|
44
|
+
}
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BlockContext, InlineContext, Line } from "@lezer/markdown";
|
|
2
|
+
/**
|
|
3
|
+
* The inline parser, by Pandoc's rule: the opener is followed by a non-space, the closer preceded by one and not
|
|
4
|
+
* followed by a digit, braces group (a `$` inside them doesn't close). So "costs $5 and $10" stays text.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseInlineMath(cx: InlineContext, next: number, pos: number): number;
|
|
7
|
+
/** The block parser: from a line starting with `$$` to the first line ending with `$$`, which may be the same line. */
|
|
8
|
+
export declare function parseMathBlock(cx: BlockContext, line: Line): boolean;
|
|
9
|
+
/** Ends a paragraph at a `$$` line. */
|
|
10
|
+
export declare const endLeafAtMathBlock: (_cx: BlockContext, line: Line) => boolean;
|
package/dist/math.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Math (spec §2.3): inline `$…$` by Pandoc's rule, and display math on `$$` lines.
|
|
2
|
+
const DOLLAR = 36;
|
|
3
|
+
const BACKSLASH = 92;
|
|
4
|
+
const OPEN_BRACE = 123;
|
|
5
|
+
const CLOSE_BRACE = 125;
|
|
6
|
+
const isSpace = (code) => code === 32 || code === 9 || code === 10 || code === 13 || code === -1;
|
|
7
|
+
const isDigit = (code) => code >= 48 && code <= 57;
|
|
8
|
+
/**
|
|
9
|
+
* The inline parser, by Pandoc's rule: the opener is followed by a non-space, the closer preceded by one and not
|
|
10
|
+
* followed by a digit, braces group (a `$` inside them doesn't close). So "costs $5 and $10" stays text.
|
|
11
|
+
*/
|
|
12
|
+
export function parseInlineMath(cx, next, pos) {
|
|
13
|
+
if (next !== DOLLAR || cx.char(pos + 1) === DOLLAR || isSpace(cx.char(pos + 1)))
|
|
14
|
+
return -1;
|
|
15
|
+
let depth = 0;
|
|
16
|
+
for (let at = pos + 1; at < cx.end; at++) {
|
|
17
|
+
const char = cx.char(at);
|
|
18
|
+
if (char === BACKSLASH)
|
|
19
|
+
at++;
|
|
20
|
+
else if (char === OPEN_BRACE)
|
|
21
|
+
depth++;
|
|
22
|
+
else if (char === CLOSE_BRACE)
|
|
23
|
+
depth = Math.max(0, depth - 1);
|
|
24
|
+
else if (char === DOLLAR && depth === 0) {
|
|
25
|
+
if (isSpace(cx.char(at - 1)))
|
|
26
|
+
return -1;
|
|
27
|
+
if (isDigit(cx.char(at + 1)))
|
|
28
|
+
continue;
|
|
29
|
+
return cx.addElement(cx.elt("InlineMath", pos, at + 1, [cx.elt("InlineMathMark", pos, pos + 1), cx.elt("InlineMathMark", at, at + 1)]));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return -1;
|
|
33
|
+
}
|
|
34
|
+
function startsMathBlock(line) {
|
|
35
|
+
return line.indent < line.baseIndent + 4 && line.text.startsWith("$$", line.pos);
|
|
36
|
+
}
|
|
37
|
+
/** Adds the `MathBlock` from `start` to the end of the current line, and moves past it. */
|
|
38
|
+
function closeMathBlock(cx, line, start) {
|
|
39
|
+
const end = cx.lineStart + line.text.trimEnd().length;
|
|
40
|
+
cx.addElement(cx.elt("MathBlock", start, end, [cx.elt("MathBlockMark", start, start + 2), cx.elt("MathBlockMark", end - 2, end)]));
|
|
41
|
+
cx.nextLine();
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
/** The block parser: from a line starting with `$$` to the first line ending with `$$`, which may be the same line. */
|
|
45
|
+
export function parseMathBlock(cx, line) {
|
|
46
|
+
if (!startsMathBlock(line))
|
|
47
|
+
return false;
|
|
48
|
+
const start = cx.lineStart + line.pos;
|
|
49
|
+
const first = line.text.slice(line.pos).trimEnd();
|
|
50
|
+
if (first.length > 4 && first.endsWith("$$"))
|
|
51
|
+
return closeMathBlock(cx, line, start);
|
|
52
|
+
while (cx.nextLine()) {
|
|
53
|
+
if (line.text.trimEnd().endsWith("$$"))
|
|
54
|
+
return closeMathBlock(cx, line, start);
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
/** Ends a paragraph at a `$$` line. */
|
|
59
|
+
export const endLeafAtMathBlock = (_cx, line) => startsMathBlock(line);
|
package/dist/nodes.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The block nodes a component line becomes (CompMD spec §3), and a line that starts with `::` but matches no form. */
|
|
2
|
+
export declare const COMPONENT_LINES: readonly ["ComponentOpen", "ComponentClose", "ComponentLeaf", "ComponentSlot", "ComponentContinuation", "ComponentMalformed"];
|
|
3
|
+
/** One of {@link COMPONENT_LINES}. */
|
|
4
|
+
export type ComponentLine = (typeof COMPONENT_LINES)[number];
|
|
5
|
+
/** Block nodes other than component lines. */
|
|
6
|
+
export declare const OTHER_BLOCKS: readonly ["Frontmatter", "MathBlock", "ComponentDefinition"];
|
|
7
|
+
/** Inline and part nodes: the pieces of a component line, an inline component, an interpolation, a wiki link, math. */
|
|
8
|
+
export declare const PARTS: readonly ["InlineComponent", "ComponentMark", "ComponentName", "ComponentSlotName", "ComponentFallback", "ComponentFallbackMark", "ParamList", "ParamBrace", "ParamKey", "ParamBreakpoint", "ParamOperator", "ParamString", "ParamEscape", "ParamExpression", "ParamExpressionMark", "ParamJson", "ParamBare", "ParamClass", "ParamClassMark", "ParamClassName", "ParamId", "ParamIdMark", "ParamIdName", "ParamInvalid", "DefinitionInfo", "DefinitionSignature", "DefinitionSeparator", "DefinitionBody", "Interpolation", "InterpolationMark", "InterpolationExpression", "WikiLink", "WikiLinkMark", "WikiLinkTarget", "WikiLinkLabel", "InlineMath", "InlineMathMark", "MathBlockMark", "FrontmatterMark", "FrontmatterKey"];
|
|
9
|
+
/**
|
|
10
|
+
* The nodes of a CEL expression (spec §8.2): in a tree of its own from `celParser`, and inside an expression param
|
|
11
|
+
* (`ParamExpression`). `CelInvalid` is what is wrong, or empty where something is missing.
|
|
12
|
+
*/
|
|
13
|
+
export declare const CEL_NODES: readonly ["CelConditional", "CelBinary", "CelUnary", "CelMember", "CelIndex", "CelCall", "CelArguments", "CelParenthesized", "CelList", "CelMap", "CelMapEntry", "CelVariable", "CelField", "CelFunction", "CelNumber", "CelString", "CelBytes", "CelBool", "CelNull", "CelLogicOperator", "CelCompareOperator", "CelArithmeticOperator", "CelIn", "CelParen", "CelBracket", "CelBrace", "CelPunctuation", "CelInvalid"];
|
|
14
|
+
/** One of {@link CEL_NODES}. */
|
|
15
|
+
export type CelNodeName = (typeof CEL_NODES)[number];
|
|
16
|
+
/**
|
|
17
|
+
* The nodes that are CompMD's own, not Markdown: an editor that draws Markdown in place shows these as their source.
|
|
18
|
+
* A component's fallback (`ComponentFallback`) is inside them but holds ordinary inline Markdown.
|
|
19
|
+
*/
|
|
20
|
+
export declare const COMPMD_NODES: ReadonlySet<string>;
|
package/dist/nodes.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// The node names the grammar adds to @lezer/markdown's tree, in one place so the parsers, the highlighting and the
|
|
2
|
+
// grammar's definition agree.
|
|
3
|
+
/** The block nodes a component line becomes (CompMD spec §3), and a line that starts with `::` but matches no form. */
|
|
4
|
+
export const COMPONENT_LINES = [
|
|
5
|
+
"ComponentOpen",
|
|
6
|
+
"ComponentClose",
|
|
7
|
+
"ComponentLeaf",
|
|
8
|
+
"ComponentSlot",
|
|
9
|
+
"ComponentContinuation",
|
|
10
|
+
"ComponentMalformed",
|
|
11
|
+
];
|
|
12
|
+
/** Block nodes other than component lines. */
|
|
13
|
+
export const OTHER_BLOCKS = ["Frontmatter", "MathBlock", "ComponentDefinition"];
|
|
14
|
+
/** Inline and part nodes: the pieces of a component line, an inline component, an interpolation, a wiki link, math. */
|
|
15
|
+
export const PARTS = [
|
|
16
|
+
"InlineComponent",
|
|
17
|
+
"ComponentMark",
|
|
18
|
+
"ComponentName",
|
|
19
|
+
"ComponentSlotName",
|
|
20
|
+
"ComponentFallback",
|
|
21
|
+
"ComponentFallbackMark",
|
|
22
|
+
"ParamList",
|
|
23
|
+
"ParamBrace",
|
|
24
|
+
"ParamKey",
|
|
25
|
+
"ParamBreakpoint",
|
|
26
|
+
"ParamOperator",
|
|
27
|
+
"ParamString",
|
|
28
|
+
"ParamEscape",
|
|
29
|
+
"ParamExpression",
|
|
30
|
+
"ParamExpressionMark",
|
|
31
|
+
"ParamJson",
|
|
32
|
+
"ParamBare",
|
|
33
|
+
"ParamClass",
|
|
34
|
+
"ParamClassMark",
|
|
35
|
+
"ParamClassName",
|
|
36
|
+
"ParamId",
|
|
37
|
+
"ParamIdMark",
|
|
38
|
+
"ParamIdName",
|
|
39
|
+
"ParamInvalid",
|
|
40
|
+
"DefinitionInfo",
|
|
41
|
+
"DefinitionSignature",
|
|
42
|
+
"DefinitionSeparator",
|
|
43
|
+
"DefinitionBody",
|
|
44
|
+
"Interpolation",
|
|
45
|
+
"InterpolationMark",
|
|
46
|
+
"InterpolationExpression",
|
|
47
|
+
"WikiLink",
|
|
48
|
+
"WikiLinkMark",
|
|
49
|
+
"WikiLinkTarget",
|
|
50
|
+
"WikiLinkLabel",
|
|
51
|
+
"InlineMath",
|
|
52
|
+
"InlineMathMark",
|
|
53
|
+
"MathBlockMark",
|
|
54
|
+
"FrontmatterMark",
|
|
55
|
+
"FrontmatterKey",
|
|
56
|
+
];
|
|
57
|
+
/**
|
|
58
|
+
* The nodes of a CEL expression (spec §8.2): in a tree of its own from `celParser`, and inside an expression param
|
|
59
|
+
* (`ParamExpression`). `CelInvalid` is what is wrong, or empty where something is missing.
|
|
60
|
+
*/
|
|
61
|
+
export const CEL_NODES = [
|
|
62
|
+
"CelConditional",
|
|
63
|
+
"CelBinary",
|
|
64
|
+
"CelUnary",
|
|
65
|
+
"CelMember",
|
|
66
|
+
"CelIndex",
|
|
67
|
+
"CelCall",
|
|
68
|
+
"CelArguments",
|
|
69
|
+
"CelParenthesized",
|
|
70
|
+
"CelList",
|
|
71
|
+
"CelMap",
|
|
72
|
+
"CelMapEntry",
|
|
73
|
+
"CelVariable",
|
|
74
|
+
"CelField",
|
|
75
|
+
"CelFunction",
|
|
76
|
+
"CelNumber",
|
|
77
|
+
"CelString",
|
|
78
|
+
"CelBytes",
|
|
79
|
+
"CelBool",
|
|
80
|
+
"CelNull",
|
|
81
|
+
"CelLogicOperator",
|
|
82
|
+
"CelCompareOperator",
|
|
83
|
+
"CelArithmeticOperator",
|
|
84
|
+
"CelIn",
|
|
85
|
+
"CelParen",
|
|
86
|
+
"CelBracket",
|
|
87
|
+
"CelBrace",
|
|
88
|
+
"CelPunctuation",
|
|
89
|
+
"CelInvalid",
|
|
90
|
+
];
|
|
91
|
+
/**
|
|
92
|
+
* The nodes that are CompMD's own, not Markdown: an editor that draws Markdown in place shows these as their source.
|
|
93
|
+
* A component's fallback (`ComponentFallback`) is inside them but holds ordinary inline Markdown.
|
|
94
|
+
*/
|
|
95
|
+
export const COMPMD_NODES = new Set([
|
|
96
|
+
...COMPONENT_LINES,
|
|
97
|
+
"InlineComponent",
|
|
98
|
+
"Interpolation",
|
|
99
|
+
"Frontmatter",
|
|
100
|
+
"ComponentDefinition",
|
|
101
|
+
]);
|
package/dist/params.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** The kind of a span inside a param list. */
|
|
2
|
+
export type ParamPart = "ParamKey" | "ParamBreakpoint" | "ParamOperator" | "ParamString" | "ParamEscape" | "ParamExpression" | "ParamExpressionMark" | "ParamJson" | "ParamBare" | "ParamClass" | "ParamClassMark" | "ParamClassName" | "ParamId" | "ParamIdMark" | "ParamIdName";
|
|
3
|
+
/** One span of a param list, as offsets into the scanned text, with the spans inside it. */
|
|
4
|
+
export interface Span {
|
|
5
|
+
readonly type: ParamPart;
|
|
6
|
+
readonly from: number;
|
|
7
|
+
readonly to: number;
|
|
8
|
+
readonly parts?: readonly Span[];
|
|
9
|
+
}
|
|
10
|
+
/** Spans found, and the offset where scanning stopped. */
|
|
11
|
+
export interface Scanned {
|
|
12
|
+
readonly spans: readonly Span[];
|
|
13
|
+
readonly end: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The params of a param body (no braces) from `from`, up to `stop` (a `}`) or the text's end.
|
|
17
|
+
*
|
|
18
|
+
* @returns the spans, and where the body ended: at `stop`, the text's end, or the first thing that isn't a param.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* scanParamBody('md:gap=l title="A"', 0); // spans ParamBreakpoint, ParamOperator, ParamKey, ParamOperator, ParamBare, …
|
|
22
|
+
*/
|
|
23
|
+
export declare function scanParamBody(text: string, from: number, stop?: string): Scanned;
|
|
24
|
+
/**
|
|
25
|
+
* A param list `{…}` whose `{` is at `from`.
|
|
26
|
+
*
|
|
27
|
+
* @returns its spans and the offset after its `}`, or `undefined` when there is no `{` at `from` or the list isn't
|
|
28
|
+
* closed.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* scanParamList('{title="A" .wide}', 0); // spans ParamKey, ParamOperator, ParamString, ParamClass; end 17
|
|
32
|
+
*/
|
|
33
|
+
export declare function scanParamList(text: string, from: number): Scanned | undefined;
|
package/dist/params.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// A CompMD param list (spec §3.1, §5.1), scanned into spans. Each value finds its own end from its first character,
|
|
2
|
+
// so a broken quote or bracket damages only its own line (spec §5). The pieces every grammar shares (a name, a bare
|
|
3
|
+
// value, an escape) are `syntax/patterns.js`'s.
|
|
4
|
+
import { literalAt } from "./cel-tokens.js";
|
|
5
|
+
import { PATTERNS } from "./syntax.js";
|
|
6
|
+
/** A component's, slot's or param key's name (spec §3.1). */
|
|
7
|
+
const NAME = new RegExp(`^${PATTERNS.name}`);
|
|
8
|
+
const BARE_VALUE = new RegExp(`^${PATTERNS.bareValue}`);
|
|
9
|
+
const ESCAPE = new RegExp(PATTERNS.stringEscape, "g");
|
|
10
|
+
const isSpace = (char) => char === " " || char === "\t";
|
|
11
|
+
/** After the `"` that closes a string starting at `from`, or the text's end. */
|
|
12
|
+
function stringEnd(text, from) {
|
|
13
|
+
for (let at = from + 1; at < text.length; at++) {
|
|
14
|
+
if (text[at] === "\\")
|
|
15
|
+
at++;
|
|
16
|
+
else if (text[at] === '"')
|
|
17
|
+
return at + 1;
|
|
18
|
+
}
|
|
19
|
+
return text.length;
|
|
20
|
+
}
|
|
21
|
+
/** A JSON value's strings: `"…"`, with escapes. */
|
|
22
|
+
const jsonStringEnd = (text, at) => (text[at] === '"' ? stringEnd(text, at) : undefined);
|
|
23
|
+
/** An expression's strings and bytes: CEL's, in every form (quoted, triple-quoted, raw, bytes). */
|
|
24
|
+
const celLiteralEnd = (text, at) => literalAt(text, at)?.to;
|
|
25
|
+
/** After the bracket matching the one at `from`, counting only outside the strings `literalEnd` finds, or the text's end. */
|
|
26
|
+
function bracketedEnd(text, from, open, close, literalEnd) {
|
|
27
|
+
let depth = 0;
|
|
28
|
+
for (let at = from; at < text.length; at++) {
|
|
29
|
+
const literal = literalEnd(text, at);
|
|
30
|
+
if (literal !== undefined)
|
|
31
|
+
at = literal - 1;
|
|
32
|
+
else if (text[at] === open)
|
|
33
|
+
depth++;
|
|
34
|
+
else if (text[at] === close && --depth === 0)
|
|
35
|
+
return at + 1;
|
|
36
|
+
}
|
|
37
|
+
return text.length;
|
|
38
|
+
}
|
|
39
|
+
/** A quoted string from `from`, with its escapes. */
|
|
40
|
+
function stringSpan(text, from) {
|
|
41
|
+
const to = stringEnd(text, from);
|
|
42
|
+
const parts = [...text.slice(from, to).matchAll(ESCAPE)].map((match) => ({ type: "ParamEscape", from: from + match.index, to: from + match.index + match[0].length }));
|
|
43
|
+
return { type: "ParamString", from, to, parts };
|
|
44
|
+
}
|
|
45
|
+
/** An expression `(…)` from `from`, with its parentheses when it is closed. */
|
|
46
|
+
function expressionSpan(text, from) {
|
|
47
|
+
const to = bracketedEnd(text, from, "(", ")", celLiteralEnd);
|
|
48
|
+
const open = { type: "ParamExpressionMark", from, to: from + 1 };
|
|
49
|
+
const closed = to - from > 1 && text[to - 1] === ")";
|
|
50
|
+
const parts = closed ? [open, { type: "ParamExpressionMark", from: to - 1, to }] : [open];
|
|
51
|
+
return { type: "ParamExpression", from, to, parts };
|
|
52
|
+
}
|
|
53
|
+
function valueSpan(text, from) {
|
|
54
|
+
switch (text[from]) {
|
|
55
|
+
case '"':
|
|
56
|
+
return stringSpan(text, from);
|
|
57
|
+
case "(":
|
|
58
|
+
return expressionSpan(text, from);
|
|
59
|
+
case "[":
|
|
60
|
+
return { type: "ParamJson", from, to: bracketedEnd(text, from, "[", "]", jsonStringEnd) };
|
|
61
|
+
case "{":
|
|
62
|
+
return { type: "ParamJson", from, to: bracketedEnd(text, from, "{", "}", jsonStringEnd) };
|
|
63
|
+
default:
|
|
64
|
+
return { type: "ParamBare", from, to: from + (BARE_VALUE.exec(text.slice(from))?.[0].length ?? 0) };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** A class `.name` or an id `#name` at `from`, its mark and its name each a span of their own. */
|
|
68
|
+
function shorthandAt(text, from) {
|
|
69
|
+
const name = NAME.exec(text.slice(from + 1));
|
|
70
|
+
if (!name)
|
|
71
|
+
return undefined;
|
|
72
|
+
const to = from + 1 + name[0].length;
|
|
73
|
+
const [type, mark, named] = text[from] === "." ? ["ParamClass", "ParamClassMark", "ParamClassName"] : ["ParamId", "ParamIdMark", "ParamIdName"];
|
|
74
|
+
const parts = [
|
|
75
|
+
{ type: mark, from, to: from + 1 },
|
|
76
|
+
{ type: named, from: from + 1, to },
|
|
77
|
+
];
|
|
78
|
+
return { spans: [{ type, from, to, parts }], end: to };
|
|
79
|
+
}
|
|
80
|
+
/** A key at `from`, with its breakpoint and the `:` after it when written `bp:key`. */
|
|
81
|
+
function keyAt(text, from) {
|
|
82
|
+
const first = NAME.exec(text.slice(from));
|
|
83
|
+
if (!first)
|
|
84
|
+
return undefined;
|
|
85
|
+
const afterFirst = from + first[0].length;
|
|
86
|
+
const key = text[afterFirst] === ":" ? NAME.exec(text.slice(afterFirst + 1)) : null;
|
|
87
|
+
if (!key)
|
|
88
|
+
return { spans: [{ type: "ParamKey", from, to: afterFirst }], end: afterFirst };
|
|
89
|
+
const end = afterFirst + 1 + key[0].length;
|
|
90
|
+
return {
|
|
91
|
+
spans: [
|
|
92
|
+
{ type: "ParamBreakpoint", from, to: afterFirst },
|
|
93
|
+
{ type: "ParamOperator", from: afterFirst, to: afterFirst + 1 },
|
|
94
|
+
{ type: "ParamKey", from: afterFirst + 1, to: end },
|
|
95
|
+
],
|
|
96
|
+
end,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** One param at `from`: `key=value`, `bp:key=value`, `.class`, `#id` or a lone `key`. */
|
|
100
|
+
function paramAt(text, from) {
|
|
101
|
+
if (text[from] === "." || text[from] === "#")
|
|
102
|
+
return shorthandAt(text, from);
|
|
103
|
+
const key = keyAt(text, from);
|
|
104
|
+
if (!key || text[key.end] !== "=")
|
|
105
|
+
return key;
|
|
106
|
+
const operator = { type: "ParamOperator", from: key.end, to: key.end + 1 };
|
|
107
|
+
const valueAt = key.end + 1;
|
|
108
|
+
if (valueAt >= text.length || isSpace(text[valueAt]) || text[valueAt] === "}") {
|
|
109
|
+
return { spans: [...key.spans, operator], end: valueAt };
|
|
110
|
+
}
|
|
111
|
+
const value = valueSpan(text, valueAt);
|
|
112
|
+
return { spans: [...key.spans, operator, value], end: value.to };
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The params of a param body (no braces) from `from`, up to `stop` (a `}`) or the text's end.
|
|
116
|
+
*
|
|
117
|
+
* @returns the spans, and where the body ended: at `stop`, the text's end, or the first thing that isn't a param.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* scanParamBody('md:gap=l title="A"', 0); // spans ParamBreakpoint, ParamOperator, ParamKey, ParamOperator, ParamBare, …
|
|
121
|
+
*/
|
|
122
|
+
export function scanParamBody(text, from, stop) {
|
|
123
|
+
const spans = [];
|
|
124
|
+
let at = from;
|
|
125
|
+
for (;;) {
|
|
126
|
+
while (isSpace(text[at]))
|
|
127
|
+
at++;
|
|
128
|
+
if (at >= text.length || text[at] === stop)
|
|
129
|
+
return { spans, end: at };
|
|
130
|
+
const param = paramAt(text, at);
|
|
131
|
+
if (!param)
|
|
132
|
+
return { spans, end: at };
|
|
133
|
+
spans.push(...param.spans);
|
|
134
|
+
at = param.end;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* A param list `{…}` whose `{` is at `from`.
|
|
139
|
+
*
|
|
140
|
+
* @returns its spans and the offset after its `}`, or `undefined` when there is no `{` at `from` or the list isn't
|
|
141
|
+
* closed.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* scanParamList('{title="A" .wide}', 0); // spans ParamKey, ParamOperator, ParamString, ParamClass; end 17
|
|
145
|
+
*/
|
|
146
|
+
export function scanParamList(text, from) {
|
|
147
|
+
if (text[from] !== "{")
|
|
148
|
+
return undefined;
|
|
149
|
+
const body = scanParamBody(text, from + 1, "}");
|
|
150
|
+
return text[body.end] === "}" ? { spans: body.spans, end: body.end + 1 } : undefined;
|
|
151
|
+
}
|
package/dist/syntax.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Each token's Lezer highlighting tag, by the token's name (`syntax/tokens.json`). */
|
|
2
|
+
export declare const TOKEN_TAGS: {
|
|
3
|
+
readonly "component-marker": "processingInstruction";
|
|
4
|
+
readonly "component-name": "tagName";
|
|
5
|
+
readonly "slot-name": "labelName";
|
|
6
|
+
readonly "fallback-bracket": "squareBracket";
|
|
7
|
+
readonly "param-brace": "brace";
|
|
8
|
+
readonly "param-key": "attributeName";
|
|
9
|
+
readonly breakpoint: "modifier";
|
|
10
|
+
readonly "param-class": "attributeName";
|
|
11
|
+
readonly "param-id": "attributeName";
|
|
12
|
+
readonly "param-operator": "operator";
|
|
13
|
+
readonly "param-string": "string";
|
|
14
|
+
readonly "string-escape": "escape";
|
|
15
|
+
readonly "param-bare": "special(string)";
|
|
16
|
+
readonly "expression-paren": "processingInstruction";
|
|
17
|
+
readonly "interpolation-brace": "processingInstruction";
|
|
18
|
+
readonly "interpolation-path": "variableName";
|
|
19
|
+
readonly "definition-separator": "separator";
|
|
20
|
+
readonly "wiki-link-target": "url";
|
|
21
|
+
readonly "wiki-link-label": "link";
|
|
22
|
+
readonly math: "monospace";
|
|
23
|
+
readonly "frontmatter-key": "keyword";
|
|
24
|
+
};
|
|
25
|
+
/** A token's name. */
|
|
26
|
+
export type Token = keyof typeof TOKEN_TAGS;
|
|
27
|
+
/** The syntax's pieces as regular-expression sources (`syntax/patterns.js`). */
|
|
28
|
+
export declare const PATTERNS: {
|
|
29
|
+
readonly name: "[A-Za-z][A-Za-z0-9_-]*";
|
|
30
|
+
readonly identifier: "[A-Za-z_][A-Za-z0-9_]*";
|
|
31
|
+
readonly interpolationPath: "[A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*";
|
|
32
|
+
readonly space: "[ \\t]";
|
|
33
|
+
readonly bareValue: "[^ \\t\\r\\n\"(){}\\[\\]]+";
|
|
34
|
+
readonly stringEscape: "\\\\[!-/:-@\\[-`{-~]";
|
|
35
|
+
readonly celString: "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"|'(?:[^'\\\\\\r\\n]|\\\\.)*'";
|
|
36
|
+
readonly jsonString: "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"";
|
|
37
|
+
readonly lines: {
|
|
38
|
+
readonly containerOpener: "(:::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)";
|
|
39
|
+
readonly containerCloser: "(:::)([ \\t]+)(/)([A-Za-z][A-Za-z0-9_-]*)";
|
|
40
|
+
readonly continuation: "(:::)([ \\t]+)(\\+)(?=[ \\t])";
|
|
41
|
+
readonly leaf: "(::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)";
|
|
42
|
+
readonly slot: "(::)([ \\t]+)(#)([A-Za-z][A-Za-z0-9_-]*)";
|
|
43
|
+
readonly malformed: "(::+)";
|
|
44
|
+
};
|
|
45
|
+
readonly inlineComponentStart: "(:)([A-Za-z][A-Za-z0-9_-]*)(\\[)";
|
|
46
|
+
readonly interpolation: "(\\{\\{)([ \\t]*)([A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*)([ \\t]*)(\\}\\})";
|
|
47
|
+
readonly wikiLink: "(\\[\\[)([^\\[\\]|\\r\\n]+)(?:(\\|)([^\\[\\]\\r\\n]+))?(\\]\\])";
|
|
48
|
+
readonly frontmatterKey: {
|
|
49
|
+
readonly yaml: "^(md)(:)";
|
|
50
|
+
readonly toml: "^(?:\\[\\[?(md)(?=[.\\]])|(md)(?=[ \\t]*[.=]))";
|
|
51
|
+
};
|
|
52
|
+
};
|
package/dist/syntax.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Generated by `node generate.js` from compmd-grammars' `syntax/`: edit `syntax/tokens.json` or `syntax/patterns.js`,
|
|
2
|
+
// then generate again.
|
|
3
|
+
/** Each token's Lezer highlighting tag, by the token's name (`syntax/tokens.json`). */
|
|
4
|
+
export const TOKEN_TAGS = {
|
|
5
|
+
"component-marker": "processingInstruction",
|
|
6
|
+
"component-name": "tagName",
|
|
7
|
+
"slot-name": "labelName",
|
|
8
|
+
"fallback-bracket": "squareBracket",
|
|
9
|
+
"param-brace": "brace",
|
|
10
|
+
"param-key": "attributeName",
|
|
11
|
+
"breakpoint": "modifier",
|
|
12
|
+
"param-class": "attributeName",
|
|
13
|
+
"param-id": "attributeName",
|
|
14
|
+
"param-operator": "operator",
|
|
15
|
+
"param-string": "string",
|
|
16
|
+
"string-escape": "escape",
|
|
17
|
+
"param-bare": "special(string)",
|
|
18
|
+
"expression-paren": "processingInstruction",
|
|
19
|
+
"interpolation-brace": "processingInstruction",
|
|
20
|
+
"interpolation-path": "variableName",
|
|
21
|
+
"definition-separator": "separator",
|
|
22
|
+
"wiki-link-target": "url",
|
|
23
|
+
"wiki-link-label": "link",
|
|
24
|
+
"math": "monospace",
|
|
25
|
+
"frontmatter-key": "keyword"
|
|
26
|
+
};
|
|
27
|
+
/** The syntax's pieces as regular-expression sources (`syntax/patterns.js`). */
|
|
28
|
+
export const PATTERNS = {
|
|
29
|
+
"name": "[A-Za-z][A-Za-z0-9_-]*",
|
|
30
|
+
"identifier": "[A-Za-z_][A-Za-z0-9_]*",
|
|
31
|
+
"interpolationPath": "[A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*",
|
|
32
|
+
"space": "[ \\t]",
|
|
33
|
+
"bareValue": "[^ \\t\\r\\n\"(){}\\[\\]]+",
|
|
34
|
+
"stringEscape": "\\\\[!-/:-@\\[-`{-~]",
|
|
35
|
+
"celString": "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"|'(?:[^'\\\\\\r\\n]|\\\\.)*'",
|
|
36
|
+
"jsonString": "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"",
|
|
37
|
+
"lines": {
|
|
38
|
+
"containerOpener": "(:::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)",
|
|
39
|
+
"containerCloser": "(:::)([ \\t]+)(/)([A-Za-z][A-Za-z0-9_-]*)",
|
|
40
|
+
"continuation": "(:::)([ \\t]+)(\\+)(?=[ \\t])",
|
|
41
|
+
"leaf": "(::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)",
|
|
42
|
+
"slot": "(::)([ \\t]+)(#)([A-Za-z][A-Za-z0-9_-]*)",
|
|
43
|
+
"malformed": "(::+)"
|
|
44
|
+
},
|
|
45
|
+
"inlineComponentStart": "(:)([A-Za-z][A-Za-z0-9_-]*)(\\[)",
|
|
46
|
+
"interpolation": "(\\{\\{)([ \\t]*)([A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*)([ \\t]*)(\\}\\})",
|
|
47
|
+
"wikiLink": "(\\[\\[)([^\\[\\]|\\r\\n]+)(?:(\\|)([^\\[\\]\\r\\n]+))?(\\]\\])",
|
|
48
|
+
"frontmatterKey": {
|
|
49
|
+
"yaml": "^(md)(:)",
|
|
50
|
+
"toml": "^(?:\\[\\[?(md)(?=[.\\]])|(md)(?=[ \\t]*[.=]))"
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Wiki links, `[[Target]]` and `[[Target|label]]` (spec §2.3). Parsed always: a grammar can't read the document's
|
|
2
|
+
// opt-in (`syntax: [wiki-links]`), as CompMD's other grammars decide.
|
|
3
|
+
const BRACKET = 91;
|
|
4
|
+
/** The inline parser. A `|` not escaped splits the target from the label; a link never spans lines. */
|
|
5
|
+
export function parseWikiLink(cx, next, pos) {
|
|
6
|
+
if (next !== BRACKET || cx.char(pos + 1) !== BRACKET)
|
|
7
|
+
return -1;
|
|
8
|
+
const text = cx.slice(pos, cx.end);
|
|
9
|
+
const close = text.indexOf("]]", 2);
|
|
10
|
+
if (close < 0)
|
|
11
|
+
return -1;
|
|
12
|
+
const inner = text.slice(2, close);
|
|
13
|
+
if (inner.includes("\n"))
|
|
14
|
+
return -1;
|
|
15
|
+
const end = pos + close + 2;
|
|
16
|
+
const bar = /(?<!\\)\|/.exec(inner)?.index;
|
|
17
|
+
const parts = bar === undefined
|
|
18
|
+
? [cx.elt("WikiLinkTarget", pos + 2, end - 2)]
|
|
19
|
+
: [
|
|
20
|
+
cx.elt("WikiLinkTarget", pos + 2, pos + 2 + bar),
|
|
21
|
+
cx.elt("WikiLinkMark", pos + 2 + bar, pos + 3 + bar),
|
|
22
|
+
cx.elt("WikiLinkLabel", pos + 3 + bar, end - 2),
|
|
23
|
+
];
|
|
24
|
+
return cx.addElement(cx.elt("WikiLink", pos, end, [cx.elt("WikiLinkMark", pos, pos + 2), ...parts, cx.elt("WikiLinkMark", end - 2, end)]));
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1,21 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compmd/lezer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"lezer",
|
|
8
|
-
"codemirror"
|
|
9
|
-
],
|
|
10
|
-
"homepage": "https://compmd.dev/",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "CompMD's grammar for CodeMirror: an @lezer/markdown extension with CompMD's components, params, CEL expressions, interpolation, wiki links and math.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
11
7
|
"repository": {
|
|
12
8
|
"type": "git",
|
|
13
9
|
"url": "git+https://github.com/comp-markdown/compmd-grammars.git",
|
|
14
10
|
"directory": "lezer"
|
|
15
11
|
},
|
|
16
|
-
"
|
|
12
|
+
"keywords": [
|
|
13
|
+
"compmd",
|
|
14
|
+
"lezer",
|
|
15
|
+
"codemirror",
|
|
16
|
+
"markdown"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
17
24
|
"files": [
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@lezer/common": "^1.2.0",
|
|
29
|
+
"@lezer/highlight": "^1.2.0",
|
|
30
|
+
"@lezer/markdown": "^1.4.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@lezer/common": "1.5.5",
|
|
34
|
+
"@lezer/highlight": "1.2.5",
|
|
35
|
+
"@lezer/markdown": "1.8.1",
|
|
36
|
+
"@types/node": "24.19.2",
|
|
37
|
+
"typescript": "6.0.3",
|
|
38
|
+
"vitest": "5.0.5"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"generate": "node generate.js",
|
|
42
|
+
"build": "tsc -p tsconfig.build.json",
|
|
43
|
+
"lint": "tsc --noEmit",
|
|
44
|
+
"test": "node generate.js --check && vitest run"
|
|
45
|
+
}
|
|
46
|
+
}
|