jpfun 0.1.0 → 0.3.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/LICENSE +200 -200
- package/README.md +191 -133
- 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 +24 -7
- package/dist/functions/ASTtypes.js +35 -16
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +48 -19
- package/dist/functions/beam/auto.js +35 -18
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +3 -2
- package/dist/functions/beam/layout.js +42 -64
- package/dist/functions/box/index.js +35 -34
- package/dist/functions/br/index.js +14 -12
- package/dist/functions/dash/index.js +37 -5
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +51 -23
- package/dist/functions/dot/index.js +16 -8
- package/dist/functions/grace/index.d.ts +8 -18
- package/dist/functions/grace/index.js +98 -99
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +98 -5
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +154 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +60 -80
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +42 -34
- package/dist/functions/stack/index.js +39 -34
- package/dist/functions/symbol/index.d.ts +21 -0
- package/dist/functions/symbol/index.js +94 -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 +22 -0
- package/dist/functions/symbol/shape.js +48 -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/f.d.ts +2 -0
- package/dist/functions/symbol/symbols/f.js +55 -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/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +43 -0
- package/dist/functions/symbol/symbols/p.d.ts +2 -0
- package/dist/functions/symbol/symbols/p.js +61 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +75 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +53 -3
- package/dist/functions/text/index.js +78 -26
- package/dist/functions/tie/index.js +103 -128
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/{over → up}/index.d.ts +1 -0
- package/dist/functions/up/index.js +370 -0
- package/dist/functions/voice/index.js +125 -108
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +227 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/jpfun.min.js +143 -0
- package/dist/layout/engine.d.ts +4 -11
- package/dist/layout/engine.js +100 -66
- package/dist/layout/model.d.ts +8 -1
- package/dist/layout/model.js +16 -6
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +51 -18
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +19 -23
- package/dist/lowering/loweringContext.js +179 -187
- package/dist/lowering/track.d.ts +33 -10
- package/dist/lowering/track.js +6 -6
- package/dist/lowering/types.d.ts +57 -43
- package/dist/lowering/types.js +20 -4
- 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 +8 -0
- package/dist/parser/parse-utils/string-utils.js +20 -0
- package/dist/parser/parserContext.d.ts +8 -5
- package/dist/parser/parserContext.js +38 -33
- package/dist/parser/preprocess.js +17 -11
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +401 -0
- package/dist/playback/event.d.ts +91 -0
- package/dist/playback/event.js +87 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +128 -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 +17 -0
- package/dist/render/text.js +35 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +217 -122
- package/package.json +14 -9
- package/dist/functions/over/index.js +0 -307
package/README.md
CHANGED
|
@@ -1,134 +1,192 @@
|
|
|
1
|
-
# jpFun
|
|
2
|
-
|
|
3
|
-
A functional numbered musical notation typesetting engine for TypeScript.
|
|
4
|
-
|
|
5
|
-
jpFun compiles a compact numbered-notation DSL into a reusable document layout that can be rendered as SVG or Canvas.
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
- Automatic spacing,
|
|
12
|
-
- Shared layout model for SVG and Canvas rendering
|
|
13
|
-
- Structured diagnostics and source locations
|
|
14
|
-
- Zero runtime dependencies
|
|
15
|
-
- Works in both Node.js and modern browsers
|
|
16
|
-
|
|
17
|
-
## Installation
|
|
18
|
-
|
|
19
|
-
```sh
|
|
20
|
-
npm install jpfun
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
jpFun is ESM-only and requires Node.js 18 or later:
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
import { compileScore } from "jpfun";
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
CommonJS `require("jpfun")` is not supported.
|
|
30
|
-
|
|
31
|
-
##
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const result = compileScore("1 2 3 | 4");
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
result.layout;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
##
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
1
|
+
# jpFun
|
|
2
|
+
|
|
3
|
+
A functional numbered musical notation typesetting engine for TypeScript.
|
|
4
|
+
|
|
5
|
+
jpFun compiles a compact numbered-notation DSL into a reusable document layout that can be rendered as SVG or Canvas.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Compact DSL with syntax sugar and function-based extensibility
|
|
10
|
+
- Exact rational timing and composable score structures
|
|
11
|
+
- Automatic spacing, explicit system breaks, and page-aware pagination
|
|
12
|
+
- Shared layout model for SVG and Canvas rendering
|
|
13
|
+
- Structured diagnostics and source locations
|
|
14
|
+
- Zero runtime dependencies
|
|
15
|
+
- Works in both Node.js and modern browsers
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install jpfun
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
jpFun is ESM-only and requires Node.js 18 or later:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { compileScore } from "jpfun";
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
CommonJS `require("jpfun")` is not supported.
|
|
30
|
+
|
|
31
|
+
## Browser via CDN
|
|
32
|
+
|
|
33
|
+
A bundled, minified build ships with the package, so no build step is needed for a plain web page. Load it with a `<script>` tag and use the `jpfun` global:
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<script src="https://unpkg.com/jpfun"></script>
|
|
37
|
+
<script>
|
|
38
|
+
const { compileScore, renderLayoutPagesToSvg } = jpfun;
|
|
39
|
+
document.body.innerHTML = renderLayoutPagesToSvg(compileScore("1 2 3 | 4 -").layout)[0];
|
|
40
|
+
</script>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For ES modules, jsDelivr bundles the package on the fly:
|
|
44
|
+
|
|
45
|
+
```html
|
|
46
|
+
<script type="module">
|
|
47
|
+
import { compileScore } from "https://cdn.jsdelivr.net/npm/jpfun/+esm";
|
|
48
|
+
</script>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Both URLs resolve to the latest published version. In production, append an exact version (`jpfun@x.y.z`) so that publishing a new release cannot change an existing page.
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { compileScore, renderLayoutPagesToSvg } from "jpfun";
|
|
57
|
+
|
|
58
|
+
const source = `
|
|
59
|
+
@1(C4)
|
|
60
|
+
@tempo(96)
|
|
61
|
+
|
|
62
|
+
1/ 2/ 3 4 | 5 -
|
|
63
|
+
`;
|
|
64
|
+
|
|
65
|
+
const result = compileScore(source);
|
|
66
|
+
const pages = renderLayoutPagesToSvg(result.layout);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`renderLayoutPagesToSvg` returns one SVG string per page. Even an infinite-height document produces one natural-height page. Each SVG can be written to a file, sent in an HTTP response, or inserted into a browser page.
|
|
70
|
+
|
|
71
|
+
## Canvas Rendering
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { compileScore, renderLayoutPagesToCanvas } from "jpfun";
|
|
75
|
+
|
|
76
|
+
const result = compileScore("1 2 3 | 4");
|
|
77
|
+
const canvas = document.querySelector("canvas");
|
|
78
|
+
const context = canvas.getContext("2d");
|
|
79
|
+
|
|
80
|
+
if (context) {
|
|
81
|
+
renderLayoutPagesToCanvas(result.layout, [context]);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Text Measurement
|
|
86
|
+
|
|
87
|
+
Layout depends on text widths, so `compileScore` needs a `TextMeasurer`. The default one estimates every character at a fixed fraction of the font size. That keeps results platform-independent, which is what tests and server-side rendering want, but it does not match the real glyph widths of a browser font — narrow letters are overestimated, so right-aligned text and `@box` bounds drift visibly.
|
|
88
|
+
|
|
89
|
+
In a browser, pass `CanvasTextMeasurer` so that measurement agrees with what the renderer draws:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { compileScore, CanvasTextMeasurer } from "jpfun";
|
|
93
|
+
|
|
94
|
+
const context = document.createElement("canvas").getContext("2d")!;
|
|
95
|
+
const result = compileScore(source, { textMeasurer: new CanvasTextMeasurer(context) });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
This applies to SVG output too, since measurement happens during layout rather than during painting. The context is only used for measuring and need not be the canvas you render into.
|
|
99
|
+
|
|
100
|
+
## Compilation Pipeline
|
|
101
|
+
|
|
102
|
+
jpFun exposes the intermediate results of its compilation pipeline:
|
|
103
|
+
|
|
104
|
+
```text
|
|
105
|
+
Source
|
|
106
|
+
-> Parser -> AST
|
|
107
|
+
-> Lowering -> timed events and relations
|
|
108
|
+
|-> Playback -> NoteOn / NoteOff / Tempo events
|
|
109
|
+
`-> Layout -> document geometry
|
|
110
|
+
-> Renderer -> SVG or Canvas
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const result = compileScore("1 2 3 | 4");
|
|
115
|
+
|
|
116
|
+
result.parser;
|
|
117
|
+
result.ast;
|
|
118
|
+
result.lowering;
|
|
119
|
+
result.layout;
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Playback is compiled independently from layout:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { compilePlayback } from "jpfun";
|
|
126
|
+
|
|
127
|
+
const playback = compilePlayback(result.lowering);
|
|
128
|
+
playback.events;
|
|
129
|
+
playback.scoreMap;
|
|
130
|
+
playback.tracks;
|
|
131
|
+
playback.durationSeconds;
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
For scores with unusually large repeat expansion, raise the control-flow budget explicitly:
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const playback = compilePlayback(result.lowering, { maxFlowSteps: 200_000 });
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The default is 65,536 visited columns. Exceeding it throws instead of returning a partial playback plan.
|
|
141
|
+
|
|
142
|
+
The plan stores device-independent `note-on`, `note-off`, and `tempo` events. Its track number is the same channel identity used by the visual Track; Web Audio, Web MIDI, and Standard MIDI File adapters consume that identity and the exact `Fraction` timestamps at their boundary.
|
|
143
|
+
|
|
144
|
+
## Syntax Example
|
|
145
|
+
|
|
146
|
+
```jpfun
|
|
147
|
+
@page(width=860px, left=38px, right=38px)
|
|
148
|
+
@1(C4)
|
|
149
|
+
@tempo(96)
|
|
150
|
+
|
|
151
|
+
N(Melody): 1/ 1/ 5/ 5/ 6/ 6/ 5 -
|
|
152
|
+
N: @div(C4 C4) G4/G4/ A4/ A/ G
|
|
153
|
+
L(Lyrics): do do so so la la so
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
See the [language reference](grammar.md) for the complete syntax.
|
|
157
|
+
|
|
158
|
+
## Runtime Support
|
|
159
|
+
|
|
160
|
+
- ESM only
|
|
161
|
+
- Node.js 18 or later
|
|
162
|
+
- Modern browsers and bundlers
|
|
163
|
+
- No DOM dependency in the compiler
|
|
164
|
+
- No runtime dependencies
|
|
165
|
+
|
|
166
|
+
## Development
|
|
167
|
+
|
|
168
|
+
jpFun is maintained in a pnpm workspace. From the repository root:
|
|
169
|
+
|
|
170
|
+
```sh
|
|
171
|
+
pnpm install
|
|
172
|
+
pnpm run build:core
|
|
173
|
+
pnpm test
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The repository also includes a browser-based Playground with diagnostics and SVG/Canvas previews:
|
|
177
|
+
|
|
178
|
+
```sh
|
|
179
|
+
pnpm run dev
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Then open <http://127.0.0.1:4173>.
|
|
183
|
+
|
|
184
|
+
## Documentation
|
|
185
|
+
|
|
186
|
+
- [Language reference](grammar.md)
|
|
187
|
+
- [Repository](https://github.com/madderscientist/jpFun)
|
|
188
|
+
- [Issue tracker](https://github.com/madderscientist/jpFun/issues)
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
134
192
|
[Apache License 2.0](LICENSE)
|
package/dist/diagnostic.js
CHANGED
|
@@ -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
|
-
|
|
136
|
-
return new WarningDiagnostic("
|
|
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
|
+
}
|
package/dist/fraction.js
ADDED
|
@@ -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,5 +1,7 @@
|
|
|
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";
|
|
4
|
+
import type { Fraction } from "../fraction.js";
|
|
3
5
|
import type { LoweringAugmenter, LoweringFinalizer, TemporalNodeBase, TimeFlowModel } from "../lowering/types.js";
|
|
4
6
|
import type { LoweringContext } from "../lowering/loweringContext.js";
|
|
5
7
|
import type { Track } from "../lowering/track.js";
|
|
@@ -13,13 +15,14 @@ export declare class ASTNodeBase {
|
|
|
13
15
|
sourceSpan: SourceSpan;
|
|
14
16
|
parent: ASTNodeBase | null;
|
|
15
17
|
get children(): ASTNodeBase[] | null;
|
|
16
|
-
constructor(sourceSpan: SourceSpan,
|
|
18
|
+
constructor(sourceSpan: SourceSpan, // 源码区间
|
|
19
|
+
parent?: ASTNodeBase | null);
|
|
17
20
|
/**
|
|
18
21
|
* lowering 固化后的 attachment 生成钩子
|
|
19
22
|
*
|
|
20
23
|
* 普通 loweringEnter/loweringExit 在递归遍历 AST 时执行,
|
|
21
24
|
* 此时尚未完成锚点归并,事件的 t、track 和 layoutLine 不可用,因此需要观察完整事件流的功能不能在普通 hook 中可靠决定分组。
|
|
22
|
-
|
|
25
|
+
* 该 hook 读取 LoweringResult 并返回新建的 LoweringAttachment,LoweringContext 将返回值追加到当前文档或 fragment 的 attachments,不会把它们放入时间列或推进时间。
|
|
23
26
|
*
|
|
24
27
|
* 例:autobeam 扫描最终列,按轨道、谱面行、拍点和显式 beam 端点决定分组,再生成 BeamLayoutAttachment
|
|
25
28
|
*/
|
|
@@ -50,15 +53,20 @@ export declare class ASTNodeBase {
|
|
|
50
53
|
timeFlowModel(): TimeFlowModel | null;
|
|
51
54
|
/**
|
|
52
55
|
* 离开当前层级的回调 同 loweringEnter
|
|
56
|
+
*
|
|
57
|
+
* timeOffset 是 children 展开后的当前时间游标。
|
|
58
|
+
* 只有在同步重写本节点已经产生的 Temporal.t/T 时才可原地修改它,避免事件时间与后继事件起点不一致。
|
|
53
59
|
*/
|
|
54
|
-
loweringExit(_ctx: LoweringContext, _track: Track): Iterable<TemporalNodeBase>;
|
|
60
|
+
loweringExit(_ctx: LoweringContext, _track: Track, _timeOffset: Fraction): Iterable<TemporalNodeBase>;
|
|
55
61
|
toString(source: string): string;
|
|
56
62
|
}
|
|
57
63
|
export declare class ASTTextNode extends ASTNodeBase {
|
|
58
64
|
}
|
|
59
65
|
export declare class ASTLabelNode extends ASTNodeBase {
|
|
60
66
|
label: string;
|
|
61
|
-
|
|
67
|
+
/** 被标注的节点。parent 是树结构、会被容器改写,不能用来表达这层关系 */
|
|
68
|
+
readonly target: ASTNodeBase;
|
|
69
|
+
constructor(sourceSpan: SourceSpan, label: string, target: ASTNodeBase, parent?: ASTNodeBase | null);
|
|
62
70
|
toString(_: string): string;
|
|
63
71
|
}
|
|
64
72
|
export declare class ASTBraceNode extends ASTNodeBase {
|
|
@@ -89,12 +97,14 @@ export interface FunctionDef {
|
|
|
89
97
|
}
|
|
90
98
|
/** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
|
|
91
99
|
export declare function resolveArgType(def: FunctionDef, name: string | undefined, index: number): paramType | undefined;
|
|
92
|
-
|
|
100
|
+
/** 别名里的第一个是主名;`@set` 的 key 和 `getArgValue` 的前缀必须用同一个 */
|
|
101
|
+
export declare function primaryName(def: FunctionDef): string;
|
|
102
|
+
export type FunctionArgs = Map<string | number, paramValue | CallArgumentInfo>;
|
|
93
103
|
export declare class ASTFunctionNode extends ASTNodeBase {
|
|
94
104
|
/**
|
|
95
|
-
*
|
|
105
|
+
* 不实现 = 对标签透明,绑定继续向前找;返回 null = 边界,绑定到此为止;返回节点 = 标签承载者
|
|
96
106
|
*/
|
|
97
|
-
labelable(): ASTFunctionNode | null;
|
|
107
|
+
labelable?(): ASTFunctionNode | null;
|
|
98
108
|
label?: string;
|
|
99
109
|
static def?: FunctionDef;
|
|
100
110
|
get def(): FunctionDef | undefined;
|
|
@@ -105,6 +115,13 @@ export declare class ASTFunctionNode extends ASTNodeBase {
|
|
|
105
115
|
get deSugarRelation(): deSugarRelationFunction;
|
|
106
116
|
getArgValue(args: FunctionArgs, ctx: ParserContext): paramValue[];
|
|
107
117
|
toString(source: string): string;
|
|
118
|
+
/**
|
|
119
|
+
* 在子树里找标签承载者
|
|
120
|
+
* 未实现 labelable = 透明,继续下潜;实现了就采信它的返回值,null 即拒绝,不再往里找。
|
|
121
|
+
* 拒绝时别退回去遍历 children —— grace 的 labelable() 内部已经走过整棵子树
|
|
122
|
+
* 只有函数节点声明 labelable,别的节点读到的必是 undefined,所以无需 instanceof
|
|
123
|
+
*/
|
|
124
|
+
static findLabelable(node: ASTNodeBase): ASTFunctionNode | null;
|
|
108
125
|
/**
|
|
109
126
|
* 用于语法糖向前结合
|
|
110
127
|
* 一般向前结合都是寻找brace或者function
|