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.
Files changed (167) hide show
  1. package/README.md +141 -11
  2. package/dist/converter/midi/convert.d.ts +50 -0
  3. package/dist/converter/midi/convert.js +700 -0
  4. package/dist/converter/midi/index.d.ts +5 -0
  5. package/dist/converter/midi/index.js +11 -0
  6. package/dist/converter/midi/json.d.ts +38 -0
  7. package/dist/converter/midi/json.js +1 -0
  8. package/dist/converter/midi/triplet.d.ts +30 -0
  9. package/dist/converter/midi/triplet.js +75 -0
  10. package/dist/converter/musicxml/dom.d.ts +25 -0
  11. package/dist/converter/musicxml/dom.js +65 -0
  12. package/dist/converter/musicxml/features.d.ts +51 -0
  13. package/dist/converter/musicxml/features.js +232 -0
  14. package/dist/converter/musicxml/index.d.ts +9 -0
  15. package/dist/converter/musicxml/index.js +1140 -0
  16. package/dist/converter/musicxml/model.d.ts +123 -0
  17. package/dist/converter/musicxml/model.js +1 -0
  18. package/dist/converter/source.d.ts +22 -0
  19. package/dist/converter/source.js +51 -0
  20. package/dist/core.d.ts +22 -0
  21. package/dist/core.js +22 -0
  22. package/dist/diagnostic.js +5 -2
  23. package/dist/fraction.d.ts +24 -0
  24. package/dist/fraction.js +132 -0
  25. package/dist/functions/ASTtypes.d.ts +51 -31
  26. package/dist/functions/ASTtypes.js +59 -38
  27. package/dist/functions/adjust/index.d.ts +2 -0
  28. package/dist/functions/adjust/index.js +193 -0
  29. package/dist/functions/arpeggio/index.d.ts +2 -0
  30. package/dist/functions/arpeggio/index.js +180 -0
  31. package/dist/functions/arpeggio/shape.d.ts +7 -0
  32. package/dist/functions/arpeggio/shape.js +122 -0
  33. package/dist/functions/bar/index.d.ts +8 -0
  34. package/dist/functions/bar/index.js +62 -25
  35. package/dist/functions/beam/auto.d.ts +1 -1
  36. package/dist/functions/beam/auto.js +33 -16
  37. package/dist/functions/beam/index.js +10 -6
  38. package/dist/functions/beam/layout.d.ts +4 -3
  39. package/dist/functions/beam/layout.js +43 -65
  40. package/dist/functions/box/index.js +37 -36
  41. package/dist/functions/br/index.js +10 -8
  42. package/dist/functions/dash/index.js +40 -7
  43. package/dist/functions/default.js +19 -8
  44. package/dist/functions/div/index.d.ts +16 -0
  45. package/dist/functions/div/index.js +45 -17
  46. package/dist/functions/dot/index.js +23 -25
  47. package/dist/functions/dyn/index.d.ts +2 -0
  48. package/dist/functions/dyn/index.js +242 -0
  49. package/dist/functions/grace/index.d.ts +8 -11
  50. package/dist/functions/grace/index.js +84 -79
  51. package/dist/functions/head/index.d.ts +2 -0
  52. package/dist/functions/head/index.js +496 -0
  53. package/dist/functions/key/index.js +98 -3
  54. package/dist/functions/meter/index.d.ts +2 -0
  55. package/dist/functions/meter/index.js +155 -0
  56. package/dist/functions/note/accidentals.d.ts +24 -3
  57. package/dist/functions/note/accidentals.js +31 -1
  58. package/dist/functions/note/index.js +106 -79
  59. package/dist/functions/note/noteNameFSM.js +52 -63
  60. package/dist/functions/page/index.d.ts +11 -2
  61. package/dist/functions/page/index.js +65 -4
  62. package/dist/functions/program/index.d.ts +2 -0
  63. package/dist/functions/program/index.js +30 -0
  64. package/dist/functions/set/index.js +43 -32
  65. package/dist/functions/stack/index.js +26 -22
  66. package/dist/functions/symbol/index.d.ts +31 -0
  67. package/dist/functions/symbol/index.js +139 -0
  68. package/dist/functions/symbol/ornament.d.ts +15 -0
  69. package/dist/functions/symbol/ornament.js +73 -0
  70. package/dist/functions/symbol/shape.d.ts +24 -0
  71. package/dist/functions/symbol/shape.js +52 -0
  72. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  73. package/dist/functions/symbol/symbols/accent.js +24 -0
  74. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  75. package/dist/functions/symbol/symbols/dc.js +82 -0
  76. package/dist/functions/symbol/symbols/ds.d.ts +2 -0
  77. package/dist/functions/symbol/symbols/ds.js +93 -0
  78. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  79. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  80. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  81. package/dist/functions/symbol/symbols/fermata.js +24 -0
  82. package/dist/functions/symbol/symbols/fine.d.ts +2 -0
  83. package/dist/functions/symbol/symbols/fine.js +12 -0
  84. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  85. package/dist/functions/symbol/symbols/mordent.js +56 -0
  86. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  87. package/dist/functions/symbol/symbols/prall.js +42 -0
  88. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  89. package/dist/functions/symbol/symbols/segno.js +82 -0
  90. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  91. package/dist/functions/symbol/symbols/tr.js +71 -0
  92. package/dist/functions/tempo/glyph.d.ts +12 -0
  93. package/dist/functions/tempo/glyph.js +30 -0
  94. package/dist/functions/tempo/index.js +57 -4
  95. package/dist/functions/temporal.d.ts +138 -0
  96. package/dist/functions/temporal.js +101 -0
  97. package/dist/functions/text/index.js +86 -27
  98. package/dist/functions/tie/index.js +136 -152
  99. package/dist/functions/tuplet/index.d.ts +2 -0
  100. package/dist/functions/tuplet/index.js +218 -0
  101. package/dist/functions/up/index.d.ts +1 -0
  102. package/dist/functions/up/index.js +258 -182
  103. package/dist/functions/voice/index.js +203 -109
  104. package/dist/functions/volta/index.d.ts +2 -0
  105. package/dist/functions/volta/index.js +230 -0
  106. package/dist/iife/core.d.ts +1 -0
  107. package/dist/iife/core.js +3 -0
  108. package/dist/iife/from-midi.d.ts +1 -0
  109. package/dist/iife/from-midi.js +15 -0
  110. package/dist/iife/from-musicxml.d.ts +1 -0
  111. package/dist/iife/from-musicxml.js +3 -0
  112. package/dist/iife/full.d.ts +1 -0
  113. package/dist/iife/full.js +3 -0
  114. package/dist/index.d.ts +3 -17
  115. package/dist/index.js +3 -17
  116. package/dist/jpfun.core.min.js +153 -0
  117. package/dist/jpfun.from-midi.min.js +9 -0
  118. package/dist/jpfun.from-musicxml.min.js +12 -0
  119. package/dist/jpfun.min.js +166 -0
  120. package/dist/layout/default.js +2 -3
  121. package/dist/layout/engine.d.ts +6 -12
  122. package/dist/layout/engine.js +115 -64
  123. package/dist/layout/model.d.ts +12 -5
  124. package/dist/layout/model.js +31 -18
  125. package/dist/layout/page.js +5 -4
  126. package/dist/layout/path.d.ts +19 -0
  127. package/dist/layout/path.js +101 -0
  128. package/dist/layout/types.d.ts +71 -20
  129. package/dist/layout/types.js +3 -1
  130. package/dist/lowering/loweringContext.d.ts +20 -20
  131. package/dist/lowering/loweringContext.js +153 -101
  132. package/dist/lowering/track.d.ts +5 -9
  133. package/dist/lowering/track.js +7 -2
  134. package/dist/lowering/types.d.ts +26 -112
  135. package/dist/lowering/types.js +1 -92
  136. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  137. package/dist/parser/parse-utils/call-utils.js +0 -6
  138. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  139. package/dist/parser/parse-utils/note-utils.js +35 -13
  140. package/dist/parser/parse-utils/string-utils.d.ts +10 -0
  141. package/dist/parser/parse-utils/string-utils.js +24 -0
  142. package/dist/parser/parserContext.d.ts +11 -9
  143. package/dist/parser/parserContext.js +65 -55
  144. package/dist/parser/preprocess.js +17 -11
  145. package/dist/pipeline.d.ts +6 -5
  146. package/dist/pipeline.js +4 -5
  147. package/dist/playback/compile.d.ts +18 -0
  148. package/dist/playback/compile.js +414 -0
  149. package/dist/playback/event.d.ts +105 -0
  150. package/dist/playback/event.js +105 -0
  151. package/dist/playback/time.d.ts +14 -0
  152. package/dist/playback/time.js +81 -0
  153. package/dist/playback/types.d.ts +129 -0
  154. package/dist/playback/types.js +3 -0
  155. package/dist/render/canvas.d.ts +4 -4
  156. package/dist/render/canvas.js +20 -5
  157. package/dist/render/index.d.ts +1 -0
  158. package/dist/render/index.js +1 -0
  159. package/dist/render/paint.d.ts +8 -0
  160. package/dist/render/paint.js +101 -0
  161. package/dist/render/svg.d.ts +3 -3
  162. package/dist/render/svg.js +10 -8
  163. package/dist/render/text.d.ts +22 -0
  164. package/dist/render/text.js +42 -0
  165. package/dist/render/types.d.ts +1 -1
  166. package/grammar.md +132 -13
  167. package/package.json +19 -6
