jpfun 0.7.0 → 0.8.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 (110) hide show
  1. package/Bravura-LICENSE.txt +93 -0
  2. package/README.md +49 -146
  3. package/dist/Bravura-LICENSE.txt +93 -0
  4. package/dist/converter/midi/convert.d.ts +0 -10
  5. package/dist/converter/midi/convert.js +11 -1
  6. package/dist/converter/musicxml/features.d.ts +15 -7
  7. package/dist/converter/musicxml/features.js +87 -18
  8. package/dist/converter/musicxml/index.js +80 -32
  9. package/dist/converter/musicxml/model.d.ts +9 -0
  10. package/dist/core.d.ts +1 -0
  11. package/dist/core.js +1 -0
  12. package/dist/diagnostic.d.ts +7 -1
  13. package/dist/diagnostic.js +40 -88
  14. package/dist/fraction.js +3 -2
  15. package/dist/functions/ASTtypes.d.ts +2 -18
  16. package/dist/functions/ASTtypes.js +0 -16
  17. package/dist/functions/arpeggio/index.js +15 -17
  18. package/dist/functions/bar/index.d.ts +8 -0
  19. package/dist/functions/bar/index.js +102 -0
  20. package/dist/functions/beam/auto.d.ts +1 -2
  21. package/dist/functions/beam/auto.js +30 -16
  22. package/dist/functions/beam/index.js +2 -3
  23. package/dist/functions/beam/layout.d.ts +1 -1
  24. package/dist/functions/beam/layout.js +1 -1
  25. package/dist/functions/br/index.d.ts +34 -2
  26. package/dist/functions/br/index.js +5 -4
  27. package/dist/functions/dash/index.js +6 -6
  28. package/dist/functions/default.js +2 -1
  29. package/dist/functions/div/index.d.ts +1 -0
  30. package/dist/functions/div/index.js +8 -4
  31. package/dist/functions/dot/index.js +2 -2
  32. package/dist/functions/dyn/index.js +49 -31
  33. package/dist/functions/gliss/index.js +75 -53
  34. package/dist/functions/grace/index.d.ts +1 -7
  35. package/dist/functions/grace/index.js +8 -13
  36. package/dist/functions/head/index.js +1 -1
  37. package/dist/functions/key/index.js +8 -10
  38. package/dist/functions/meter/index.d.ts +56 -1
  39. package/dist/functions/meter/index.js +12 -7
  40. package/dist/functions/note/index.js +5 -5
  41. package/dist/functions/note/noteName.d.ts +16 -0
  42. package/dist/functions/note/noteName.js +88 -0
  43. package/dist/functions/note/source-transform.js +6 -5
  44. package/dist/functions/ottava/index.js +24 -8
  45. package/dist/functions/page/index.d.ts +1 -0
  46. package/dist/functions/page/index.js +20 -11
  47. package/dist/functions/pagebreak/index.js +2 -2
  48. package/dist/functions/rest/index.js +3 -1
  49. package/dist/functions/set/index.js +4 -4
  50. package/dist/functions/symbol/index.d.ts +21 -2
  51. package/dist/functions/symbol/index.js +69 -24
  52. package/dist/functions/symbol/ornament.d.ts +1 -2
  53. package/dist/functions/symbol/shape.d.ts +6 -0
  54. package/dist/functions/symbol/shape.js +16 -3
  55. package/dist/functions/symbol/symbols/coda.d.ts +4 -0
  56. package/dist/functions/symbol/symbols/coda.js +56 -0
  57. package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
  58. package/dist/functions/symbol/symbols/dynamics.js +18 -8
  59. package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
  60. package/dist/functions/symbol/symbols/harmonic.js +11 -0
  61. package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
  62. package/dist/functions/symbol/symbols/marcato.js +22 -0
  63. package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
  64. package/dist/functions/symbol/symbols/staccato.js +24 -0
  65. package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
  66. package/dist/functions/symbol/symbols/tenuto.js +11 -0
  67. package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
  68. package/dist/functions/symbol/symbols/tocoda.js +16 -0
  69. package/dist/functions/symbol/symbols/turn.d.ts +3 -0
  70. package/dist/functions/symbol/symbols/turn.js +50 -0
  71. package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
  72. package/dist/functions/symbol/symbols/wheel.js +36 -0
  73. package/dist/functions/temporal.d.ts +1 -0
  74. package/dist/functions/temporal.js +1 -0
  75. package/dist/functions/text/index.js +15 -6
  76. package/dist/functions/tie/index.js +30 -6
  77. package/dist/functions/tremolo/index.d.ts +2 -0
  78. package/dist/functions/tremolo/index.js +319 -0
  79. package/dist/functions/tuplet/index.js +4 -8
  80. package/dist/functions/up/index.js +2 -2
  81. package/dist/functions/voice/index.js +17 -10
  82. package/dist/jpfun.core.min.js +57 -49
  83. package/dist/jpfun.from-midi.min.js +5 -5
  84. package/dist/jpfun.from-musicxml.min.js +8 -8
  85. package/dist/jpfun.min.js +62 -54
  86. package/dist/layout/default.d.ts +0 -1
  87. package/dist/layout/default.js +0 -1
  88. package/dist/lowering/loweringContext.d.ts +11 -13
  89. package/dist/lowering/loweringContext.js +26 -33
  90. package/dist/lowering/types.d.ts +1 -3
  91. package/dist/parser/parse-utils/note-utils.d.ts +1 -1
  92. package/dist/parser/parse-utils/note-utils.js +8 -9
  93. package/dist/parser/parse-utils/string-utils.d.ts +3 -2
  94. package/dist/parser/parse-utils/string-utils.js +16 -3
  95. package/dist/parser/parserContext.js +9 -2
  96. package/dist/pipeline.js +0 -1
  97. package/dist/playback/compile.js +120 -70
  98. package/dist/playback/event.d.ts +1 -1
  99. package/dist/playback/event.js +9 -9
  100. package/dist/playback/time.js +42 -31
  101. package/dist/playback/types.d.ts +3 -1
  102. package/dist/render/canvas.d.ts +2 -0
  103. package/dist/render/canvas.js +25 -22
  104. package/dist/render/recording.js +1 -4
  105. package/dist/render/translate.d.ts +0 -1
  106. package/dist/render/translate.js +6 -23
  107. package/grammar.md +26 -20
  108. package/package.json +3 -3
  109. package/dist/functions/note/noteNameFSM.d.ts +0 -8
  110. package/dist/functions/note/noteNameFSM.js +0 -181
