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
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { parseNoteName } from "./noteNameFSM.js";
|
|
4
|
+
import { prepareAccidental } from "./accidentals.js";
|
|
5
|
+
class NoteFunction extends ASTFunctionNode {
|
|
6
|
+
static def = {
|
|
7
|
+
name: ["note", "n"],
|
|
8
|
+
description: "创建音符",
|
|
9
|
+
example: `@note(name, acc, octave, color)
|
|
10
|
+
参数说明:
|
|
11
|
+
- name: [必填]音符名,可以是大写字母或者数字,不允许小写字母(会和降号冲突)。此参数写法有语法糖,见下。
|
|
12
|
+
其中 0 是休止符,8 是隐形占位,9 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
|
|
13
|
+
- acc: [可选]的额外升降号字符串,例如 "##" 表示再升两个半音,"b" 表示再降一个半音。
|
|
14
|
+
- octave: [可选]八度,类型为数字。如果 name 是字母,则此项代表绝对八度;如果是数字,则此项代表相对八度。
|
|
15
|
+
|
|
16
|
+
语法糖:[音名][升降号][八度] 音名和升降号可以交换
|
|
17
|
+
例:A3# === @note(A3#) === @note(A, #, 3)
|
|
18
|
+
也支持 A99##bn 的写法。可以设置 note.octave 改变默认的绝对八度
|
|
19
|
+
|
|
20
|
+
支持 数字音名, 此时 octave 为相对八度
|
|
21
|
+
支持使用数字音名时使用相对八度,如 "1,," 代表在当前基准音(由上下文属性"1=?"决定)的基础上降低两倍八度,"1'" 代表在当前基准音的基础上提高一个八度
|
|
22
|
+
例:1#' === @note(1#') === @note(1, #, 1)。此时支持升降号写音名前面,如 #1'
|
|
23
|
+
`,
|
|
24
|
+
allowExtraArgs: false,
|
|
25
|
+
args: [
|
|
26
|
+
{
|
|
27
|
+
name: "name",
|
|
28
|
+
type: "string",
|
|
29
|
+
default: null,
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "acc",
|
|
33
|
+
type: "string",
|
|
34
|
+
default: "",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "octave",
|
|
38
|
+
type: "number",
|
|
39
|
+
default: 4, // 如果是数字则默认值为0 需要代码中区分
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "color",
|
|
43
|
+
type: "string",
|
|
44
|
+
default: "#000",
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
};
|
|
48
|
+
static deSugarAtom(source, start, end) {
|
|
49
|
+
const parseResult = parseNoteName(source, start, end);
|
|
50
|
+
if (parseResult instanceof Diagnostic)
|
|
51
|
+
return null;
|
|
52
|
+
const argMap = new Map();
|
|
53
|
+
argMap.set("name", parseResult.name);
|
|
54
|
+
if (parseResult.octave)
|
|
55
|
+
argMap.set("octave", parseResult.octave);
|
|
56
|
+
if (parseResult.acc)
|
|
57
|
+
argMap.set("acc", parseResult.acc);
|
|
58
|
+
const node = {
|
|
59
|
+
kind: "call",
|
|
60
|
+
typed: true,
|
|
61
|
+
name: "note",
|
|
62
|
+
args: argMap,
|
|
63
|
+
span: { start, end: parseResult.next },
|
|
64
|
+
syntaxKind: "atom",
|
|
65
|
+
};
|
|
66
|
+
return { next: parseResult.next, node };
|
|
67
|
+
}
|
|
68
|
+
labelable() { return this; }
|
|
69
|
+
loweringEnter() {
|
|
70
|
+
return [new NoteTemporalNode(this)];
|
|
71
|
+
}
|
|
72
|
+
// 原始输入
|
|
73
|
+
name;
|
|
74
|
+
octave;
|
|
75
|
+
acc;
|
|
76
|
+
color;
|
|
77
|
+
size;
|
|
78
|
+
constructor(sourceSpan, args, ctx, parent = null) {
|
|
79
|
+
super(sourceSpan, parent);
|
|
80
|
+
[this.name, this.acc, this.octave, this.color] = this.getArgValue(args, ctx);
|
|
81
|
+
this.size = ctx.fontSize;
|
|
82
|
+
// 创建时就固化参数值
|
|
83
|
+
// 校验 note name
|
|
84
|
+
const parseResult = parseNoteName(this.name);
|
|
85
|
+
if (parseResult instanceof Diagnostic) {
|
|
86
|
+
parseResult.span = sourceSpan; // 定位到整个函数调用
|
|
87
|
+
throw parseResult;
|
|
88
|
+
}
|
|
89
|
+
this.name = parseResult.name;
|
|
90
|
+
// 校验octave
|
|
91
|
+
const inputOctave = args.get("octave") ?? args.get(2);
|
|
92
|
+
if (inputOctave !== void 0) {
|
|
93
|
+
if (parseResult.octave !== null && parseResult.octave != inputOctave)
|
|
94
|
+
throw new ErrorDiagnostic("E_NOTE_OCTAVE_CONFLICT", `函数 @note 的参数 "octave" 与音符名称中的八度信息冲突: ${inputOctave} != ${parseResult.octave}`, sourceSpan);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// 没有传入octave 此时this.octave是字母模式下的默认值 仅在数字模式下才需要被覆盖
|
|
98
|
+
if (!parseResult.absOctave)
|
|
99
|
+
this.octave = parseResult.octave ?? 0;
|
|
100
|
+
}
|
|
101
|
+
// 补充acc
|
|
102
|
+
if (parseResult.acc !== null)
|
|
103
|
+
this.acc = parseResult.acc + this.acc;
|
|
104
|
+
}
|
|
105
|
+
toString() {
|
|
106
|
+
return `@n(${this.name}, ${this.acc}, ${this.octave}, ${this.color})`;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export const NoteNode = NoteFunction;
|
|
110
|
+
import { TemporalNodeBase } from "../../lowering/types.js";
|
|
111
|
+
import { resolveLetterNameToJianpu, resolveNoteMidi } from "../../parser/parse-utils/note-utils.js";
|
|
112
|
+
// 8 是隐形占位,9 是只打拍不发音的节拍记号,空串表示不绘制
|
|
113
|
+
const NOTE_GLYPH = { "8": "", "9": "X" };
|
|
114
|
+
function createNumberStyle(ast) {
|
|
115
|
+
return {
|
|
116
|
+
fontSize: ast.size,
|
|
117
|
+
fontFamily: '"Cascadia Mono", Consolas, "Liberation Mono", "Noto Sans Mono", monospace',
|
|
118
|
+
fontWeight: 400,
|
|
119
|
+
textAlign: "center",
|
|
120
|
+
fill: ast.color,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
class NoteTemporalNode extends TemporalNodeBase {
|
|
124
|
+
// 时间固化后的参数
|
|
125
|
+
activeBpm = null; // 用于播放的时候的速度
|
|
126
|
+
resolvedMidi = null; // MIDI音高 name是数字则需要在onTimeState中基于当前调性偏移 播放的音高
|
|
127
|
+
name; // 数字
|
|
128
|
+
acc; // 升降号
|
|
129
|
+
octave; // 绝对值为点的个数
|
|
130
|
+
/** prepareLayout 生成的局部升降号位置 */
|
|
131
|
+
accidentals = [];
|
|
132
|
+
/** 上八度点属于主体,下八度点通过通用装饰流排在其他下方符号之后 */
|
|
133
|
+
upperOctaveDotY = [];
|
|
134
|
+
lowerOctaveDotY = [];
|
|
135
|
+
octaveDotRadius = 0;
|
|
136
|
+
numberY = 0;
|
|
137
|
+
constructor(ast) {
|
|
138
|
+
super();
|
|
139
|
+
this.ast = ast;
|
|
140
|
+
this.T = 1;
|
|
141
|
+
this.t = 0;
|
|
142
|
+
this.type = 2 /* ColType.DEFAULT */;
|
|
143
|
+
this.name = ast.name;
|
|
144
|
+
this.acc = ast.acc;
|
|
145
|
+
this.octave = ast.octave;
|
|
146
|
+
this.initLayoutBox();
|
|
147
|
+
}
|
|
148
|
+
prepareLayout(context) {
|
|
149
|
+
this.accidentals.length = 0;
|
|
150
|
+
this.upperOctaveDotY.length = 0;
|
|
151
|
+
this.lowerOctaveDotY.length = 0;
|
|
152
|
+
const { size, color } = this.ast;
|
|
153
|
+
const numberStyle = createNumberStyle(this.ast);
|
|
154
|
+
const mainMetrics = context.textMeasurer.measureText("0", numberStyle);
|
|
155
|
+
// 升降号贴近数字左上角,但不参与数字中心 anchor 的计算
|
|
156
|
+
// 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
|
|
157
|
+
const accidentalGap = size * 0.01;
|
|
158
|
+
const accidentalRaise = size * 0.42;
|
|
159
|
+
const accidentalShapes = [];
|
|
160
|
+
// 把每个升降字符转换为 note 私有的局部路径
|
|
161
|
+
for (const accidental of this.acc) {
|
|
162
|
+
const shape = prepareAccidental(accidental, size);
|
|
163
|
+
if (shape)
|
|
164
|
+
accidentalShapes.push(shape);
|
|
165
|
+
}
|
|
166
|
+
let accidentalWidth = 0;
|
|
167
|
+
// 汇总升降号区域宽度,为数字单元保留固定起点
|
|
168
|
+
for (const shape of accidentalShapes) {
|
|
169
|
+
if (accidentalWidth > 0)
|
|
170
|
+
accidentalWidth += accidentalGap;
|
|
171
|
+
accidentalWidth += shape.w;
|
|
172
|
+
}
|
|
173
|
+
const dotCount = Math.abs(this.octave);
|
|
174
|
+
this.octaveDotRadius = size * 0.065;
|
|
175
|
+
const dotGap = size * 0.08;
|
|
176
|
+
const dotStep = this.octaveDotRadius * 2 + dotGap;
|
|
177
|
+
const octaveSpace = dotCount === 0
|
|
178
|
+
? 0
|
|
179
|
+
: dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap + dotGap;
|
|
180
|
+
const topSpace = this.octave > 0 ? octaveSpace : 0;
|
|
181
|
+
const mainX = accidentalWidth + (accidentalShapes.length > 0 ? accidentalGap : 0);
|
|
182
|
+
const mainY = topSpace;
|
|
183
|
+
this.box.w = mainX + mainMetrics.w;
|
|
184
|
+
this.box.h = topSpace + mainMetrics.h;
|
|
185
|
+
this.box.anchor = mainX + mainMetrics.w / 2;
|
|
186
|
+
this.box.visualAxis = mainY + mainMetrics.h / 2;
|
|
187
|
+
this.numberY = mainY + mainMetrics.baseline;
|
|
188
|
+
// 升降号和附点参与完整盒布局,但不计入数字主体范围,减时线因此只画在数字下
|
|
189
|
+
this.ports["body.left"] = { x: this.box.anchor - mainMetrics.w / 2, y: this.box.visualAxis };
|
|
190
|
+
this.ports["body.right"] = { x: this.box.anchor + mainMetrics.w / 2, y: this.box.visualAxis };
|
|
191
|
+
// 覆盖 dot 的默认“右边界 + 视觉轴”,让附点贴合数字字形的视觉位置
|
|
192
|
+
this.ports["dot"] = {
|
|
193
|
+
x: this.box.w,
|
|
194
|
+
y: mainY + mainMetrics.h * 0.64,
|
|
195
|
+
};
|
|
196
|
+
// 升降号位于完整盒的左侧区域,但不会进入核心有效范围
|
|
197
|
+
let accidentalX = 0;
|
|
198
|
+
// 所有升降号按同一数字 baseline 排列在数字左侧
|
|
199
|
+
for (const shape of accidentalShapes) {
|
|
200
|
+
this.accidentals.push({
|
|
201
|
+
shape,
|
|
202
|
+
x: accidentalX,
|
|
203
|
+
y: this.numberY - shape.baseline - accidentalRaise,
|
|
204
|
+
});
|
|
205
|
+
accidentalX += shape.w + accidentalGap;
|
|
206
|
+
}
|
|
207
|
+
if (this.octave > 0) {
|
|
208
|
+
// 上八度点从靠近数字的位置向上依次排列
|
|
209
|
+
for (let i = 0; i < dotCount; i++) {
|
|
210
|
+
this.upperOctaveDotY.push(topSpace - dotGap - this.octaveDotRadius - i * dotStep);
|
|
211
|
+
}
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (this.octave >= 0)
|
|
215
|
+
return;
|
|
216
|
+
const lowerDotHeight = dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap;
|
|
217
|
+
const lowerDots = {
|
|
218
|
+
below: {
|
|
219
|
+
// order 只表达通用的由近到远顺序
|
|
220
|
+
// 时值类装饰可以选择较小 order,下八度点选择较大 order
|
|
221
|
+
order: 100,
|
|
222
|
+
gap: dotGap,
|
|
223
|
+
height: lowerDotHeight,
|
|
224
|
+
place: y => {
|
|
225
|
+
// 下八度点从引擎分配的外层区域顶部向下排列
|
|
226
|
+
for (let i = 0; i < dotCount; i++) {
|
|
227
|
+
this.lowerOctaveDotY.push(y + this.octaveDotRadius + i * dotStep);
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
paint: painter => {
|
|
232
|
+
// 下八度点读取已经冻结的局部圆心位置
|
|
233
|
+
for (const y of this.lowerOctaveDotY) {
|
|
234
|
+
painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, this.octaveDotRadius, { fill: color });
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
this.decorations.push(lowerDots);
|
|
239
|
+
}
|
|
240
|
+
finalizeLayout() {
|
|
241
|
+
this.ports["lyric"] = { x: this.box.anchor, y: this.box.h };
|
|
242
|
+
}
|
|
243
|
+
paint(painter) {
|
|
244
|
+
const color = this.ast.color;
|
|
245
|
+
const numberStyle = createNumberStyle(this.ast);
|
|
246
|
+
for (const accidental of this.accidentals) {
|
|
247
|
+
painter.drawPath(accidental.shape.commands, { stroke: color, strokeWidth: accidental.shape.strokeWidth }, {
|
|
248
|
+
x: this.box.x + accidental.x,
|
|
249
|
+
y: this.box.y + accidental.y,
|
|
250
|
+
scaleX: accidental.shape.w,
|
|
251
|
+
scaleY: accidental.shape.h,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
const glyph = NOTE_GLYPH[this.name] ?? this.name;
|
|
255
|
+
if (glyph) {
|
|
256
|
+
painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, numberStyle);
|
|
257
|
+
}
|
|
258
|
+
// 上八度点属于主体绘制,不参与下方装饰流
|
|
259
|
+
for (const y of this.upperOctaveDotY) {
|
|
260
|
+
painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, this.octaveDotRadius, { fill: color });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
onTimeState(state) {
|
|
264
|
+
const keySignature = typeof state.keySignature === "string" ? state.keySignature : "C4";
|
|
265
|
+
this.activeBpm = Number(state.bpm) || 120;
|
|
266
|
+
this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
|
|
267
|
+
// 数字音名本身就是简谱显示形式,直接保留原始数字和相对八度
|
|
268
|
+
if (this.name >= "0" && this.name <= "9") {
|
|
269
|
+
if (this.name === "0" || this.name >= "8") {
|
|
270
|
+
this.octave = 0;
|
|
271
|
+
this.acc = ""; // 0和8、9不显示升降号
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
// 字母音名需要在当前调性下转换成简谱级数
|
|
276
|
+
const jianpuPitch = resolveLetterNameToJianpu(this.name, this.acc, this.octave, keySignature);
|
|
277
|
+
if (jianpuPitch) {
|
|
278
|
+
this.name = jianpuPitch.renderName;
|
|
279
|
+
this.acc = jianpuPitch.renderAcc;
|
|
280
|
+
this.octave = jianpuPitch.renderOctave;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
const LETTER_NOTE_START_RE = /^[A-G]/;
|
|
3
|
+
const NUM_NOTE_START_RE = /^[0-9]/;
|
|
4
|
+
const SHARP_RE = /^[#bn]+/;
|
|
5
|
+
const ABS_OCTAVE_RE = /^[+-]?\d+/;
|
|
6
|
+
const RELATIVE_OCTAVE_RE = /^[,']+/;
|
|
7
|
+
export function parseNoteName(str, start = 0, end = str.length) {
|
|
8
|
+
if ((end - start) === 0)
|
|
9
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 不能为空`, { start, end });
|
|
10
|
+
// 用状态机解决 详细信息查看FSM.drawio
|
|
11
|
+
let state = 0;
|
|
12
|
+
let name = null, acc = null, octave = null, absOctave = false;
|
|
13
|
+
let match;
|
|
14
|
+
for (let pos = start; pos < end; pos++) {
|
|
15
|
+
const ch = str[pos];
|
|
16
|
+
switch (state) {
|
|
17
|
+
case 0:
|
|
18
|
+
if (LETTER_NOTE_START_RE.test(ch)) {
|
|
19
|
+
name = ch;
|
|
20
|
+
absOctave = true;
|
|
21
|
+
state = 1;
|
|
22
|
+
}
|
|
23
|
+
else if (NUM_NOTE_START_RE.test(ch)) {
|
|
24
|
+
name = ch;
|
|
25
|
+
absOctave = false;
|
|
26
|
+
state = 4;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const match = str.slice(pos, end).match(SHARP_RE);
|
|
30
|
+
if (match) {
|
|
31
|
+
acc = match[0];
|
|
32
|
+
pos += acc.length - 1;
|
|
33
|
+
absOctave = false;
|
|
34
|
+
state = 7;
|
|
35
|
+
}
|
|
36
|
+
else
|
|
37
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 首字符 "${ch}" 不合法,期望 A-G (a-g) 或 0-9 开头,或在开头直接使用升降号(#/b/n)`, { start: pos, end: pos + 1 });
|
|
38
|
+
}
|
|
39
|
+
break;
|
|
40
|
+
case 1:
|
|
41
|
+
const subs = str.slice(pos, end);
|
|
42
|
+
match = subs.match(ABS_OCTAVE_RE);
|
|
43
|
+
if (match) {
|
|
44
|
+
octave = parseInt(match[0], 10);
|
|
45
|
+
pos += match[0].length - 1;
|
|
46
|
+
state = 2;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
match = subs.match(SHARP_RE);
|
|
50
|
+
if (match) {
|
|
51
|
+
acc = match[0];
|
|
52
|
+
pos += acc.length - 1;
|
|
53
|
+
state = 3;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
name: name,
|
|
58
|
+
next: pos,
|
|
59
|
+
acc: null,
|
|
60
|
+
octave: null,
|
|
61
|
+
absOctave,
|
|
62
|
+
};
|
|
63
|
+
case 2:
|
|
64
|
+
match = str.slice(pos, end).match(SHARP_RE);
|
|
65
|
+
if (match) {
|
|
66
|
+
acc = match[0];
|
|
67
|
+
return {
|
|
68
|
+
name: name,
|
|
69
|
+
next: pos + acc.length,
|
|
70
|
+
acc,
|
|
71
|
+
octave,
|
|
72
|
+
absOctave,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
name: name,
|
|
77
|
+
next: pos,
|
|
78
|
+
acc: null,
|
|
79
|
+
octave: octave,
|
|
80
|
+
absOctave,
|
|
81
|
+
};
|
|
82
|
+
case 3:
|
|
83
|
+
match = str.slice(pos, end).match(ABS_OCTAVE_RE);
|
|
84
|
+
if (match) {
|
|
85
|
+
octave = parseInt(match[0], 10);
|
|
86
|
+
return {
|
|
87
|
+
name: name,
|
|
88
|
+
next: pos + match[0].length,
|
|
89
|
+
acc,
|
|
90
|
+
octave,
|
|
91
|
+
absOctave,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
name: name,
|
|
96
|
+
next: pos,
|
|
97
|
+
acc: acc,
|
|
98
|
+
octave: null,
|
|
99
|
+
absOctave,
|
|
100
|
+
};
|
|
101
|
+
case 4:
|
|
102
|
+
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
103
|
+
if (match) {
|
|
104
|
+
const octave_str = match[0];
|
|
105
|
+
octave = relativeOctaveFromString(octave_str);
|
|
106
|
+
pos += octave_str.length - 1;
|
|
107
|
+
state = 5;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
match = str.slice(pos, end).match(SHARP_RE);
|
|
111
|
+
if (match) {
|
|
112
|
+
acc = match[0];
|
|
113
|
+
pos += acc.length - 1;
|
|
114
|
+
state = 6;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
name: name,
|
|
119
|
+
next: pos,
|
|
120
|
+
acc: null,
|
|
121
|
+
octave: octave,
|
|
122
|
+
absOctave,
|
|
123
|
+
};
|
|
124
|
+
case 5:
|
|
125
|
+
match = str.slice(pos, end).match(SHARP_RE);
|
|
126
|
+
if (match) {
|
|
127
|
+
acc = match[0];
|
|
128
|
+
return {
|
|
129
|
+
name: name,
|
|
130
|
+
next: pos + acc.length,
|
|
131
|
+
acc,
|
|
132
|
+
octave: octave,
|
|
133
|
+
absOctave,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
name: name,
|
|
138
|
+
next: pos,
|
|
139
|
+
acc: null,
|
|
140
|
+
octave: octave,
|
|
141
|
+
absOctave,
|
|
142
|
+
};
|
|
143
|
+
case 6:
|
|
144
|
+
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
145
|
+
if (match) {
|
|
146
|
+
const octave_str = match[0];
|
|
147
|
+
octave = relativeOctaveFromString(octave_str);
|
|
148
|
+
return {
|
|
149
|
+
name: name,
|
|
150
|
+
next: pos + octave_str.length,
|
|
151
|
+
acc: acc,
|
|
152
|
+
octave: octave,
|
|
153
|
+
absOctave,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
name: name,
|
|
158
|
+
next: pos,
|
|
159
|
+
acc: acc,
|
|
160
|
+
octave: octave,
|
|
161
|
+
absOctave,
|
|
162
|
+
};
|
|
163
|
+
case 7:
|
|
164
|
+
if (NUM_NOTE_START_RE.test(ch)) {
|
|
165
|
+
name = ch;
|
|
166
|
+
state = 6;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
else
|
|
170
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "${ch}"`, { start: pos, end: pos + 1 });
|
|
171
|
+
default:
|
|
172
|
+
return Diagnostic.error.Bug("音符名称解析状态机进入了未定义的状态", { start: pos, end });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
name: name,
|
|
177
|
+
next: end,
|
|
178
|
+
acc,
|
|
179
|
+
octave,
|
|
180
|
+
absOctave
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function relativeOctaveFromString(octaveStr) {
|
|
184
|
+
let octave = 0;
|
|
185
|
+
for (let i = 0; i < octaveStr.length; i++) {
|
|
186
|
+
if (octaveStr[i] === ',')
|
|
187
|
+
octave--;
|
|
188
|
+
else if (octaveStr[i] === "'")
|
|
189
|
+
octave++;
|
|
190
|
+
}
|
|
191
|
+
return octave;
|
|
192
|
+
}
|