@@ -1,5 +1,7 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
- import { readCall, trimRange, removeQuote } from "./parse-utils/call-utils.js";
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 = ctx.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
- if (node instanceof ASTFunctionNode) {
101
- // 标签绑到节点自己指定的承载者上
102
- const target = node.labelable();
103
- if (target)
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
- set fontSize(size) {
111
- if (typeof size === "string") {
112
- const l = parseLength(size);
113
- if (l instanceof Diagnostic)
114
- throw l;
115
- this.variables["fontsize"] = this.length2px(l);
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
- else
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())?.prototype.def;
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
- const labelList = this.labelableNodes;
251
- if (labelList.length > 0) {
252
- const tgt = labelList[labelList.length - 1];
253
- if (tgt.label !== void 0)
254
- this.diagnostics.push(Diagnostic.warning.LabelAlreadyExists(node.label, tgt.label, node.span));
255
- tgt.label = node.label;
256
- // 同时创建一个LabelNode 供编辑器等工具使用
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
- this.diagnostics.push(Diagnostic.warning.LabelWithoutTarget(node.label, node.span));
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?.prototype.def;
392
- const defArgs = def?.args;
393
- if (!callFNClass || !defArgs) {
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.valueSpan);
414
+ args.set(key, arg);
419
415
  continue;
420
416
  }
421
- const value = this.parseArgWithType(arg.valueSpan.start, arg.valueSpan.end, type, callNode.span.start);
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(argStart, argEnd, type, funcStart) {
431
+ parseArgWithType(span, type, funcStart) {
436
432
  // 先去空白
437
- const r = trimRange(this.source, argStart, argEnd);
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
- if (this.strict)
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
- if (e instanceof Diagnostic)
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 ??= argStart;
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.fontSize;
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
- // 高频创建空格串,按长度缓存,减少 repeat 带来的重复分配
26
- // 考虑到后续会频繁解析 保留此全局量
27
- const SPACE_CACHE = new Map();
26
+ // 掩码几乎都是短注释和续行反斜杠,长的既少重复又要一直占着内存,所以只缓存短的
27
+ const MAX_CACHED_SPACES = 32;
28
+ const SHORT_SPACES = [];
28
29
  function getSpaces(length) {
29
- let cached = SPACE_CACHE.get(length);
30
- if (cached !== undefined)
31
- return cached;
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
- inQuote = ch;
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;
@@ -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 { ParserContext } from "./parser/parserContext.js";
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
- fontSize?: number;
9
+ variables?: Record<string, any>;
10
10
  }
11
11
  export interface CompileScoreResult {
12
12
  lineStarts: number[];
13
- parser: ParserContext;
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("./index.js").SyntaxAnalysis;
35
- diagnostics: import("./diagnostic.js").Diagnostic[];
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, fontSize, ...layoutOptions } = options;
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
- parser,
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;