jpfun 0.1.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 +201 -0
- package/README.md +134 -0
- package/dist/diagnostic.d.ts +27 -0
- package/dist/diagnostic.js +141 -0
- package/dist/functions/ASTtypes.d.ts +115 -0
- package/dist/functions/ASTtypes.js +187 -0
- package/dist/functions/bar/index.d.ts +2 -0
- package/dist/functions/bar/index.js +180 -0
- package/dist/functions/beam/auto.d.ts +4 -0
- package/dist/functions/beam/auto.js +202 -0
- package/dist/functions/beam/index.d.ts +2 -0
- package/dist/functions/beam/index.js +54 -0
- package/dist/functions/beam/layout.d.ts +9 -0
- package/dist/functions/beam/layout.js +233 -0
- package/dist/functions/box/index.d.ts +2 -0
- package/dist/functions/box/index.js +202 -0
- package/dist/functions/br/index.d.ts +2 -0
- package/dist/functions/br/index.js +83 -0
- package/dist/functions/dash/index.d.ts +2 -0
- package/dist/functions/dash/index.js +61 -0
- package/dist/functions/default.d.ts +2 -0
- package/dist/functions/default.js +32 -0
- package/dist/functions/div/index.d.ts +50 -0
- package/dist/functions/div/index.js +190 -0
- package/dist/functions/dot/index.d.ts +2 -0
- package/dist/functions/dot/index.js +160 -0
- package/dist/functions/grace/index.d.ts +84 -0
- package/dist/functions/grace/index.js +464 -0
- package/dist/functions/key/index.d.ts +2 -0
- package/dist/functions/key/index.js +40 -0
- package/dist/functions/note/accidentals.d.ts +10 -0
- package/dist/functions/note/accidentals.js +60 -0
- package/dist/functions/note/index.d.ts +2 -0
- package/dist/functions/note/index.js +284 -0
- package/dist/functions/note/noteNameFSM.d.ts +8 -0
- package/dist/functions/note/noteNameFSM.js +192 -0
- package/dist/functions/over/index.d.ts +2 -0
- package/dist/functions/over/index.js +307 -0
- package/dist/functions/page/index.d.ts +20 -0
- package/dist/functions/page/index.js +63 -0
- package/dist/functions/set/index.d.ts +2 -0
- package/dist/functions/set/index.js +55 -0
- package/dist/functions/stack/index.d.ts +2 -0
- package/dist/functions/stack/index.js +164 -0
- package/dist/functions/tempo/index.d.ts +2 -0
- package/dist/functions/tempo/index.js +37 -0
- package/dist/functions/text/index.d.ts +2 -0
- package/dist/functions/text/index.js +65 -0
- package/dist/functions/tie/index.d.ts +2 -0
- package/dist/functions/tie/index.js +301 -0
- package/dist/functions/voice/index.d.ts +3 -0
- package/dist/functions/voice/index.js +714 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/layout/default.d.ts +12 -0
- package/dist/layout/default.js +27 -0
- package/dist/layout/engine.d.ts +47 -0
- package/dist/layout/engine.js +410 -0
- package/dist/layout/model.d.ts +73 -0
- package/dist/layout/model.js +422 -0
- package/dist/layout/page.d.ts +27 -0
- package/dist/layout/page.js +121 -0
- package/dist/layout/types.d.ts +197 -0
- package/dist/layout/types.js +1 -0
- package/dist/lowering/loweringContext.d.ts +126 -0
- package/dist/lowering/loweringContext.js +625 -0
- package/dist/lowering/track.d.ts +61 -0
- package/dist/lowering/track.js +35 -0
- package/dist/lowering/types.d.ts +176 -0
- package/dist/lowering/types.js +82 -0
- package/dist/parser/grammarType.d.ts +81 -0
- package/dist/parser/grammarType.js +1 -0
- package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
- package/dist/parser/parse-utils/brace-utils.js +29 -0
- package/dist/parser/parse-utils/call-utils.d.ts +24 -0
- package/dist/parser/parse-utils/call-utils.js +178 -0
- package/dist/parser/parse-utils/label-utils.d.ts +4 -0
- package/dist/parser/parse-utils/label-utils.js +17 -0
- package/dist/parser/parse-utils/length-utils.d.ts +3 -0
- package/dist/parser/parse-utils/length-utils.js +20 -0
- package/dist/parser/parse-utils/note-utils.d.ts +10 -0
- package/dist/parser/parse-utils/note-utils.js +111 -0
- package/dist/parser/parserContext.d.ts +93 -0
- package/dist/parser/parserContext.js +542 -0
- package/dist/parser/preprocess.d.ts +6 -0
- package/dist/parser/preprocess.js +193 -0
- package/dist/parser/types.d.ts +31 -0
- package/dist/parser/types.js +2 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.js +51 -0
- package/dist/render/canvas.d.ts +19 -0
- package/dist/render/canvas.js +121 -0
- package/dist/render/index.d.ts +5 -0
- package/dist/render/index.js +5 -0
- package/dist/render/recording.d.ts +46 -0
- package/dist/render/recording.js +76 -0
- package/dist/render/svg.d.ts +18 -0
- package/dist/render/svg.js +126 -0
- package/dist/render/text.d.ts +6 -0
- package/dist/render/text.js +17 -0
- package/dist/render/types.d.ts +86 -0
- package/dist/render/types.js +1 -0
- package/grammar.md +123 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
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
|
+
- Concise DSL with function-based extensibility
|
|
10
|
+
- Numbered notes, rests, chords, lyrics, voices, grace notes, ties, and beams
|
|
11
|
+
- Automatic spacing, line breaking, and 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
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { compileScore, renderLayoutToSvg } from "jpfun";
|
|
35
|
+
|
|
36
|
+
const source = `
|
|
37
|
+
@1(C4)
|
|
38
|
+
@tempo(96)
|
|
39
|
+
|
|
40
|
+
1/ 2/ 3 4 | 5 -
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
const result = compileScore(source);
|
|
44
|
+
const svg = renderLayoutToSvg(result.layout);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`renderLayoutToSvg` returns an SVG string that can be written to a file, sent in an HTTP response, or inserted into a browser page.
|
|
48
|
+
|
|
49
|
+
## Canvas Rendering
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { compileScore, renderLayoutToCanvas } from "jpfun";
|
|
53
|
+
|
|
54
|
+
const result = compileScore("1 2 3 | 4");
|
|
55
|
+
const canvas = document.querySelector("canvas");
|
|
56
|
+
const context = canvas.getContext("2d");
|
|
57
|
+
|
|
58
|
+
if (context) {
|
|
59
|
+
renderLayoutToCanvas(result.layout, context);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Compilation Pipeline
|
|
64
|
+
|
|
65
|
+
jpFun exposes the intermediate results of its compilation pipeline:
|
|
66
|
+
|
|
67
|
+
```text
|
|
68
|
+
Source
|
|
69
|
+
-> Parser -> AST
|
|
70
|
+
-> Lowering -> timed events and relations
|
|
71
|
+
-> Layout -> document geometry
|
|
72
|
+
-> Renderer -> SVG or Canvas
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const result = compileScore("1 2 3 | 4");
|
|
77
|
+
|
|
78
|
+
result.parser;
|
|
79
|
+
result.ast;
|
|
80
|
+
result.lowering;
|
|
81
|
+
result.layout;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This makes it possible to build editors, diagnostics, playback tools, and custom renderers on top of the same compilation result.
|
|
85
|
+
|
|
86
|
+
## Syntax Example
|
|
87
|
+
|
|
88
|
+
```jpfun
|
|
89
|
+
@page(width=860px, left=38px, right=38px)
|
|
90
|
+
@1(C4)
|
|
91
|
+
@tempo(96)
|
|
92
|
+
|
|
93
|
+
N(Melody): 1/ 1/ 5/ 5/ 6/ 6/ 5 -
|
|
94
|
+
N: @div(C4 C4) G4/G4/ A4/ A/ G
|
|
95
|
+
L(Lyrics): do do so so la la so
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
See the [language reference](grammar.md) for the complete syntax.
|
|
99
|
+
|
|
100
|
+
## Runtime Support
|
|
101
|
+
|
|
102
|
+
- ESM only
|
|
103
|
+
- Node.js 18 or later
|
|
104
|
+
- Modern browsers and bundlers
|
|
105
|
+
- No DOM dependency in the compiler
|
|
106
|
+
- No runtime dependencies
|
|
107
|
+
|
|
108
|
+
## Development
|
|
109
|
+
|
|
110
|
+
jpFun is maintained in a pnpm workspace. From the repository root:
|
|
111
|
+
|
|
112
|
+
```sh
|
|
113
|
+
pnpm install
|
|
114
|
+
pnpm run build:core
|
|
115
|
+
pnpm test
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The repository also includes a browser-based Playground with diagnostics and SVG/Canvas previews:
|
|
119
|
+
|
|
120
|
+
```sh
|
|
121
|
+
pnpm run dev
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Then open <http://127.0.0.1:4173>.
|
|
125
|
+
|
|
126
|
+
## Documentation
|
|
127
|
+
|
|
128
|
+
- [Language reference](grammar.md)
|
|
129
|
+
- [Repository](https://github.com/madderscientist/jpFun)
|
|
130
|
+
- [Issue tracker](https://github.com/madderscientist/jpFun/issues)
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
[Apache License 2.0](LICENSE)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SourceSpan } from "./parser/types.js";
|
|
2
|
+
export interface LineColRange {
|
|
3
|
+
startLine: number;
|
|
4
|
+
startColumn: number;
|
|
5
|
+
endLine: number;
|
|
6
|
+
endColumn: number;
|
|
7
|
+
}
|
|
8
|
+
export declare abstract class Diagnostic {
|
|
9
|
+
/** 诊断代码 */
|
|
10
|
+
code: string;
|
|
11
|
+
/** 诊断消息,描述问题的具体信息 */
|
|
12
|
+
message: string;
|
|
13
|
+
/** 诊断位置,指示问题在源代码中的位置 */
|
|
14
|
+
span: SourceSpan;
|
|
15
|
+
constructor(code: string, message: string, span: SourceSpan);
|
|
16
|
+
toLineCol(lineStarts: ArrayLike<number>): LineColRange;
|
|
17
|
+
static offsetToLineCol(offset: number, lineStarts: ArrayLike<number>): {
|
|
18
|
+
line: number;
|
|
19
|
+
column: number;
|
|
20
|
+
};
|
|
21
|
+
static error: any;
|
|
22
|
+
static warning: any;
|
|
23
|
+
}
|
|
24
|
+
export declare class ErrorDiagnostic extends Diagnostic {
|
|
25
|
+
}
|
|
26
|
+
export declare class WarningDiagnostic extends Diagnostic {
|
|
27
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
export class Diagnostic {
|
|
2
|
+
/** 诊断代码 */
|
|
3
|
+
code;
|
|
4
|
+
/** 诊断消息,描述问题的具体信息 */
|
|
5
|
+
message;
|
|
6
|
+
/** 诊断位置,指示问题在源代码中的位置 */
|
|
7
|
+
span;
|
|
8
|
+
constructor(code, message, span) {
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.message = message;
|
|
11
|
+
this.span = span;
|
|
12
|
+
}
|
|
13
|
+
toLineCol(lineStarts) {
|
|
14
|
+
const start = Diagnostic.offsetToLineCol(this.span.start, lineStarts);
|
|
15
|
+
const end = Diagnostic.offsetToLineCol(this.span.end, lineStarts);
|
|
16
|
+
return {
|
|
17
|
+
startLine: start.line,
|
|
18
|
+
startColumn: start.column,
|
|
19
|
+
endLine: end.line,
|
|
20
|
+
endColumn: end.column,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
static offsetToLineCol(offset, lineStarts) {
|
|
24
|
+
const count = lineStarts.length;
|
|
25
|
+
if (count === 0) {
|
|
26
|
+
return { line: 1, column: Math.max(0, offset) + 1 };
|
|
27
|
+
}
|
|
28
|
+
let target = offset;
|
|
29
|
+
if (target < 0)
|
|
30
|
+
target = 0;
|
|
31
|
+
let lo = 0;
|
|
32
|
+
let hi = count - 1;
|
|
33
|
+
while (lo <= hi) {
|
|
34
|
+
const mid = (lo + hi) >> 1;
|
|
35
|
+
if (lineStarts[mid] <= target)
|
|
36
|
+
lo = mid + 1;
|
|
37
|
+
else
|
|
38
|
+
hi = mid - 1;
|
|
39
|
+
}
|
|
40
|
+
const lineIndex = Math.max(0, hi);
|
|
41
|
+
const lineStart = lineStarts[lineIndex] ?? 0;
|
|
42
|
+
return {
|
|
43
|
+
line: lineIndex + 1,
|
|
44
|
+
column: target - lineStart + 1,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
static error;
|
|
48
|
+
static warning;
|
|
49
|
+
}
|
|
50
|
+
// 两个基类区分 Warning 和 Error
|
|
51
|
+
export class ErrorDiagnostic extends Diagnostic {
|
|
52
|
+
}
|
|
53
|
+
;
|
|
54
|
+
export class WarningDiagnostic extends Diagnostic {
|
|
55
|
+
}
|
|
56
|
+
;
|
|
57
|
+
Diagnostic.error = {
|
|
58
|
+
Bug: (message, span) => {
|
|
59
|
+
return new ErrorDiagnostic("E_BUG", `解析器错误: ${message},请联系开发者修复这个问题`, span);
|
|
60
|
+
},
|
|
61
|
+
MissingArg: (functionName, argumentName, span) => {
|
|
62
|
+
const argname = typeof argumentName === "number" ? `[${argumentName}]` : `"${argumentName}"`;
|
|
63
|
+
return new ErrorDiagnostic("E_MISSING_REQUIRED_ARG", `函数@${functionName} 缺少必需的参数 ${argname}`, span);
|
|
64
|
+
},
|
|
65
|
+
InvalidArgType: (functionName, argumentName, expectedType, actualType, span) => {
|
|
66
|
+
const argname = typeof argumentName === "number" ? `[${argumentName}]` : `"${argumentName}"`;
|
|
67
|
+
return new ErrorDiagnostic("E_WRONG_ARG_TYPE", `函数 @${functionName} 的参数 ${argname} 类型错误: 得到了 ${actualType},但期望 ${expectedType}`, span);
|
|
68
|
+
},
|
|
69
|
+
PosAfterNamedArg: (functionName, span) => {
|
|
70
|
+
return new ErrorDiagnostic("E_POSARG_AFTER_KWARG", `函数 @${functionName} 的位置参数出现在命名参数之后,这是不允许的`, span);
|
|
71
|
+
},
|
|
72
|
+
EmptyContent: (functionName, argumentName, span) => {
|
|
73
|
+
const argname = typeof argumentName === "number" ? `[${argumentName}]` : `"${argumentName}"`;
|
|
74
|
+
return new ErrorDiagnostic("E_EMPTY_CONTENT", `函数 @${functionName} 的参数 ${argname} 内容为空(或无可用内容);请提供有效内容`, span);
|
|
75
|
+
},
|
|
76
|
+
UnterminatedCall: (functionName, span) => {
|
|
77
|
+
return new ErrorDiagnostic("E_UNTERMINATED_CALL", `函数调用未闭合: @${functionName}(...)`, span);
|
|
78
|
+
},
|
|
79
|
+
UnterminatedString: (span) => {
|
|
80
|
+
return new ErrorDiagnostic("E_UNTERMINATED_String", `引号未闭合`, span);
|
|
81
|
+
},
|
|
82
|
+
UnknownFunction: (functionName, span) => {
|
|
83
|
+
return new ErrorDiagnostic("E_UNKNOWN_FUNCTION", `未知函数: @${functionName},请检查拼写`, span);
|
|
84
|
+
},
|
|
85
|
+
UnknownLabel: (labelName, span) => {
|
|
86
|
+
return new ErrorDiagnostic("E_UNKNOWN_LABEL", `未知标签: "${labelName}"`, span);
|
|
87
|
+
},
|
|
88
|
+
InvalidTonality: (tonality, span) => {
|
|
89
|
+
return new ErrorDiagnostic("E_INVALID_TONALITY", `无效的调性: "${tonality}",请提供有效的调性名称,必须 [一个大写字母][变音记号#bn只能一个][绝对八度数字],后二者可选`, span);
|
|
90
|
+
},
|
|
91
|
+
BreakInsideEvent: (span) => {
|
|
92
|
+
return new ErrorDiagnostic("E_BREAK_INSIDE_EVENT", `换行切断了一个仍在持续的事件;请把长音写成多个事件,或调整 @br 的位置`, span);
|
|
93
|
+
},
|
|
94
|
+
InvalidPageConfig: (message, span) => {
|
|
95
|
+
return new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", message, span);
|
|
96
|
+
},
|
|
97
|
+
PageOverflow: (required, available, span) => {
|
|
98
|
+
return new ErrorDiagnostic("E_PAGE_OVERFLOW", `谱面行需要 ${required}px 高度,但页面内容区只有 ${available}px`, span);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
Diagnostic.warning = {
|
|
102
|
+
UnknownFunction: (functionName, span) => {
|
|
103
|
+
return new WarningDiagnostic("W_UNKNOWN_FUNCTION", `未知函数: @${functionName},将被当作普通文本处理`, span);
|
|
104
|
+
},
|
|
105
|
+
UnknownNamedArg: (functionName, argumentName, span) => {
|
|
106
|
+
return new WarningDiagnostic("W_UNKNOWN_NAMED_ARG", `函数 @${functionName} 收到未知的命名参数 "${argumentName}"`, span);
|
|
107
|
+
},
|
|
108
|
+
TooManyPosArgs: (functionName, expected, actual, span) => {
|
|
109
|
+
return new WarningDiagnostic("W_TOO_MANY_POS_ARGS", `函数 @${functionName} 接收了过多的位置参数,期望 ${expected} 个但得到了 ${actual} 个`, span);
|
|
110
|
+
},
|
|
111
|
+
InvalidNumber: (value, span) => {
|
|
112
|
+
return new WarningDiagnostic("W_INVALID_NUMBER", `无效的数字: "${value}";将使用默认值(若存在)`, span);
|
|
113
|
+
},
|
|
114
|
+
InvalidBoolean: (value, span) => {
|
|
115
|
+
return new WarningDiagnostic("W_INVALID_BOOL", `无效的布尔值: "${value}": 应为 'true' 或 'false';将使用默认值(若存在)`, span);
|
|
116
|
+
},
|
|
117
|
+
InvalidLength: (value, span) => {
|
|
118
|
+
return new WarningDiagnostic("W_INVALID_LENGTH", `无效的长度值: "${value}";请提供有效的长度值,例如 "10px" 或 "2em";将使用默认值(若存在)`, span);
|
|
119
|
+
},
|
|
120
|
+
InvalidLengthUnit: (unit, span) => {
|
|
121
|
+
return new WarningDiagnostic("W_INVALID_LENGTH_UNIT", `无效的长度单位: "${unit}";仅支持 "px" 和 "em";请检查函数参数或变量定义;将使用默认值(若存在)`, span);
|
|
122
|
+
},
|
|
123
|
+
InvalidContent: (span) => {
|
|
124
|
+
return new WarningDiagnostic("W_INVALID_CONTENT", `内容解析失败,错误原因见上;将使用默认值(但往往不存在,极有可能导致错误)`, span);
|
|
125
|
+
},
|
|
126
|
+
EmptyContent: (functionName, argumentName, span) => {
|
|
127
|
+
return new WarningDiagnostic("W_EMPTY_CONTENT", `函数 @${functionName} 的参数 "${argumentName}" 内容为空(或无可用内容)`, span);
|
|
128
|
+
},
|
|
129
|
+
UnmatchedBrace: (span) => {
|
|
130
|
+
return new WarningDiagnostic("W_UNMATCHED_BRACE", `未匹配的大括号: 缺少右大括号'}';将尝试自动修复`, span);
|
|
131
|
+
},
|
|
132
|
+
LabelWithoutTarget: (labelName, span) => {
|
|
133
|
+
return new WarningDiagnostic("W_LABEL_WITHOUT_TARGET", `标签 "${labelName}" 没有可绑定的目标;标签将被忽略`, span);
|
|
134
|
+
},
|
|
135
|
+
LabelAlreadyExists: (newLabel, oldLabel, span) => {
|
|
136
|
+
return new WarningDiagnostic("W_LABEL_ALREADY_EXISTS", `标签 "${newLabel}" 已经存在(为 "${oldLabel}");新标签将覆盖旧标签`, span);
|
|
137
|
+
},
|
|
138
|
+
ZeroTimeTrack: (span, trackOrder) => {
|
|
139
|
+
return new WarningDiagnostic("W_ZERO_TIME_TRACK", `第${trackOrder}个子轨道的时长为0`, span);
|
|
140
|
+
},
|
|
141
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { SourceSpan, LengthValue, deSugarAtomFunction, deSugarRelationFunction } from "../parser/types.js";
|
|
2
|
+
import type { ParserContext } from "../parser/parserContext.js";
|
|
3
|
+
import type { LoweringAugmenter, LoweringFinalizer, TemporalNodeBase, TimeFlowModel } from "../lowering/types.js";
|
|
4
|
+
import type { LoweringContext } from "../lowering/loweringContext.js";
|
|
5
|
+
import type { Track } from "../lowering/track.js";
|
|
6
|
+
import type { LayoutDecorationHandler } from "../layout/types.js";
|
|
7
|
+
export type { SourceSpan, ParserContext, LengthValue };
|
|
8
|
+
export type paramType = "string" | "number" | "boolean" | "length" | "content" | "label";
|
|
9
|
+
export type paramValue = string | number | boolean | LengthValue | ASTBraceNode | ASTNodeBase;
|
|
10
|
+
/** 固化到 Temporal.addon 的函数语义统一使用 `@主函数名` */
|
|
11
|
+
export declare function functionAddonKey<const Name extends string>(name: Name): `@${Name}`;
|
|
12
|
+
export declare class ASTNodeBase {
|
|
13
|
+
sourceSpan: SourceSpan;
|
|
14
|
+
parent: ASTNodeBase | null;
|
|
15
|
+
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;
|
|
38
|
+
/**
|
|
39
|
+
* 进入当前层级的回调
|
|
40
|
+
* @param ctx 当前的 lowering 上下文
|
|
41
|
+
* @param track 当前事件所在的纵向音轨
|
|
42
|
+
* @returns 当前节点产生的事件
|
|
43
|
+
*/
|
|
44
|
+
loweringEnter(_ctx: LoweringContext, _track: Track): Iterable<TemporalNodeBase>;
|
|
45
|
+
/**
|
|
46
|
+
* 时间求解模式
|
|
47
|
+
* 由 loweringContext 调用,决定当前节点在时间求解阶段的展开方式
|
|
48
|
+
* @returns 返回 null 表示没有子元素,返回对象表示参与时间求解
|
|
49
|
+
*/
|
|
50
|
+
timeFlowModel(): TimeFlowModel | null;
|
|
51
|
+
/**
|
|
52
|
+
* 离开当前层级的回调 同 loweringEnter
|
|
53
|
+
*/
|
|
54
|
+
loweringExit(_ctx: LoweringContext, _track: Track): Iterable<TemporalNodeBase>;
|
|
55
|
+
toString(source: string): string;
|
|
56
|
+
}
|
|
57
|
+
export declare class ASTTextNode extends ASTNodeBase {
|
|
58
|
+
}
|
|
59
|
+
export declare class ASTLabelNode extends ASTNodeBase {
|
|
60
|
+
label: string;
|
|
61
|
+
constructor(sourceSpan: SourceSpan, label: string, parent: ASTNodeBase);
|
|
62
|
+
toString(_: string): string;
|
|
63
|
+
}
|
|
64
|
+
export declare class ASTBraceNode extends ASTNodeBase {
|
|
65
|
+
content: ASTNodeBase[];
|
|
66
|
+
get children(): ASTNodeBase[];
|
|
67
|
+
constructor(sourceSpan: SourceSpan, content: ASTNodeBase[], parent?: ASTNodeBase | null);
|
|
68
|
+
innerSpan(): SourceSpan;
|
|
69
|
+
static getContentSpan(content: ASTNodeBase[] | ASTNodeBase): SourceSpan;
|
|
70
|
+
timeFlowModel(): {
|
|
71
|
+
children: ASTNodeBase[];
|
|
72
|
+
mode: "sequence";
|
|
73
|
+
};
|
|
74
|
+
toString(source: string): string;
|
|
75
|
+
}
|
|
76
|
+
export interface FunctionArgDef {
|
|
77
|
+
name?: string;
|
|
78
|
+
type: paramType;
|
|
79
|
+
/** 参数默认值 null表示必填 否则报错 */
|
|
80
|
+
default: paramValue | null;
|
|
81
|
+
}
|
|
82
|
+
export interface FunctionDef {
|
|
83
|
+
name: string | string[];
|
|
84
|
+
description: string;
|
|
85
|
+
example: string;
|
|
86
|
+
allowExtraArgs: boolean;
|
|
87
|
+
extraArgType?: paramType;
|
|
88
|
+
args: FunctionArgDef[];
|
|
89
|
+
}
|
|
90
|
+
/** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
|
|
91
|
+
export declare function resolveArgType(def: FunctionDef, name: string | undefined, index: number): paramType | undefined;
|
|
92
|
+
export type FunctionArgs = Map<string | number, paramValue | SourceSpan>;
|
|
93
|
+
export declare class ASTFunctionNode extends ASTNodeBase {
|
|
94
|
+
/**
|
|
95
|
+
* 返回真正承载标签的 AST 节点,null 表示不可被标注
|
|
96
|
+
*/
|
|
97
|
+
labelable(): ASTFunctionNode | null;
|
|
98
|
+
label?: string;
|
|
99
|
+
static def?: FunctionDef;
|
|
100
|
+
get def(): FunctionDef | undefined;
|
|
101
|
+
get callName(): string;
|
|
102
|
+
static deSugarAtom: deSugarAtomFunction;
|
|
103
|
+
get deSugarAtom(): deSugarAtomFunction;
|
|
104
|
+
static deSugarRelation: deSugarRelationFunction;
|
|
105
|
+
get deSugarRelation(): deSugarRelationFunction;
|
|
106
|
+
getArgValue(args: FunctionArgs, ctx: ParserContext): paramValue[];
|
|
107
|
+
toString(source: string): string;
|
|
108
|
+
/**
|
|
109
|
+
* 用于语法糖向前结合
|
|
110
|
+
* 一般向前结合都是寻找brace或者function
|
|
111
|
+
* 文本节点会打断连接 注意解析时已经跳过了空格 因此允许语法糖之间有空格
|
|
112
|
+
*/
|
|
113
|
+
static findLastFuncContentNode(nodes: ASTNodeBase[], i: number): ASTNodeBase | null;
|
|
114
|
+
}
|
|
115
|
+
export type ASTFunctionClass = new (sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent: ASTNodeBase | null) => ASTFunctionNode;
|