jpfun 0.2.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/README.md +69 -11
- 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 +39 -10
- package/dist/functions/beam/auto.js +32 -15
- 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 +9 -7
- package/dist/functions/dash/index.js +33 -1
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +11 -3
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +82 -76
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +97 -3
- 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 +45 -65
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +37 -29
- package/dist/functions/stack/index.js +26 -22
- 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 -2
- package/dist/functions/text/index.js +76 -24
- package/dist/functions/tie/index.js +102 -127
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +245 -171
- package/dist/functions/voice/index.js +86 -68
- 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 +65 -59
- 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 +18 -20
- package/dist/lowering/loweringContext.js +132 -94
- package/dist/lowering/track.d.ts +1 -1
- package/dist/lowering/track.js +1 -1
- package/dist/lowering/types.d.ts +49 -12
- package/dist/lowering/types.js +9 -3
- 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 +93 -10
- package/package.json +10 -6
|
@@ -170,9 +170,3 @@ export function findRightParen(source, start, end) {
|
|
|
170
170
|
}
|
|
171
171
|
return -1;
|
|
172
172
|
}
|
|
173
|
-
/** 识别字符串参数时用于剥离双引号;单引号不是字符串语法 */
|
|
174
|
-
export function removeQuote(source) {
|
|
175
|
-
if (source.startsWith('"') && source.endsWith('"'))
|
|
176
|
-
return source.slice(1, -1);
|
|
177
|
-
return source;
|
|
178
|
-
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
|
|
1
2
|
export declare const NoteNameMap: Record<string, number>;
|
|
2
3
|
export interface RelativeJianpuPitch {
|
|
3
4
|
renderName: string;
|
|
@@ -7,4 +8,11 @@ export interface RelativeJianpuPitch {
|
|
|
7
8
|
export declare function tonality2Midi(tonality: string, defaultOctave: number): number;
|
|
8
9
|
export declare function acc2Offset(acc: string, ignoreNatural?: boolean, initOffset?: number): number;
|
|
9
10
|
export declare function resolveNoteMidi(name: string, acc: string, octave: number, keySignature: string): number | null;
|
|
11
|
+
/**
|
|
12
|
+
* 固化音符在当前大调中的位置,返回“沿调内音级移动”的闭包
|
|
13
|
+
*
|
|
14
|
+
* 交出闭包而不是数据结构,播放层因此无需认识任何调性模型。
|
|
15
|
+
* 基准位置不包含主体的临时升降号。
|
|
16
|
+
*/
|
|
17
|
+
export declare function createDiatonicTranspose(name: string, acc: string, octave: number, keySignature: string): ((steps: number) => number) | undefined;
|
|
10
18
|
export declare function resolveLetterNameToJianpu(name: string, acc: string, octave: number, keySignature: string): RelativeJianpuPitch | null;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Diagnostic } from "../../diagnostic.js";
|
|
2
|
+
/** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
|
|
2
3
|
export const NoteNameMap = {
|
|
3
4
|
"C": 0,
|
|
4
5
|
"C#": 1, "Db": 1,
|
|
@@ -69,6 +70,34 @@ export function resolveNoteMidi(name, acc, octave, keySignature) {
|
|
|
69
70
|
}
|
|
70
71
|
return (octave + 1) * 12 + noteValue + offset;
|
|
71
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* 固化音符在当前大调中的位置,返回“沿调内音级移动”的闭包
|
|
75
|
+
*
|
|
76
|
+
* 交出闭包而不是数据结构,播放层因此无需认识任何调性模型。
|
|
77
|
+
* 基准位置不包含主体的临时升降号。
|
|
78
|
+
*/
|
|
79
|
+
export function createDiatonicTranspose(name, acc, octave, keySignature) {
|
|
80
|
+
let degreeIndex;
|
|
81
|
+
let relativeOctave;
|
|
82
|
+
if (name >= "1" && name <= "7") {
|
|
83
|
+
degreeIndex = Number(name) - 1;
|
|
84
|
+
relativeOctave = octave;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const relative = resolveLetterNameToJianpu(name, acc, octave, keySignature);
|
|
88
|
+
if (!relative)
|
|
89
|
+
return undefined;
|
|
90
|
+
degreeIndex = Number(relative.renderName) - 1;
|
|
91
|
+
relativeOctave = relative.renderOctave;
|
|
92
|
+
}
|
|
93
|
+
const tonicMidi = tonality2Midi(keySignature, 4);
|
|
94
|
+
const base = relativeOctave * 7 + degreeIndex;
|
|
95
|
+
return steps => {
|
|
96
|
+
const total = base + steps;
|
|
97
|
+
const shift = Math.floor(total / 7);
|
|
98
|
+
return tonicMidi + MAJOR_SCALE_OFFSETS[((total % 7) + 7) % 7] + shift * 12;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
72
101
|
function wrapPitchClass(value) {
|
|
73
102
|
return ((value % 12) + 12) % 12;
|
|
74
103
|
}
|
|
@@ -86,22 +115,15 @@ function offsetToAccidental(offset) {
|
|
|
86
115
|
// 把绝对字母音高转换成“相对当前调性的简谱显示信息”。
|
|
87
116
|
// 这里保留字母拼写带来的调内级数信息,因此像 key=D 时的 C 会被解释成 b7,而不是 #6。
|
|
88
117
|
export function resolveLetterNameToJianpu(name, acc, octave, keySignature) {
|
|
89
|
-
const
|
|
90
|
-
const tonicMidi = tonality2Midi(keySignature, 4);
|
|
91
|
-
const tonicPitchClass = wrapPitchClass(tonicMidi);
|
|
92
|
-
const notePitchClass = wrapPitchClass(resolveNoteMidi(name, acc, octave, keySignature) ?? NaN);
|
|
93
|
-
const tonicIndex = LETTER_ORDER.indexOf(tonicLetter);
|
|
118
|
+
const tonicIndex = LETTER_ORDER.indexOf(keySignature[0]);
|
|
94
119
|
const noteIndex = LETTER_ORDER.indexOf(name);
|
|
95
|
-
if (tonicIndex < 0 || noteIndex < 0 || Number.isNaN(notePitchClass)) {
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
const degreeIndex = (noteIndex - tonicIndex + LETTER_ORDER.length) % LETTER_ORDER.length;
|
|
99
|
-
const expectedPitchClass = wrapPitchClass(tonicPitchClass + MAJOR_SCALE_OFFSETS[degreeIndex]);
|
|
100
|
-
const accidentalOffset = normalizeAccidentalOffset(notePitchClass - expectedPitchClass);
|
|
101
120
|
const actualMidi = resolveNoteMidi(name, acc, octave, keySignature);
|
|
102
|
-
if (actualMidi === null)
|
|
121
|
+
if (tonicIndex < 0 || noteIndex < 0 || actualMidi === null)
|
|
103
122
|
return null;
|
|
104
|
-
|
|
123
|
+
const tonicMidi = tonality2Midi(keySignature, 4);
|
|
124
|
+
const degreeIndex = (noteIndex - tonicIndex + LETTER_ORDER.length) % LETTER_ORDER.length;
|
|
125
|
+
const expectedPitchClass = wrapPitchClass(tonicMidi + MAJOR_SCALE_OFFSETS[degreeIndex]);
|
|
126
|
+
const accidentalOffset = normalizeAccidentalOffset(wrapPitchClass(actualMidi) - expectedPitchClass);
|
|
105
127
|
const octaveOffset = (actualMidi - tonicMidi - MAJOR_SCALE_OFFSETS[degreeIndex] - accidentalOffset) / 12;
|
|
106
128
|
return {
|
|
107
129
|
renderName: String(degreeIndex + 1),
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字符串字面量的唯一规则来源:预处理、`@text` 糖、`L:` 歌词糖共用
|
|
3
|
+
* 扫描与反转义必须互为镜像,否则会出现“扫得对、还原错”
|
|
4
|
+
*/
|
|
5
|
+
/** 找与 quotePos 处开引号配对的闭引号;找不到返回 -1 */
|
|
6
|
+
export declare function findClosingQuote(source: string, quotePos: number, end: number): number;
|
|
7
|
+
/** 剥掉成对的双引号并反转义 `\X` -> `X`;裸参数没走转义扫描,原样返回 */
|
|
8
|
+
export declare function removeQuote(source: string): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字符串字面量的唯一规则来源:预处理、`@text` 糖、`L:` 歌词糖共用
|
|
3
|
+
* 扫描与反转义必须互为镜像,否则会出现“扫得对、还原错”
|
|
4
|
+
*/
|
|
5
|
+
/** 找与 quotePos 处开引号配对的闭引号;找不到返回 -1 */
|
|
6
|
+
export function findClosingQuote(source, quotePos, end) {
|
|
7
|
+
for (let i = quotePos + 1; i < end; i++) {
|
|
8
|
+
if (source[i] === "\\")
|
|
9
|
+
i++;
|
|
10
|
+
else if (source[i] === '"')
|
|
11
|
+
return i;
|
|
12
|
+
}
|
|
13
|
+
return -1;
|
|
14
|
+
}
|
|
15
|
+
/** 剥掉成对的双引号并反转义 `\X` -> `X`;裸参数没走转义扫描,原样返回 */
|
|
16
|
+
export function removeQuote(source) {
|
|
17
|
+
if (source.length < 2 || !source.startsWith('"') || !source.endsWith('"'))
|
|
18
|
+
return source;
|
|
19
|
+
return source.slice(1, -1).replace(/\\(.)/gs, "$1");
|
|
20
|
+
}
|
|
@@ -4,6 +4,8 @@ import { GrammarCallNode, GrammarNode, type SyntaxAnalysis } from "./grammarType
|
|
|
4
4
|
import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue } from "../functions/ASTtypes.js";
|
|
5
5
|
export declare const DEFAULT_FONT_SIZE = 22;
|
|
6
6
|
export declare const DEFAULT_OCTAVE = 4;
|
|
7
|
+
/** 系统变量,供 `@set` 读取 */
|
|
8
|
+
export declare const SYSTEM_VARIABLE_TYPES: Record<string, paramType>;
|
|
7
9
|
export declare class ParserContext {
|
|
8
10
|
/** 根解析器为 0,内容参数和大括号子解析器依次加一 */
|
|
9
11
|
readonly scopeDepth: number;
|
|
@@ -23,15 +25,15 @@ export declare class ParserContext {
|
|
|
23
25
|
deSugarAtomFns: deSugarAtomFunction[];
|
|
24
26
|
/** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
|
|
25
27
|
deSugarRelationFns: deSugarRelationFunction[];
|
|
26
|
-
/** .labelable()
|
|
27
|
-
labelableNodes: ASTFunctionNode[];
|
|
28
|
+
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
29
|
+
labelableNodes: (ASTFunctionNode | null)[];
|
|
28
30
|
nodes: ASTNodeBase[];
|
|
29
31
|
constructor(ctx: ParserContext | {
|
|
30
32
|
source: string;
|
|
31
33
|
diagnostics?: Diagnostic[];
|
|
32
34
|
variables?: Map<string, any>;
|
|
33
35
|
functions?: Map<string, ASTFunctionClass>;
|
|
34
|
-
labelableNodes?: ASTFunctionNode[];
|
|
36
|
+
labelableNodes?: (ASTFunctionNode | null)[];
|
|
35
37
|
toConsume?: ASTNodeBase[];
|
|
36
38
|
commentSpans?: SourceSpan[];
|
|
37
39
|
});
|
|
@@ -41,9 +43,10 @@ export declare class ParserContext {
|
|
|
41
43
|
];
|
|
42
44
|
pushNode(node: ASTNodeBase): void;
|
|
43
45
|
get fontSize(): number;
|
|
44
|
-
set fontSize(size: number
|
|
46
|
+
set fontSize(size: number);
|
|
45
47
|
get strict(): boolean;
|
|
46
48
|
set strict(value: boolean);
|
|
49
|
+
setVariable(name: string, value: paramValue): void;
|
|
47
50
|
registerFunctions(functionClasses: ASTFunctionClass[]): void;
|
|
48
51
|
/**
|
|
49
52
|
* 扫描 GrammarNode 并构造 AST;不产出 syntax
|
|
@@ -85,7 +88,7 @@ export declare class ParserContext {
|
|
|
85
88
|
* funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
|
|
86
89
|
* 解析成功返回值 解析失败返回null
|
|
87
90
|
*/
|
|
88
|
-
parseArgWithType(
|
|
91
|
+
parseArgWithType(span: SourceSpan, type?: paramType, funcStart?: number): paramValue | null;
|
|
89
92
|
length2px(length: LengthValue): number;
|
|
90
93
|
}
|
|
91
94
|
export declare function skipSpaces(s: string, i: number, end: number): number;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
-
import { readCall, trimRange
|
|
2
|
+
import { readCall, trimRange } from "./parse-utils/call-utils.js";
|
|
3
|
+
import { removeQuote } from "./parse-utils/string-utils.js";
|
|
3
4
|
import { readBrace } from "./parse-utils/brace-utils.js";
|
|
4
5
|
import { readLabel } from "./parse-utils/label-utils.js";
|
|
5
6
|
import { parseLength } from "./parse-utils/length-utils.js";
|
|
@@ -23,6 +24,11 @@ function classifySyntaxValue(type, text) {
|
|
|
23
24
|
export const DEFAULT_FONT_SIZE = 22;
|
|
24
25
|
const DEFAULT_STRICT_MODE = false;
|
|
25
26
|
export const DEFAULT_OCTAVE = 4;
|
|
27
|
+
/** 系统变量,供 `@set` 读取 */
|
|
28
|
+
export const SYSTEM_VARIABLE_TYPES = {
|
|
29
|
+
fontsize: "length",
|
|
30
|
+
strict: "boolean",
|
|
31
|
+
};
|
|
26
32
|
// 解析上下文
|
|
27
33
|
export class ParserContext {
|
|
28
34
|
/** 根解析器为 0,内容参数和大括号子解析器依次加一 */
|
|
@@ -43,7 +49,7 @@ export class ParserContext {
|
|
|
43
49
|
deSugarAtomFns;
|
|
44
50
|
/** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
|
|
45
51
|
deSugarRelationFns;
|
|
46
|
-
/** .labelable()
|
|
52
|
+
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
47
53
|
labelableNodes;
|
|
48
54
|
nodes; // 解析结果
|
|
49
55
|
constructor(ctx) {
|
|
@@ -97,25 +103,16 @@ export class ParserContext {
|
|
|
97
103
|
}
|
|
98
104
|
pushNode(node) {
|
|
99
105
|
this.nodes.push(node);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this.labelableNodes.push(target);
|
|
105
|
-
}
|
|
106
|
+
// 未实现的对标签透明不入表,null 入表成为边界
|
|
107
|
+
const target = node.labelable?.();
|
|
108
|
+
if (target !== undefined)
|
|
109
|
+
this.labelableNodes.push(target);
|
|
106
110
|
}
|
|
107
111
|
get fontSize() {
|
|
108
112
|
return this.variables["fontsize"] ?? DEFAULT_FONT_SIZE;
|
|
109
113
|
}
|
|
110
114
|
set fontSize(size) {
|
|
111
|
-
|
|
112
|
-
const l = parseLength(size);
|
|
113
|
-
if (l instanceof Diagnostic)
|
|
114
|
-
throw l;
|
|
115
|
-
this.variables["fontsize"] = this.length2px(l);
|
|
116
|
-
}
|
|
117
|
-
else
|
|
118
|
-
this.variables["fontsize"] = size;
|
|
115
|
+
this.variables["fontsize"] = size;
|
|
119
116
|
}
|
|
120
117
|
get strict() {
|
|
121
118
|
return this.variables["strict"] ?? DEFAULT_STRICT_MODE;
|
|
@@ -123,6 +120,14 @@ export class ParserContext {
|
|
|
123
120
|
set strict(value) {
|
|
124
121
|
this.variables["strict"] = value;
|
|
125
122
|
}
|
|
123
|
+
setVariable(name, value) {
|
|
124
|
+
if (name === "fontsize")
|
|
125
|
+
this.fontSize = this.length2px(value);
|
|
126
|
+
else if (name === "strict")
|
|
127
|
+
this.strict = value;
|
|
128
|
+
else
|
|
129
|
+
this.variables[name] = value;
|
|
130
|
+
}
|
|
126
131
|
registerFunctions(functionClasses) {
|
|
127
132
|
const map = this.functions;
|
|
128
133
|
for (const funcClass of functionClasses) {
|
|
@@ -247,20 +252,18 @@ export class ParserContext {
|
|
|
247
252
|
this.nodes.push(new ASTBraceNode(node.span, subParser.parse(node.span.start + 1, node.span.end - 1), null));
|
|
248
253
|
break;
|
|
249
254
|
case "label":
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const labelNode = new ASTLabelNode(node.span, node.label, tgt);
|
|
258
|
-
// 处理待消费列表只处理没有parent的 而label是有parent的 label其实无所谓parent
|
|
259
|
-
this.nodes.push(labelNode);
|
|
255
|
+
// 透明节点不入表,所以表尾就是绑定终点:null 是边界,已被占用的承载者同样到此为止
|
|
256
|
+
const tgt = this.labelableNodes.at(-1);
|
|
257
|
+
if (!tgt) {
|
|
258
|
+
this.diagnostics.push(Diagnostic.warning.LabelWithoutTarget(node.label, node.span));
|
|
259
|
+
}
|
|
260
|
+
else if (tgt.label !== void 0) {
|
|
261
|
+
this.diagnostics.push(Diagnostic.warning.TargetAlreadyLabeled(node.label, tgt.label, node.span));
|
|
260
262
|
}
|
|
261
263
|
else {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
+
tgt.label = node.label;
|
|
265
|
+
// 同时创建一个LabelNode 供编辑器等工具使用
|
|
266
|
+
this.nodes.push(new ASTLabelNode(node.span, node.label, tgt));
|
|
264
267
|
}
|
|
265
268
|
break;
|
|
266
269
|
case "sugar":
|
|
@@ -415,10 +418,10 @@ export class ParserContext {
|
|
|
415
418
|
this.diagnostics.push(Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.length, i + 1, arg.valueSpan));
|
|
416
419
|
}
|
|
417
420
|
else
|
|
418
|
-
args.set(key, arg
|
|
421
|
+
args.set(key, arg);
|
|
419
422
|
continue;
|
|
420
423
|
}
|
|
421
|
-
const value = this.parseArgWithType(arg.valueSpan
|
|
424
|
+
const value = this.parseArgWithType(arg.valueSpan, type, callNode.span.start);
|
|
422
425
|
if (value !== null)
|
|
423
426
|
args.set(key, value);
|
|
424
427
|
}
|
|
@@ -432,9 +435,9 @@ export class ParserContext {
|
|
|
432
435
|
* funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
|
|
433
436
|
* 解析成功返回值 解析失败返回null
|
|
434
437
|
*/
|
|
435
|
-
parseArgWithType(
|
|
438
|
+
parseArgWithType(span, type, funcStart) {
|
|
436
439
|
// 先去空白
|
|
437
|
-
const r = trimRange(this.source,
|
|
440
|
+
const r = trimRange(this.source, span.start, span.end);
|
|
438
441
|
const start = r.start, end = r.end;
|
|
439
442
|
if (start >= end)
|
|
440
443
|
return null; // 纯空白参数视为未提供
|
|
@@ -477,10 +480,12 @@ export class ParserContext {
|
|
|
477
480
|
}
|
|
478
481
|
case "label":
|
|
479
482
|
// 应对潜在的问题: 如果先解析了content会导致ctx.lavelableNodes被污染
|
|
480
|
-
funcStart ??=
|
|
483
|
+
funcStart ??= span.start;
|
|
481
484
|
// label情况下返回对象 从后向前查找以支持标签覆盖
|
|
482
485
|
for (let i = this.labelableNodes.length - 1; i >= 0; i--) {
|
|
483
486
|
const node = this.labelableNodes[i];
|
|
487
|
+
if (!node)
|
|
488
|
+
continue; // 边界标记不参与按名查找
|
|
484
489
|
if (node.sourceSpan.start >= funcStart)
|
|
485
490
|
continue; // 只能绑定在函数定义之前的节点上
|
|
486
491
|
if (node.label == text)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { findClosingQuote } from "./parse-utils/string-utils.js";
|
|
1
2
|
/**
|
|
2
3
|
* 预处理核心模块(单次扫描):
|
|
3
4
|
* 1. 构建 lineStarts(用于 offset -> 行列映射)
|
|
4
|
-
* 2. 处理 `%`
|
|
5
|
+
* 2. 处理 `%` 行注释:仅在非字符串上下文触发,注释文本等长替换为空格;引号必须配对才算字符串
|
|
5
6
|
* 3. 处理行尾连续反斜杠:
|
|
6
7
|
* - 设行尾连续 `\` 个数为 n;
|
|
7
8
|
* - 第 1/3/5... 个 `\` 替换为空格,第 2/4/6... 个保留;
|
|
@@ -22,16 +23,13 @@ const CHAR_BOM = 65279;
|
|
|
22
23
|
const CHAR_QUOTE_DOUBLE = 34;
|
|
23
24
|
const CHAR_BACKSLASH = 92;
|
|
24
25
|
const CHAR_PERCENT = 37;
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
const
|
|
26
|
+
// 掩码几乎都是短注释和续行反斜杠,长的既少重复又要一直占着内存,所以只缓存短的
|
|
27
|
+
const MAX_CACHED_SPACES = 32;
|
|
28
|
+
const SHORT_SPACES = [];
|
|
28
29
|
function getSpaces(length) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
cached = " ".repeat(length);
|
|
33
|
-
SPACE_CACHE.set(length, cached);
|
|
34
|
-
return cached;
|
|
30
|
+
if (length > MAX_CACHED_SPACES)
|
|
31
|
+
return " ".repeat(length);
|
|
32
|
+
return SHORT_SPACES[length] ??= " ".repeat(length);
|
|
35
33
|
}
|
|
36
34
|
/**
|
|
37
35
|
* 判断 非换行空白
|
|
@@ -86,6 +84,7 @@ export function preprocessSource(source) {
|
|
|
86
84
|
* 当前行末尾候选连续 `\` 片段(允许后面跟空白),用于换行处一次性判定
|
|
87
85
|
*/
|
|
88
86
|
let inQuote = 0;
|
|
87
|
+
let noPairedQuoteAfter = false;
|
|
89
88
|
let escaped = false;
|
|
90
89
|
let pendingCommentStart = -1;
|
|
91
90
|
let lastSignificantIndex = -1;
|
|
@@ -144,7 +143,14 @@ export function preprocessSource(source) {
|
|
|
144
143
|
continue;
|
|
145
144
|
}
|
|
146
145
|
if (ch === CHAR_QUOTE_DOUBLE) {
|
|
147
|
-
|
|
146
|
+
// 配对才算字符串,否则一个孤立引号会让后文的 `%` 全部失去注释语义
|
|
147
|
+
// 一次前瞻失败即证明剩余源码里没有可配对的引号,置位后不再前瞻,整体仍是 O(n)
|
|
148
|
+
if (!noPairedQuoteAfter) {
|
|
149
|
+
if (findClosingQuote(source, i, sourceLength) < 0)
|
|
150
|
+
noPairedQuoteAfter = true;
|
|
151
|
+
else
|
|
152
|
+
inQuote = ch;
|
|
153
|
+
}
|
|
148
154
|
updateLineTailState(i, ch);
|
|
149
155
|
i++;
|
|
150
156
|
continue;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LoweringResult } from "../lowering/types.js";
|
|
2
|
+
import type { PlaybackPlan } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* 把 lowering 的时间流编译成完整的演奏计划
|
|
5
|
+
*
|
|
6
|
+
* 函数通过以下入口参与 playback(按作用顺序):
|
|
7
|
+
* - [hook] Temporal.playbackMarks / PlaybackFlow.playbackFlow:生成事件前先展开控制流;反复线贴起点标记并回跳,volta 按遍数跳过列
|
|
8
|
+
* - [state] Temporal.playbackState:每次实际访问节点时,把 lowering 已固化的 BPM 登记到演奏轴;note、tempo 和正时值 dash 都用它恢复记谱位置的速度
|
|
9
|
+
* - [hook] Temporal.emitPlayback:按展开后的列序逐次调用;note 登记 NoteOn/NoteOff,up/grace 通过 emitter.play 递归登记折叠成员
|
|
10
|
+
* - [API] PlaybackEmitter.control:emit 时只登记控制;所有节点访问完成后按演奏时刻执行,同刻合并后生成 Tempo;fermata 用它修改 bpmScale
|
|
11
|
+
* - [API] PlaybackEmitter.affectFollowing:把 transform 加入当前 play frame;后续目标音完整登记 NoteOn/NoteOff 后,将冻结的 transform 紧接着写入顺序流;accent、tr、mordent 使用它
|
|
12
|
+
* - [API] PlaybackEmitter.defer:把 hook 写在当前节点之后;状态扫描完成后重放顺序流,走到该位置才执行,可见完整 Tempo 和此前音符;dash 用它延长前一组 NoteOff
|
|
13
|
+
* - [hook] PlaybackRelation.applyPlayback:顺序流重放完并首次排序后执行,可观察和改写完整事件计划;tie 用它合并相距很远的端点
|
|
14
|
+
* - comparePlaybackDraftEvents / finalizePlaybackEvents:最后稳定排序,再校验 NoteOn/NoteOff 配对并剥离 origins、order 等编译期字段
|
|
15
|
+
*/
|
|
16
|
+
export declare function compilePlayback(lowering: LoweringResult, options?: {
|
|
17
|
+
maxFlowSteps?: number;
|
|
18
|
+
}): PlaybackPlan;
|