@@ -4,7 +4,6 @@ import type { LayoutPrepareContext } from "./types.js";
4
4
  /**
5
5
  * 从函数类声明创建排版上下文
6
6
  *
7
- * 与 LoweringContext.registerFunctions 使用相同的注册思想
8
7
  * 新增装饰函数只修改自己的类,不修改 layout 引擎
9
8
  */
10
9
  export declare function createLayoutPrepareContext(functionClasses: Iterable<ASTFunctionClass>, options?: {
@@ -3,7 +3,6 @@ import { defaultTextMeasurer } from "../render/text.js";
3
3
  /**
4
4
  * 从函数类声明创建排版上下文
5
5
  *
6
- * 与 LoweringContext.registerFunctions 使用相同的注册思想
7
6
  * 新增装饰函数只修改自己的类,不修改 layout 引擎
8
7
  */
9
8
  export function createLayoutPrepareContext(functionClasses, options = {}) {
@@ -1,31 +1,28 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
2
  import { Fraction } from "../fraction.js";
3
- import { ASTFunctionClass, ASTNodeBase } from "../functions/ASTtypes.js";
3
+ import { ASTNodeBase } from "../functions/ASTtypes.js";
4
4
  import type { PageConfig } from "../layout/types.js";
5
5
  import { TemporalNodeBase } from "../functions/temporal.js";
6
- import type { LoweringGroup, LoweringAttachment, LoweringResult } from "./types.js";
6
+ import type { LoweringFinalizer, LoweringGroup, LoweringAttachment, LoweringResult } from "./types.js";
7
7
  import { Track } from "./track.js";
8
8
  /**
9
9
  * 把 AST 展开并固化为时间列、attachment 和页面配置
10
10
  *
11
11
  * 标准用法:
12
12
  * 1. 创建 LoweringContext
13
- * 2. 调用 registerFunctions,传入与 parser 相同的函数类列表
14
- * 3. 调用 lowerDocument(root),得到 LoweringResult
15
- * 4. 把结果交给 layoutDocument
13
+ * 2. 调用 lowerDocument(root),得到 LoweringResult
14
+ * 3. 把结果交给 layoutDocument
16
15
  *
17
16
  * 大多数 AST 函数通过以下入口参与 lowering(按时间顺序):
18
17
  * - [hook] loweringEnter/loweringExit:进入或离开节点时生成 TemporalNode、管理作用域
19
18
  * - [API] LoweringContext.beginLoweringGroup/endLoweringGroup:通常在 enter/exit 中被调用,以栈方式观察当前子树产生的 TemporalNode 和 attachment
20
19
  * - [API] LoweringContext.addAttachment:注册不推进时间的附属对象;当前外层分组会收到该对象
20
+ * - [API] LoweringContext.deferFinalize:在 enter/exit 中按需登记本轮收尾回调,支持通过 order 指定执行优先级
21
21
  * - [config] timeFlowModel:声明子节点按 sequence 或 parallel 推进时间;parallel 还要声明音轨复用键与纵向排列策略
22
22
  * - [hook] Temporal.onTimeState:锚点归并后固化调性、速度等状态
23
- * - [hook] loweringAugment:最终列和 layoutLine 固化后扫描结果并生成要追加的 attachment
24
- * - [hook] loweringFinalize:所有派生 attachment 追加完成后,按注册顺序处理完整结果
25
23
  */
26
24
  export declare class LoweringContext {
27
- private loweringAugmenters;
28
- private loweringFinalizers;
25
+ private activeFinalizers;
29
26
  private cnt;
30
27
  private usedTracks;
31
28
  /** 不推进时间的附属对象 */
@@ -45,8 +42,6 @@ export declare class LoweringContext {
45
42
  /** 收集本轮 lowering 产生的诊断信息 */
46
43
  readonly diagnostics: Diagnostic[];
47
44
  constructor(diagnostics?: Diagnostic[]);
48
- /** 供外部调用: 初始化 hook */
49
- registerFunctions(functionClasses: ASTFunctionClass[]): void;
50
45
  /** 供 page 函数调用 */
51
46
  setPageConfig(page: PageConfig): void;
52
47
  /**
@@ -70,8 +65,10 @@ export declare class LoweringContext {
70
65
  * 嵌套分组按退出顺序注册,因此天然是由内向外计算边界
71
66
  */
72
67
  endLoweringGroup(owner: ASTNodeBase): LoweringGroup;
73
- /** 先统一生成派生 attachment,再按注册顺序执行最终处理 */
68
+ /** 按显式 order 升序执行本轮登记的收尾回调 */
74
69
  private postprocessResult;
70
+ /** 登记本轮需要执行的收尾回调;order 越小越先执行,默认 0 */
71
+ deferFinalize(finalize: LoweringFinalizer, order?: number): void;
75
72
  /**
76
73
  * 注册不推进时间的排版对象
77
74
  * 被 tie、beam、box 等函数使用
@@ -95,8 +92,9 @@ export declare class LoweringContext {
95
92
  * 递归展开一棵子树,得到它的时间列与结束时间
96
93
  * @param timeOffset 当前节点的时间偏移量(单位QN) 由父节点传入,会被原地修改
97
94
  * @param track 当前所在的纵向音轨
95
+ * @param columns 当前时间列数组,会被原地修改
98
96
  */
99
- trackedEvents(node: ASTNodeBase, timeOffset: Fraction, track: Track): TimeColumn[];
97
+ trackedEvents(node: ASTNodeBase, timeOffset: Fraction, track: Track, columns?: TimeColumn[]): TimeColumn[];
100
98
  /**
101
99
  * 将 hook 返回的事件规范化、加入索引与分组,再追加到时间列
102
100
  * 原地修改 timeOffset
@@ -7,22 +7,19 @@ import { Track } from "./track.js";
7
7
  *
8
8
  * 标准用法:
9
9
  * 1. 创建 LoweringContext
10
- * 2. 调用 registerFunctions,传入与 parser 相同的函数类列表
11
- * 3. 调用 lowerDocument(root),得到 LoweringResult
12
- * 4. 把结果交给 layoutDocument
10
+ * 2. 调用 lowerDocument(root),得到 LoweringResult
11
+ * 3. 把结果交给 layoutDocument
13
12
  *
14
13
  * 大多数 AST 函数通过以下入口参与 lowering(按时间顺序):
15
14
  * - [hook] loweringEnter/loweringExit:进入或离开节点时生成 TemporalNode、管理作用域
16
15
  * - [API] LoweringContext.beginLoweringGroup/endLoweringGroup:通常在 enter/exit 中被调用,以栈方式观察当前子树产生的 TemporalNode 和 attachment
17
16
  * - [API] LoweringContext.addAttachment:注册不推进时间的附属对象;当前外层分组会收到该对象
17
+ * - [API] LoweringContext.deferFinalize:在 enter/exit 中按需登记本轮收尾回调,支持通过 order 指定执行优先级
18
18
  * - [config] timeFlowModel:声明子节点按 sequence 或 parallel 推进时间;parallel 还要声明音轨复用键与纵向排列策略
19
19
  * - [hook] Temporal.onTimeState:锚点归并后固化调性、速度等状态
20
- * - [hook] loweringAugment:最终列和 layoutLine 固化后扫描结果并生成要追加的 attachment
21
- * - [hook] loweringFinalize:所有派生 attachment 追加完成后,按注册顺序处理完整结果
22
20
  */
23
21
  export class LoweringContext {
24
- loweringAugmenters = [];
25
- loweringFinalizers = [];
22
+ activeFinalizers = new Map();
26
23
  cnt = 0; // 生成唯一id的计数器
27
24
  usedTracks = new Set();
28
25
  /** 不推进时间的附属对象 */
@@ -44,18 +41,6 @@ export class LoweringContext {
44
41
  constructor(diagnostics = []) {
45
42
  this.diagnostics = diagnostics;
46
43
  }
47
- /** 供外部调用: 初始化 hook */
48
- registerFunctions(functionClasses) {
49
- const augmenters = this.loweringAugmenters = [];
50
- const finalizers = this.loweringFinalizers = [];
51
- for (const functionClass of functionClasses) {
52
- if (functionClass.loweringAugment)
53
- augmenters.push(functionClass.loweringAugment);
54
- const finalize = functionClass.loweringFinalize;
55
- if (finalize && !finalizers.includes(finalize))
56
- finalizers.push(finalize); // 判重是因为pb直接引用了br的实现
57
- }
58
- }
59
44
  /** 供 page 函数调用 */
60
45
  setPageConfig(page) {
61
46
  this.page = page;
@@ -69,6 +54,7 @@ export class LoweringContext {
69
54
  lowerDocument(node) {
70
55
  this.cnt = 0;
71
56
  this.usedTracks.clear();
57
+ this.activeFinalizers.clear();
72
58
  this.attachments = [];
73
59
  this.page = void 0;
74
60
  this.astToTemporal = new Map();
@@ -179,22 +165,30 @@ export class LoweringContext {
179
165
  this.addAttachment(active.group.attachment);
180
166
  return active.group;
181
167
  }
182
- /** 先统一生成派生 attachment,再按注册顺序执行最终处理 */
168
+ /** 按显式 order 升序执行本轮登记的收尾回调 */
183
169
  postprocessResult(result) {
184
170
  if (this.activeLoweringGroups.length > 0)
185
171
  throw new Error("Lowering groups must be closed before post-processing");
186
- const additions = [];
187
- for (const augment of this.loweringAugmenters) {
188
- for (const attachment of augment(result))
189
- additions.push(attachment);
172
+ const size = this.activeFinalizers.size;
173
+ if (size === 0)
174
+ return result;
175
+ if (size === 1) {
176
+ this.activeFinalizers.keys().next().value(result);
177
+ return result;
190
178
  }
191
- for (const attachment of additions)
192
- this.addAttachment(attachment);
193
- // 进行校验或者其他动作
194
- for (const finalize of this.loweringFinalizers)
179
+ const scheduled = [...this.activeFinalizers.entries()]
180
+ .sort((left, right) => left[1] - right[1]);
181
+ for (const [finalize] of scheduled)
195
182
  finalize(result);
196
183
  return result;
197
184
  }
185
+ /** 登记本轮需要执行的收尾回调;order 越小越先执行,默认 0 */
186
+ deferFinalize(finalize, order = 0) {
187
+ const current = this.activeFinalizers.get(finalize);
188
+ if (current === undefined || order < current) {
189
+ this.activeFinalizers.set(finalize, order);
190
+ }
191
+ }
198
192
  /**
199
193
  * 注册不推进时间的排版对象
200
194
  * 被 tie、beam、box 等函数使用
@@ -242,9 +236,9 @@ export class LoweringContext {
242
236
  * 递归展开一棵子树,得到它的时间列与结束时间
243
237
  * @param timeOffset 当前节点的时间偏移量(单位QN) 由父节点传入,会被原地修改
244
238
  * @param track 当前所在的纵向音轨
239
+ * @param columns 当前时间列数组,会被原地修改
245
240
  */
246
- trackedEvents(node, timeOffset, track) {
247
- const columns = [];
241
+ trackedEvents(node, timeOffset, track, columns = []) {
248
242
  this.appendEvents(node.loweringEnter(this, track), node, timeOffset, track, columns);
249
243
  // 处理子元素
250
244
  const model = node.timeFlowModel(this);
@@ -253,8 +247,7 @@ export class LoweringContext {
253
247
  case "sequence":
254
248
  {
255
249
  for (const c of model.children) {
256
- for (const column of this.trackedEvents(c, timeOffset, track))
257
- columns.push(column);
250
+ this.trackedEvents(c, timeOffset, track, columns);
258
251
  }
259
252
  }
260
253
  break;
@@ -346,7 +339,7 @@ export class LoweringContext {
346
339
  return tracks[0];
347
340
  const result = [];
348
341
  // 归并多个轨道
349
- const p = new Uint16Array(l); // 当前指向第几个
342
+ const p = new Uint32Array(l); // 当前指向第几个
350
343
  const dt = Array.from({ length: l }, () => new Fraction()); // 每个轨道的偏移量
351
344
  const anchorBuffer = [];
352
345
  const heap = new HeapSort(l); // 保证每个轨在堆里最多一个
@@ -14,9 +14,7 @@ export interface LoweringContent {
14
14
  nodes: TemporalNodeBase[];
15
15
  attachments: LoweringAttachment[];
16
16
  }
17
- /** 时间流固化后生成额外 attachment;读取统一追加前的结果,不直接修改结果 */
18
- export type LoweringAugmenter = (result: LoweringResult) => Iterable<LoweringAttachment>;
19
- /** 所有 augmenter 结果追加完成后的最终处理;按注册顺序执行,可修改完整结果或抛出语义错误 */
17
+ /** 收尾阶段回调;按显式 order 升序执行,可追加派生 attachment、修改结果或抛出语义错误 */
20
18
  export type LoweringFinalizer = (result: LoweringResult) => void;
21
19
  /**
22
20
  * lowering 的输出
@@ -7,7 +7,7 @@ export interface RelativeJianpuPitch {
7
7
  renderAcc: string;
8
8
  renderOctave: number;
9
9
  }
10
- export declare function tonality2Midi(tonality: string, defaultOctave: number): number;
10
+ export declare function tonality2Midi(tonality: string, defaultOctave?: number): number | null;
11
11
  export declare function acc2Offset(acc: string, ignoreNatural?: boolean, initOffset?: number): number;
12
12
  export declare function resolveNoteMidi(name: string, acc: string, octave: number, keySignature: string): number | null;
13
13
  /**
@@ -1,4 +1,3 @@
1
- import { Diagnostic } from "../../diagnostic.js";
2
1
  export const SHARP_NOTE_NAMES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
3
2
  export const FLAT_NOTE_NAMES = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
4
3
  /** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
@@ -25,9 +24,9 @@ export const NoteNameMap = {
25
24
  };
26
25
  const LETTER_ORDER = ["C", "D", "E", "F", "G", "A", "B"];
27
26
  const MAJOR_SCALE_OFFSETS = [0, 2, 4, 5, 7, 9, 11];
28
- // 输入 C4 D#3 B3b G, A 等音符字符串,输出 MIDI 数值
29
- // 要求非常严格:必须 [大写字母][升降号#bn只能一个][绝对八度]
30
- export function tonality2Midi(tonality, defaultOctave) {
27
+ // 输入 C4 D#3 B3b G, A 等音符字符串,输出 MIDI 数值;格式不合法返回 null
28
+ // 要求非常严格:必须 [大写字母或数字][升降号#bn只能一个][绝对八度]
29
+ export function tonality2Midi(tonality, defaultOctave = 4) {
31
30
  // 解析音名
32
31
  const note = tonality[0];
33
32
  let acc = "";
@@ -40,13 +39,13 @@ export function tonality2Midi(tonality, defaultOctave) {
40
39
  // 组合音名和升降号
41
40
  let noteValue = NoteNameMap[note + acc];
42
41
  if (noteValue === undefined)
43
- throw Diagnostic.error.InvalidTonality(tonality, { start: 0, end: i });
42
+ return null;
44
43
  // 解析八度
45
44
  if (i < tonality.length) {
46
45
  const octave = tonality.slice(i);
47
46
  defaultOctave = Number(octave);
48
47
  if (!/^-?\d+$/.test(octave) || !Number.isSafeInteger(defaultOctave)) {
49
- throw Diagnostic.error.InvalidTonality(tonality, { start: i, end: tonality.length });
48
+ return null;
50
49
  }
51
50
  }
52
51
  return (defaultOctave + 1) * 12 + noteValue;
@@ -70,7 +69,7 @@ export function resolveNoteMidi(name, acc, octave, keySignature) {
70
69
  return null;
71
70
  const offset = acc2Offset(acc);
72
71
  if (name >= "1" && name <= "7") {
73
- return tonality2Midi(keySignature, 4) + noteValue + offset + octave * 12;
72
+ return tonality2Midi(keySignature) + noteValue + offset + octave * 12;
74
73
  }
75
74
  return (octave + 1) * 12 + noteValue + offset;
76
75
  }
@@ -94,7 +93,7 @@ export function createDiatonicTranspose(name, acc, octave, keySignature) {
94
93
  degreeIndex = Number(relative.renderName) - 1;
95
94
  relativeOctave = relative.renderOctave;
96
95
  }
97
- const tonicMidi = tonality2Midi(keySignature, 4);
96
+ const tonicMidi = tonality2Midi(keySignature);
98
97
  const base = relativeOctave * 7 + degreeIndex;
99
98
  return steps => {
100
99
  const total = base + steps;
@@ -124,7 +123,7 @@ export function resolveLetterNameToJianpu(name, acc, octave, keySignature) {
124
123
  const actualMidi = resolveNoteMidi(name, acc, octave, keySignature);
125
124
  if (tonicIndex < 0 || noteIndex < 0 || actualMidi === null)
126
125
  return null;
127
- const tonicMidi = tonality2Midi(keySignature, 4);
126
+ const tonicMidi = tonality2Midi(keySignature);
128
127
  const degreeIndex = (noteIndex - tonicIndex + LETTER_ORDER.length) % LETTER_ORDER.length;
129
128
  const expectedPitchClass = wrapPitchClass(tonicMidi + MAJOR_SCALE_OFFSETS[degreeIndex]);
130
129
  const accidentalOffset = normalizeAccidentalOffset(wrapPitchClass(actualMidi) - expectedPitchClass);
@@ -2,9 +2,10 @@
2
2
  * 字符串字面量的唯一规则来源:预处理、`@text` 糖、`L:` 歌词糖共用
3
3
  * 扫描与反转义必须互为镜像,否则会出现“扫得对、还原错”
4
4
  */
5
+ import { Diagnostic } from "../../diagnostic.js";
5
6
  /** 找与 quotePos 处开引号配对的闭引号;找不到返回 -1 */
6
7
  export declare function findClosingQuote(source: string, quotePos: number, end: number): number;
7
- /** 剥掉成对的双引号并反转义 `\X` -> `X`;裸参数没走转义扫描,原样返回 */
8
- export declare function removeQuote(source: string): string;
8
+ /** 剥掉成对的双引号并反转义;解码失败时返回相对 source 的诊断,裸参数原样返回 */
9
+ export declare function parseString(source: string): string | Diagnostic;
9
10
  /** 生成双引号包裹的 jpFun 源码,仅转义反斜杠和双引号;与 JSON.stringify 不同,保留真实换行和制表符 */
10
11
  export declare function quote(value: string): string;
@@ -2,6 +2,7 @@
2
2
  * 字符串字面量的唯一规则来源:预处理、`@text` 糖、`L:` 歌词糖共用
3
3
  * 扫描与反转义必须互为镜像,否则会出现“扫得对、还原错”
4
4
  */
5
+ import { Diagnostic } from "../../diagnostic.js";
5
6
  /** 找与 quotePos 处开引号配对的闭引号;找不到返回 -1 */
6
7
  export function findClosingQuote(source, quotePos, end) {
7
8
  for (let i = quotePos + 1; i < end; i++) {
@@ -12,11 +13,23 @@ export function findClosingQuote(source, quotePos, end) {
12
13
  }
13
14
  return -1;
14
15
  }
15
- /** 剥掉成对的双引号并反转义 `\X` -> `X`;裸参数没走转义扫描,原样返回 */
16
- export function removeQuote(source) {
16
+ /** 剥掉成对的双引号并反转义;解码失败时返回相对 source 的诊断,裸参数原样返回 */
17
+ export function parseString(source) {
17
18
  if (source.length < 2 || !source.startsWith('"') || !source.endsWith('"'))
18
19
  return source;
19
- return source.slice(1, -1).replace(/\\(.)/gs, "$1");
20
+ let diagnostic;
21
+ // 连同不完整转义一起匹配以便诊断;替换结果不会被再次扫描
22
+ const text = source.slice(1, -1).replace(/\\(u.{0,4}|.)/gs, (escape, body, offset) => {
23
+ if (body[0] !== "u")
24
+ return body;
25
+ if (!/^u[0-9a-f]{4}$/i.test(body)) {
26
+ const start = offset + 1;
27
+ diagnostic ??= Diagnostic.warning.InvalidUnicodeEscape({ start, end: start + escape.length });
28
+ return escape;
29
+ }
30
+ return String.fromCharCode(Number.parseInt(body.slice(1), 16));
31
+ });
32
+ return diagnostic ?? text;
20
33
  }
21
34
  /** 生成双引号包裹的 jpFun 源码,仅转义反斜杠和双引号;与 JSON.stringify 不同,保留真实换行和制表符 */
22
35
  export function quote(value) {
@@ -1,7 +1,7 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
2
  import { DEFAULT_TEXT_FONT, JIANPU_NUMBER_FONT } from "../render/text.js";
3
3
  import { readCall, trimRange } from "./parse-utils/call-utils.js";
4
- import { removeQuote } from "./parse-utils/string-utils.js";
4
+ import { parseString } from "./parse-utils/string-utils.js";
5
5
  import { readBrace } from "./parse-utils/brace-utils.js";
6
6
  import { readLabel } from "./parse-utils/label-utils.js";
7
7
  import { parseLength } from "./parse-utils/length-utils.js";
@@ -529,7 +529,14 @@ export class ParserContext {
529
529
  return l;
530
530
  // 其他类型一律视为string
531
531
  default:
532
- return removeQuote(text);
532
+ const s = parseString(text);
533
+ if (s instanceof Diagnostic) {
534
+ s.span.start += start;
535
+ s.span.end += start;
536
+ this.diagnostics.push(s);
537
+ return text.slice(1, -1);
538
+ }
539
+ return s;
533
540
  }
534
541
  }
535
542
  length2px(length) {
package/dist/pipeline.js CHANGED
@@ -20,7 +20,6 @@ export function compileScore(source, options = {}) {
20
20
  parser.registerFunctions(functions);
21
21
  const ast = new ASTBraceNode({ start: 0, end: source.length }, parser.parse());
22
22
  const loweringContext = new LoweringContext(parser.diagnostics);
23
- loweringContext.registerFunctions(functions);
24
23
  const lowering = loweringContext.lowerDocument(ast);
25
24
  const layoutContext = createLayoutPrepareContext(functions, { textMeasurer });
26
25
  const layout = layoutDocument(lowering, layoutContext, layoutOptions);