jpfun 0.2.0 → 0.4.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 +141 -11
- package/dist/converter/midi/convert.d.ts +50 -0
- package/dist/converter/midi/convert.js +700 -0
- package/dist/converter/midi/index.d.ts +5 -0
- package/dist/converter/midi/index.js +11 -0
- package/dist/converter/midi/json.d.ts +38 -0
- package/dist/converter/midi/json.js +1 -0
- package/dist/converter/midi/triplet.d.ts +30 -0
- package/dist/converter/midi/triplet.js +75 -0
- package/dist/converter/musicxml/dom.d.ts +25 -0
- package/dist/converter/musicxml/dom.js +65 -0
- package/dist/converter/musicxml/features.d.ts +51 -0
- package/dist/converter/musicxml/features.js +232 -0
- package/dist/converter/musicxml/index.d.ts +9 -0
- package/dist/converter/musicxml/index.js +1140 -0
- package/dist/converter/musicxml/model.d.ts +123 -0
- package/dist/converter/musicxml/model.js +1 -0
- package/dist/converter/source.d.ts +22 -0
- package/dist/converter/source.js +51 -0
- package/dist/core.d.ts +22 -0
- package/dist/core.js +22 -0
- package/dist/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +51 -31
- package/dist/functions/ASTtypes.js +59 -38
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/arpeggio/index.d.ts +2 -0
- package/dist/functions/arpeggio/index.js +180 -0
- package/dist/functions/arpeggio/shape.d.ts +7 -0
- package/dist/functions/arpeggio/shape.js +122 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +62 -25
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +33 -16
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +4 -3
- package/dist/functions/beam/layout.js +43 -65
- package/dist/functions/box/index.js +37 -36
- package/dist/functions/br/index.js +10 -8
- package/dist/functions/dash/index.js +40 -7
- package/dist/functions/default.js +19 -8
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +23 -25
- package/dist/functions/dyn/index.d.ts +2 -0
- package/dist/functions/dyn/index.js +242 -0
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +84 -79
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +496 -0
- package/dist/functions/key/index.js +98 -3
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +155 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +106 -79
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +11 -2
- package/dist/functions/page/index.js +65 -4
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +30 -0
- package/dist/functions/set/index.js +43 -32
- package/dist/functions/stack/index.js +26 -22
- package/dist/functions/symbol/index.d.ts +31 -0
- package/dist/functions/symbol/index.js +139 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +24 -0
- package/dist/functions/symbol/shape.js +52 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/ds.d.ts +2 -0
- package/dist/functions/symbol/symbols/ds.js +93 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +168 -0
- package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/fine.d.ts +2 -0
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +56 -0
- package/dist/functions/symbol/symbols/prall.d.ts +2 -0
- package/dist/functions/symbol/symbols/prall.js +42 -0
- package/dist/functions/symbol/symbols/segno.d.ts +4 -0
- package/dist/functions/symbol/symbols/segno.js +82 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +71 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +57 -4
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +86 -27
- package/dist/functions/tie/index.js +136 -152
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +218 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +258 -182
- package/dist/functions/voice/index.js +203 -109
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +230 -0
- package/dist/iife/core.d.ts +1 -0
- package/dist/iife/core.js +3 -0
- package/dist/iife/from-midi.d.ts +1 -0
- package/dist/iife/from-midi.js +15 -0
- package/dist/iife/from-musicxml.d.ts +1 -0
- package/dist/iife/from-musicxml.js +3 -0
- package/dist/iife/full.d.ts +1 -0
- package/dist/iife/full.js +3 -0
- package/dist/index.d.ts +3 -17
- package/dist/index.js +3 -17
- package/dist/jpfun.core.min.js +153 -0
- package/dist/jpfun.from-midi.min.js +9 -0
- package/dist/jpfun.from-musicxml.min.js +12 -0
- package/dist/jpfun.min.js +166 -0
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +6 -12
- package/dist/layout/engine.js +115 -64
- package/dist/layout/model.d.ts +12 -5
- package/dist/layout/model.js +31 -18
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +71 -20
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +20 -20
- package/dist/lowering/loweringContext.js +153 -101
- package/dist/lowering/track.d.ts +5 -9
- package/dist/lowering/track.js +7 -2
- package/dist/lowering/types.d.ts +26 -112
- package/dist/lowering/types.js +1 -92
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +10 -0
- package/dist/parser/parse-utils/string-utils.js +24 -0
- package/dist/parser/parserContext.d.ts +11 -9
- package/dist/parser/parserContext.js +65 -55
- package/dist/parser/preprocess.js +17 -11
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +414 -0
- package/dist/playback/event.d.ts +105 -0
- package/dist/playback/event.js +105 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +129 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +22 -0
- package/dist/render/text.js +42 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +132 -13
- package/package.json +19 -6
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
-
import {
|
|
2
|
+
import { DEFAULT_TEXT_FONT, JIANPU_NUMBER_FONT } from "../render/text.js";
|
|
3
|
+
import { readCall, trimRange } from "./parse-utils/call-utils.js";
|
|
4
|
+
import { removeQuote } from "./parse-utils/string-utils.js";
|
|
3
5
|
import { readBrace } from "./parse-utils/brace-utils.js";
|
|
4
6
|
import { readLabel } from "./parse-utils/label-utils.js";
|
|
5
7
|
import { parseLength } from "./parse-utils/length-utils.js";
|
|
@@ -21,10 +23,16 @@ function classifySyntaxValue(type, text) {
|
|
|
21
23
|
return undefined;
|
|
22
24
|
}
|
|
23
25
|
export const DEFAULT_FONT_SIZE = 22;
|
|
24
|
-
const DEFAULT_STRICT_MODE = false;
|
|
25
26
|
export const DEFAULT_OCTAVE = 4;
|
|
26
27
|
// 解析上下文
|
|
27
28
|
export class ParserContext {
|
|
29
|
+
/** 系统变量,供 `@set` 读取 */
|
|
30
|
+
static systemVariables = {
|
|
31
|
+
fontsize: { type: "length", default: DEFAULT_FONT_SIZE },
|
|
32
|
+
font: { type: "string", default: DEFAULT_TEXT_FONT },
|
|
33
|
+
numberfont: { type: "string", default: JIANPU_NUMBER_FONT },
|
|
34
|
+
strict: { type: "boolean", default: false },
|
|
35
|
+
};
|
|
28
36
|
/** 根解析器为 0,内容参数和大括号子解析器依次加一 */
|
|
29
37
|
scopeDepth;
|
|
30
38
|
/** 源代码 */
|
|
@@ -43,7 +51,7 @@ export class ParserContext {
|
|
|
43
51
|
deSugarAtomFns;
|
|
44
52
|
/** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
|
|
45
53
|
deSugarRelationFns;
|
|
46
|
-
/** .labelable()
|
|
54
|
+
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
47
55
|
labelableNodes;
|
|
48
56
|
nodes; // 解析结果
|
|
49
57
|
constructor(ctx) {
|
|
@@ -70,7 +78,8 @@ export class ParserContext {
|
|
|
70
78
|
tokens: (ctx.commentSpans ?? []).map(span => ({ kind: "comment", span })),
|
|
71
79
|
calls: [],
|
|
72
80
|
};
|
|
73
|
-
this.variables =
|
|
81
|
+
this.variables = Object.fromEntries(Object.entries(ParserContext.systemVariables).map(([name, definition]) => [name, definition.default]));
|
|
82
|
+
Object.assign(this.variables, ctx.variables);
|
|
74
83
|
this.functions = ctx.functions ?? new Map();
|
|
75
84
|
this.labelableNodes = ctx.labelableNodes ?? [];
|
|
76
85
|
this.nodes = ctx.toConsume ?? [];
|
|
@@ -97,31 +106,20 @@ export class ParserContext {
|
|
|
97
106
|
}
|
|
98
107
|
pushNode(node) {
|
|
99
108
|
this.nodes.push(node);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this.labelableNodes.push(target);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
get fontSize() {
|
|
108
|
-
return this.variables["fontsize"] ?? DEFAULT_FONT_SIZE;
|
|
109
|
+
// 未实现的对标签透明不入表,null 入表成为边界
|
|
110
|
+
const target = node.labelable?.();
|
|
111
|
+
if (target !== undefined)
|
|
112
|
+
this.labelableNodes.push(target);
|
|
109
113
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
// 统一管理系统变量和函数私有变量
|
|
115
|
+
setVariable(name, value) {
|
|
116
|
+
if (Object.hasOwn(ParserContext.systemVariables, name)) {
|
|
117
|
+
if (name === "fontsize" && value)
|
|
118
|
+
value = this.length2px(value);
|
|
119
|
+
if (value === undefined || value === "" || value === 0)
|
|
120
|
+
value = ParserContext.systemVariables[name].default;
|
|
116
121
|
}
|
|
117
|
-
|
|
118
|
-
this.variables["fontsize"] = size;
|
|
119
|
-
}
|
|
120
|
-
get strict() {
|
|
121
|
-
return this.variables["strict"] ?? DEFAULT_STRICT_MODE;
|
|
122
|
-
}
|
|
123
|
-
set strict(value) {
|
|
124
|
-
this.variables["strict"] = value;
|
|
122
|
+
this.variables[name] = value;
|
|
125
123
|
}
|
|
126
124
|
registerFunctions(functionClasses) {
|
|
127
125
|
const map = this.functions;
|
|
@@ -191,7 +189,7 @@ export class ParserContext {
|
|
|
191
189
|
this.addSyntax("function", call.nameSpan);
|
|
192
190
|
this.addSyntax("punctuation", call.openParenSpan);
|
|
193
191
|
this.addSyntax("punctuation", call.closeParenSpan);
|
|
194
|
-
const def = this.functions.get(call.name.toLowerCase())?.
|
|
192
|
+
const def = this.functions.get(call.name.toLowerCase())?.def;
|
|
195
193
|
for (let index = 0; index < call.args.length; index++) {
|
|
196
194
|
const arg = call.args[index];
|
|
197
195
|
this.addSyntax("property", arg.nameSpan);
|
|
@@ -247,20 +245,18 @@ export class ParserContext {
|
|
|
247
245
|
this.nodes.push(new ASTBraceNode(node.span, subParser.parse(node.span.start + 1, node.span.end - 1), null));
|
|
248
246
|
break;
|
|
249
247
|
case "label":
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const labelNode = new ASTLabelNode(node.span, node.label, tgt);
|
|
258
|
-
// 处理待消费列表只处理没有parent的 而label是有parent的 label其实无所谓parent
|
|
259
|
-
this.nodes.push(labelNode);
|
|
248
|
+
// 透明节点不入表,所以表尾就是绑定终点:null 是边界,已被占用的承载者同样到此为止
|
|
249
|
+
const tgt = this.labelableNodes.at(-1);
|
|
250
|
+
if (!tgt) {
|
|
251
|
+
this.diagnostics.push(Diagnostic.warning.LabelWithoutTarget(node.label, node.span));
|
|
252
|
+
}
|
|
253
|
+
else if (tgt.label !== void 0) {
|
|
254
|
+
this.diagnostics.push(Diagnostic.warning.TargetAlreadyLabeled(node.label, tgt.label, node.span));
|
|
260
255
|
}
|
|
261
256
|
else {
|
|
262
|
-
|
|
263
|
-
|
|
257
|
+
tgt.label = node.label;
|
|
258
|
+
// 同时创建一个LabelNode 供编辑器等工具使用
|
|
259
|
+
this.nodes.push(new ASTLabelNode(node.span, node.label, tgt));
|
|
264
260
|
}
|
|
265
261
|
break;
|
|
266
262
|
case "sugar":
|
|
@@ -388,10 +384,9 @@ export class ParserContext {
|
|
|
388
384
|
}
|
|
389
385
|
parseCallNode(callNode) {
|
|
390
386
|
const callFNClass = this.functions.get(callNode.name.toLowerCase());
|
|
391
|
-
const def = callFNClass?.
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
if (this.strict) {
|
|
387
|
+
const def = callFNClass?.def;
|
|
388
|
+
if (!callFNClass || !def) {
|
|
389
|
+
if (this.variables.strict) {
|
|
395
390
|
throw Diagnostic.error.UnknownFunction(callNode.name, callNode.span);
|
|
396
391
|
}
|
|
397
392
|
else {
|
|
@@ -400,6 +395,7 @@ export class ParserContext {
|
|
|
400
395
|
// 未知函数,保留位置,但直接跳过
|
|
401
396
|
return new ASTFunctionNode(callNode.span, null);
|
|
402
397
|
}
|
|
398
|
+
const defArgs = def.args;
|
|
403
399
|
// 用实际传参查询定义
|
|
404
400
|
if (!callNode.typed) {
|
|
405
401
|
const args = new Map();
|
|
@@ -415,10 +411,10 @@ export class ParserContext {
|
|
|
415
411
|
this.diagnostics.push(Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.length, i + 1, arg.valueSpan));
|
|
416
412
|
}
|
|
417
413
|
else
|
|
418
|
-
args.set(key, arg
|
|
414
|
+
args.set(key, arg);
|
|
419
415
|
continue;
|
|
420
416
|
}
|
|
421
|
-
const value = this.parseArgWithType(arg.valueSpan
|
|
417
|
+
const value = this.parseArgWithType(arg.valueSpan, type, callNode.span.start);
|
|
422
418
|
if (value !== null)
|
|
423
419
|
args.set(key, value);
|
|
424
420
|
}
|
|
@@ -432,9 +428,9 @@ export class ParserContext {
|
|
|
432
428
|
* funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
|
|
433
429
|
* 解析成功返回值 解析失败返回null
|
|
434
430
|
*/
|
|
435
|
-
parseArgWithType(
|
|
431
|
+
parseArgWithType(span, type, funcStart) {
|
|
436
432
|
// 先去空白
|
|
437
|
-
const r = trimRange(this.source,
|
|
433
|
+
const r = trimRange(this.source, span.start, span.end);
|
|
438
434
|
const start = r.start, end = r.end;
|
|
439
435
|
if (start >= end)
|
|
440
436
|
return null; // 纯空白参数视为未提供
|
|
@@ -457,7 +453,13 @@ export class ParserContext {
|
|
|
457
453
|
this.diagnostics.push(Diagnostic.warning.InvalidBoolean(text, r));
|
|
458
454
|
return null;
|
|
459
455
|
}
|
|
460
|
-
case "content":
|
|
456
|
+
case "content": {
|
|
457
|
+
// 用于失败后恢复(撤销改动)
|
|
458
|
+
const candidates = this.labelableNodes;
|
|
459
|
+
const length = candidates.length;
|
|
460
|
+
const target = candidates.at(-1);
|
|
461
|
+
const label = target?.label;
|
|
462
|
+
const hadLabel = target && Object.hasOwn(target, "label");
|
|
461
463
|
// 开启新的局部解析器
|
|
462
464
|
try {
|
|
463
465
|
const subParser = new ParserContext(this);
|
|
@@ -467,20 +469,28 @@ export class ParserContext {
|
|
|
467
469
|
return new ASTBraceNode(r, n, null);
|
|
468
470
|
}
|
|
469
471
|
catch (e) {
|
|
470
|
-
|
|
472
|
+
candidates.length = length;
|
|
473
|
+
if (target) {
|
|
474
|
+
if (hadLabel)
|
|
475
|
+
target.label = label;
|
|
476
|
+
else
|
|
477
|
+
delete target.label;
|
|
478
|
+
}
|
|
479
|
+
if (this.variables.strict || !(e instanceof Diagnostic))
|
|
471
480
|
throw e;
|
|
472
481
|
// 当前参数会被跳过,因此在恢复点记录被吞掉的具体错误
|
|
473
|
-
|
|
474
|
-
this.diagnostics.push(e);
|
|
475
|
-
this.diagnostics.push(Diagnostic.warning.InvalidContent(r));
|
|
482
|
+
this.diagnostics.push(e, Diagnostic.warning.InvalidContent(r));
|
|
476
483
|
return null;
|
|
477
484
|
}
|
|
485
|
+
}
|
|
478
486
|
case "label":
|
|
479
487
|
// 应对潜在的问题: 如果先解析了content会导致ctx.lavelableNodes被污染
|
|
480
|
-
funcStart ??=
|
|
488
|
+
funcStart ??= span.start;
|
|
481
489
|
// label情况下返回对象 从后向前查找以支持标签覆盖
|
|
482
490
|
for (let i = this.labelableNodes.length - 1; i >= 0; i--) {
|
|
483
491
|
const node = this.labelableNodes[i];
|
|
492
|
+
if (!node)
|
|
493
|
+
continue; // 边界标记不参与按名查找
|
|
484
494
|
if (node.sourceSpan.start >= funcStart)
|
|
485
495
|
continue; // 只能绑定在函数定义之前的节点上
|
|
486
496
|
if (node.label == text)
|
|
@@ -508,7 +518,7 @@ export class ParserContext {
|
|
|
508
518
|
length2px(length) {
|
|
509
519
|
// 不进行错误判断了: 来自 parseArgWithType 的不会出问题
|
|
510
520
|
if (length.unit === "em")
|
|
511
|
-
return length.value * this.
|
|
521
|
+
return length.value * this.variables.fontsize;
|
|
512
522
|
else
|
|
513
523
|
return length.value;
|
|
514
524
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { findClosingQuote } from "./parse-utils/string-utils.js";
|
|
1
2
|
/**
|
|
2
3
|
* 预处理核心模块(单次扫描):
|
|
3
4
|
* 1. 构建 lineStarts(用于 offset -> 行列映射)
|
|
4
|
-
* 2. 处理 `%`
|
|
5
|
+
* 2. 处理 `%` 行注释:仅在非字符串上下文触发,注释文本等长替换为空格;引号必须配对才算字符串
|
|
5
6
|
* 3. 处理行尾连续反斜杠:
|
|
6
7
|
* - 设行尾连续 `\` 个数为 n;
|
|
7
8
|
* - 第 1/3/5... 个 `\` 替换为空格,第 2/4/6... 个保留;
|
|
@@ -22,16 +23,13 @@ const CHAR_BOM = 65279;
|
|
|
22
23
|
const CHAR_QUOTE_DOUBLE = 34;
|
|
23
24
|
const CHAR_BACKSLASH = 92;
|
|
24
25
|
const CHAR_PERCENT = 37;
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
const
|
|
26
|
+
// 掩码几乎都是短注释和续行反斜杠,长的既少重复又要一直占着内存,所以只缓存短的
|
|
27
|
+
const MAX_CACHED_SPACES = 32;
|
|
28
|
+
const SHORT_SPACES = [];
|
|
28
29
|
function getSpaces(length) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
cached = " ".repeat(length);
|
|
33
|
-
SPACE_CACHE.set(length, cached);
|
|
34
|
-
return cached;
|
|
30
|
+
if (length > MAX_CACHED_SPACES)
|
|
31
|
+
return " ".repeat(length);
|
|
32
|
+
return SHORT_SPACES[length] ??= " ".repeat(length);
|
|
35
33
|
}
|
|
36
34
|
/**
|
|
37
35
|
* 判断 非换行空白
|
|
@@ -86,6 +84,7 @@ export function preprocessSource(source) {
|
|
|
86
84
|
* 当前行末尾候选连续 `\` 片段(允许后面跟空白),用于换行处一次性判定
|
|
87
85
|
*/
|
|
88
86
|
let inQuote = 0;
|
|
87
|
+
let noPairedQuoteAfter = false;
|
|
89
88
|
let escaped = false;
|
|
90
89
|
let pendingCommentStart = -1;
|
|
91
90
|
let lastSignificantIndex = -1;
|
|
@@ -144,7 +143,14 @@ export function preprocessSource(source) {
|
|
|
144
143
|
continue;
|
|
145
144
|
}
|
|
146
145
|
if (ch === CHAR_QUOTE_DOUBLE) {
|
|
147
|
-
|
|
146
|
+
// 配对才算字符串,否则一个孤立引号会让后文的 `%` 全部失去注释语义
|
|
147
|
+
// 一次前瞻失败即证明剩余源码里没有可配对的引号,置位后不再前瞻,整体仍是 O(n)
|
|
148
|
+
if (!noPairedQuoteAfter) {
|
|
149
|
+
if (findClosingQuote(source, i, sourceLength) < 0)
|
|
150
|
+
noPairedQuoteAfter = true;
|
|
151
|
+
else
|
|
152
|
+
inQuote = ch;
|
|
153
|
+
}
|
|
148
154
|
updateLineTailState(i, ch);
|
|
149
155
|
i++;
|
|
150
156
|
continue;
|
package/dist/pipeline.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { ASTBraceNode, type ASTFunctionClass } from "./functions/ASTtypes.js";
|
|
2
2
|
import { type DocumentLayoutOptions, type DocumentLayoutResult } from "./layout/engine.js";
|
|
3
3
|
import type { LoweringResult } from "./lowering/types.js";
|
|
4
|
-
import {
|
|
4
|
+
import type { Diagnostic } from "./diagnostic.js";
|
|
5
5
|
import type { TextMeasurer } from "./render/types.js";
|
|
6
6
|
export interface CompileScoreOptions extends DocumentLayoutOptions {
|
|
7
7
|
functions?: ASTFunctionClass[];
|
|
8
8
|
textMeasurer?: TextMeasurer;
|
|
9
|
-
|
|
9
|
+
variables?: Record<string, any>;
|
|
10
10
|
}
|
|
11
11
|
export interface CompileScoreResult {
|
|
12
12
|
lineStarts: number[];
|
|
13
|
-
|
|
13
|
+
maskedSource: string;
|
|
14
|
+
diagnostics: Diagnostic[];
|
|
14
15
|
ast: ASTBraceNode;
|
|
15
16
|
lowering: LoweringResult;
|
|
16
17
|
layout: DocumentLayoutResult;
|
|
@@ -31,6 +32,6 @@ export declare function compileScore(source: string, options?: CompileScoreOptio
|
|
|
31
32
|
* 该入口会把这些问题记录到 diagnostics,并尽量返回已经识别的 call/token,供高亮和补全继续工作
|
|
32
33
|
*/
|
|
33
34
|
export declare function analyzeScoreSyntax(source: string, functions?: ASTFunctionClass[]): {
|
|
34
|
-
syntax: import("./
|
|
35
|
-
diagnostics:
|
|
35
|
+
syntax: import("./core.js").SyntaxAnalysis;
|
|
36
|
+
diagnostics: Diagnostic[];
|
|
36
37
|
};
|
package/dist/pipeline.js
CHANGED
|
@@ -14,11 +14,9 @@ import { preprocessSource } from "./parser/preprocess.js";
|
|
|
14
14
|
* 任意 Painter 后端可以直接消费 layout
|
|
15
15
|
*/
|
|
16
16
|
export function compileScore(source, options = {}) {
|
|
17
|
-
const { functions = defaultFunctions, textMeasurer,
|
|
17
|
+
const { functions = defaultFunctions, textMeasurer, variables, ...layoutOptions } = options;
|
|
18
18
|
const { maskedSource, lineStarts } = preprocessSource(source);
|
|
19
|
-
const parser = new ParserContext({ source: maskedSource });
|
|
20
|
-
if (fontSize)
|
|
21
|
-
parser.fontSize = fontSize;
|
|
19
|
+
const parser = new ParserContext({ source: maskedSource, variables });
|
|
22
20
|
parser.registerFunctions(functions);
|
|
23
21
|
const ast = new ASTBraceNode({ start: 0, end: source.length }, parser.parse());
|
|
24
22
|
const loweringContext = new LoweringContext(parser.diagnostics);
|
|
@@ -28,7 +26,8 @@ export function compileScore(source, options = {}) {
|
|
|
28
26
|
const layout = layoutDocument(lowering, layoutContext, layoutOptions);
|
|
29
27
|
return {
|
|
30
28
|
lineStarts,
|
|
31
|
-
|
|
29
|
+
maskedSource,
|
|
30
|
+
diagnostics: parser.diagnostics,
|
|
32
31
|
ast,
|
|
33
32
|
lowering,
|
|
34
33
|
layout,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LoweringResult } from "../lowering/types.js";
|
|
2
|
+
import type { PlaybackPlan } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* 把 lowering 的时间流编译成完整的演奏计划
|
|
5
|
+
*
|
|
6
|
+
* 函数通过以下入口参与 playback(按作用顺序):
|
|
7
|
+
* - [hook] Temporal.playbackMarks / PlaybackFlow.playbackFlow:生成事件前先展开控制流;反复线贴起点标记并回跳,volta 按遍数跳过列
|
|
8
|
+
* - [state] Temporal.playbackState:每次实际访问节点时,把 lowering 已固化的 BPM 登记到演奏轴;note、tempo 和正时值 dash 都用它恢复记谱位置的速度
|
|
9
|
+
* - [hook] Temporal.emitPlayback:按展开后的列序逐次调用;note 登记 NoteOn/NoteOff,up/grace 通过 emitter.play 递归登记折叠成员
|
|
10
|
+
* - [API] PlaybackEmitter.control:emit 时只登记控制;所有节点访问完成后按演奏时刻执行,同刻合并后生成 Tempo;fermata 用它修改 bpmScale
|
|
11
|
+
* - [API] PlaybackEmitter.affectFollowing:把 transform 加入当前 play frame;后续目标音完整登记 NoteOn/NoteOff 后,将冻结的 transform 紧接着写入顺序流;accent、tr、波音使用它
|
|
12
|
+
* - [API] PlaybackEmitter.defer:把 hook 写在当前节点之后;状态扫描完成后重放顺序流,走到该位置才执行,可见完整 Tempo 和此前音符;dash 用它延长前一组 NoteOff
|
|
13
|
+
* - [hook] PlaybackRelation.applyPlayback:顺序流重放完并首次排序后执行,可观察和改写完整事件计划;tie 用它合并相距很远的端点
|
|
14
|
+
* - comparePlaybackDraftEvents / finalizePlaybackEvents:最后稳定排序,再校验 NoteOn/NoteOff 配对并剥离 origins、order 等编译期字段
|
|
15
|
+
*/
|
|
16
|
+
export declare function compilePlayback(lowering: LoweringResult, options?: {
|
|
17
|
+
maxFlowSteps?: number;
|
|
18
|
+
}): PlaybackPlan;
|