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
@@ -0,0 +1,123 @@
1
+ import type { Fraction } from "../../fraction.js";
2
+ export type MusicXmlArpeggio = "none" | "up" | "down";
3
+ /** 单个书面音高,tie 标记保留在音高上以支持和弦成员独立连音 */
4
+ export interface MusicXmlPitch {
5
+ step: string;
6
+ alter: number;
7
+ octave: number;
8
+ tieStart: boolean;
9
+ tieStop: boolean;
10
+ }
11
+ /** 一个 lane 上的原子事件,同起止音高已合并为一个和弦事件 */
12
+ export interface MusicXmlEvent {
13
+ start: Fraction;
14
+ duration: Fraction;
15
+ pitches: MusicXmlPitch[];
16
+ rest: boolean;
17
+ order: number;
18
+ modifiers: {
19
+ name: string;
20
+ placement: "above" | "below";
21
+ }[];
22
+ annotations: {
23
+ text: string;
24
+ placement: "above" | "below";
25
+ boxed?: boolean;
26
+ }[];
27
+ preGraces: MusicXmlPitch[][];
28
+ postGraces: MusicXmlPitch[][];
29
+ lyrics: Map<string, string>;
30
+ arpeggio?: MusicXmlArpeggio;
31
+ program?: number;
32
+ timeModification?: {
33
+ actual: number;
34
+ normal: number;
35
+ start: boolean;
36
+ stop: boolean;
37
+ };
38
+ }
39
+ /** MusicXML 的 part + staff + voice 对应一条 jpFun lane */
40
+ export interface MusicXmlLane {
41
+ partId: string;
42
+ partName: string;
43
+ staff: string;
44
+ voice: string;
45
+ events: MusicXmlEvent[];
46
+ }
47
+ /** 全谱时间流上的拍号状态点 */
48
+ export interface MusicXmlMeterPoint {
49
+ at: Fraction;
50
+ numerator: number;
51
+ denominator: number;
52
+ }
53
+ /** 全谱时间流上的调号状态点 */
54
+ export interface MusicXmlKeyPoint {
55
+ at: Fraction;
56
+ fifths: number;
57
+ mode: string;
58
+ }
59
+ /** 尚未附着到事件的力度或文字方向 */
60
+ export interface MusicXmlDirectionPoint {
61
+ at: Fraction;
62
+ partId: string;
63
+ staff: string;
64
+ voice: string;
65
+ placement: "above" | "below";
66
+ dynamic?: string;
67
+ texts: {
68
+ text: string;
69
+ boxed: boolean;
70
+ }[];
71
+ }
72
+ /** 参与楔形线配对的原始起止点 */
73
+ export interface MusicXmlWedgePoint {
74
+ at: Fraction;
75
+ partId: string;
76
+ staff: string;
77
+ voice: string;
78
+ number: string;
79
+ type: "crescendo" | "diminuendo" | "stop";
80
+ }
81
+ /** 已配对的渐强或渐弱区间 */
82
+ export interface MusicXmlWedgeSpan {
83
+ from: MusicXmlWedgePoint;
84
+ end: Fraction;
85
+ }
86
+ /** 参与房子配对的原始起止点 */
87
+ export interface MusicXmlEndingPoint {
88
+ at: Fraction;
89
+ type: "start" | "stop" | "discontinue";
90
+ passes: number[];
91
+ }
92
+ /** 已配对的房子区间,遍数语义由起点持有 */
93
+ export interface MusicXmlEndingSpan {
94
+ from: MusicXmlEndingPoint;
95
+ end: Fraction;
96
+ }
97
+ /** XML 解析完成后的格式无关乐谱模型,渲染阶段不再读取 DOM */
98
+ export interface ParsedMusicXmlScore {
99
+ title: string;
100
+ subtitle: string;
101
+ creator: string;
102
+ page?: {
103
+ width: number;
104
+ height: number;
105
+ top: number;
106
+ bottom: number;
107
+ left: number;
108
+ right: number;
109
+ };
110
+ lanes: MusicXmlLane[];
111
+ meters: MusicXmlMeterPoint[];
112
+ keys: MusicXmlKeyPoint[];
113
+ tempos: {
114
+ at: Fraction;
115
+ bpm: number;
116
+ }[];
117
+ wedges: MusicXmlWedgeSpan[];
118
+ endings: MusicXmlEndingSpan[];
119
+ bars: Map<string, string>;
120
+ measureBoundaries: Set<string>;
121
+ lineBreaks: Set<string>;
122
+ end: Fraction;
123
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ export { quote } from "../parser/parse-utils/string-utils.js";
2
+ export type PitchMode = "relative" | "absolute";
3
+ export interface SourcePitch {
4
+ name: string;
5
+ accidental: string;
6
+ octave: number;
7
+ }
8
+ interface HeadOptions {
9
+ title?: string;
10
+ subtitle?: string;
11
+ author?: string;
12
+ key: string;
13
+ meter: readonly [numerator: number, denominator: number];
14
+ tempo: number;
15
+ }
16
+ /** 按书写顺序把多个零时长标记叠到主体上方。 */
17
+ export declare function attachAbove(source: string, additions: readonly string[]): string;
18
+ /** 把已解释为目标记谱方式的音高写成 jpFun token。 */
19
+ export declare function renderPitch(pitch: SourcePitch, mode: PitchMode, suffix?: string): string;
20
+ export declare function renderHead(options: HeadOptions): string;
21
+ /** 把“每个声部的各系统”转置成“每个系统的完整声部组” */
22
+ export declare function renderSystems(groups: readonly (readonly string[])[]): string;
@@ -0,0 +1,51 @@
1
+ /** MIDI 与 MusicXML 共享的 jpFun 源码片段生成工具 */
2
+ import { quote } from "../parser/parse-utils/string-utils.js";
3
+ export { quote } from "../parser/parse-utils/string-utils.js";
4
+ const HEAD_TEXT_PRESET = {
5
+ title: "size=2em, align=center",
6
+ subtitle: "size=0.85em, align=center",
7
+ author: "size=0.8em, align=right",
8
+ };
9
+ /** 按书写顺序把多个零时长标记叠到主体上方。 */
10
+ export function attachAbove(source, additions) {
11
+ for (let index = additions.length - 1; index >= 0; index--) {
12
+ source = `{${source} ^ ${additions[index]}}`;
13
+ }
14
+ return source;
15
+ }
16
+ /** 把已解释为目标记谱方式的音高写成 jpFun token。 */
17
+ export function renderPitch(pitch, mode, suffix = "") {
18
+ if (mode === "absolute") {
19
+ return pitch.octave === 0
20
+ ? `@note(${pitch.name}, ${pitch.accidental}, 0)${suffix}`
21
+ : `${pitch.name}${pitch.accidental}${pitch.octave}${suffix}`;
22
+ }
23
+ const octaveMarks = pitch.octave > 0 ? "'".repeat(pitch.octave) : ",".repeat(-pitch.octave);
24
+ return `${pitch.accidental}${pitch.name}${octaveMarks}${suffix}`;
25
+ }
26
+ function renderHeadText(field, value) {
27
+ // 会被注释、续行或 DSL 语法误读的文本改用显式 @text,其余保留简洁的 H.* 裸文本
28
+ const needsExplicitText = value.trim() !== value
29
+ || /[\r\n%]/.test(value) || /^\s*[@{]/.test(value) || value.endsWith("\\");
30
+ return needsExplicitText
31
+ ? `H.${field}: @text(${quote(value)}, ${HEAD_TEXT_PRESET[field]})`
32
+ : `H.${field}: ${value}`;
33
+ }
34
+ export function renderHead(options) {
35
+ const [numerator, denominator] = options.meter;
36
+ const lines = [];
37
+ if (options.title)
38
+ lines.push(renderHeadText("title", options.title));
39
+ if (options.subtitle)
40
+ lines.push(renderHeadText("subtitle", options.subtitle));
41
+ if (options.author)
42
+ lines.push(renderHeadText("author", options.author));
43
+ lines.push(`H.signature: 1=${options.key} ${numerator}/${denominator}`);
44
+ lines.push(`H.tempo: ${options.tempo}`);
45
+ return lines.join("\n");
46
+ }
47
+ /** 把“每个声部的各系统”转置成“每个系统的完整声部组” */
48
+ export function renderSystems(groups) {
49
+ const systemCount = Math.max(0, ...groups.map(lines => lines.length));
50
+ return Array.from({ length: systemCount }, (_, index) => groups.map(lines => lines[index]).filter(Boolean).join("\n")).join("\n\n");
51
+ }
package/dist/core.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ export * from "./pipeline.js";
2
+ export * from "./functions/ASTtypes.js";
3
+ export * from "./functions/temporal.js";
4
+ export * from "./functions/default.js";
5
+ export * from "./diagnostic.js";
6
+ export * from "./fraction.js";
7
+ export * from "./parser/types.js";
8
+ export * from "./parser/grammarType.js";
9
+ export * from "./parser/parserContext.js";
10
+ export * from "./parser/preprocess.js";
11
+ export * from "./lowering/types.js";
12
+ export * from "./lowering/loweringContext.js";
13
+ export * from "./lowering/track.js";
14
+ export * from "./playback/types.js";
15
+ export * from "./playback/time.js";
16
+ export * from "./playback/compile.js";
17
+ export * from "./layout/types.js";
18
+ export * from "./layout/model.js";
19
+ export * from "./layout/default.js";
20
+ export * from "./layout/page.js";
21
+ export * from "./layout/engine.js";
22
+ export * from "./render/index.js";
package/dist/core.js ADDED
@@ -0,0 +1,22 @@
1
+ export * from "./pipeline.js";
2
+ export * from "./functions/ASTtypes.js";
3
+ export * from "./functions/temporal.js";
4
+ export * from "./functions/default.js";
5
+ export * from "./diagnostic.js";
6
+ export * from "./fraction.js";
7
+ export * from "./parser/types.js";
8
+ export * from "./parser/grammarType.js";
9
+ export * from "./parser/parserContext.js";
10
+ export * from "./parser/preprocess.js";
11
+ export * from "./lowering/types.js";
12
+ export * from "./lowering/loweringContext.js";
13
+ export * from "./lowering/track.js";
14
+ export * from "./playback/types.js";
15
+ export * from "./playback/time.js";
16
+ export * from "./playback/compile.js";
17
+ export * from "./layout/types.js";
18
+ export * from "./layout/model.js";
19
+ export * from "./layout/default.js";
20
+ export * from "./layout/page.js";
21
+ export * from "./layout/engine.js";
22
+ export * from "./render/index.js";
@@ -132,8 +132,11 @@ Diagnostic.warning = {
132
132
  LabelWithoutTarget: (labelName, span) => {
133
133
  return new WarningDiagnostic("W_LABEL_WITHOUT_TARGET", `标签 "${labelName}" 没有可绑定的目标;标签将被忽略`, span);
134
134
  },
135
- LabelAlreadyExists: (newLabel, oldLabel, span) => {
136
- return new WarningDiagnostic("W_LABEL_ALREADY_EXISTS", `标签 "${newLabel}" 已经存在(为 "${oldLabel}");新标签将覆盖旧标签`, span);
135
+ TargetAlreadyLabeled: (newLabel, oldLabel, span) => {
136
+ return new WarningDiagnostic("W_TARGET_ALREADY_LABELED", `目标已标注为 "${oldLabel}","${newLabel}" 将被忽略`, span);
137
+ },
138
+ UnresolvedEndpoint: (functionName, span) => {
139
+ return new WarningDiagnostic("W_UNRESOLVED_ENDPOINT", `@${functionName} 有端点找不到对应的谱面对象;该关系将被忽略`, span);
137
140
  },
138
141
  ZeroTimeTrack: (span, trackOrder) => {
139
142
  return new WarningDiagnostic("W_ZERO_TIME_TRACK", `第${trackOrder}个子轨道的时长为0`, span);
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 分数类
3
+ * 为了性能没有做过多的类型检查,使用时请确保传入的参数是整数
4
+ */
5
+ export declare class Fraction {
6
+ private _numerator;
7
+ private _denominator;
8
+ constructor(numerator?: number, denominator?: number);
9
+ get numerator(): number;
10
+ get denominator(): number;
11
+ set(numerator: number, denominator?: number): this;
12
+ copyFrom(other: Fraction): this;
13
+ clone(): Fraction;
14
+ add(value: Fraction | number, denominator?: number): this;
15
+ sub(value: Fraction | number, denominator?: number): this;
16
+ mul(value: Fraction | number, denominator?: number): this;
17
+ div(value: Fraction | number, denominator?: number): this;
18
+ divPow2(power?: number): this;
19
+ compare(value: Fraction | number, denominator?: number): number;
20
+ equals(value: Fraction | number, denominator?: number): boolean;
21
+ isZero(): boolean;
22
+ toNumber(): number;
23
+ toString(): string;
24
+ }
@@ -0,0 +1,132 @@
1
+ // 认为输入为正数
2
+ function gcd(a, b) {
3
+ while (b !== 0) {
4
+ const r = a % b;
5
+ a = b;
6
+ b = r;
7
+ }
8
+ return a;
9
+ }
10
+ /**
11
+ * 分数类
12
+ * 为了性能没有做过多的类型检查,使用时请确保传入的参数是整数
13
+ */
14
+ export class Fraction {
15
+ _numerator = 0;
16
+ _denominator = 1;
17
+ constructor(numerator = 0, denominator = 1) {
18
+ this.set(numerator, denominator);
19
+ }
20
+ get numerator() {
21
+ return this._numerator;
22
+ }
23
+ get denominator() {
24
+ return this._denominator;
25
+ }
26
+ set(numerator, denominator = 1) {
27
+ if (denominator === 0)
28
+ throw new RangeError("Denominator must not be zero");
29
+ if (denominator < 0) {
30
+ numerator = -numerator;
31
+ denominator = -denominator;
32
+ }
33
+ const divisor = gcd(Math.abs(numerator), denominator);
34
+ this._numerator = numerator / divisor;
35
+ this._denominator = denominator / divisor;
36
+ return this;
37
+ }
38
+ copyFrom(other) {
39
+ this._numerator = other._numerator;
40
+ this._denominator = other._denominator;
41
+ return this;
42
+ }
43
+ clone() {
44
+ return new Fraction(this._numerator, this._denominator);
45
+ }
46
+ add(value, denominator = 1) {
47
+ let numerator;
48
+ if (typeof value === "number") {
49
+ numerator = value;
50
+ }
51
+ else {
52
+ numerator = value._numerator;
53
+ denominator = value._denominator;
54
+ }
55
+ return this.set(this._numerator * denominator + numerator * this._denominator, this._denominator * denominator);
56
+ }
57
+ sub(value, denominator = 1) {
58
+ let numerator;
59
+ if (typeof value === "number") {
60
+ numerator = value;
61
+ }
62
+ else {
63
+ numerator = value._numerator;
64
+ denominator = value._denominator;
65
+ }
66
+ return this.set(this._numerator * denominator - numerator * this._denominator, this._denominator * denominator);
67
+ }
68
+ mul(value, denominator = 1) {
69
+ let numerator;
70
+ if (typeof value === "number") {
71
+ numerator = value;
72
+ }
73
+ else {
74
+ numerator = value._numerator;
75
+ denominator = value._denominator;
76
+ }
77
+ return this.set(this._numerator * numerator, this._denominator * denominator);
78
+ }
79
+ div(value, denominator = 1) {
80
+ let numerator;
81
+ if (typeof value === "number") {
82
+ numerator = value;
83
+ }
84
+ else {
85
+ numerator = value._numerator;
86
+ denominator = value._denominator;
87
+ }
88
+ if (numerator === 0)
89
+ throw new RangeError("Cannot divide by zero");
90
+ return this.set(this._numerator * denominator, this._denominator * numerator);
91
+ }
92
+ divPow2(power = 1) {
93
+ if (this._numerator === 0)
94
+ return this;
95
+ while (power > 0 && this._numerator % 2 === 0) {
96
+ this._numerator /= 2;
97
+ power--;
98
+ }
99
+ this._denominator *= 2 ** power;
100
+ return this;
101
+ }
102
+ compare(value, denominator = 1) {
103
+ let numerator;
104
+ if (typeof value === "number") {
105
+ numerator = value;
106
+ }
107
+ else {
108
+ numerator = value._numerator;
109
+ denominator = value._denominator;
110
+ }
111
+ const difference = this._numerator * denominator - numerator * this._denominator;
112
+ return difference < 0 ? -1 : difference > 0 ? 1 : 0;
113
+ }
114
+ equals(value, denominator = 1) {
115
+ if (value instanceof Fraction) {
116
+ return this._numerator === value._numerator
117
+ && this._denominator === value._denominator;
118
+ }
119
+ return this._numerator * denominator === value * this._denominator;
120
+ }
121
+ isZero() {
122
+ return this._numerator === 0;
123
+ }
124
+ toNumber() {
125
+ return this._numerator / this._denominator;
126
+ }
127
+ toString() {
128
+ return this._denominator === 1
129
+ ? String(this._numerator)
130
+ : `${this._numerator}/${this._denominator}`;
131
+ }
132
+ }
@@ -1,6 +1,9 @@
1
1
  import type { SourceSpan, LengthValue, deSugarAtomFunction, deSugarRelationFunction } from "../parser/types.js";
2
+ import type { CallArgumentInfo } from "../parser/grammarType.js";
2
3
  import type { ParserContext } from "../parser/parserContext.js";
3
- import type { LoweringAugmenter, LoweringFinalizer, TemporalNodeBase, TimeFlowModel } from "../lowering/types.js";
4
+ import type { Fraction } from "../fraction.js";
5
+ import type { LoweringAugmenter, LoweringFinalizer, TimeFlowModel } from "../lowering/types.js";
6
+ import type { TemporalNodeBase } from "./temporal.js";
4
7
  import type { LoweringContext } from "../lowering/loweringContext.js";
5
8
  import type { Track } from "../lowering/track.js";
6
9
  import type { LayoutDecorationHandler } from "../layout/types.js";
@@ -13,28 +16,8 @@ export declare class ASTNodeBase {
13
16
  sourceSpan: SourceSpan;
14
17
  parent: ASTNodeBase | null;
15
18
  get children(): ASTNodeBase[] | null;
16
- constructor(sourceSpan: SourceSpan, parent?: ASTNodeBase | null);
17
- /**
18
- * lowering 固化后的 attachment 生成钩子
19
- *
20
- * 普通 loweringEnter/loweringExit 在递归遍历 AST 时执行,
21
- * 此时尚未完成锚点归并,事件的 t、track 和 layoutLine 不可用,因此需要观察完整事件流的功能不能在普通 hook 中可靠决定分组。
22
- * 该 hook 读取 LoweringResult 并返回新建的 LayoutAttachment,LoweringContext 将返回值追加到当前文档或 fragment 的 attachments,不会把它们放入时间列或推进时间。
23
- *
24
- * 例:autobeam 扫描最终列,按轨道、谱面行、拍点和显式 beam 端点决定分组,再生成 BeamLayoutAttachment
25
- */
26
- static loweringAugment?: LoweringAugmenter;
27
- /**
28
- * 所有 loweringAugment 结果追加完成后的最终处理;此时已经得到了完整的 lowering 语义
29
- * 可以进行校验(如 beam 和 tie)或者修改某些值
30
- */
31
- static loweringFinalize?: LoweringFinalizer;
32
- /**
33
- * 装饰函数可选的排版声明
34
- * layout 引擎按 key 收集,不需要识别具体函数类
35
- * addon 中的 `@key` 标志会使用该装饰
36
- */
37
- static layoutDecorationHandler?: LayoutDecorationHandler;
19
+ constructor(sourceSpan: SourceSpan, // 源码区间
20
+ parent?: ASTNodeBase | null);
38
21
  /**
39
22
  * 进入当前层级的回调
40
23
  * @param ctx 当前的 lowering 上下文
@@ -44,21 +27,27 @@ export declare class ASTNodeBase {
44
27
  loweringEnter(_ctx: LoweringContext, _track: Track): Iterable<TemporalNodeBase>;
45
28
  /**
46
29
  * 时间求解模式
47
- * 由 loweringContext 调用,决定当前节点在时间求解阶段的展开方式
30
+ * loweringEnter 返回的事件已规范化并加入索引后调用,决定子节点的展开方式。
31
+ * 需要本轮事件时通过 ctx.getTemporalNodes 查询
48
32
  * @returns 返回 null 表示没有子元素,返回对象表示参与时间求解
49
33
  */
50
- timeFlowModel(): TimeFlowModel | null;
34
+ timeFlowModel(_ctx: LoweringContext): TimeFlowModel | null;
51
35
  /**
52
36
  * 离开当前层级的回调 同 loweringEnter
37
+ *
38
+ * timeOffset 是 children 展开后的当前时间游标。
39
+ * 只有在同步重写本节点已经产生的 Temporal.t/T 时才可原地修改它,避免事件时间与后继事件起点不一致。
53
40
  */
54
- loweringExit(_ctx: LoweringContext, _track: Track): Iterable<TemporalNodeBase>;
41
+ loweringExit(_ctx: LoweringContext, _track: Track, _timeOffset: Fraction): Iterable<TemporalNodeBase>;
55
42
  toString(source: string): string;
56
43
  }
57
44
  export declare class ASTTextNode extends ASTNodeBase {
58
45
  }
59
46
  export declare class ASTLabelNode extends ASTNodeBase {
60
47
  label: string;
61
- constructor(sourceSpan: SourceSpan, label: string, parent: ASTNodeBase);
48
+ /** 被标注的节点。parent 是树结构、会被容器改写,不能用来表达这层关系 */
49
+ readonly target: ASTNodeBase;
50
+ constructor(sourceSpan: SourceSpan, label: string, target: ASTNodeBase, parent?: ASTNodeBase | null);
62
51
  toString(_: string): string;
63
52
  }
64
53
  export declare class ASTBraceNode extends ASTNodeBase {
@@ -89,12 +78,14 @@ export interface FunctionDef {
89
78
  }
90
79
  /** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
91
80
  export declare function resolveArgType(def: FunctionDef, name: string | undefined, index: number): paramType | undefined;
92
- export type FunctionArgs = Map<string | number, paramValue | SourceSpan>;
81
+ /** 别名里的第一个是主名;`@set` 的 key 和 `getArgValue` 的前缀必须用同一个 */
82
+ export declare function primaryName(def: FunctionDef): string;
83
+ export type FunctionArgs = Map<string | number, paramValue | CallArgumentInfo>;
93
84
  export declare class ASTFunctionNode extends ASTNodeBase {
94
85
  /**
95
- * 返回真正承载标签的 AST 节点,null 表示不可被标注
86
+ * 不实现 = 对标签透明,绑定继续向前找;返回 null = 边界,绑定到此为止;返回节点 = 标签承载者
96
87
  */
97
- labelable(): ASTFunctionNode | null;
88
+ labelable?(): ASTFunctionNode | null;
98
89
  label?: string;
99
90
  static def?: FunctionDef;
100
91
  get def(): FunctionDef | undefined;
@@ -105,6 +96,35 @@ export declare class ASTFunctionNode extends ASTNodeBase {
105
96
  get deSugarRelation(): deSugarRelationFunction;
106
97
  getArgValue(args: FunctionArgs, ctx: ParserContext): paramValue[];
107
98
  toString(source: string): string;
99
+ /**
100
+ * lowering 固化后的 attachment 生成钩子
101
+ *
102
+ * 普通 loweringEnter/loweringExit 在递归遍历 AST 时执行,
103
+ * 此时尚未完成锚点归并,事件的 t、track 和 layoutLine 不可用,因此需要观察完整事件流的功能不能在普通 hook 中可靠决定分组。
104
+ * 该 hook 读取 LoweringResult 并返回新建的 LoweringAttachment,LoweringContext 将返回值追加到当前文档或 fragment 的 attachments,不会把它们放入时间列或推进时间。
105
+ *
106
+ * 例:autobeam 扫描最终列,按轨道、谱面行、拍点和显式 beam 端点决定分组,再生成 BeamLayoutAttachment
107
+ */
108
+ static loweringAugment?: LoweringAugmenter;
109
+ /**
110
+ * 所有 loweringAugment 结果追加完成后的最终处理;此时已经得到了完整的 lowering 语义
111
+ * 所有派生 attachment 追加完成后,按函数注册顺序处理完整结果
112
+ * 可以进行校验(如 beam 和 tie)或者修改某些值
113
+ */
114
+ static loweringFinalize?: LoweringFinalizer;
115
+ /**
116
+ * 装饰函数可选的排版声明
117
+ * layout 引擎按 key 收集,不需要识别具体函数类
118
+ * addon 中的 `@key` 标志会使用该装饰
119
+ */
120
+ static layoutDecorationHandler?: LayoutDecorationHandler;
121
+ /**
122
+ * 在子树里找标签承载者
123
+ * 未实现 labelable = 透明,继续下潜;实现了就采信它的返回值,null 即拒绝,不再往里找。
124
+ * 拒绝时别退回去遍历 children —— grace 的 labelable() 内部已经走过整棵子树
125
+ * 只有函数节点声明 labelable,别的节点读到的必是 undefined,所以无需 instanceof
126
+ */
127
+ static findLabelable(node: ASTNodeBase): ASTFunctionNode | null;
108
128
  /**
109
129
  * 用于语法糖向前结合
110
130
  * 一般向前结合都是寻找brace或者function
@@ -112,4 +132,4 @@ export declare class ASTFunctionNode extends ASTNodeBase {
112
132
  */
113
133
  static findLastFuncContentNode(nodes: ASTNodeBase[], i: number): ASTNodeBase | null;
114
134
  }
115
- export type ASTFunctionClass = new (sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent: ASTNodeBase | null) => ASTFunctionNode;
135
+ export type ASTFunctionClass = (new (sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent: ASTNodeBase | null) => ASTFunctionNode) & Pick<typeof ASTFunctionNode, "prototype" | "def" | "deSugarAtom" | "deSugarRelation" | "loweringAugment" | "loweringFinalize" | "layoutDecorationHandler">;