jpfun 1.0.0 → 1.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 +1 -1
- package/dist/diagnostic.d.ts +1 -0
- package/dist/diagnostic.js +1 -0
- package/dist/functions/ASTtypes.d.ts +2 -3
- package/dist/functions/ASTtypes.js +2 -3
- package/dist/functions/adjust/index.js +15 -67
- package/dist/functions/bar/index.js +11 -7
- package/dist/functions/beam/auto.js +15 -11
- package/dist/functions/beam/layout.js +22 -48
- package/dist/functions/br/index.d.ts +9 -3
- package/dist/functions/br/index.js +20 -16
- package/dist/functions/dash/index.js +13 -11
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +2 -2
- package/dist/functions/div/index.js +5 -9
- package/dist/functions/dot/index.js +5 -9
- package/dist/functions/dyn/index.js +125 -53
- package/dist/functions/gliss/index.js +81 -7
- package/dist/functions/grace/index.d.ts +3 -10
- package/dist/functions/grace/index.js +25 -57
- package/dist/functions/head/index.js +6 -7
- package/dist/functions/key/index.js +1 -0
- package/dist/functions/meter/index.js +7 -5
- package/dist/functions/note/index.d.ts +5 -2
- package/dist/functions/note/index.js +162 -19
- package/dist/functions/note/source-transform.d.ts +8 -2
- package/dist/functions/note/source-transform.js +25 -11
- package/dist/functions/ottava/index.js +8 -7
- package/dist/functions/page/index.d.ts +1 -36
- package/dist/functions/page/index.js +11 -15
- package/dist/functions/pagebreak/index.js +2 -1
- package/dist/functions/rest/index.js +5 -5
- package/dist/functions/set/index.js +2 -2
- package/dist/functions/stack/index.js +7 -7
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +10 -1
- package/dist/functions/tempo/index.js +8 -3
- package/dist/functions/temporal.d.ts +21 -6
- package/dist/functions/temporal.js +1 -1
- package/dist/functions/text/index.js +3 -5
- package/dist/functions/up/index.js +37 -34
- package/dist/functions/voice/connections.js +2 -2
- package/dist/functions/voice/lyrics.d.ts +2 -0
- package/dist/functions/voice/lyrics.js +4 -4
- package/dist/functions/voice/voice.d.ts +2 -1
- package/dist/functions/voice/voice.js +9 -15
- package/dist/functions/voice/voices.d.ts +6 -4
- package/dist/functions/voice/voices.js +10 -10
- package/dist/index.d.ts +6 -34
- package/dist/index.js +6 -34
- package/dist/jpfun.min.js +49 -46
- package/dist/layout/engine.js +237 -232
- package/dist/layout/model.d.ts +1 -1
- package/dist/layout/page.d.ts +2 -2
- package/dist/layout/page.js +8 -22
- package/dist/layout/types.d.ts +4 -5
- package/dist/lowering/accidentalMemory.d.ts +33 -0
- package/dist/lowering/accidentalMemory.js +84 -0
- package/dist/lowering/loweringContext.d.ts +12 -7
- package/dist/lowering/loweringContext.js +54 -51
- package/dist/lowering/types.d.ts +2 -3
- package/dist/parser/grammarType.d.ts +10 -4
- package/dist/parser/parse-utils/brace-utils.js +4 -3
- package/dist/parser/parse-utils/call-utils.js +7 -7
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +1 -1
- package/dist/parser/parserContext.d.ts +30 -34
- package/dist/parser/parserContext.js +77 -181
- package/dist/parser/preprocess.js +10 -23
- package/dist/parser/syntax.d.ts +9 -0
- package/dist/parser/syntax.js +109 -0
- package/dist/parser/types.d.ts +2 -1
- package/dist/pipeline.js +3 -2
- package/dist/playback/compile.js +26 -18
- package/dist/playback/event.d.ts +1 -2
- package/dist/playback/event.js +2 -3
- package/dist/playback/types.d.ts +0 -1
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/grammar.md +8 -6
- package/package.json +2 -2
- package/dist/functions/voice/index.d.ts +0 -2
- package/dist/functions/voice/index.js +0 -2
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { deSugarAtomFunction,
|
|
1
|
+
import { deSugarAtomFunction, LengthValue, SourceSpan } from "./types.js";
|
|
2
2
|
import { Diagnostic } from "../diagnostic.js";
|
|
3
|
-
import { GrammarCallNode, GrammarCallNodeRaw, GrammarNode
|
|
3
|
+
import { GrammarCallNode, GrammarCallNodeRaw, GrammarNode } from "./grammarType.js";
|
|
4
4
|
import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue } from "../functions/ASTtypes.js";
|
|
5
|
+
export declare const BOOL_RE: RegExp;
|
|
5
6
|
export declare const DEFAULT_FONT_SIZE = 18;
|
|
6
7
|
export declare const DEFAULT_NUMBER_WEIGHT = 700;
|
|
7
8
|
export declare class ParserContext {
|
|
@@ -11,14 +12,13 @@ export declare class ParserContext {
|
|
|
11
12
|
default: paramValue;
|
|
12
13
|
description: string;
|
|
13
14
|
}>;
|
|
14
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* 根解析器为 0,内容参数和大括号子解析器依次加一
|
|
17
|
+
* 分组只存在于 AST 路径,导致语法糖并不会和函数调用一样开辟新的层级,因此只有 0(顶层)是可信的,非零的具体数值不可依赖
|
|
18
|
+
*/
|
|
15
19
|
readonly scopeDepth: number;
|
|
16
20
|
/** 源代码 */
|
|
17
21
|
readonly source: string;
|
|
18
|
-
/** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
|
|
19
|
-
readonly syntax: SyntaxAnalysis;
|
|
20
|
-
/** syntax.tokens 前缀注释区间的扫描游标 */
|
|
21
|
-
private commentIndex;
|
|
22
22
|
/** 解析过程中产生的诊断信息,包含错误和警告 */
|
|
23
23
|
diagnostics: Diagnostic[];
|
|
24
24
|
/** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
|
|
@@ -27,15 +27,15 @@ export declare class ParserContext {
|
|
|
27
27
|
documentDeclarations: Record<string, any>;
|
|
28
28
|
/** 函数定义查找表 */
|
|
29
29
|
functions: Map<string, ASTFunctionClass>;
|
|
30
|
-
/**
|
|
30
|
+
/** 第一轮原子去糖:将文本转为函数调用,或 sugar 标记;第二轮由 sugar 节点自带的 relation 处理 */
|
|
31
31
|
deSugarAtomFns: deSugarAtomFunction[];
|
|
32
|
-
/** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
|
|
33
|
-
deSugarRelationFns: deSugarRelationFunction[];
|
|
34
32
|
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
35
33
|
labelableNodes: (ASTFunctionNode | null)[];
|
|
36
34
|
/** 当前正在实例化的显式函数调用节点,供构造函数按需读取原始参数区间 */
|
|
37
35
|
callNode?: GrammarCallNodeRaw;
|
|
38
36
|
nodes: ASTNodeBase[];
|
|
37
|
+
/** 等待右操作数的中缀语法糖,见 deferRightOperand */
|
|
38
|
+
private readonly pendingRight;
|
|
39
39
|
constructor(ctx: ParserContext | {
|
|
40
40
|
source: string;
|
|
41
41
|
diagnostics?: Diagnostic[];
|
|
@@ -43,59 +43,55 @@ export declare class ParserContext {
|
|
|
43
43
|
functions?: Map<string, ASTFunctionClass>;
|
|
44
44
|
labelableNodes?: (ASTFunctionNode | null)[];
|
|
45
45
|
toConsume?: ASTNodeBase[];
|
|
46
|
-
commentSpans?: SourceSpan[];
|
|
47
46
|
});
|
|
48
|
-
static getDeSugarFns(classes: Iterable<ASTFunctionClass>): [
|
|
49
|
-
deSugarAtomFunction[],
|
|
50
|
-
deSugarRelationFunction[]
|
|
51
|
-
];
|
|
47
|
+
static getDeSugarFns(classes: Iterable<ASTFunctionClass>): deSugarAtomFunction[];
|
|
52
48
|
pushNode(node: ASTNodeBase): void;
|
|
53
49
|
setVariable(name: string, value: paramValue | undefined): void;
|
|
54
50
|
registerFunctions(functionClasses: ASTFunctionClass[]): void;
|
|
55
51
|
/**
|
|
56
|
-
* 扫描 GrammarNode 并构造 AST
|
|
57
|
-
*
|
|
52
|
+
* 扫描 GrammarNode 并构造 AST,用于最终渲染
|
|
53
|
+
* 编辑器语法视图不构造 AST,见 syntax.ts 的 collectSyntax
|
|
58
54
|
*/
|
|
59
55
|
parse(start?: number, end?: number): ASTNodeBase[];
|
|
60
|
-
/**
|
|
61
|
-
* 词法层:容错扫描 GrammarNode 并收集 syntax
|
|
62
|
-
* 用于防抖时的编辑器高亮和补全
|
|
63
|
-
*/
|
|
64
|
-
parseSyntax(start?: number, end?: number): SyntaxAnalysis;
|
|
65
|
-
private addSyntax;
|
|
66
|
-
/** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
|
|
67
|
-
private recordSyntax;
|
|
68
56
|
/**
|
|
69
57
|
* 向前寻找最后一个非文本节点下标;
|
|
70
58
|
* 若末尾是标签,则将“被标注节点..标签”原位打包为 ASTBraceNode
|
|
71
59
|
*/
|
|
72
60
|
findLastOperandIndex(): number;
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
61
|
+
/**
|
|
62
|
+
* 摘出末尾最后一个操作数(含其后置标签)
|
|
63
|
+
* 传入 chain 时,若该操作数恰是同一 chain 挂起合成的唯一右操作数,先合成再返回:同类中缀由此左结合
|
|
64
|
+
*/
|
|
65
|
+
takeLeftOperand(chain?: unknown): ASTNodeBase | null;
|
|
66
|
+
/**
|
|
67
|
+
* 中缀语法糖的右操作数是本层剩余部分的首个非文本节点,要等本层解析完才能确定。
|
|
68
|
+
* 这里只挂起:已有节点留作左侧,后续节点解析到新数组,由 makeNodes 收尾时交给 combine;没有右操作数时 combine 收到 undefined
|
|
69
|
+
* 默认右结合;登记 chain 后,下一个同 chain 的中缀可经 takeLeftOperand 提前合成本帧。提前合成的结果不会进入 nodes,combine 不应有登记之类的副作用
|
|
70
|
+
*/
|
|
71
|
+
deferRightOperand(combine: (right?: ASTNodeBase) => ASTNodeBase, chain?: unknown): void;
|
|
77
72
|
/**
|
|
78
73
|
* 第二轮去糖 & 函数节点创建
|
|
79
74
|
* 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
|
|
80
75
|
*/
|
|
81
|
-
makeNodes(nodes: (GrammarNode | number)[]
|
|
76
|
+
makeNodes(nodes: (GrammarNode | number)[]): void;
|
|
82
77
|
/** 追加文本,与紧邻的上一个文本节点合并;新建区间对象,避免改写语法节点的 span */
|
|
83
78
|
private pushText;
|
|
84
79
|
/**
|
|
85
80
|
* 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
|
|
86
81
|
* 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
|
|
87
82
|
* 必须将第一阶段语法糖识别放到grammar解析中,比如 `@fn()`,引号若作为语法糖会导致后面的 `@` 无效
|
|
88
|
-
*
|
|
89
|
-
*
|
|
83
|
+
* observe 在每个节点识别后、校验前收到该节点及当前上下文;返回 true 即接管该节点:其语法错误只记录诊断不抛出(保留 partial call),
|
|
84
|
+
* 供编辑器在输入途中继续工作。不传或返回 false 时照常抛出。observe 不得改变“识别出什么结构”,否则两条路径会认不同的语言
|
|
90
85
|
*/
|
|
91
|
-
parseGrammar(p: number, end: number,
|
|
86
|
+
parseGrammar(p: number, end: number, observe?: (node: GrammarNode, ctx: ParserContext) => boolean): (GrammarNode | number)[];
|
|
92
87
|
parseCallNode(callNode: GrammarCallNode): ASTFunctionNode;
|
|
93
88
|
/**
|
|
94
89
|
* 根据参数类型解析参数值,解析过程中会记录诊断信息
|
|
95
90
|
* funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
|
|
96
91
|
* 解析成功返回值 解析失败返回null
|
|
92
|
+
* required 的 content 解析失败时直接抛出内部错误:返回 null 只会换来掩盖真因的缺参错误
|
|
97
93
|
*/
|
|
98
|
-
parseArgWithType(span: SourceSpan, type?: paramType, funcStart?: number): paramValue | null;
|
|
94
|
+
parseArgWithType(span: SourceSpan, type?: paramType, funcStart?: number, required?: boolean): paramValue | null;
|
|
99
95
|
length2px(length: LengthValue): number;
|
|
100
96
|
}
|
|
101
97
|
export declare function skipSpaces(s: string, i: number, end: number): number;
|
|
@@ -6,23 +6,9 @@ import { parseString } from "./parse-utils/string-utils.js";
|
|
|
6
6
|
import { readBrace } from "./parse-utils/brace-utils.js";
|
|
7
7
|
import { readLabel } from "./parse-utils/label-utils.js";
|
|
8
8
|
import { parseLength } from "./parse-utils/length-utils.js";
|
|
9
|
-
import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, resolveArgType } from "../functions/ASTtypes.js";
|
|
9
|
+
import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, resolveArgDef, resolveArgType } from "../functions/ASTtypes.js";
|
|
10
10
|
// bool 字面量 严格要求小写
|
|
11
|
-
const BOOL_RE = /^(true|false)$/;
|
|
12
|
-
/** 有函数参数契约时直接用声明类型;未知参数回落到真正的字面量解析器,不另写一套正则 */
|
|
13
|
-
function classifySyntaxValue(type, text) {
|
|
14
|
-
if (type)
|
|
15
|
-
return type === "content" ? undefined : type;
|
|
16
|
-
if (BOOL_RE.test(text))
|
|
17
|
-
return "boolean";
|
|
18
|
-
if (text.startsWith('"'))
|
|
19
|
-
return "string";
|
|
20
|
-
if (!isNaN(Number(text)))
|
|
21
|
-
return "number";
|
|
22
|
-
if (!(parseLength(text) instanceof Diagnostic))
|
|
23
|
-
return "length";
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
11
|
+
export const BOOL_RE = /^(true|false)$/;
|
|
26
12
|
export const DEFAULT_FONT_SIZE = 18;
|
|
27
13
|
export const DEFAULT_NUMBER_WEIGHT = 700;
|
|
28
14
|
// 解析上下文
|
|
@@ -55,14 +41,13 @@ export class ParserContext {
|
|
|
55
41
|
description: "开启严格的语法检查"
|
|
56
42
|
}
|
|
57
43
|
};
|
|
58
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* 根解析器为 0,内容参数和大括号子解析器依次加一
|
|
46
|
+
* 分组只存在于 AST 路径,导致语法糖并不会和函数调用一样开辟新的层级,因此只有 0(顶层)是可信的,非零的具体数值不可依赖
|
|
47
|
+
*/
|
|
59
48
|
scopeDepth;
|
|
60
49
|
/** 源代码 */
|
|
61
50
|
source;
|
|
62
|
-
/** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
|
|
63
|
-
syntax;
|
|
64
|
-
/** syntax.tokens 前缀注释区间的扫描游标 */
|
|
65
|
-
commentIndex;
|
|
66
51
|
/** 解析过程中产生的诊断信息,包含错误和警告 */
|
|
67
52
|
diagnostics;
|
|
68
53
|
/** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
|
|
@@ -71,15 +56,15 @@ export class ParserContext {
|
|
|
71
56
|
documentDeclarations;
|
|
72
57
|
/** 函数定义查找表 */
|
|
73
58
|
functions;
|
|
74
|
-
/**
|
|
59
|
+
/** 第一轮原子去糖:将文本转为函数调用,或 sugar 标记;第二轮由 sugar 节点自带的 relation 处理 */
|
|
75
60
|
deSugarAtomFns;
|
|
76
|
-
/** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
|
|
77
|
-
deSugarRelationFns;
|
|
78
61
|
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
79
62
|
labelableNodes;
|
|
80
63
|
/** 当前正在实例化的显式函数调用节点,供构造函数按需读取原始参数区间 */
|
|
81
64
|
callNode;
|
|
82
65
|
nodes; // 解析结果
|
|
66
|
+
/** 等待右操作数的中缀语法糖,见 deferRightOperand */
|
|
67
|
+
pendingRight = [];
|
|
83
68
|
constructor(ctx) {
|
|
84
69
|
if (ctx instanceof ParserContext) {
|
|
85
70
|
// 构建子上下文
|
|
@@ -87,12 +72,9 @@ export class ParserContext {
|
|
|
87
72
|
this.documentDeclarations = ctx.documentDeclarations;
|
|
88
73
|
this.source = ctx.source;
|
|
89
74
|
this.diagnostics = ctx.diagnostics; // 诊断信息全局共享
|
|
90
|
-
this.syntax = ctx.syntax;
|
|
91
|
-
this.commentIndex = ctx.commentIndex;
|
|
92
75
|
this.variables = { ...ctx.variables }; // 继承但不修改父上下文的变量
|
|
93
76
|
this.functions = ctx.functions; // 函数定义全局共享
|
|
94
77
|
this.deSugarAtomFns = ctx.deSugarAtomFns; // 去糖方法全局共享
|
|
95
|
-
this.deSugarRelationFns = ctx.deSugarRelationFns; // 去糖方法全局共享
|
|
96
78
|
this.labelableNodes = ctx.labelableNodes; // 标签是全局属性
|
|
97
79
|
this.nodes = []; // 待消费节点不共享,每个上下文单独维护
|
|
98
80
|
}
|
|
@@ -101,36 +83,21 @@ export class ParserContext {
|
|
|
101
83
|
this.documentDeclarations = {};
|
|
102
84
|
this.source = ctx.source;
|
|
103
85
|
this.diagnostics = ctx.diagnostics ?? [];
|
|
104
|
-
this.syntax = {
|
|
105
|
-
tokens: (ctx.commentSpans ?? []).map(span => ({ kind: "comment", span })),
|
|
106
|
-
calls: [],
|
|
107
|
-
};
|
|
108
|
-
this.commentIndex = 0;
|
|
109
86
|
this.variables = Object.fromEntries(Object.entries(ParserContext.systemVariables).map(([name, definition]) => [name, definition.default]));
|
|
110
87
|
Object.assign(this.variables, ctx.variables);
|
|
111
88
|
this.functions = ctx.functions ?? new Map();
|
|
112
89
|
this.labelableNodes = ctx.labelableNodes ?? [];
|
|
113
90
|
this.nodes = ctx.toConsume ?? [];
|
|
114
|
-
|
|
115
|
-
[this.deSugarAtomFns, this.deSugarRelationFns] = ParserContext.getDeSugarFns(this.functions.values());
|
|
116
|
-
else
|
|
117
|
-
this.deSugarAtomFns = [], this.deSugarRelationFns = [];
|
|
91
|
+
this.deSugarAtomFns = ParserContext.getDeSugarFns(this.functions.values());
|
|
118
92
|
}
|
|
119
93
|
}
|
|
120
94
|
static getDeSugarFns(classes) {
|
|
121
|
-
//
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (deSugarAtom && deSugarAtom !== ASTFunctionNode.deSugarAtom)
|
|
128
|
-
deSugarAtomFns.push(deSugarAtom);
|
|
129
|
-
const deSugarRelation = fnClass.deSugarRelation;
|
|
130
|
-
if (deSugarRelation && deSugarRelation !== ASTFunctionNode.deSugarRelation)
|
|
131
|
-
deSugarRelationFns.push(deSugarRelation);
|
|
132
|
-
}
|
|
133
|
-
return [deSugarAtomFns, deSugarRelationFns];
|
|
95
|
+
// 按函数去重:一个类有多个别名
|
|
96
|
+
const fns = new Set();
|
|
97
|
+
for (const fnClass of classes)
|
|
98
|
+
fns.add(fnClass.deSugarAtom);
|
|
99
|
+
fns.delete(ASTFunctionNode.deSugarAtom);
|
|
100
|
+
return [...fns];
|
|
134
101
|
}
|
|
135
102
|
pushNode(node) {
|
|
136
103
|
this.nodes.push(node);
|
|
@@ -163,100 +130,17 @@ export class ParserContext {
|
|
|
163
130
|
map.set(name.toLowerCase(), funcClass);
|
|
164
131
|
}
|
|
165
132
|
}
|
|
166
|
-
|
|
133
|
+
this.deSugarAtomFns = ParserContext.getDeSugarFns(this.functions.values());
|
|
167
134
|
}
|
|
168
135
|
//====== 解析相关 ======//
|
|
169
136
|
/**
|
|
170
|
-
* 扫描 GrammarNode 并构造 AST
|
|
171
|
-
*
|
|
137
|
+
* 扫描 GrammarNode 并构造 AST,用于最终渲染
|
|
138
|
+
* 编辑器语法视图不构造 AST,见 syntax.ts 的 collectSyntax
|
|
172
139
|
*/
|
|
173
140
|
parse(start = 0, end = this.source.length) {
|
|
174
141
|
this.makeNodes(this.parseGrammar(start, end));
|
|
175
142
|
return this.nodes;
|
|
176
143
|
}
|
|
177
|
-
/**
|
|
178
|
-
* 词法层:容错扫描 GrammarNode 并收集 syntax
|
|
179
|
-
* 用于防抖时的编辑器高亮和补全
|
|
180
|
-
*/
|
|
181
|
-
parseSyntax(start = 0, end = this.source.length) {
|
|
182
|
-
this.parseGrammar(start, end, true);
|
|
183
|
-
if (this.scopeDepth === 0) {
|
|
184
|
-
const cmp = (a, b) => a.span.start - b.span.start || a.span.end - b.span.end;
|
|
185
|
-
this.syntax.tokens.sort(cmp);
|
|
186
|
-
this.syntax.calls.sort(cmp);
|
|
187
|
-
}
|
|
188
|
-
return this.syntax;
|
|
189
|
-
}
|
|
190
|
-
// syntax 路径独占这些 span,AST 路径不会读取或改写它们;跨注释区间按前缀 comment token 切开以保证互不重叠
|
|
191
|
-
addSyntax(kind, span) {
|
|
192
|
-
if (!span || span.start >= span.end)
|
|
193
|
-
return;
|
|
194
|
-
let start = span.start;
|
|
195
|
-
const tokens = this.syntax.tokens;
|
|
196
|
-
while (tokens[this.commentIndex]?.kind === "comment" && tokens[this.commentIndex].span.end <= start) {
|
|
197
|
-
this.commentIndex++;
|
|
198
|
-
}
|
|
199
|
-
for (let i = this.commentIndex; i < tokens.length; i++) {
|
|
200
|
-
const token = tokens[i];
|
|
201
|
-
if (token.kind !== "comment" || token.span.start >= span.end)
|
|
202
|
-
break;
|
|
203
|
-
if (start < token.span.start) {
|
|
204
|
-
tokens.push({ kind, span: { start, end: token.span.start } });
|
|
205
|
-
}
|
|
206
|
-
start = token.span.end;
|
|
207
|
-
this.commentIndex = i + 1;
|
|
208
|
-
}
|
|
209
|
-
if (start < span.end) {
|
|
210
|
-
tokens.push({ kind, span: { start, end: span.end } });
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
/** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
|
|
214
|
-
recordSyntax(node) {
|
|
215
|
-
if (node.kind === "brace") {
|
|
216
|
-
this.addSyntax("punctuation", { start: node.span.start, end: node.span.start + 1 });
|
|
217
|
-
const close = Math.min(this.source.length, node.span.end - 1);
|
|
218
|
-
new ParserContext(this).parseSyntax(node.span.start + 1, close);
|
|
219
|
-
if (this.source[close] === "}")
|
|
220
|
-
this.addSyntax("punctuation", { start: close, end: close + 1 });
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
if (node.kind === "label") {
|
|
224
|
-
this.addSyntax("label", node.span);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
if (node.kind === "sugar" || node.typed) {
|
|
228
|
-
if (Array.isArray(node.syntaxKind)) {
|
|
229
|
-
for (const token of node.syntaxKind)
|
|
230
|
-
this.addSyntax(token.kind, token.span);
|
|
231
|
-
}
|
|
232
|
-
else
|
|
233
|
-
this.addSyntax(node.syntaxKind ?? "operator", node.span);
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
// syntax.calls 只暴露源码结构,不带 grammar 阶段的字段
|
|
237
|
-
const { kind, typed, syntaxKind, ...call } = node;
|
|
238
|
-
this.syntax.calls.push(call);
|
|
239
|
-
this.addSyntax("function", call.nameSpan);
|
|
240
|
-
this.addSyntax("punctuation", call.openParenSpan);
|
|
241
|
-
const def = this.functions.get(call.name.toLowerCase())?.def;
|
|
242
|
-
for (let index = 0; index < call.args.length; index++) {
|
|
243
|
-
const arg = call.args[index];
|
|
244
|
-
this.addSyntax("property", arg.nameSpan);
|
|
245
|
-
this.addSyntax("punctuation", arg.equalsSpan);
|
|
246
|
-
const type = def && resolveArgType(def, callArgName(this.source, arg), index);
|
|
247
|
-
if (type === "content") {
|
|
248
|
-
new ParserContext(this).parseSyntax(arg.valueSpan.start, arg.valueSpan.end);
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
const text = this.source.slice(arg.valueSpan.start, arg.valueSpan.end);
|
|
252
|
-
const kind = classifySyntaxValue(type, text);
|
|
253
|
-
if (kind)
|
|
254
|
-
this.addSyntax(kind, arg.valueSpan);
|
|
255
|
-
}
|
|
256
|
-
this.addSyntax("punctuation", arg.commaSpan);
|
|
257
|
-
}
|
|
258
|
-
this.addSyntax("punctuation", call.closeParenSpan);
|
|
259
|
-
}
|
|
260
144
|
/**
|
|
261
145
|
* 向前寻找最后一个非文本节点下标;
|
|
262
146
|
* 若末尾是标签,则将“被标注节点..标签”原位打包为 ASTBraceNode
|
|
@@ -280,42 +164,39 @@ export class ParserContext {
|
|
|
280
164
|
}
|
|
281
165
|
return left;
|
|
282
166
|
}
|
|
283
|
-
/**
|
|
284
|
-
|
|
167
|
+
/**
|
|
168
|
+
* 摘出末尾最后一个操作数(含其后置标签)
|
|
169
|
+
* 传入 chain 时,若该操作数恰是同一 chain 挂起合成的唯一右操作数,先合成再返回:同类中缀由此左结合
|
|
170
|
+
*/
|
|
171
|
+
takeLeftOperand(chain) {
|
|
285
172
|
const index = this.findLastOperandIndex();
|
|
286
173
|
if (index < 0)
|
|
287
174
|
return null;
|
|
288
175
|
const node = this.nodes[index];
|
|
289
176
|
this.nodes.length = index;
|
|
290
|
-
|
|
177
|
+
const top = this.pendingRight.at(-1);
|
|
178
|
+
if (chain === undefined || top?.chain !== chain || this.nodes.some(n => !(n instanceof ASTTextNode)))
|
|
179
|
+
return node;
|
|
180
|
+
this.pendingRight.pop();
|
|
181
|
+
this.nodes = top.left;
|
|
182
|
+
return top.combine(node);
|
|
291
183
|
}
|
|
292
|
-
/**
|
|
293
|
-
|
|
294
|
-
|
|
184
|
+
/**
|
|
185
|
+
* 中缀语法糖的右操作数是本层剩余部分的首个非文本节点,要等本层解析完才能确定。
|
|
186
|
+
* 这里只挂起:已有节点留作左侧,后续节点解析到新数组,由 makeNodes 收尾时交给 combine;没有右操作数时 combine 收到 undefined
|
|
187
|
+
* 默认右结合;登记 chain 后,下一个同 chain 的中缀可经 takeLeftOperand 提前合成本帧。提前合成的结果不会进入 nodes,combine 不应有登记之类的副作用
|
|
188
|
+
*/
|
|
189
|
+
deferRightOperand(combine, chain) {
|
|
190
|
+
this.pendingRight.push({ left: this.nodes, combine, chain });
|
|
295
191
|
this.nodes = [];
|
|
296
|
-
try {
|
|
297
|
-
this.makeNodes(nodes, at);
|
|
298
|
-
for (let i = 0; i < this.nodes.length; i++) {
|
|
299
|
-
const right = this.nodes[i];
|
|
300
|
-
if (right instanceof ASTTextNode)
|
|
301
|
-
continue;
|
|
302
|
-
storage.push(combine(right));
|
|
303
|
-
while (++i < this.nodes.length)
|
|
304
|
-
storage.push(this.nodes[i]);
|
|
305
|
-
return nodes.length;
|
|
306
|
-
}
|
|
307
|
-
return null;
|
|
308
|
-
}
|
|
309
|
-
finally {
|
|
310
|
-
this.nodes = storage;
|
|
311
|
-
}
|
|
312
192
|
}
|
|
313
193
|
/**
|
|
314
194
|
* 第二轮去糖 & 函数节点创建
|
|
315
195
|
* 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
|
|
316
196
|
*/
|
|
317
|
-
makeNodes(nodes
|
|
318
|
-
|
|
197
|
+
makeNodes(nodes) {
|
|
198
|
+
const base = this.pendingRight.length; // 只收尾本次登记的合成,同一上下文可重入
|
|
199
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
319
200
|
const node = nodes[i];
|
|
320
201
|
if (typeof node === "number") {
|
|
321
202
|
this.pushText(node, node + 1);
|
|
@@ -346,18 +227,29 @@ export class ParserContext {
|
|
|
346
227
|
}
|
|
347
228
|
break;
|
|
348
229
|
case "sugar":
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
this.pushText(node.span.start, node.span.end); // 匹配失败,保留为文本
|
|
230
|
+
const next = node.relation(this, nodes, i);
|
|
231
|
+
if (next === null)
|
|
232
|
+
this.pushText(node.span.start, node.span.end); // 发出者放弃,保留为文本
|
|
233
|
+
else
|
|
234
|
+
i = next - 1; // 循环会+1
|
|
357
235
|
break;
|
|
358
236
|
}
|
|
359
237
|
}
|
|
360
|
-
|
|
238
|
+
// 由内向外合成,最外层的左侧就是原输出数组;各层剩余节点倒序暂存、最后接回,整体线性
|
|
239
|
+
const tail = [];
|
|
240
|
+
while (this.pendingRight.length > base) {
|
|
241
|
+
const { left, combine } = this.pendingRight.pop();
|
|
242
|
+
const right = this.nodes;
|
|
243
|
+
let i = 0;
|
|
244
|
+
while (right[i] instanceof ASTTextNode)
|
|
245
|
+
i++;
|
|
246
|
+
left.push(combine(right[i]));
|
|
247
|
+
for (let k = right.length - 1; k > i; k--)
|
|
248
|
+
tail.push(right[k]);
|
|
249
|
+
this.nodes = left;
|
|
250
|
+
}
|
|
251
|
+
while (tail.length)
|
|
252
|
+
this.nodes.push(tail.pop());
|
|
361
253
|
}
|
|
362
254
|
/** 追加文本,与紧邻的上一个文本节点合并;新建区间对象,避免改写语法节点的 span */
|
|
363
255
|
pushText(start, end) {
|
|
@@ -371,23 +263,23 @@ export class ParserContext {
|
|
|
371
263
|
* 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
|
|
372
264
|
* 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
|
|
373
265
|
* 必须将第一阶段语法糖识别放到grammar解析中,比如 `@fn()`,引号若作为语法糖会导致后面的 `@` 无效
|
|
374
|
-
*
|
|
375
|
-
*
|
|
266
|
+
* observe 在每个节点识别后、校验前收到该节点及当前上下文;返回 true 即接管该节点:其语法错误只记录诊断不抛出(保留 partial call),
|
|
267
|
+
* 供编辑器在输入途中继续工作。不传或返回 false 时照常抛出。observe 不得改变“识别出什么结构”,否则两条路径会认不同的语言
|
|
376
268
|
*/
|
|
377
|
-
parseGrammar(p, end,
|
|
269
|
+
parseGrammar(p, end, observe) {
|
|
378
270
|
const nodes = [];
|
|
271
|
+
/** 返回观察者是否接管该节点 */
|
|
379
272
|
const emit = (node) => {
|
|
380
273
|
nodes.push(node);
|
|
381
|
-
|
|
382
|
-
this.recordSyntax(node);
|
|
274
|
+
return observe?.(node, this) ?? false;
|
|
383
275
|
};
|
|
384
276
|
outer: while ((p = skipSpaces(this.source, p, end)) < end) {
|
|
385
277
|
const ch = this.source[p];
|
|
386
278
|
if (ch === "@") {
|
|
387
279
|
const call = readCall(this.source, p, end);
|
|
388
280
|
if (call) {
|
|
389
|
-
//
|
|
390
|
-
emit(Object.assign(call, {
|
|
281
|
+
// 先交给观察者再校验:抛错时编辑器仍拿得到已识别的结构
|
|
282
|
+
const tolerant = emit(Object.assign(call, {
|
|
391
283
|
kind: "call",
|
|
392
284
|
typed: false
|
|
393
285
|
}));
|
|
@@ -400,7 +292,7 @@ export class ParserContext {
|
|
|
400
292
|
else if (!isNamed && positionArgEnd) {
|
|
401
293
|
// 位置参数不允许出现在命名参数之后
|
|
402
294
|
const error = Diagnostic.error.PosAfterNamedArg(call.name, arg.span);
|
|
403
|
-
if (!
|
|
295
|
+
if (!tolerant)
|
|
404
296
|
throw error;
|
|
405
297
|
this.diagnostics.push(error);
|
|
406
298
|
}
|
|
@@ -408,7 +300,7 @@ export class ParserContext {
|
|
|
408
300
|
// 括号未闭合
|
|
409
301
|
if (!call.closeParenSpan) {
|
|
410
302
|
const fatal = Diagnostic.error.UnterminatedCall(call.name, call.span);
|
|
411
|
-
if (!
|
|
303
|
+
if (!tolerant)
|
|
412
304
|
throw fatal;
|
|
413
305
|
this.diagnostics.push(fatal);
|
|
414
306
|
}
|
|
@@ -488,13 +380,16 @@ export class ParserContext {
|
|
|
488
380
|
const key = name ?? i;
|
|
489
381
|
if (type === void 0) {
|
|
490
382
|
if (!def.allowExtraArgs) {
|
|
491
|
-
this.diagnostics.push(
|
|
383
|
+
this.diagnostics.push(name ?
|
|
384
|
+
Diagnostic.warning.UnknownArg(callNode.name, this.source.slice(arg.nameSpan.start, arg.nameSpan.end), arg.span) :
|
|
385
|
+
Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.filter(arg => !arg.namedOnly).length, i + 1, arg.valueSpan));
|
|
492
386
|
}
|
|
493
387
|
else
|
|
494
388
|
args.set(key, arg);
|
|
495
389
|
continue;
|
|
496
390
|
}
|
|
497
|
-
const
|
|
391
|
+
const required = resolveArgDef(def, name, i)?.default === null;
|
|
392
|
+
const value = this.parseArgWithType(arg.valueSpan, type, callNode.span.start, required);
|
|
498
393
|
if (value !== null)
|
|
499
394
|
args.set(key, value);
|
|
500
395
|
}
|
|
@@ -509,8 +404,9 @@ export class ParserContext {
|
|
|
509
404
|
* 根据参数类型解析参数值,解析过程中会记录诊断信息
|
|
510
405
|
* funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
|
|
511
406
|
* 解析成功返回值 解析失败返回null
|
|
407
|
+
* required 的 content 解析失败时直接抛出内部错误:返回 null 只会换来掩盖真因的缺参错误
|
|
512
408
|
*/
|
|
513
|
-
parseArgWithType(span, type, funcStart) {
|
|
409
|
+
parseArgWithType(span, type, funcStart, required = false) {
|
|
514
410
|
// 先去空白
|
|
515
411
|
const r = trimRange(this.source, span.start, span.end);
|
|
516
412
|
const start = r.start, end = r.end;
|
|
@@ -557,7 +453,7 @@ export class ParserContext {
|
|
|
557
453
|
else
|
|
558
454
|
delete target.label;
|
|
559
455
|
}
|
|
560
|
-
if (this.variables.strict || !(e instanceof Diagnostic))
|
|
456
|
+
if (required || this.variables.strict || !(e instanceof Diagnostic))
|
|
561
457
|
throw e;
|
|
562
458
|
// 当前参数会被跳过,因此在恢复点记录被吞掉的具体错误
|
|
563
459
|
this.diagnostics.push(e, Diagnostic.warning.InvalidContent(r));
|
|
@@ -79,15 +79,14 @@ export function preprocessSource(source) {
|
|
|
79
79
|
};
|
|
80
80
|
/**
|
|
81
81
|
* 扫描状态说明:
|
|
82
|
-
* -
|
|
83
|
-
* -
|
|
82
|
+
* - quoteEnd:当前/上一个字符串的闭引号位置,前瞻失败后为 -1;`i <= quoteEnd` 即在字符串内,转义规则只由 findClosingQuote 决定
|
|
83
|
+
* - noPairedQuoteAfter:前瞻失败过,后文不再有可配对的引号
|
|
84
84
|
* - pendingCommentStart:>=0 表示已遇到 `%`,等待到行尾/EOF 收口注释
|
|
85
85
|
* - tailBackslashRunStart/tailBackslashRunCount:
|
|
86
86
|
* 当前行末尾候选连续 `\` 片段(允许后面跟空白),用于换行处一次性判定
|
|
87
87
|
*/
|
|
88
|
-
let
|
|
88
|
+
let quoteEnd = -1;
|
|
89
89
|
let noPairedQuoteAfter = false;
|
|
90
|
-
let escaped = false;
|
|
91
90
|
let pendingCommentStart = -1;
|
|
92
91
|
let tailBackslashRunStart = -1;
|
|
93
92
|
let tailBackslashRunCount = 0;
|
|
@@ -120,23 +119,16 @@ export function preprocessSource(source) {
|
|
|
120
119
|
}
|
|
121
120
|
if (isOddBackslashRun)
|
|
122
121
|
pushReplaceRange(i, i + newlineWidth);
|
|
123
|
-
else if (
|
|
122
|
+
else if (i > quoteEnd && newlineWidth === 2)
|
|
124
123
|
pushReplaceRange(i, i + 1);
|
|
125
124
|
// 新行重置行内状态
|
|
126
125
|
tailBackslashRunStart = -1;
|
|
127
126
|
tailBackslashRunCount = 0;
|
|
128
|
-
escaped = false;
|
|
129
127
|
i += newlineWidth;
|
|
130
128
|
continue;
|
|
131
129
|
}
|
|
132
|
-
//
|
|
133
|
-
if (
|
|
134
|
-
if (escaped)
|
|
135
|
-
escaped = false;
|
|
136
|
-
else if (ch === CHAR_BACKSLASH)
|
|
137
|
-
escaped = true;
|
|
138
|
-
else if (ch === inQuote)
|
|
139
|
-
inQuote = 0;
|
|
130
|
+
// 字符串内不识别引号与注释,只参与行尾反斜杠判定;单独成快路径,字符串密集时比与下方合流快约 20%
|
|
131
|
+
if (i <= quoteEnd) {
|
|
140
132
|
if (!isNonLineWhitespace(ch))
|
|
141
133
|
updateLineTailState(i, ch);
|
|
142
134
|
i++;
|
|
@@ -146,17 +138,12 @@ export function preprocessSource(source) {
|
|
|
146
138
|
// 配对才算字符串,否则一个孤立引号会让后文的 `%` 全部失去注释语义
|
|
147
139
|
// 一次前瞻失败即证明剩余源码里没有可配对的引号,置位后不再前瞻,整体仍是 O(n)
|
|
148
140
|
if (!noPairedQuoteAfter) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
else
|
|
152
|
-
inQuote = ch;
|
|
141
|
+
quoteEnd = findClosingQuote(source, i, sourceLength);
|
|
142
|
+
noPairedQuoteAfter = quoteEnd < 0;
|
|
153
143
|
}
|
|
154
|
-
updateLineTailState(i, ch);
|
|
155
|
-
i++;
|
|
156
|
-
continue;
|
|
157
144
|
}
|
|
158
|
-
|
|
159
|
-
|
|
145
|
+
else if (ch === CHAR_PERCENT) {
|
|
146
|
+
// `%` 到行尾是注释;直接跳到换行,减少无效判断
|
|
160
147
|
pendingCommentStart = i;
|
|
161
148
|
i++;
|
|
162
149
|
while (i < sourceLength) {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SyntaxAnalysis } from "./grammarType.js";
|
|
2
|
+
import { ParserContext } from "./parserContext.js";
|
|
3
|
+
import type { SourceSpan } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* 编辑器语法视图:以词法层模式观察 parseGrammar,收集着色 token 与调用结构,不构造 AST
|
|
6
|
+
* 节点按源码顺序发出、子区间就地递归,所以 tokens 有序且互不重叠,calls 按起点严格递增,无需排序;
|
|
7
|
+
* 注释在预处理阶段已被掩码,最后从 commentSpans 归并回来
|
|
8
|
+
*/
|
|
9
|
+
export declare function collectSyntax(parser: ParserContext, commentSpans: SourceSpan[]): SyntaxAnalysis;
|