jpfun 1.0.0 → 1.1.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/functions/bar/index.js +3 -0
- package/dist/functions/default.js +2 -1
- package/dist/functions/gliss/index.js +81 -7
- package/dist/functions/grace/index.d.ts +1 -8
- package/dist/functions/grace/index.js +20 -48
- package/dist/functions/key/index.js +1 -0
- package/dist/functions/note/index.d.ts +5 -2
- package/dist/functions/note/index.js +158 -13
- package/dist/functions/note/source-transform.d.ts +8 -2
- package/dist/functions/note/source-transform.js +25 -11
- package/dist/functions/page/index.d.ts +1 -36
- package/dist/functions/page/index.js +4 -4
- package/dist/functions/set/index.js +2 -2
- package/dist/functions/stack/index.js +6 -4
- package/dist/functions/temporal.d.ts +3 -0
- package/dist/functions/text/index.js +2 -2
- package/dist/functions/up/index.js +9 -7
- 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.js +2 -2
- package/dist/functions/voice/voices.d.ts +2 -1
- package/dist/functions/voice/voices.js +2 -2
- package/dist/index.d.ts +6 -34
- package/dist/index.js +6 -34
- package/dist/jpfun.min.js +41 -41
- package/dist/layout/engine.js +13 -21
- package/dist/layout/model.d.ts +1 -1
- package/dist/layout/types.d.ts +2 -1
- package/dist/lowering/accidentalMemory.d.ts +33 -0
- package/dist/lowering/accidentalMemory.js +84 -0
- package/dist/lowering/loweringContext.js +4 -0
- package/dist/parser/grammarType.d.ts +6 -1
- 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 +25 -25
- package/dist/parser/parserContext.js +54 -146
- package/dist/parser/preprocess.js +10 -23
- package/dist/parser/syntax.d.ts +9 -0
- package/dist/parser/syntax.js +109 -0
- package/dist/pipeline.js +3 -2
- package/dist/playback/compile.js +4 -6
- package/dist/playback/event.d.ts +1 -2
- package/dist/playback/event.js +2 -3
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/grammar.md +1 -0
- package/package.json +1 -1
- package/dist/functions/voice/index.d.ts +0 -2
- package/dist/functions/voice/index.js +0 -2
|
@@ -8,21 +8,7 @@ import { readLabel } from "./parse-utils/label-utils.js";
|
|
|
8
8
|
import { parseLength } from "./parse-utils/length-utils.js";
|
|
9
9
|
import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, 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` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
|
|
@@ -80,6 +65,8 @@ export class ParserContext {
|
|
|
80
65
|
/** 当前正在实例化的显式函数调用节点,供构造函数按需读取原始参数区间 */
|
|
81
66
|
callNode;
|
|
82
67
|
nodes; // 解析结果
|
|
68
|
+
/** 等待右操作数的中缀语法糖,见 deferRightOperand */
|
|
69
|
+
pendingRight = [];
|
|
83
70
|
constructor(ctx) {
|
|
84
71
|
if (ctx instanceof ParserContext) {
|
|
85
72
|
// 构建子上下文
|
|
@@ -87,8 +74,6 @@ export class ParserContext {
|
|
|
87
74
|
this.documentDeclarations = ctx.documentDeclarations;
|
|
88
75
|
this.source = ctx.source;
|
|
89
76
|
this.diagnostics = ctx.diagnostics; // 诊断信息全局共享
|
|
90
|
-
this.syntax = ctx.syntax;
|
|
91
|
-
this.commentIndex = ctx.commentIndex;
|
|
92
77
|
this.variables = { ...ctx.variables }; // 继承但不修改父上下文的变量
|
|
93
78
|
this.functions = ctx.functions; // 函数定义全局共享
|
|
94
79
|
this.deSugarAtomFns = ctx.deSugarAtomFns; // 去糖方法全局共享
|
|
@@ -101,11 +86,6 @@ export class ParserContext {
|
|
|
101
86
|
this.documentDeclarations = {};
|
|
102
87
|
this.source = ctx.source;
|
|
103
88
|
this.diagnostics = ctx.diagnostics ?? [];
|
|
104
|
-
this.syntax = {
|
|
105
|
-
tokens: (ctx.commentSpans ?? []).map(span => ({ kind: "comment", span })),
|
|
106
|
-
calls: [],
|
|
107
|
-
};
|
|
108
|
-
this.commentIndex = 0;
|
|
109
89
|
this.variables = Object.fromEntries(Object.entries(ParserContext.systemVariables).map(([name, definition]) => [name, definition.default]));
|
|
110
90
|
Object.assign(this.variables, ctx.variables);
|
|
111
91
|
this.functions = ctx.functions ?? new Map();
|
|
@@ -167,96 +147,13 @@ export class ParserContext {
|
|
|
167
147
|
}
|
|
168
148
|
//====== 解析相关 ======//
|
|
169
149
|
/**
|
|
170
|
-
* 扫描 GrammarNode 并构造 AST
|
|
171
|
-
*
|
|
150
|
+
* 扫描 GrammarNode 并构造 AST,用于最终渲染
|
|
151
|
+
* 编辑器语法视图不构造 AST,见 syntax.ts 的 collectSyntax
|
|
172
152
|
*/
|
|
173
153
|
parse(start = 0, end = this.source.length) {
|
|
174
154
|
this.makeNodes(this.parseGrammar(start, end));
|
|
175
155
|
return this.nodes;
|
|
176
156
|
}
|
|
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
157
|
/**
|
|
261
158
|
* 向前寻找最后一个非文本节点下标;
|
|
262
159
|
* 若末尾是标签,则将“被标注节点..标签”原位打包为 ASTBraceNode
|
|
@@ -280,42 +177,39 @@ export class ParserContext {
|
|
|
280
177
|
}
|
|
281
178
|
return left;
|
|
282
179
|
}
|
|
283
|
-
/**
|
|
284
|
-
|
|
180
|
+
/**
|
|
181
|
+
* 摘出末尾最后一个操作数(含其后置标签)
|
|
182
|
+
* 传入 chain 时,若该操作数恰是同一 chain 挂起合成的唯一右操作数,先合成再返回:同类中缀由此左结合
|
|
183
|
+
*/
|
|
184
|
+
takeLeftOperand(chain) {
|
|
285
185
|
const index = this.findLastOperandIndex();
|
|
286
186
|
if (index < 0)
|
|
287
187
|
return null;
|
|
288
188
|
const node = this.nodes[index];
|
|
289
189
|
this.nodes.length = index;
|
|
290
|
-
|
|
190
|
+
const top = this.pendingRight.at(-1);
|
|
191
|
+
if (chain === undefined || top?.chain !== chain || this.nodes.some(n => !(n instanceof ASTTextNode)))
|
|
192
|
+
return node;
|
|
193
|
+
this.pendingRight.pop();
|
|
194
|
+
this.nodes = top.left;
|
|
195
|
+
return top.combine(node);
|
|
291
196
|
}
|
|
292
|
-
/**
|
|
293
|
-
|
|
294
|
-
|
|
197
|
+
/**
|
|
198
|
+
* 中缀语法糖的右操作数是本层剩余部分的首个非文本节点,要等本层解析完才能确定。
|
|
199
|
+
* 这里只挂起:已有节点留作左侧,后续节点解析到新数组,由 makeNodes 收尾时交给 combine;没有右操作数时 combine 收到 undefined
|
|
200
|
+
* 默认右结合;登记 chain 后,下一个同 chain 的中缀可经 takeLeftOperand 提前合成本帧。提前合成的结果不会进入 nodes,combine 不应有登记之类的副作用
|
|
201
|
+
*/
|
|
202
|
+
deferRightOperand(combine, chain) {
|
|
203
|
+
this.pendingRight.push({ left: this.nodes, combine, chain });
|
|
295
204
|
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
205
|
}
|
|
313
206
|
/**
|
|
314
207
|
* 第二轮去糖 & 函数节点创建
|
|
315
208
|
* 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
|
|
316
209
|
*/
|
|
317
|
-
makeNodes(nodes
|
|
318
|
-
|
|
210
|
+
makeNodes(nodes) {
|
|
211
|
+
const base = this.pendingRight.length; // 只收尾本次登记的合成,同一上下文可重入
|
|
212
|
+
outer: for (let i = 0; i < nodes.length; i++) {
|
|
319
213
|
const node = nodes[i];
|
|
320
214
|
if (typeof node === "number") {
|
|
321
215
|
this.pushText(node, node + 1);
|
|
@@ -357,7 +251,21 @@ export class ParserContext {
|
|
|
357
251
|
break;
|
|
358
252
|
}
|
|
359
253
|
}
|
|
360
|
-
|
|
254
|
+
// 由内向外合成,最外层的左侧就是原输出数组;各层剩余节点倒序暂存、最后接回,整体线性
|
|
255
|
+
const tail = [];
|
|
256
|
+
while (this.pendingRight.length > base) {
|
|
257
|
+
const { left, combine } = this.pendingRight.pop();
|
|
258
|
+
const right = this.nodes;
|
|
259
|
+
let i = 0;
|
|
260
|
+
while (right[i] instanceof ASTTextNode)
|
|
261
|
+
i++;
|
|
262
|
+
left.push(combine(right[i]));
|
|
263
|
+
for (let k = right.length - 1; k > i; k--)
|
|
264
|
+
tail.push(right[k]);
|
|
265
|
+
this.nodes = left;
|
|
266
|
+
}
|
|
267
|
+
while (tail.length)
|
|
268
|
+
this.nodes.push(tail.pop());
|
|
361
269
|
}
|
|
362
270
|
/** 追加文本,与紧邻的上一个文本节点合并;新建区间对象,避免改写语法节点的 span */
|
|
363
271
|
pushText(start, end) {
|
|
@@ -371,23 +279,23 @@ export class ParserContext {
|
|
|
371
279
|
* 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
|
|
372
280
|
* 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
|
|
373
281
|
* 必须将第一阶段语法糖识别放到grammar解析中,比如 `@fn()`,引号若作为语法糖会导致后面的 `@` 无效
|
|
374
|
-
*
|
|
375
|
-
*
|
|
282
|
+
* observe 在每个节点识别后、校验前收到该节点及当前上下文;返回 true 即接管该节点:其语法错误只记录诊断不抛出(保留 partial call),
|
|
283
|
+
* 供编辑器在输入途中继续工作。不传或返回 false 时照常抛出。observe 不得改变“识别出什么结构”,否则两条路径会认不同的语言
|
|
376
284
|
*/
|
|
377
|
-
parseGrammar(p, end,
|
|
285
|
+
parseGrammar(p, end, observe) {
|
|
378
286
|
const nodes = [];
|
|
287
|
+
/** 返回观察者是否接管该节点 */
|
|
379
288
|
const emit = (node) => {
|
|
380
289
|
nodes.push(node);
|
|
381
|
-
|
|
382
|
-
this.recordSyntax(node);
|
|
290
|
+
return observe?.(node, this) ?? false;
|
|
383
291
|
};
|
|
384
292
|
outer: while ((p = skipSpaces(this.source, p, end)) < end) {
|
|
385
293
|
const ch = this.source[p];
|
|
386
294
|
if (ch === "@") {
|
|
387
295
|
const call = readCall(this.source, p, end);
|
|
388
296
|
if (call) {
|
|
389
|
-
//
|
|
390
|
-
emit(Object.assign(call, {
|
|
297
|
+
// 先交给观察者再校验:抛错时编辑器仍拿得到已识别的结构
|
|
298
|
+
const tolerant = emit(Object.assign(call, {
|
|
391
299
|
kind: "call",
|
|
392
300
|
typed: false
|
|
393
301
|
}));
|
|
@@ -400,7 +308,7 @@ export class ParserContext {
|
|
|
400
308
|
else if (!isNamed && positionArgEnd) {
|
|
401
309
|
// 位置参数不允许出现在命名参数之后
|
|
402
310
|
const error = Diagnostic.error.PosAfterNamedArg(call.name, arg.span);
|
|
403
|
-
if (!
|
|
311
|
+
if (!tolerant)
|
|
404
312
|
throw error;
|
|
405
313
|
this.diagnostics.push(error);
|
|
406
314
|
}
|
|
@@ -408,7 +316,7 @@ export class ParserContext {
|
|
|
408
316
|
// 括号未闭合
|
|
409
317
|
if (!call.closeParenSpan) {
|
|
410
318
|
const fatal = Diagnostic.error.UnterminatedCall(call.name, call.span);
|
|
411
|
-
if (!
|
|
319
|
+
if (!tolerant)
|
|
412
320
|
throw fatal;
|
|
413
321
|
this.diagnostics.push(fatal);
|
|
414
322
|
}
|
|
@@ -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;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
+
import { resolveArgType } from "../functions/ASTtypes.js";
|
|
3
|
+
import { callArgName } from "./parse-utils/call-utils.js";
|
|
4
|
+
import { parseLength } from "./parse-utils/length-utils.js";
|
|
5
|
+
import { BOOL_RE, ParserContext } from "./parserContext.js";
|
|
6
|
+
/** 有函数参数契约时直接用声明类型;未知参数回落到真正的字面量解析器,不另写一套正则 */
|
|
7
|
+
function classifySyntaxValue(type, text) {
|
|
8
|
+
if (type)
|
|
9
|
+
return type === "content" ? undefined : type;
|
|
10
|
+
if (BOOL_RE.test(text))
|
|
11
|
+
return "boolean";
|
|
12
|
+
if (text.startsWith('"'))
|
|
13
|
+
return "string";
|
|
14
|
+
if (!isNaN(Number(text)))
|
|
15
|
+
return "number";
|
|
16
|
+
if (!(parseLength(text) instanceof Diagnostic))
|
|
17
|
+
return "length";
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 编辑器语法视图:以词法层模式观察 parseGrammar,收集着色 token 与调用结构,不构造 AST
|
|
22
|
+
* 节点按源码顺序发出、子区间就地递归,所以 tokens 有序且互不重叠,calls 按起点严格递增,无需排序;
|
|
23
|
+
* 注释在预处理阶段已被掩码,最后从 commentSpans 归并回来
|
|
24
|
+
*/
|
|
25
|
+
export function collectSyntax(parser, commentSpans) {
|
|
26
|
+
const { source, functions } = parser;
|
|
27
|
+
const tokens = [];
|
|
28
|
+
const calls = [];
|
|
29
|
+
// 词法层不构造 AST,识别阶段的 span 没有其他读写者,可直接引用
|
|
30
|
+
const add = (kind, span) => {
|
|
31
|
+
if (span && span.start < span.end)
|
|
32
|
+
tokens.push({ kind, span });
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* 把已识别的 GrammarNode 映射为源码 token;content/brace 在子上下文中继续递归,作用域深度随之加一
|
|
36
|
+
* 总是返回 true 接管节点:输入途中的语法错误只记诊断,继续扫描以保留已识别的结构
|
|
37
|
+
*/
|
|
38
|
+
const record = (node, ctx) => {
|
|
39
|
+
if (node.kind === "brace") {
|
|
40
|
+
add("punctuation", { start: node.span.start, end: node.span.start + 1 });
|
|
41
|
+
const close = Math.min(source.length, node.span.end - 1);
|
|
42
|
+
new ParserContext(ctx).parseGrammar(node.span.start + 1, close, record);
|
|
43
|
+
if (source[close] === "}")
|
|
44
|
+
add("punctuation", { start: close, end: close + 1 });
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (node.kind === "label") {
|
|
48
|
+
add("label", node.span);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
if (node.kind === "sugar" || node.typed) {
|
|
52
|
+
if (Array.isArray(node.syntaxKind)) {
|
|
53
|
+
for (const token of node.syntaxKind)
|
|
54
|
+
add(token.kind, token.span);
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
add(node.syntaxKind ?? "operator", node.span);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
// calls 只暴露源码结构,不带 grammar 阶段的字段
|
|
61
|
+
const { kind, typed, syntaxKind, ...call } = node;
|
|
62
|
+
calls.push(call);
|
|
63
|
+
add("function", call.nameSpan);
|
|
64
|
+
add("punctuation", call.openParenSpan);
|
|
65
|
+
const def = functions.get(call.name.toLowerCase())?.def;
|
|
66
|
+
for (let index = 0; index < call.args.length; index++) {
|
|
67
|
+
const arg = call.args[index];
|
|
68
|
+
add("property", arg.nameSpan);
|
|
69
|
+
add("punctuation", arg.equalsSpan);
|
|
70
|
+
const type = def && resolveArgType(def, callArgName(source, arg), index);
|
|
71
|
+
if (type === "content") {
|
|
72
|
+
new ParserContext(ctx).parseGrammar(arg.valueSpan.start, arg.valueSpan.end, record);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const valueKind = classifySyntaxValue(type, source.slice(arg.valueSpan.start, arg.valueSpan.end));
|
|
76
|
+
if (valueKind)
|
|
77
|
+
add(valueKind, arg.valueSpan);
|
|
78
|
+
}
|
|
79
|
+
add("punctuation", arg.commaSpan);
|
|
80
|
+
}
|
|
81
|
+
add("punctuation", call.closeParenSpan);
|
|
82
|
+
return true;
|
|
83
|
+
};
|
|
84
|
+
parser.parseGrammar(0, source.length, record);
|
|
85
|
+
return { tokens: interleaveComments(tokens, commentSpans), calls };
|
|
86
|
+
}
|
|
87
|
+
/** 线性归并注释;未裁剪的值(如歌词行)会跨过注释,在注释处切开,保证输出有序且互不重叠 */
|
|
88
|
+
function interleaveComments(tokens, comments) {
|
|
89
|
+
if (comments.length === 0)
|
|
90
|
+
return tokens;
|
|
91
|
+
const out = [];
|
|
92
|
+
let c = 0;
|
|
93
|
+
for (const token of tokens) {
|
|
94
|
+
const { kind, span } = token;
|
|
95
|
+
let start = span.start;
|
|
96
|
+
for (; c < comments.length && comments[c].start < span.end; c++) {
|
|
97
|
+
const comment = comments[c];
|
|
98
|
+
if (start < comment.start)
|
|
99
|
+
out.push({ kind, span: { start, end: comment.start } });
|
|
100
|
+
out.push({ kind: "comment", span: comment });
|
|
101
|
+
start = Math.max(start, comment.end);
|
|
102
|
+
}
|
|
103
|
+
if (start < span.end)
|
|
104
|
+
out.push(start === span.start ? token : { kind, span: { start, end: span.end } });
|
|
105
|
+
}
|
|
106
|
+
for (; c < comments.length; c++)
|
|
107
|
+
out.push({ kind: "comment", span: comments[c] });
|
|
108
|
+
return out;
|
|
109
|
+
}
|
package/dist/pipeline.js
CHANGED
|
@@ -5,6 +5,7 @@ import { layoutDocument, } from "./layout/engine.js";
|
|
|
5
5
|
import { LoweringContext } from "./lowering/loweringContext.js";
|
|
6
6
|
import { ParserContext } from "./parser/parserContext.js";
|
|
7
7
|
import { preprocessSource } from "./parser/preprocess.js";
|
|
8
|
+
import { collectSyntax } from "./parser/syntax.js";
|
|
8
9
|
/**
|
|
9
10
|
* 默认的源码到布局流水线
|
|
10
11
|
*
|
|
@@ -40,10 +41,10 @@ export function compileScore(source, options = {}) {
|
|
|
40
41
|
*/
|
|
41
42
|
export function analyzeScoreSyntax(source, functions = defaultFunctions) {
|
|
42
43
|
const { maskedSource, commentSpans } = preprocessSource(source);
|
|
43
|
-
const parser = new ParserContext({ source: maskedSource
|
|
44
|
+
const parser = new ParserContext({ source: maskedSource });
|
|
44
45
|
parser.registerFunctions(functions);
|
|
45
46
|
return {
|
|
46
|
-
syntax: parser
|
|
47
|
+
syntax: collectSyntax(parser, commentSpans),
|
|
47
48
|
diagnostics: parser.diagnostics,
|
|
48
49
|
};
|
|
49
50
|
}
|
package/dist/playback/compile.js
CHANGED
|
@@ -172,7 +172,6 @@ export function compilePlayback(lowering, options) {
|
|
|
172
172
|
const notes = [];
|
|
173
173
|
const transforms = new Map();
|
|
174
174
|
const activePrograms = new Map();
|
|
175
|
-
let nextEventOrder = 0;
|
|
176
175
|
let nextNoteId = 0;
|
|
177
176
|
const changes = [{ kind: "initial", at: new Fraction(), origins: [] }];
|
|
178
177
|
const factors = new Map();
|
|
@@ -228,7 +227,7 @@ export function compilePlayback(lowering, options) {
|
|
|
228
227
|
const program = node.playbackState?.program;
|
|
229
228
|
if (program !== undefined && activePrograms.get(track) !== program) {
|
|
230
229
|
activePrograms.set(track, program);
|
|
231
|
-
events.push({ kind: "program-change", at: start.clone(),
|
|
230
|
+
events.push({ kind: "program-change", at: start.clone(), track, program });
|
|
232
231
|
}
|
|
233
232
|
const bpm = node.playbackState?.bpm;
|
|
234
233
|
if (bpm !== undefined)
|
|
@@ -267,7 +266,7 @@ export function compilePlayback(lowering, options) {
|
|
|
267
266
|
spanScales.set(span, scales);
|
|
268
267
|
span.end.add(duration);
|
|
269
268
|
},
|
|
270
|
-
emit: event => events.push({ ...event, at: event.at.clone()
|
|
269
|
+
emit: event => events.push({ ...event, at: event.at.clone() }),
|
|
271
270
|
control: (at, apply) => scheduleControl(at, lineage, apply),
|
|
272
271
|
/** 只登记效果身份和比例,范围等结构与连接确定后再计算 */
|
|
273
272
|
scaleFollowingBpm(key, numerator, denominator = 1, options) {
|
|
@@ -437,7 +436,6 @@ export function compilePlayback(lowering, options) {
|
|
|
437
436
|
kind: "tempo",
|
|
438
437
|
at: at.clone(),
|
|
439
438
|
bpm: snapshot.effectiveBpm,
|
|
440
|
-
order: nextEventOrder++,
|
|
441
439
|
});
|
|
442
440
|
}
|
|
443
441
|
}
|
|
@@ -519,12 +517,12 @@ export function compilePlayback(lowering, options) {
|
|
|
519
517
|
const { start, end, track, midi, velocity, percussion, sourceSpans } = sound;
|
|
520
518
|
const noteId = nextNoteId++;
|
|
521
519
|
const on = {
|
|
522
|
-
kind: "note-on", at: start.clone(), noteId,
|
|
520
|
+
kind: "note-on", at: start.clone(), noteId,
|
|
523
521
|
track, midi, velocity, percussion,
|
|
524
522
|
sourceSpans: [...sourceSpans],
|
|
525
523
|
};
|
|
526
524
|
const off = {
|
|
527
|
-
kind: "note-off", at: end.clone(), noteId,
|
|
525
|
+
kind: "note-off", at: end.clone(), noteId,
|
|
528
526
|
track, midi,
|
|
529
527
|
};
|
|
530
528
|
audible.add(track);
|
package/dist/playback/event.d.ts
CHANGED
|
@@ -45,7 +45,6 @@ export interface PlaybackOrigin {
|
|
|
45
45
|
}
|
|
46
46
|
interface PlaybackDraftEventBase {
|
|
47
47
|
at: Fraction;
|
|
48
|
-
order: number;
|
|
49
48
|
}
|
|
50
49
|
/** 声音展开后生成的点事件;最终输出前压实轨道编号并剥离编译期字段 */
|
|
51
50
|
export interface PlaybackDraftNoteOnEvent extends PlaybackDraftEventBase {
|
|
@@ -84,7 +83,7 @@ export interface PlaybackEventInput {
|
|
|
84
83
|
numerator: number;
|
|
85
84
|
denominator: number;
|
|
86
85
|
}
|
|
87
|
-
/**
|
|
86
|
+
/** 同刻先处理系统状态,再关音和开音;相同类别依赖稳定排序保持原发布次序 */
|
|
88
87
|
export declare function comparePlaybackDraftEvents(left: PlaybackDraftEvent, right: PlaybackDraftEvent): number;
|
|
89
88
|
/**
|
|
90
89
|
* 压实发声 Track 编号并剥掉编译期字段,得到稳定、可序列化的公开事件
|
package/dist/playback/event.js
CHANGED
|
@@ -5,10 +5,9 @@ const EVENT_PRIORITY = {
|
|
|
5
5
|
"note-off": 3,
|
|
6
6
|
"note-on": 4,
|
|
7
7
|
};
|
|
8
|
-
/**
|
|
8
|
+
/** 同刻先处理系统状态,再关音和开音;相同类别依赖稳定排序保持原发布次序 */
|
|
9
9
|
export function comparePlaybackDraftEvents(left, right) {
|
|
10
|
-
|
|
11
|
-
return byTime || EVENT_PRIORITY[left.kind] - EVENT_PRIORITY[right.kind] || left.order - right.order;
|
|
10
|
+
return left.at.compare(right.at) || EVENT_PRIORITY[left.kind] - EVENT_PRIORITY[right.kind];
|
|
12
11
|
}
|
|
13
12
|
/**
|
|
14
13
|
* 压实发声 Track 编号并剥掉编译期字段,得到稳定、可序列化的公开事件
|
package/dist/render/index.d.ts
CHANGED
package/dist/render/index.js
CHANGED
package/grammar.md
CHANGED
|
@@ -80,6 +80,7 @@ sidebar:
|
|
|
80
80
|
- `1`~`7`:相对音级,`octave` 默认 `0`;后缀 `'` 升八度、`,` 降八度,如 `1'`、`6,,`。
|
|
81
81
|
- `A`~`G`:绝对音名,`octave` 默认 `4`,如 `C4`、`F#5`;实际音高不随调性变化。
|
|
82
82
|
- `acc`:`#` 升半音、`b` 降半音、`n` 还原,可重复;简写如 `#1'`、`b6,`、`C#4`。
|
|
83
|
+
- `force`:强制显示实际生效的变音号(无变音显示 `n`),不改变音高;简写为音名末尾的 `!`,如 `C4!`、`#1'!`、`@n(C!, #, 4)`。
|
|
83
84
|
- `0` / `Z` 休止,`8` 隐形静音占位,`9` / `X` 打击节拍记号;均忽略升降号与八度。
|
|
84
85
|
- JE 兼容:`(`、`]` 降八度,`[`、`)` 升八度;可嵌套,不越过当前 `{}`。
|
|
85
86
|
|
package/package.json
CHANGED