jpfun 0.8.0 → 1.0.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 +10 -15
- package/dist/diagnostic.d.ts +29 -2
- package/dist/diagnostic.js +5 -4
- package/dist/functions/ASTtypes.d.ts +1 -14
- package/dist/functions/ASTtypes.js +5 -36
- package/dist/functions/adjust/index.js +45 -33
- package/dist/functions/arpeggio/index.js +44 -48
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.d.ts +3 -0
- package/dist/functions/bar/index.js +21 -11
- package/dist/functions/beam/auto.js +54 -123
- package/dist/functions/beam/index.js +4 -9
- package/dist/functions/beam/layout.js +3 -2
- package/dist/functions/box/index.js +1 -6
- package/dist/functions/dash/index.js +6 -4
- package/dist/functions/div/index.d.ts +3 -5
- package/dist/functions/div/index.js +14 -12
- package/dist/functions/dot/index.js +3 -9
- package/dist/functions/dyn/index.js +7 -26
- package/dist/functions/gliss/index.js +99 -91
- package/dist/functions/grace/index.d.ts +2 -5
- package/dist/functions/grace/index.js +47 -94
- package/dist/functions/head/index.js +32 -36
- package/dist/functions/key/index.js +9 -12
- package/dist/functions/meter/index.d.ts +1 -0
- package/dist/functions/meter/index.js +6 -3
- package/dist/functions/meter/shape.d.ts +1 -1
- package/dist/functions/note/accidentals.d.ts +3 -3
- package/dist/functions/note/accidentals.js +29 -16
- package/dist/functions/note/index.js +12 -14
- package/dist/functions/note/source-transform.d.ts +15 -0
- package/dist/functions/note/source-transform.js +75 -71
- package/dist/functions/ottava/index.js +12 -32
- package/dist/functions/page/index.js +15 -17
- package/dist/functions/rest/index.js +10 -10
- package/dist/functions/slide/index.js +1 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/temporal.d.ts +11 -5
- package/dist/functions/temporal.js +22 -2
- package/dist/functions/text/index.js +24 -30
- package/dist/functions/tie/index.js +42 -41
- package/dist/functions/tremolo/index.js +1 -1
- package/dist/functions/tuplet/index.js +1 -6
- package/dist/functions/up/index.js +82 -88
- package/dist/functions/voice/connections.d.ts +3 -2
- package/dist/functions/voice/connections.js +48 -10
- package/dist/functions/voice/index.d.ts +2 -3
- package/dist/functions/voice/index.js +2 -970
- package/dist/functions/voice/lyrics.d.ts +34 -0
- package/dist/functions/voice/lyrics.js +393 -0
- package/dist/functions/voice/voice.d.ts +67 -0
- package/dist/functions/voice/voice.js +405 -0
- package/dist/functions/voice/voices.d.ts +106 -0
- package/dist/functions/voice/voices.js +424 -0
- package/dist/functions/volta/index.js +6 -23
- package/dist/index.d.ts +97 -3
- package/dist/index.js +97 -3
- package/dist/jpfun.min.js +63 -67
- package/dist/layout/engine.d.ts +4 -3
- package/dist/layout/engine.js +190 -24
- package/dist/layout/model.d.ts +12 -4
- package/dist/layout/model.js +16 -11
- package/dist/layout/page.d.ts +2 -1
- package/dist/layout/page.js +15 -15
- package/dist/layout/path.js +1 -1
- package/dist/layout/range.d.ts +7 -6
- package/dist/layout/range.js +23 -14
- package/dist/layout/types.d.ts +19 -30
- package/dist/lowering/loweringContext.d.ts +8 -11
- package/dist/lowering/loweringContext.js +7 -13
- package/dist/lowering/track.d.ts +8 -1
- package/dist/lowering/types.d.ts +12 -8
- package/dist/parser/grammarType.d.ts +3 -3
- package/dist/parser/parse-utils/brace-utils.js +7 -17
- package/dist/parser/parse-utils/call-utils.d.ts +4 -3
- package/dist/parser/parse-utils/call-utils.js +23 -66
- package/dist/parser/parse-utils/note-utils.d.ts +7 -3
- package/dist/parser/parse-utils/note-utils.js +25 -40
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +8 -6
- package/dist/parser/parserContext.d.ts +17 -11
- package/dist/parser/parserContext.js +111 -65
- package/dist/parser/preprocess.d.ts +7 -0
- package/dist/parser/preprocess.js +21 -19
- package/dist/pipeline.d.ts +1 -1
- package/dist/pipeline.js +2 -2
- package/dist/playback/compile.js +14 -15
- package/dist/playback/event.d.ts +6 -6
- package/dist/playback/event.js +13 -49
- package/dist/render/canvas.d.ts +0 -2
- package/dist/render/canvas.js +50 -56
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.d.ts +7 -0
- package/dist/render/paint.js +70 -20
- package/dist/render/recording.d.ts +8 -1
- package/dist/render/recording.js +12 -0
- package/dist/render/svg.d.ts +5 -2
- package/dist/render/svg.js +70 -48
- package/dist/render/text.d.ts +37 -2
- package/dist/render/text.js +136 -1
- package/dist/render/translate.d.ts +3 -1
- package/dist/render/translate.js +6 -0
- package/dist/render/types.d.ts +9 -2
- package/grammar.md +18 -14
- package/package.json +8 -18
- package/dist/converter/midi/convert.d.ts +0 -42
- package/dist/converter/midi/convert.js +0 -729
- package/dist/converter/midi/index.d.ts +0 -5
- package/dist/converter/midi/index.js +0 -11
- package/dist/converter/midi/json.d.ts +0 -38
- package/dist/converter/midi/json.js +0 -1
- package/dist/converter/midi/triplet.d.ts +0 -30
- package/dist/converter/midi/triplet.js +0 -75
- package/dist/converter/musicxml/dom.d.ts +0 -25
- package/dist/converter/musicxml/dom.js +0 -65
- package/dist/converter/musicxml/features.d.ts +0 -114
- package/dist/converter/musicxml/features.js +0 -501
- package/dist/converter/musicxml/index.d.ts +0 -14
- package/dist/converter/musicxml/index.js +0 -1295
- package/dist/converter/musicxml/model.d.ts +0 -176
- package/dist/converter/musicxml/model.js +0 -1
- package/dist/converter/musicxml/resolve.d.ts +0 -10
- package/dist/converter/musicxml/resolve.js +0 -152
- package/dist/converter/source.d.ts +0 -22
- package/dist/converter/source.js +0 -51
- package/dist/core.d.ts +0 -24
- package/dist/core.js +0 -24
- package/dist/iife/core.d.ts +0 -1
- package/dist/iife/core.js +0 -3
- package/dist/iife/from-midi.d.ts +0 -1
- package/dist/iife/from-midi.js +0 -15
- package/dist/iife/from-musicxml.d.ts +0 -1
- package/dist/iife/from-musicxml.js +0 -3
- package/dist/iife/full.d.ts +0 -1
- package/dist/iife/full.js +0 -3
- package/dist/jpfun.core.min.js +0 -123
- package/dist/jpfun.from-midi.min.js +0 -9
- package/dist/jpfun.from-musicxml.min.js +0 -12
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export const SHARP_NOTE_NAMES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
|
2
2
|
export const FLAT_NOTE_NAMES = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
|
|
3
|
+
export const NOTE_LETTERS = "CDEFGAB";
|
|
3
4
|
/** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
|
|
4
5
|
export const NoteNameMap = {
|
|
5
6
|
"C": 0,
|
|
@@ -22,33 +23,32 @@ export const NoteNameMap = {
|
|
|
22
23
|
"6": 9,
|
|
23
24
|
"7": 11
|
|
24
25
|
};
|
|
25
|
-
const LETTER_ORDER = ["C", "D", "E", "F", "G", "A", "B"];
|
|
26
26
|
const MAJOR_SCALE_OFFSETS = [0, 2, 4, 5, 7, 9, 11];
|
|
27
|
-
// 输入 C4 D#3
|
|
28
|
-
// 要求非常严格:必须 [
|
|
29
|
-
export function tonality2Midi(tonality
|
|
27
|
+
// 输入 C4 D#3 Bb3 等调性字符串,输出 MIDI 数值;格式不合法返回 null
|
|
28
|
+
// 要求非常严格:必须 [大写字母][最多一个 #b][绝对八度]
|
|
29
|
+
export function tonality2Midi(tonality) {
|
|
30
30
|
// 解析音名
|
|
31
31
|
const note = tonality[0];
|
|
32
|
+
if (!note || note < "A" || note > "G")
|
|
33
|
+
return null;
|
|
32
34
|
let acc = "";
|
|
33
35
|
let i = 1;
|
|
34
36
|
// 解析升降号
|
|
35
|
-
if (i < tonality.length && (tonality[i] ===
|
|
37
|
+
if (i < tonality.length && (tonality[i] === "#" || tonality[i] === "b")) {
|
|
36
38
|
acc = tonality[i];
|
|
37
39
|
i++;
|
|
38
40
|
}
|
|
39
41
|
// 组合音名和升降号
|
|
40
|
-
|
|
42
|
+
const noteValue = NoteNameMap[note + acc];
|
|
41
43
|
if (noteValue === undefined)
|
|
42
44
|
return null;
|
|
43
45
|
// 解析八度
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
46
|
+
const octaveText = tonality.slice(i);
|
|
47
|
+
const octave = Number(octaveText);
|
|
48
|
+
if (!/^-?\d+$/.test(octaveText) || !Number.isSafeInteger(octave)) {
|
|
49
|
+
return null;
|
|
50
50
|
}
|
|
51
|
-
return (
|
|
51
|
+
return (octave + 1) * 12 + noteValue;
|
|
52
52
|
}
|
|
53
53
|
// ignoreNatural: 是否忽略还原号对升降号的重置作用。默认为true,即默认自然音符不重置acc
|
|
54
54
|
// initOffset: 该小节累加的升降号偏移量 仅在ignoreNatural=false时有意义
|
|
@@ -77,54 +77,39 @@ export function resolveNoteMidi(name, acc, octave, keySignature) {
|
|
|
77
77
|
* 固化音符在当前大调中的位置,返回“沿调内音级移动”的闭包
|
|
78
78
|
*
|
|
79
79
|
* 交出闭包而不是数据结构,播放层因此无需认识任何调性模型。
|
|
80
|
-
*
|
|
80
|
+
* 基准位置基于已归一化的简谱级数与相对八度,不包含主体的临时升降号。
|
|
81
81
|
*/
|
|
82
|
-
export function createDiatonicTranspose(name,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (name >= "1" && name <= "7") {
|
|
86
|
-
degreeIndex = Number(name) - 1;
|
|
87
|
-
relativeOctave = octave;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
const relative = resolveLetterNameToJianpu(name, acc, octave, keySignature);
|
|
91
|
-
if (!relative)
|
|
92
|
-
return undefined;
|
|
93
|
-
degreeIndex = Number(relative.renderName) - 1;
|
|
94
|
-
relativeOctave = relative.renderOctave;
|
|
95
|
-
}
|
|
82
|
+
export function createDiatonicTranspose(name, octave, keySignature) {
|
|
83
|
+
if (name < "1" || name > "7")
|
|
84
|
+
return undefined;
|
|
96
85
|
const tonicMidi = tonality2Midi(keySignature);
|
|
97
|
-
const base =
|
|
86
|
+
const base = octave * 7 + Number(name) - 1;
|
|
98
87
|
return steps => {
|
|
99
88
|
const total = base + steps;
|
|
100
89
|
const shift = Math.floor(total / 7);
|
|
101
90
|
return tonicMidi + MAJOR_SCALE_OFFSETS[((total % 7) + 7) % 7] + shift * 12;
|
|
102
91
|
};
|
|
103
92
|
}
|
|
104
|
-
function wrapPitchClass(value) {
|
|
93
|
+
export function wrapPitchClass(value) {
|
|
105
94
|
return ((value % 12) + 12) % 12;
|
|
106
95
|
}
|
|
107
|
-
function normalizeAccidentalOffset(value) {
|
|
96
|
+
export function normalizeAccidentalOffset(value) {
|
|
108
97
|
const wrapped = wrapPitchClass(value);
|
|
109
98
|
return wrapped > 6 ? wrapped - 12 : wrapped;
|
|
110
99
|
}
|
|
111
|
-
function offsetToAccidental(offset) {
|
|
112
|
-
|
|
113
|
-
return "";
|
|
114
|
-
if (offset > 0)
|
|
115
|
-
return "#".repeat(offset);
|
|
116
|
-
return "b".repeat(-offset);
|
|
100
|
+
export function offsetToAccidental(offset) {
|
|
101
|
+
return offset < 0 ? "b".repeat(-offset) : "#".repeat(offset);
|
|
117
102
|
}
|
|
118
103
|
// 把绝对字母音高转换成“相对当前调性的简谱显示信息”。
|
|
119
104
|
// 这里保留字母拼写带来的调内级数信息,因此像 key=D 时的 C 会被解释成 b7,而不是 #6。
|
|
120
105
|
export function resolveLetterNameToJianpu(name, acc, octave, keySignature) {
|
|
121
|
-
const tonicIndex =
|
|
122
|
-
const noteIndex =
|
|
106
|
+
const tonicIndex = NOTE_LETTERS.indexOf(keySignature[0]);
|
|
107
|
+
const noteIndex = NOTE_LETTERS.indexOf(name);
|
|
123
108
|
const actualMidi = resolveNoteMidi(name, acc, octave, keySignature);
|
|
124
109
|
if (tonicIndex < 0 || noteIndex < 0 || actualMidi === null)
|
|
125
110
|
return null;
|
|
126
111
|
const tonicMidi = tonality2Midi(keySignature);
|
|
127
|
-
const degreeIndex = (noteIndex - tonicIndex +
|
|
112
|
+
const degreeIndex = (noteIndex - tonicIndex + NOTE_LETTERS.length) % NOTE_LETTERS.length;
|
|
128
113
|
const expectedPitchClass = wrapPitchClass(tonicMidi + MAJOR_SCALE_OFFSETS[degreeIndex]);
|
|
129
114
|
const accidentalOffset = normalizeAccidentalOffset(wrapPitchClass(actualMidi) - expectedPitchClass);
|
|
130
115
|
const octaveOffset = (actualMidi - tonicMidi - MAJOR_SCALE_OFFSETS[degreeIndex] - accidentalOffset) / 12;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { Diagnostic } from "../../diagnostic.js";
|
|
6
6
|
/** 找与 quotePos 处开引号配对的闭引号;找不到返回 -1 */
|
|
7
7
|
export declare function findClosingQuote(source: string, quotePos: number, end: number): number;
|
|
8
|
-
/**
|
|
8
|
+
/** 剥掉成对的双引号(若有)并解码编码/定界级转义(`\"`、`\\`、`\uXXXX`);其余 `\X` 原样保留给具体函数解析 */
|
|
9
9
|
export declare function parseString(source: string): string | Diagnostic;
|
|
10
10
|
/** 生成双引号包裹的 jpFun 源码,仅转义反斜杠和双引号;与 JSON.stringify 不同,保留真实换行和制表符 */
|
|
11
11
|
export declare function quote(value: string): string;
|
|
@@ -13,17 +13,19 @@ export function findClosingQuote(source, quotePos, end) {
|
|
|
13
13
|
}
|
|
14
14
|
return -1;
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
16
|
+
/** 剥掉成对的双引号(若有)并解码编码/定界级转义(`\"`、`\\`、`\uXXXX`);其余 `\X` 原样保留给具体函数解析 */
|
|
17
17
|
export function parseString(source) {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const quoted = source.length >= 2 && source.startsWith('"') && source.endsWith('"');
|
|
19
|
+
const raw = quoted ? source.slice(1, -1) : source;
|
|
20
20
|
let diagnostic;
|
|
21
21
|
// 连同不完整转义一起匹配以便诊断;替换结果不会被再次扫描
|
|
22
|
-
const text =
|
|
23
|
-
if (body
|
|
22
|
+
const text = raw.replace(/\\(u.{0,4}|.)/gs, (escape, body, offset) => {
|
|
23
|
+
if (body === '"' || body === "\\")
|
|
24
24
|
return body;
|
|
25
|
+
if (body[0] !== "u")
|
|
26
|
+
return escape;
|
|
25
27
|
if (!/^u[0-9a-f]{4}$/i.test(body)) {
|
|
26
|
-
const start = offset + 1;
|
|
28
|
+
const start = offset + (quoted ? 1 : 0);
|
|
27
29
|
diagnostic ??= Diagnostic.warning.InvalidUnicodeEscape({ start, end: start + escape.length });
|
|
28
30
|
return escape;
|
|
29
31
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { deSugarAtomFunction, deSugarRelationFunction, LengthValue, SourceSpan } from "./types.js";
|
|
2
2
|
import { Diagnostic } from "../diagnostic.js";
|
|
3
|
-
import { GrammarCallNode, GrammarNode, type SyntaxAnalysis } from "./grammarType.js";
|
|
3
|
+
import { GrammarCallNode, GrammarCallNodeRaw, GrammarNode, type SyntaxAnalysis } from "./grammarType.js";
|
|
4
4
|
import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue } from "../functions/ASTtypes.js";
|
|
5
|
-
export declare const DEFAULT_FONT_SIZE =
|
|
6
|
-
export declare const
|
|
5
|
+
export declare const DEFAULT_FONT_SIZE = 18;
|
|
6
|
+
export declare const DEFAULT_NUMBER_WEIGHT = 700;
|
|
7
7
|
export declare class ParserContext {
|
|
8
8
|
/** 系统变量,供 `@set` 读取 */
|
|
9
9
|
static systemVariables: Record<string, {
|
|
10
10
|
type: paramType;
|
|
11
11
|
default: paramValue;
|
|
12
|
+
description: string;
|
|
12
13
|
}>;
|
|
13
14
|
/** 根解析器为 0,内容参数和大括号子解析器依次加一 */
|
|
14
15
|
readonly scopeDepth: number;
|
|
@@ -32,6 +33,8 @@ export declare class ParserContext {
|
|
|
32
33
|
deSugarRelationFns: deSugarRelationFunction[];
|
|
33
34
|
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
34
35
|
labelableNodes: (ASTFunctionNode | null)[];
|
|
36
|
+
/** 当前正在实例化的显式函数调用节点,供构造函数按需读取原始参数区间 */
|
|
37
|
+
callNode?: GrammarCallNodeRaw;
|
|
35
38
|
nodes: ASTNodeBase[];
|
|
36
39
|
constructor(ctx: ParserContext | {
|
|
37
40
|
source: string;
|
|
@@ -62,19 +65,22 @@ export declare class ParserContext {
|
|
|
62
65
|
private addSyntax;
|
|
63
66
|
/** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
|
|
64
67
|
private recordSyntax;
|
|
68
|
+
/**
|
|
69
|
+
* 向前寻找最后一个非文本节点下标;
|
|
70
|
+
* 若末尾是标签,则将“被标注节点..标签”原位打包为 ASTBraceNode
|
|
71
|
+
*/
|
|
72
|
+
findLastOperandIndex(): number;
|
|
73
|
+
/** 摘出末尾最后一个操作数(含其后置标签) */
|
|
74
|
+
takeLeftOperand(): ASTNodeBase | null;
|
|
75
|
+
/** 解析右侧剩余序列,将首个非文本节点交给 combine 合成后连同后续节点写回 this.nodes */
|
|
76
|
+
consumeRightOperand(nodes: (GrammarNode | number)[], at: number, combine: (right: ASTNodeBase) => ASTNodeBase): number | null;
|
|
65
77
|
/**
|
|
66
78
|
* 第二轮去糖 & 函数节点创建
|
|
67
79
|
* 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
|
|
68
|
-
* 对于需要后面Node的语法糖(没有终止符),可以调用此函数,具体做法为:
|
|
69
|
-
* 1. 先用构造函数开启子上下文
|
|
70
|
-
* 2. 再用新的上下文解析剩余的nodes
|
|
71
|
-
* 3. 选取需要的,利用老的ctx构建函数节点
|
|
72
|
-
* 4. 将新ctx的有用信息合并回老的ctx
|
|
73
|
-
* 之所以这么麻烦,是因为创建语法糖对应节点时需要保留当前上下文
|
|
74
|
-
*
|
|
75
|
-
* 对于有终止符的直接搜索后面的终止符即可。扫过的地方普通文本会再次被合并
|
|
76
80
|
*/
|
|
77
81
|
makeNodes(nodes: (GrammarNode | number)[], i?: number, end?: number): number;
|
|
82
|
+
/** 追加文本,与紧邻的上一个文本节点合并;新建区间对象,避免改写语法节点的 span */
|
|
83
|
+
private pushText;
|
|
78
84
|
/**
|
|
79
85
|
* 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
|
|
80
86
|
* 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Diagnostic } from "../diagnostic.js";
|
|
2
2
|
import { DEFAULT_TEXT_FONT, JIANPU_NUMBER_FONT } from "../render/text.js";
|
|
3
|
-
import {
|
|
3
|
+
import { isNonLineWhitespace } from "./preprocess.js";
|
|
4
|
+
import { callArgName, readCall, trimRange } from "./parse-utils/call-utils.js";
|
|
4
5
|
import { parseString } from "./parse-utils/string-utils.js";
|
|
5
6
|
import { readBrace } from "./parse-utils/brace-utils.js";
|
|
6
7
|
import { readLabel } from "./parse-utils/label-utils.js";
|
|
@@ -22,16 +23,37 @@ function classifySyntaxValue(type, text) {
|
|
|
22
23
|
return "length";
|
|
23
24
|
return undefined;
|
|
24
25
|
}
|
|
25
|
-
export const DEFAULT_FONT_SIZE =
|
|
26
|
-
export const
|
|
26
|
+
export const DEFAULT_FONT_SIZE = 18;
|
|
27
|
+
export const DEFAULT_NUMBER_WEIGHT = 700;
|
|
27
28
|
// 解析上下文
|
|
28
29
|
export class ParserContext {
|
|
29
30
|
/** 系统变量,供 `@set` 读取 */
|
|
30
31
|
static systemVariables = {
|
|
31
|
-
fontsize: {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
fontsize: {
|
|
33
|
+
type: "length",
|
|
34
|
+
default: DEFAULT_FONT_SIZE,
|
|
35
|
+
description: "基准字号,也是 `em` 长度的换算基准"
|
|
36
|
+
},
|
|
37
|
+
font: {
|
|
38
|
+
type: "string",
|
|
39
|
+
default: DEFAULT_TEXT_FONT,
|
|
40
|
+
description: "普通文字字体"
|
|
41
|
+
},
|
|
42
|
+
numberfont: {
|
|
43
|
+
type: "string",
|
|
44
|
+
default: JIANPU_NUMBER_FONT,
|
|
45
|
+
description: "音符及同类谱面数字字体"
|
|
46
|
+
},
|
|
47
|
+
numberweight: {
|
|
48
|
+
type: "number",
|
|
49
|
+
default: DEFAULT_NUMBER_WEIGHT,
|
|
50
|
+
description: "主谱面数字字重,仅控制 `note`、`meter`、`rest`"
|
|
51
|
+
},
|
|
52
|
+
strict: {
|
|
53
|
+
type: "boolean",
|
|
54
|
+
default: false,
|
|
55
|
+
description: "开启严格的语法检查"
|
|
56
|
+
}
|
|
35
57
|
};
|
|
36
58
|
/** 根解析器为 0,内容参数和大括号子解析器依次加一 */
|
|
37
59
|
scopeDepth;
|
|
@@ -55,6 +77,8 @@ export class ParserContext {
|
|
|
55
77
|
deSugarRelationFns;
|
|
56
78
|
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
57
79
|
labelableNodes;
|
|
80
|
+
/** 当前正在实例化的显式函数调用节点,供构造函数按需读取原始参数区间 */
|
|
81
|
+
callNode;
|
|
58
82
|
nodes; // 解析结果
|
|
59
83
|
constructor(ctx) {
|
|
60
84
|
if (ctx instanceof ParserContext) {
|
|
@@ -201,7 +225,12 @@ export class ParserContext {
|
|
|
201
225
|
return;
|
|
202
226
|
}
|
|
203
227
|
if (node.kind === "sugar" || node.typed) {
|
|
204
|
-
|
|
228
|
+
if (Array.isArray(node.syntaxKind)) {
|
|
229
|
+
for (const token of node.syntaxKind)
|
|
230
|
+
this.addSyntax(token.kind, token.span);
|
|
231
|
+
}
|
|
232
|
+
else
|
|
233
|
+
this.addSyntax(node.syntaxKind ?? "operator", node.span);
|
|
205
234
|
return;
|
|
206
235
|
}
|
|
207
236
|
// syntax.calls 只暴露源码结构,不带 grammar 阶段的字段
|
|
@@ -214,10 +243,7 @@ export class ParserContext {
|
|
|
214
243
|
const arg = call.args[index];
|
|
215
244
|
this.addSyntax("property", arg.nameSpan);
|
|
216
245
|
this.addSyntax("punctuation", arg.equalsSpan);
|
|
217
|
-
const
|
|
218
|
-
? this.source.slice(arg.nameSpan.start, arg.nameSpan.end).toLowerCase()
|
|
219
|
-
: void 0;
|
|
220
|
-
const type = def && resolveArgType(def, name, index);
|
|
246
|
+
const type = def && resolveArgType(def, callArgName(this.source, arg), index);
|
|
221
247
|
if (type === "content") {
|
|
222
248
|
new ParserContext(this).parseSyntax(arg.valueSpan.start, arg.valueSpan.end);
|
|
223
249
|
}
|
|
@@ -231,29 +257,68 @@ export class ParserContext {
|
|
|
231
257
|
}
|
|
232
258
|
this.addSyntax("punctuation", call.closeParenSpan);
|
|
233
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* 向前寻找最后一个非文本节点下标;
|
|
262
|
+
* 若末尾是标签,则将“被标注节点..标签”原位打包为 ASTBraceNode
|
|
263
|
+
*/
|
|
264
|
+
findLastOperandIndex() {
|
|
265
|
+
let left = this.nodes.length - 1;
|
|
266
|
+
while (left >= 0 && this.nodes[left] instanceof ASTTextNode)
|
|
267
|
+
left--;
|
|
268
|
+
if (left < 0)
|
|
269
|
+
return -1;
|
|
270
|
+
const node = this.nodes[left];
|
|
271
|
+
if (node instanceof ASTLabelNode) {
|
|
272
|
+
let tgt = node.target;
|
|
273
|
+
while (tgt.parent)
|
|
274
|
+
tgt = tgt.parent;
|
|
275
|
+
const j = this.nodes.lastIndexOf(tgt, left - 1);
|
|
276
|
+
if (j < 0)
|
|
277
|
+
return -1;
|
|
278
|
+
this.nodes.splice(j, left - j + 1, new ASTBraceNode({ start: tgt.sourceSpan.start, end: node.sourceSpan.end }, this.nodes.slice(j, left + 1), null));
|
|
279
|
+
return j;
|
|
280
|
+
}
|
|
281
|
+
return left;
|
|
282
|
+
}
|
|
283
|
+
/** 摘出末尾最后一个操作数(含其后置标签) */
|
|
284
|
+
takeLeftOperand() {
|
|
285
|
+
const index = this.findLastOperandIndex();
|
|
286
|
+
if (index < 0)
|
|
287
|
+
return null;
|
|
288
|
+
const node = this.nodes[index];
|
|
289
|
+
this.nodes.length = index;
|
|
290
|
+
return node;
|
|
291
|
+
}
|
|
292
|
+
/** 解析右侧剩余序列,将首个非文本节点交给 combine 合成后连同后续节点写回 this.nodes */
|
|
293
|
+
consumeRightOperand(nodes, at, combine) {
|
|
294
|
+
const storage = this.nodes;
|
|
295
|
+
this.nodes = [];
|
|
296
|
+
try {
|
|
297
|
+
this.makeNodes(nodes, at);
|
|
298
|
+
for (let i = 0; i < this.nodes.length; i++) {
|
|
299
|
+
const right = this.nodes[i];
|
|
300
|
+
if (right instanceof ASTTextNode)
|
|
301
|
+
continue;
|
|
302
|
+
storage.push(combine(right));
|
|
303
|
+
while (++i < this.nodes.length)
|
|
304
|
+
storage.push(this.nodes[i]);
|
|
305
|
+
return nodes.length;
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
this.nodes = storage;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
234
313
|
/**
|
|
235
314
|
* 第二轮去糖 & 函数节点创建
|
|
236
315
|
* 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
|
|
237
|
-
* 对于需要后面Node的语法糖(没有终止符),可以调用此函数,具体做法为:
|
|
238
|
-
* 1. 先用构造函数开启子上下文
|
|
239
|
-
* 2. 再用新的上下文解析剩余的nodes
|
|
240
|
-
* 3. 选取需要的,利用老的ctx构建函数节点
|
|
241
|
-
* 4. 将新ctx的有用信息合并回老的ctx
|
|
242
|
-
* 之所以这么麻烦,是因为创建语法糖对应节点时需要保留当前上下文
|
|
243
|
-
*
|
|
244
|
-
* 对于有终止符的直接搜索后面的终止符即可。扫过的地方普通文本会再次被合并
|
|
245
316
|
*/
|
|
246
317
|
makeNodes(nodes, i = 0, end = nodes.length) {
|
|
247
318
|
outer: for (; i < end; i++) {
|
|
248
319
|
const node = nodes[i];
|
|
249
320
|
if (typeof node === "number") {
|
|
250
|
-
|
|
251
|
-
const prev = this.nodes.at(-1);
|
|
252
|
-
if (prev instanceof ASTTextNode && prev.sourceSpan.end === node) {
|
|
253
|
-
prev.sourceSpan.end = node + 1;
|
|
254
|
-
}
|
|
255
|
-
else
|
|
256
|
-
this.nodes.push(new ASTTextNode({ start: node, end: node + 1 }, null));
|
|
321
|
+
this.pushText(node, node + 1);
|
|
257
322
|
continue;
|
|
258
323
|
}
|
|
259
324
|
switch (node.kind) {
|
|
@@ -288,18 +353,20 @@ export class ParserContext {
|
|
|
288
353
|
continue outer;
|
|
289
354
|
}
|
|
290
355
|
}
|
|
291
|
-
// 匹配失败,保留为文本
|
|
292
|
-
const prev = this.nodes.at(-1);
|
|
293
|
-
if (prev instanceof ASTTextNode && prev.sourceSpan.end === node.span.start) {
|
|
294
|
-
prev.sourceSpan.end = node.span.end;
|
|
295
|
-
}
|
|
296
|
-
else
|
|
297
|
-
this.nodes.push(new ASTTextNode(node.span, null));
|
|
356
|
+
this.pushText(node.span.start, node.span.end); // 匹配失败,保留为文本
|
|
298
357
|
break;
|
|
299
358
|
}
|
|
300
359
|
}
|
|
301
360
|
return i;
|
|
302
361
|
}
|
|
362
|
+
/** 追加文本,与紧邻的上一个文本节点合并;新建区间对象,避免改写语法节点的 span */
|
|
363
|
+
pushText(start, end) {
|
|
364
|
+
const prev = this.nodes.at(-1);
|
|
365
|
+
if (prev instanceof ASTTextNode && prev.sourceSpan.end === start)
|
|
366
|
+
prev.sourceSpan.end = end;
|
|
367
|
+
else
|
|
368
|
+
this.nodes.push(new ASTTextNode({ start, end }, null));
|
|
369
|
+
}
|
|
303
370
|
/**
|
|
304
371
|
* 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
|
|
305
372
|
* 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
|
|
@@ -365,11 +432,8 @@ export class ParserContext {
|
|
|
365
432
|
// 找到结尾
|
|
366
433
|
let braceEnd = readBrace(this.source, p, end);
|
|
367
434
|
// 大括号不匹配 报警 但继续解析 相当于补了一个右大括号
|
|
368
|
-
if (braceEnd < 0
|
|
369
|
-
this.diagnostics.push(Diagnostic.warning.UnmatchedBrace({
|
|
370
|
-
start: p,
|
|
371
|
-
end: end
|
|
372
|
-
}));
|
|
435
|
+
if (braceEnd < 0) {
|
|
436
|
+
this.diagnostics.push(Diagnostic.warning.UnmatchedBrace({ start: p, end }));
|
|
373
437
|
braceEnd = end;
|
|
374
438
|
}
|
|
375
439
|
emit({
|
|
@@ -390,11 +454,8 @@ export class ParserContext {
|
|
|
390
454
|
}
|
|
391
455
|
}
|
|
392
456
|
catch (e) {
|
|
393
|
-
//
|
|
394
|
-
if (e instanceof Diagnostic)
|
|
395
|
-
this.diagnostics.push(e);
|
|
396
|
-
}
|
|
397
|
-
else
|
|
457
|
+
// 原子级去糖一般不出错。Diagnostic 交给捕获方记录,避免内容参数恢复时重复
|
|
458
|
+
if (!(e instanceof Diagnostic))
|
|
398
459
|
this.diagnostics.push(Diagnostic.error.Bug(e.toString(), { start: p, end }));
|
|
399
460
|
throw e;
|
|
400
461
|
}
|
|
@@ -422,9 +483,7 @@ export class ParserContext {
|
|
|
422
483
|
const args = new Map();
|
|
423
484
|
for (let i = 0; i < callNode.args.length; i++) {
|
|
424
485
|
const arg = callNode.args[i];
|
|
425
|
-
const name = arg
|
|
426
|
-
? this.source.slice(arg.nameSpan.start, arg.nameSpan.end).toLowerCase()
|
|
427
|
-
: void 0;
|
|
486
|
+
const name = callArgName(this.source, arg);
|
|
428
487
|
const type = resolveArgType(def, name, i);
|
|
429
488
|
const key = name ?? i;
|
|
430
489
|
if (type === void 0) {
|
|
@@ -439,8 +498,10 @@ export class ParserContext {
|
|
|
439
498
|
if (value !== null)
|
|
440
499
|
args.set(key, value);
|
|
441
500
|
}
|
|
501
|
+
this.callNode = callNode;
|
|
442
502
|
return new callFNClass(callNode.span, args, this, null);
|
|
443
503
|
} // 完全信任 typed 时的 arg
|
|
504
|
+
this.callNode = undefined;
|
|
444
505
|
// 构造函数内用定义查询实际传参
|
|
445
506
|
return new callFNClass(callNode.span, callNode.args, this, null);
|
|
446
507
|
}
|
|
@@ -534,7 +595,7 @@ export class ParserContext {
|
|
|
534
595
|
s.span.start += start;
|
|
535
596
|
s.span.end += start;
|
|
536
597
|
this.diagnostics.push(s);
|
|
537
|
-
return text.slice(1, -1);
|
|
598
|
+
return text.length >= 2 && text.startsWith('"') && text.endsWith('"') ? text.slice(1, -1) : text;
|
|
538
599
|
}
|
|
539
600
|
return s;
|
|
540
601
|
}
|
|
@@ -549,28 +610,13 @@ export class ParserContext {
|
|
|
549
610
|
}
|
|
550
611
|
// 换行转义已经在预处理解决了,这里不用再跳过了
|
|
551
612
|
export function skipSpaces(s, i, end) {
|
|
552
|
-
while (i < end &&
|
|
613
|
+
while (i < end && isNonLineWhitespace(s.charCodeAt(i)))
|
|
553
614
|
i++;
|
|
554
615
|
return i;
|
|
555
616
|
}
|
|
556
617
|
/** 反向跳过非换行空白,越界返回 -1 */
|
|
557
618
|
export function skipSpacesBack(s, i, start = 0) {
|
|
558
|
-
while (i >= start &&
|
|
619
|
+
while (i >= start && isNonLineWhitespace(s.charCodeAt(i)))
|
|
559
620
|
i--;
|
|
560
621
|
return i;
|
|
561
622
|
}
|
|
562
|
-
function isWhitespaceButNotNewline(ch) {
|
|
563
|
-
switch (ch) {
|
|
564
|
-
case ' ':
|
|
565
|
-
case '\t':
|
|
566
|
-
case '\v': // 垂直制表符
|
|
567
|
-
case '\f': // 换页符
|
|
568
|
-
case '\u00A0': // 不间断空格
|
|
569
|
-
case '\uFEFF': // 零宽不连字空格
|
|
570
|
-
case '\u3000': // 中文全角空格
|
|
571
|
-
return true;
|
|
572
|
-
default:
|
|
573
|
-
return false;
|
|
574
|
-
// \n 和 \r 不算
|
|
575
|
-
}
|
|
576
|
-
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { SourceSpan } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* 判断非换行空白(对齐 ECMAScript `\s` 中除 `\r`/`\n`/`\u2028`/`\u2029` 外的全部空白字符):
|
|
4
|
+
* - ASCII 空白:`' '` (32)、`'\t'` (9)、`'\v'` (11)、`'\f'` (12)
|
|
5
|
+
* - Unicode 空白:`'\u00A0'` 不间断空格、`'\u1680'` 欧甘空格、`'\u2000'`..`'\u200A'` (8192..8202) 排版空格族、
|
|
6
|
+
* `'\u202F'` 窄不间断空格、`'\u205F'` 中等数学空格、`'\u3000'` 中文全角空格、`'\uFEFF'` BOM
|
|
7
|
+
*/
|
|
8
|
+
export declare function isNonLineWhitespace(ch: number): boolean;
|
|
2
9
|
export declare function preprocessSource(source: string): {
|
|
3
10
|
maskedSource: string;
|
|
4
11
|
lineStarts: number[];
|
|
@@ -9,21 +9,21 @@ import { findClosingQuote } from "./parse-utils/string-utils.js";
|
|
|
9
9
|
* - 若 n 为奇数,则该行换行符也替换为空格(续行)
|
|
10
10
|
* 4. 字符串外 CRLF 中的 CR 等长替换为空格,词法层只消费 LF
|
|
11
11
|
*/
|
|
12
|
-
const CHAR_LF = 10;
|
|
13
|
-
const CHAR_CR = 13;
|
|
14
|
-
const CHAR_TAB = 9;
|
|
15
|
-
const CHAR_VTAB = 11;
|
|
16
|
-
const CHAR_FF = 12;
|
|
17
|
-
const CHAR_SPACE = 32;
|
|
18
|
-
const CHAR_NBSP = 160;
|
|
19
|
-
const CHAR_OGHAM_SPACE_MARK = 5760;
|
|
20
|
-
const CHAR_NARROW_NO_BREAK_SPACE = 8239;
|
|
21
|
-
const CHAR_MEDIUM_MATHEMATICAL_SPACE = 8287;
|
|
22
|
-
const CHAR_IDEOGRAPHIC_SPACE = 12288;
|
|
23
|
-
const CHAR_BOM = 65279;
|
|
24
|
-
const CHAR_QUOTE_DOUBLE = 34;
|
|
25
|
-
const CHAR_BACKSLASH = 92;
|
|
26
|
-
const CHAR_PERCENT = 37;
|
|
12
|
+
const CHAR_LF = 10; // \n 换行
|
|
13
|
+
const CHAR_CR = 13; // \r 回车
|
|
14
|
+
const CHAR_TAB = 9; // \t 水平制表符
|
|
15
|
+
const CHAR_VTAB = 11; // \v 垂直制表符
|
|
16
|
+
const CHAR_FF = 12; // \f 换页符
|
|
17
|
+
const CHAR_SPACE = 32; // ' ' 普通空格
|
|
18
|
+
const CHAR_NBSP = 160; // \u00A0 不间断空格
|
|
19
|
+
const CHAR_OGHAM_SPACE_MARK = 5760; // \u1680 欧甘空格
|
|
20
|
+
const CHAR_NARROW_NO_BREAK_SPACE = 8239; // \u202F 窄不间断空格
|
|
21
|
+
const CHAR_MEDIUM_MATHEMATICAL_SPACE = 8287; // \u205F 中等数学空格
|
|
22
|
+
const CHAR_IDEOGRAPHIC_SPACE = 12288; // \u3000 中文全角空格
|
|
23
|
+
const CHAR_BOM = 65279; // \uFEFF 零宽不连字空格 (BOM)
|
|
24
|
+
const CHAR_QUOTE_DOUBLE = 34; // '"'
|
|
25
|
+
const CHAR_BACKSLASH = 92; // '\\'
|
|
26
|
+
const CHAR_PERCENT = 37; // '%'
|
|
27
27
|
// 掩码几乎都是短注释和续行反斜杠,长的既少重复又要一直占着内存,所以只缓存短的
|
|
28
28
|
const MAX_CACHED_SPACES = 32;
|
|
29
29
|
const SHORT_SPACES = [];
|
|
@@ -33,10 +33,12 @@ function getSpaces(length) {
|
|
|
33
33
|
return SHORT_SPACES[length] ??= " ".repeat(length);
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
36
|
+
* 判断非换行空白(对齐 ECMAScript `\s` 中除 `\r`/`\n`/`\u2028`/`\u2029` 外的全部空白字符):
|
|
37
|
+
* - ASCII 空白:`' '` (32)、`'\t'` (9)、`'\v'` (11)、`'\f'` (12)
|
|
38
|
+
* - Unicode 空白:`'\u00A0'` 不间断空格、`'\u1680'` 欧甘空格、`'\u2000'`..`'\u200A'` (8192..8202) 排版空格族、
|
|
39
|
+
* `'\u202F'` 窄不间断空格、`'\u205F'` 中等数学空格、`'\u3000'` 中文全角空格、`'\uFEFF'` BOM
|
|
38
40
|
*/
|
|
39
|
-
function isNonLineWhitespace(ch) {
|
|
41
|
+
export function isNonLineWhitespace(ch) {
|
|
40
42
|
// 热路径优先处理 ASCII
|
|
41
43
|
if (ch === CHAR_SPACE || ch === CHAR_TAB || ch === CHAR_VTAB || ch === CHAR_FF)
|
|
42
44
|
return true;
|
|
@@ -44,7 +46,7 @@ function isNonLineWhitespace(ch) {
|
|
|
44
46
|
return false;
|
|
45
47
|
return (ch === CHAR_NBSP ||
|
|
46
48
|
ch === CHAR_OGHAM_SPACE_MARK ||
|
|
47
|
-
(ch >= 8192 && ch <= 8202) ||
|
|
49
|
+
(ch >= 8192 && ch <= 8202) || // \u2000..\u200A:En/Em Quad、En/Em/Three-Per-Em/Four-Per-Em/Six-Per-Em/Figure/Punctuation/Thin/Hair Space
|
|
48
50
|
ch === CHAR_NARROW_NO_BREAK_SPACE ||
|
|
49
51
|
ch === CHAR_MEDIUM_MATHEMATICAL_SPACE ||
|
|
50
52
|
ch === CHAR_IDEOGRAPHIC_SPACE ||
|
package/dist/pipeline.d.ts
CHANGED
|
@@ -32,6 +32,6 @@ export declare function compileScore(source: string, options?: CompileScoreOptio
|
|
|
32
32
|
* 该入口会把这些问题记录到 diagnostics,并尽量返回已经识别的 call/token,供高亮和补全继续工作
|
|
33
33
|
*/
|
|
34
34
|
export declare function analyzeScoreSyntax(source: string, functions?: ASTFunctionClass[]): {
|
|
35
|
-
syntax: import("./
|
|
35
|
+
syntax: import("./index.js").SyntaxAnalysis;
|
|
36
36
|
diagnostics: Diagnostic[];
|
|
37
37
|
};
|
package/dist/pipeline.js
CHANGED
|
@@ -19,8 +19,8 @@ export function compileScore(source, options = {}) {
|
|
|
19
19
|
const parser = new ParserContext({ source: maskedSource, variables });
|
|
20
20
|
parser.registerFunctions(functions);
|
|
21
21
|
const ast = new ASTBraceNode({ start: 0, end: source.length }, parser.parse());
|
|
22
|
-
const loweringContext = new LoweringContext(
|
|
23
|
-
const lowering = loweringContext.lowerDocument(ast);
|
|
22
|
+
const loweringContext = new LoweringContext();
|
|
23
|
+
const lowering = loweringContext.lowerDocument(ast, parser.diagnostics);
|
|
24
24
|
const layoutContext = createLayoutPrepareContext(functions, { textMeasurer });
|
|
25
25
|
const layout = layoutDocument(lowering, layoutContext, layoutOptions);
|
|
26
26
|
return {
|