jpfun 0.7.0 → 0.9.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/Bravura-LICENSE.txt +93 -0
- package/README.md +49 -146
- package/dist/Bravura-LICENSE.txt +93 -0
- package/dist/converter/midi/convert.d.ts +0 -10
- package/dist/converter/midi/convert.js +19 -56
- package/dist/converter/musicxml/features.d.ts +16 -8
- package/dist/converter/musicxml/features.js +88 -19
- package/dist/converter/musicxml/index.js +88 -50
- package/dist/converter/musicxml/model.d.ts +9 -0
- package/dist/converter/source.d.ts +4 -7
- package/dist/converter/source.js +11 -9
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/diagnostic.d.ts +7 -1
- package/dist/diagnostic.js +40 -88
- package/dist/fraction.js +3 -2
- package/dist/functions/ASTtypes.d.ts +3 -32
- package/dist/functions/ASTtypes.js +5 -52
- package/dist/functions/adjust/index.js +45 -33
- package/dist/functions/arpeggio/index.js +50 -55
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +105 -8
- package/dist/functions/beam/auto.d.ts +1 -2
- package/dist/functions/beam/auto.js +80 -135
- package/dist/functions/beam/index.js +6 -12
- package/dist/functions/beam/layout.d.ts +1 -1
- package/dist/functions/beam/layout.js +1 -1
- package/dist/functions/box/index.js +0 -6
- package/dist/functions/br/index.d.ts +34 -2
- package/dist/functions/br/index.js +5 -4
- package/dist/functions/dash/index.js +9 -10
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +3 -4
- package/dist/functions/div/index.js +18 -14
- package/dist/functions/dot/index.js +5 -11
- package/dist/functions/dyn/index.js +55 -57
- package/dist/functions/gliss/index.js +131 -103
- package/dist/functions/grace/index.d.ts +3 -12
- package/dist/functions/grace/index.js +52 -102
- package/dist/functions/head/index.js +10 -7
- package/dist/functions/key/index.js +13 -15
- package/dist/functions/meter/index.d.ts +56 -1
- package/dist/functions/meter/index.js +15 -10
- package/dist/functions/note/index.js +6 -6
- package/dist/functions/note/noteName.d.ts +16 -0
- package/dist/functions/note/noteName.js +88 -0
- package/dist/functions/note/source-transform.js +8 -10
- package/dist/functions/ottava/index.js +35 -40
- package/dist/functions/page/index.d.ts +1 -0
- package/dist/functions/page/index.js +27 -21
- package/dist/functions/pagebreak/index.js +2 -2
- package/dist/functions/rest/index.js +5 -5
- package/dist/functions/set/index.js +4 -4
- package/dist/functions/slide/index.js +0 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/symbol/index.d.ts +21 -2
- package/dist/functions/symbol/index.js +69 -24
- package/dist/functions/symbol/ornament.d.ts +1 -2
- package/dist/functions/symbol/shape.d.ts +6 -0
- package/dist/functions/symbol/shape.js +16 -3
- package/dist/functions/symbol/symbols/coda.d.ts +4 -0
- package/dist/functions/symbol/symbols/coda.js +56 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
- package/dist/functions/symbol/symbols/dynamics.js +18 -8
- package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
- package/dist/functions/symbol/symbols/harmonic.js +11 -0
- package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
- package/dist/functions/symbol/symbols/marcato.js +22 -0
- package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
- package/dist/functions/symbol/symbols/staccato.js +24 -0
- package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
- package/dist/functions/symbol/symbols/tenuto.js +11 -0
- package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
- package/dist/functions/symbol/symbols/tocoda.js +16 -0
- package/dist/functions/symbol/symbols/turn.d.ts +3 -0
- package/dist/functions/symbol/symbols/turn.js +50 -0
- package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
- package/dist/functions/symbol/symbols/wheel.js +36 -0
- package/dist/functions/temporal.d.ts +3 -2
- package/dist/functions/temporal.js +8 -0
- package/dist/functions/text/index.js +38 -35
- package/dist/functions/tie/index.js +44 -43
- package/dist/functions/tremolo/index.d.ts +2 -0
- package/dist/functions/tremolo/index.js +318 -0
- package/dist/functions/tuplet/index.js +4 -14
- package/dist/functions/up/index.js +81 -85
- package/dist/functions/voice/connections.d.ts +2 -1
- package/dist/functions/voice/connections.js +48 -10
- package/dist/functions/voice/index.d.ts +2 -3
- package/dist/functions/voice/index.js +2 -963
- package/dist/functions/voice/lyrics.d.ts +30 -0
- package/dist/functions/voice/lyrics.js +289 -0
- package/dist/functions/voice/voice.d.ts +63 -0
- package/dist/functions/voice/voice.js +373 -0
- package/dist/functions/voice/voices.d.ts +106 -0
- package/dist/functions/voice/voices.js +423 -0
- package/dist/functions/volta/index.js +5 -23
- package/dist/jpfun.core.min.js +76 -59
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +81 -64
- package/dist/layout/default.d.ts +0 -1
- package/dist/layout/default.js +0 -1
- package/dist/layout/engine.js +30 -18
- package/dist/layout/model.d.ts +0 -1
- package/dist/layout/model.js +1 -4
- package/dist/layout/page.d.ts +2 -1
- package/dist/layout/page.js +14 -14
- package/dist/layout/range.d.ts +7 -6
- package/dist/layout/range.js +23 -14
- package/dist/layout/types.d.ts +9 -28
- package/dist/lowering/loweringContext.d.ts +18 -23
- package/dist/lowering/loweringContext.js +33 -46
- package/dist/lowering/track.d.ts +8 -1
- package/dist/lowering/types.d.ts +13 -11
- package/dist/parser/parse-utils/call-utils.d.ts +1 -2
- package/dist/parser/parse-utils/call-utils.js +6 -34
- package/dist/parser/parse-utils/note-utils.d.ts +3 -3
- package/dist/parser/parse-utils/note-utils.js +21 -33
- package/dist/parser/parse-utils/string-utils.d.ts +3 -2
- package/dist/parser/parse-utils/string-utils.js +20 -5
- package/dist/parser/parserContext.d.ts +9 -9
- package/dist/parser/parserContext.js +62 -11
- package/dist/pipeline.js +2 -3
- package/dist/playback/compile.js +131 -82
- package/dist/playback/event.d.ts +6 -6
- package/dist/playback/event.js +13 -49
- package/dist/playback/time.js +42 -31
- package/dist/playback/types.d.ts +3 -1
- package/dist/render/canvas.js +4 -22
- package/dist/render/recording.js +1 -4
- package/dist/render/svg.d.ts +0 -1
- package/dist/render/svg.js +33 -31
- package/dist/render/text.d.ts +36 -1
- package/dist/render/text.js +134 -0
- package/dist/render/translate.d.ts +0 -1
- package/dist/render/translate.js +6 -23
- package/dist/render/types.d.ts +0 -2
- package/grammar.md +39 -30
- package/package.json +3 -3
- package/dist/functions/note/noteNameFSM.d.ts +0 -8
- package/dist/functions/note/noteNameFSM.js +0 -181
- 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/diagnostic.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const PLACEHOLDER_RE = /\{(\w+)\}/g;
|
|
1
2
|
export class Diagnostic {
|
|
2
3
|
/** 诊断代码 */
|
|
3
4
|
code;
|
|
@@ -5,10 +6,24 @@ export class Diagnostic {
|
|
|
5
6
|
message;
|
|
6
7
|
/** 诊断位置,指示问题在源代码中的位置 */
|
|
7
8
|
span;
|
|
8
|
-
|
|
9
|
+
/** 诊断消息中的结构化参数(构造时复制并字符串化,不修改或持有原对象) */
|
|
10
|
+
params;
|
|
11
|
+
// 多语言支持:仅需要替换此函数
|
|
12
|
+
static format(code, message, params) {
|
|
13
|
+
return params
|
|
14
|
+
? message.replace(PLACEHOLDER_RE, (match, k) => Object.hasOwn(params, k) ? params[k] : match)
|
|
15
|
+
: message;
|
|
16
|
+
}
|
|
17
|
+
constructor(code, message, span, params) {
|
|
9
18
|
this.code = code;
|
|
10
|
-
this.message = message;
|
|
11
19
|
this.span = span;
|
|
20
|
+
if (params) {
|
|
21
|
+
const dict = {};
|
|
22
|
+
for (const key of Object.keys(params))
|
|
23
|
+
dict[key] = String(params[key]);
|
|
24
|
+
this.params = dict;
|
|
25
|
+
}
|
|
26
|
+
this.message = Diagnostic.format(code, message, this.params);
|
|
12
27
|
}
|
|
13
28
|
toLineCol(lineStarts) {
|
|
14
29
|
const start = Diagnostic.offsetToLineCol(this.span.start, lineStarts);
|
|
@@ -47,98 +62,35 @@ export class Diagnostic {
|
|
|
47
62
|
static error;
|
|
48
63
|
static warning;
|
|
49
64
|
}
|
|
65
|
+
const formatArg = (arg) => typeof arg === "number" ? `[${arg}]` : `"${arg}"`;
|
|
50
66
|
// 两个基类区分 Warning 和 Error
|
|
51
67
|
export class ErrorDiagnostic extends Diagnostic {
|
|
52
68
|
}
|
|
53
|
-
;
|
|
54
69
|
export class WarningDiagnostic extends Diagnostic {
|
|
55
70
|
}
|
|
56
|
-
;
|
|
57
71
|
Diagnostic.error = {
|
|
58
|
-
Bug: (message, span) => {
|
|
59
|
-
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
|
|
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", `换行或换页切断了一个仍在持续的事件;请把长音写成多个事件,或调整换行、换页的位置`, 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
|
-
}
|
|
72
|
+
Bug: (message, span) => new ErrorDiagnostic("E_BUG", "解析器错误: {message},请联系开发者修复这个问题", span, { message }),
|
|
73
|
+
MissingArg: (functionName, argumentName, span) => new ErrorDiagnostic("E_MISSING_REQUIRED_ARG", "函数@{fn} 缺少必需的参数 {arg}", span, { fn: functionName, arg: formatArg(argumentName) }),
|
|
74
|
+
PosAfterNamedArg: (functionName, span) => new ErrorDiagnostic("E_POSARG_AFTER_KWARG", "函数 @{fn} 的位置参数出现在命名参数之后,这是不允许的", span, { fn: functionName }),
|
|
75
|
+
EmptyContent: (functionName, argumentName, span) => new ErrorDiagnostic("E_EMPTY_CONTENT", "函数 @{fn} 的参数 {arg} 内容为空(或无可用内容);请提供有效内容", span, { fn: functionName, arg: formatArg(argumentName) }),
|
|
76
|
+
UnterminatedCall: (functionName, span) => new ErrorDiagnostic("E_UNTERMINATED_CALL", "函数调用未闭合: @{fn}(...)", span, { fn: functionName }),
|
|
77
|
+
UnknownFunction: (functionName, span) => new ErrorDiagnostic("E_UNKNOWN_FUNCTION", "未知函数: @{fn},请检查拼写", span, { fn: functionName }),
|
|
78
|
+
UnknownLabel: (labelName, span) => new ErrorDiagnostic("E_UNKNOWN_LABEL", '未知标签: "{label}"', span, { label: labelName }),
|
|
79
|
+
BreakInsideEvent: (span) => new ErrorDiagnostic("E_BREAK_INSIDE_EVENT", "换行或换页切断了一个仍在持续的事件;请把长音写成多个事件,或调整换行、换页的位置", span),
|
|
80
|
+
PageOverflow: (required, available, span) => new ErrorDiagnostic("E_PAGE_OVERFLOW", "谱面行需要 {required}px 高度,但页面内容区只有 {available}px", span, { required, available }),
|
|
100
81
|
};
|
|
101
82
|
Diagnostic.warning = {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
},
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
},
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
},
|
|
114
|
-
|
|
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
|
-
TargetAlreadyLabeled: (newLabel, oldLabel, span) => {
|
|
136
|
-
return new WarningDiagnostic("W_TARGET_ALREADY_LABELED", `目标已标注为 "${oldLabel}","${newLabel}" 将被忽略`, span);
|
|
137
|
-
},
|
|
138
|
-
UnresolvedEndpoint: (functionName, span) => {
|
|
139
|
-
return new WarningDiagnostic("W_UNRESOLVED_ENDPOINT", `@${functionName} 有端点找不到对应的谱面对象;该关系将被忽略`, span);
|
|
140
|
-
},
|
|
141
|
-
ZeroTimeTrack: (span, trackOrder) => {
|
|
142
|
-
return new WarningDiagnostic("W_ZERO_TIME_TRACK", `第${trackOrder}个子轨道的时长为0`, span);
|
|
143
|
-
},
|
|
83
|
+
InvalidUnicodeEscape: (span) => new WarningDiagnostic("W_STRING_INVALID_UNICODE_ESCAPE", "无效的 Unicode 转义:请使用 \\uXXXX(四位十六进制数字);将保留该字符串原文", span),
|
|
84
|
+
UnknownFunction: (functionName, span) => new WarningDiagnostic("W_UNKNOWN_FUNCTION", "未知函数: @{fn},将被当作普通文本处理", span, { fn: functionName }),
|
|
85
|
+
TooManyPosArgs: (functionName, expected, actual, span) => new WarningDiagnostic("W_TOO_MANY_POS_ARGS", "函数 @{fn} 接收了过多的位置参数,期望 {expected} 个但得到了 {actual} 个", span, { fn: functionName, expected, actual }),
|
|
86
|
+
InvalidNumber: (value, span) => new WarningDiagnostic("W_INVALID_NUMBER", '无效的数字: "{value}";将使用默认值(若存在)', span, { value }),
|
|
87
|
+
InvalidBoolean: (value, span) => new WarningDiagnostic("W_INVALID_BOOL", '无效的布尔值: "{value}": 应为 \'true\' 或 \'false\';将使用默认值(若存在)', span, { value }),
|
|
88
|
+
InvalidLength: (value, span) => new WarningDiagnostic("W_INVALID_LENGTH", '无效的长度值: "{value}";请提供有效的长度值,例如 "10px" 或 "2em";将使用默认值(若存在)', span, { value }),
|
|
89
|
+
InvalidLengthUnit: (unit, span) => new WarningDiagnostic("W_INVALID_LENGTH_UNIT", '无效的长度单位: "{unit}";仅支持 "px" 和 "em";请检查函数参数或变量定义;将使用默认值(若存在)', span, { unit }),
|
|
90
|
+
InvalidContent: (span) => new WarningDiagnostic("W_INVALID_CONTENT", "内容解析失败,错误原因见上;将使用默认值(但往往不存在,极有可能导致错误)", span),
|
|
91
|
+
UnmatchedBrace: (span) => new WarningDiagnostic("W_UNMATCHED_BRACE", "未匹配的大括号: 缺少右大括号'}';将尝试自动修复", span),
|
|
92
|
+
LabelWithoutTarget: (labelName, span) => new WarningDiagnostic("W_LABEL_WITHOUT_TARGET", '标签 "{label}" 没有可绑定的目标;标签将被忽略', span, { label: labelName }),
|
|
93
|
+
TargetAlreadyLabeled: (newLabel, oldLabel, span) => new WarningDiagnostic("W_TARGET_ALREADY_LABELED", '目标已标注为 "{oldLabel}","{newLabel}" 将被忽略', span, { oldLabel, newLabel }),
|
|
94
|
+
UnresolvedEndpoint: (functionName, span) => new WarningDiagnostic("W_UNRESOLVED_ENDPOINT", "@{fn} 有端点找不到对应的谱面对象;该关系将被忽略", span, { fn: functionName }),
|
|
95
|
+
ZeroTimeTrack: (span, trackOrder) => new WarningDiagnostic("W_ZERO_TIME_TRACK", "第{track}个子轨道的时长为0", span, { track: trackOrder }),
|
|
144
96
|
};
|
package/dist/fraction.js
CHANGED
|
@@ -15,7 +15,8 @@ export class Fraction {
|
|
|
15
15
|
_numerator = 0;
|
|
16
16
|
_denominator = 1;
|
|
17
17
|
constructor(numerator = 0, denominator = 1) {
|
|
18
|
-
|
|
18
|
+
if (numerator !== 0 || denominator !== 1)
|
|
19
|
+
this.set(numerator, denominator);
|
|
19
20
|
}
|
|
20
21
|
get numerator() {
|
|
21
22
|
return this._numerator;
|
|
@@ -44,7 +45,7 @@ export class Fraction {
|
|
|
44
45
|
return this;
|
|
45
46
|
}
|
|
46
47
|
clone() {
|
|
47
|
-
return new Fraction(
|
|
48
|
+
return new Fraction().copyFrom(this);
|
|
48
49
|
}
|
|
49
50
|
add(value, denominator = 1) {
|
|
50
51
|
let numerator;
|
|
@@ -2,7 +2,7 @@ import type { SourceSpan, LengthValue, deSugarAtomFunction, deSugarRelationFunct
|
|
|
2
2
|
import type { CallArgumentInfo } from "../parser/grammarType.js";
|
|
3
3
|
import type { ParserContext } from "../parser/parserContext.js";
|
|
4
4
|
import type { Fraction } from "../fraction.js";
|
|
5
|
-
import type {
|
|
5
|
+
import type { TimeFlowModel } from "../lowering/types.js";
|
|
6
6
|
import type { TemporalNodeBase } from "./temporal.js";
|
|
7
7
|
import type { LoweringContext } from "../lowering/loweringContext.js";
|
|
8
8
|
import type { Track } from "../lowering/track.js";
|
|
@@ -29,7 +29,7 @@ export declare class ASTNodeBase {
|
|
|
29
29
|
* 时间求解模式
|
|
30
30
|
* loweringEnter 返回的事件已规范化并加入索引后调用,决定子节点的展开方式。
|
|
31
31
|
* 需要本轮事件时通过 ctx.getTemporalNodes 查询
|
|
32
|
-
* @returns 返回 null
|
|
32
|
+
* @returns 返回 null 表示不由通用递归器展开子元素;默认将 children 按 sequence 依次展开
|
|
33
33
|
*/
|
|
34
34
|
timeFlowModel(_ctx: LoweringContext): TimeFlowModel | null;
|
|
35
35
|
/**
|
|
@@ -54,12 +54,6 @@ export declare class ASTBraceNode extends ASTNodeBase {
|
|
|
54
54
|
content: ASTNodeBase[];
|
|
55
55
|
get children(): ASTNodeBase[];
|
|
56
56
|
constructor(sourceSpan: SourceSpan, content: ASTNodeBase[], parent?: ASTNodeBase | null);
|
|
57
|
-
innerSpan(): SourceSpan;
|
|
58
|
-
static getContentSpan(content: ASTNodeBase[] | ASTNodeBase): SourceSpan;
|
|
59
|
-
timeFlowModel(): {
|
|
60
|
-
children: ASTNodeBase[];
|
|
61
|
-
mode: "sequence";
|
|
62
|
-
};
|
|
63
57
|
toString(source: string): string;
|
|
64
58
|
}
|
|
65
59
|
export interface FunctionArgDef {
|
|
@@ -96,27 +90,9 @@ export declare class ASTFunctionNode extends ASTNodeBase {
|
|
|
96
90
|
get def(): FunctionDef | undefined;
|
|
97
91
|
get callName(): string;
|
|
98
92
|
static deSugarAtom: deSugarAtomFunction;
|
|
99
|
-
get deSugarAtom(): deSugarAtomFunction;
|
|
100
93
|
static deSugarRelation: deSugarRelationFunction;
|
|
101
|
-
get deSugarRelation(): deSugarRelationFunction;
|
|
102
94
|
getArgValue(args: FunctionArgs, ctx: ParserContext): paramValue[];
|
|
103
95
|
toString(source: string): string;
|
|
104
|
-
/**
|
|
105
|
-
* lowering 固化后的 attachment 生成钩子
|
|
106
|
-
*
|
|
107
|
-
* 普通 loweringEnter/loweringExit 在递归遍历 AST 时执行,
|
|
108
|
-
* 此时尚未完成锚点归并,事件的 t、track 和 layoutLine 不可用,因此需要观察完整事件流的功能不能在普通 hook 中可靠决定分组。
|
|
109
|
-
* 该 hook 读取 LoweringResult 并返回新建的 LoweringAttachment,LoweringContext 将返回值追加到当前文档或 fragment 的 attachments,不会把它们放入时间列或推进时间。
|
|
110
|
-
*
|
|
111
|
-
* 例:autobeam 扫描最终列,按轨道、谱面行、拍点和显式 beam 端点决定分组,再生成 BeamLayoutAttachment
|
|
112
|
-
*/
|
|
113
|
-
static loweringAugment?: LoweringAugmenter;
|
|
114
|
-
/**
|
|
115
|
-
* 所有 loweringAugment 结果追加完成后的最终处理;此时已经得到了完整的 lowering 语义
|
|
116
|
-
* 所有派生 attachment 追加完成后,按函数注册顺序处理完整结果
|
|
117
|
-
* 可以进行校验(如 beam 和 tie)或者修改某些值
|
|
118
|
-
*/
|
|
119
|
-
static loweringFinalize?: LoweringFinalizer;
|
|
120
96
|
/**
|
|
121
97
|
* 装饰函数可选的排版声明
|
|
122
98
|
* layout 引擎按 key 收集,不需要识别具体函数类
|
|
@@ -130,10 +106,5 @@ export declare class ASTFunctionNode extends ASTNodeBase {
|
|
|
130
106
|
* 只有函数节点声明 labelable,别的节点读到的必是 undefined,所以无需 instanceof
|
|
131
107
|
*/
|
|
132
108
|
static findLabelable(node: ASTNodeBase): ASTFunctionNode | null;
|
|
133
|
-
/**
|
|
134
|
-
* 用于语法糖向前结合
|
|
135
|
-
* 从指定位置向前寻找最后一个可由后缀语法糖包装的节点,跳过不产生语义对象的文本
|
|
136
|
-
*/
|
|
137
|
-
static findLastFuncContentIndex(nodes: ASTNodeBase[], i: number): number;
|
|
138
109
|
}
|
|
139
|
-
export type ASTFunctionClass = (new (sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent: ASTNodeBase | null) => ASTFunctionNode) & Pick<typeof ASTFunctionNode, "prototype" | "def" | "deSugarAtom" | "deSugarRelation" | "
|
|
110
|
+
export type ASTFunctionClass = (new (sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent: ASTNodeBase | null) => ASTFunctionNode) & Pick<typeof ASTFunctionNode, "prototype" | "def" | "deSugarAtom" | "deSugarRelation" | "layoutDecorationHandler">;
|
|
@@ -26,9 +26,12 @@ export class ASTNodeBase {
|
|
|
26
26
|
* 时间求解模式
|
|
27
27
|
* loweringEnter 返回的事件已规范化并加入索引后调用,决定子节点的展开方式。
|
|
28
28
|
* 需要本轮事件时通过 ctx.getTemporalNodes 查询
|
|
29
|
-
* @returns 返回 null
|
|
29
|
+
* @returns 返回 null 表示不由通用递归器展开子元素;默认将 children 按 sequence 依次展开
|
|
30
30
|
*/
|
|
31
|
-
timeFlowModel(_ctx) {
|
|
31
|
+
timeFlowModel(_ctx) {
|
|
32
|
+
const children = this.children;
|
|
33
|
+
return children ? { mode: "sequence", children } : null;
|
|
34
|
+
}
|
|
32
35
|
/**
|
|
33
36
|
* 离开当前层级的回调 同 loweringEnter
|
|
34
37
|
*
|
|
@@ -72,27 +75,6 @@ export class ASTBraceNode extends ASTNodeBase {
|
|
|
72
75
|
this.content = content;
|
|
73
76
|
this.content.forEach(item => item.parent = this);
|
|
74
77
|
}
|
|
75
|
-
innerSpan() {
|
|
76
|
-
return ASTBraceNode.getContentSpan(this.content);
|
|
77
|
-
}
|
|
78
|
-
// 是复制的
|
|
79
|
-
static getContentSpan(content) {
|
|
80
|
-
if (Array.isArray(content)) {
|
|
81
|
-
if (content.length === 0)
|
|
82
|
-
return { start: 0, end: 0 };
|
|
83
|
-
const firstSpan = ASTBraceNode.getContentSpan(content[0]);
|
|
84
|
-
const lastSpan = ASTBraceNode.getContentSpan(content[content.length - 1]);
|
|
85
|
-
return { start: firstSpan.start, end: lastSpan.end };
|
|
86
|
-
}
|
|
87
|
-
else
|
|
88
|
-
return { start: content.sourceSpan.start, end: content.sourceSpan.end };
|
|
89
|
-
}
|
|
90
|
-
timeFlowModel() {
|
|
91
|
-
return {
|
|
92
|
-
children: this.content,
|
|
93
|
-
mode: "sequence",
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
78
|
toString(source) {
|
|
97
79
|
return `{${this.content.map(item => item.toString(source)).join("")}}`;
|
|
98
80
|
}
|
|
@@ -134,9 +116,7 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
134
116
|
}
|
|
135
117
|
// 去糖函数 详见 ParserContext 中 deSugarAtomFns 和 deSugarRelationFns 的定义
|
|
136
118
|
static deSugarAtom = () => null; // 默认没有去糖,子类只需要定义static deSugarAtom方法即可
|
|
137
|
-
get deSugarAtom() { return this.constructor.deSugarAtom; }
|
|
138
119
|
static deSugarRelation = () => null;
|
|
139
|
-
get deSugarRelation() { return this.constructor.deSugarRelation; }
|
|
140
120
|
// 按声明顺序取值,位置编号独立于仅命名参数
|
|
141
121
|
getArgValue(args, ctx) {
|
|
142
122
|
const def = this.def;
|
|
@@ -162,22 +142,6 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
162
142
|
return super.toString(source);
|
|
163
143
|
return `@${name} `;
|
|
164
144
|
}
|
|
165
|
-
/**
|
|
166
|
-
* lowering 固化后的 attachment 生成钩子
|
|
167
|
-
*
|
|
168
|
-
* 普通 loweringEnter/loweringExit 在递归遍历 AST 时执行,
|
|
169
|
-
* 此时尚未完成锚点归并,事件的 t、track 和 layoutLine 不可用,因此需要观察完整事件流的功能不能在普通 hook 中可靠决定分组。
|
|
170
|
-
* 该 hook 读取 LoweringResult 并返回新建的 LoweringAttachment,LoweringContext 将返回值追加到当前文档或 fragment 的 attachments,不会把它们放入时间列或推进时间。
|
|
171
|
-
*
|
|
172
|
-
* 例:autobeam 扫描最终列,按轨道、谱面行、拍点和显式 beam 端点决定分组,再生成 BeamLayoutAttachment
|
|
173
|
-
*/
|
|
174
|
-
static loweringAugment;
|
|
175
|
-
/**
|
|
176
|
-
* 所有 loweringAugment 结果追加完成后的最终处理;此时已经得到了完整的 lowering 语义
|
|
177
|
-
* 所有派生 attachment 追加完成后,按函数注册顺序处理完整结果
|
|
178
|
-
* 可以进行校验(如 beam 和 tie)或者修改某些值
|
|
179
|
-
*/
|
|
180
|
-
static loweringFinalize;
|
|
181
145
|
/**
|
|
182
146
|
* 装饰函数可选的排版声明
|
|
183
147
|
* layout 引擎按 key 收集,不需要识别具体函数类
|
|
@@ -201,15 +165,4 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
201
165
|
}
|
|
202
166
|
return null;
|
|
203
167
|
}
|
|
204
|
-
/**
|
|
205
|
-
* 用于语法糖向前结合
|
|
206
|
-
* 从指定位置向前寻找最后一个可由后缀语法糖包装的节点,跳过不产生语义对象的文本
|
|
207
|
-
*/
|
|
208
|
-
static findLastFuncContentIndex(nodes, i) {
|
|
209
|
-
for (; i >= 0; i--) {
|
|
210
|
-
if (nodes[i] instanceof ASTFunctionNode || nodes[i] instanceof ASTBraceNode)
|
|
211
|
-
return i;
|
|
212
|
-
}
|
|
213
|
-
return -1;
|
|
214
|
-
}
|
|
215
168
|
}
|
|
@@ -52,38 +52,34 @@ class AdjustFunction extends ASTFunctionNode {
|
|
|
52
52
|
dw;
|
|
53
53
|
dh;
|
|
54
54
|
get children() { return [this.target]; }
|
|
55
|
-
timeFlowModel() {
|
|
56
|
-
return {
|
|
57
|
-
children: [this.target],
|
|
58
|
-
mode: "sequence",
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
55
|
/** 微调不创建时间事件,只收集作用域内的对象与关系对象 */
|
|
62
56
|
loweringEnter(ctx) {
|
|
63
|
-
const targets = { temporal: 0, attachment: 0 };
|
|
64
57
|
const group = {
|
|
65
|
-
|
|
58
|
+
temporals: 0,
|
|
59
|
+
attachments: [],
|
|
66
60
|
onTemporal: node => {
|
|
67
61
|
if (!isVisualTemporalNode(node))
|
|
68
62
|
return;
|
|
69
|
-
|
|
63
|
+
group.temporals++;
|
|
70
64
|
offsetTemporal(node, this);
|
|
71
65
|
},
|
|
72
66
|
onAttachment: attachment => {
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
targets.attachment++;
|
|
76
|
-
offsetAttachment(attachment, targets, this.dx, this.dy);
|
|
67
|
+
if (isLayoutAttachment(attachment))
|
|
68
|
+
group.attachments.push(attachment);
|
|
77
69
|
},
|
|
78
70
|
};
|
|
79
71
|
ctx.beginLoweringGroup(this, group);
|
|
80
72
|
return [];
|
|
81
73
|
}
|
|
82
74
|
loweringExit(ctx) {
|
|
83
|
-
const
|
|
84
|
-
if (
|
|
75
|
+
const { temporals, attachments } = ctx.endLoweringGroup(this);
|
|
76
|
+
if (temporals === 0 && attachments.length === 0) {
|
|
85
77
|
ctx.diagnostics.push(new WarningDiagnostic("W_ADJUST_NO_TARGET", "@adjust 没有找到可调整的对象", this.sourceSpan));
|
|
86
78
|
}
|
|
79
|
+
else if (temporals === 0 && (this.dx !== 0 || this.dy !== 0)) {
|
|
80
|
+
for (const attachment of attachments)
|
|
81
|
+
offsetAttachment(attachment, this.dx, this.dy);
|
|
82
|
+
}
|
|
87
83
|
return [];
|
|
88
84
|
}
|
|
89
85
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
@@ -102,58 +98,74 @@ class AdjustFunction extends ASTFunctionNode {
|
|
|
102
98
|
}
|
|
103
99
|
export const AdjustNode = AdjustFunction;
|
|
104
100
|
/** 在原对象上组合布局生命周期,保持布局索引依赖的对象身份 */
|
|
105
|
-
function offsetTemporal(node,
|
|
106
|
-
const { dx, dy, dw, dh } = owner;
|
|
101
|
+
function offsetTemporal(node, { dx, dy, dw, dh }) {
|
|
107
102
|
if (dx === 0 && dy === 0 && dw === 0 && dh === 0)
|
|
108
103
|
return;
|
|
104
|
+
const firstOffset = !node.placementOffset && (dx !== 0 || dy !== 0);
|
|
109
105
|
if (dx !== 0 || dy !== 0) {
|
|
110
106
|
node.placementOffset = { x: (node.placementOffset?.x ?? 0) + dx, y: (node.placementOffset?.y ?? 0) + dy };
|
|
111
107
|
}
|
|
112
108
|
let placedX;
|
|
113
109
|
let placedY;
|
|
114
110
|
const finalize = node.finalizeLayout;
|
|
115
|
-
const onPlaced = node.onPlaced;
|
|
116
111
|
node.finalizeLayout = (context) => {
|
|
117
112
|
finalize?.call(node, context);
|
|
113
|
+
placedX = placedY = undefined;
|
|
118
114
|
const width = Math.max(0, node.box.w + dw);
|
|
119
115
|
node.box.anchor = node.box.w > 0 ? node.box.anchor * width / node.box.w : 0;
|
|
120
116
|
node.box.w = width;
|
|
121
117
|
node.box.h = Math.max(0, node.box.h + dh);
|
|
122
118
|
};
|
|
119
|
+
if (!firstOffset)
|
|
120
|
+
return;
|
|
121
|
+
let { x, y } = node.box;
|
|
122
|
+
Object.defineProperties(node.box, {
|
|
123
|
+
x: {
|
|
124
|
+
get: () => x,
|
|
125
|
+
set(v) { if (v === placedX && x === placedX)
|
|
126
|
+
placedX = undefined; x = v; }
|
|
127
|
+
},
|
|
128
|
+
y: {
|
|
129
|
+
get: () => y,
|
|
130
|
+
set(v) { if (v === placedY && y === placedY)
|
|
131
|
+
placedY = undefined; y = v; }
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
const onPlaced = node.onPlaced;
|
|
123
135
|
node.onPlaced = () => {
|
|
124
|
-
if (
|
|
125
|
-
node.
|
|
126
|
-
if (
|
|
127
|
-
node.
|
|
136
|
+
if (x !== placedX)
|
|
137
|
+
x += node.placementOffset.x;
|
|
138
|
+
if (y !== placedY)
|
|
139
|
+
y += node.placementOffset.y;
|
|
128
140
|
onPlaced?.call(node);
|
|
129
|
-
placedX =
|
|
130
|
-
placedY =
|
|
141
|
+
placedX = x;
|
|
142
|
+
placedY = y;
|
|
131
143
|
};
|
|
132
144
|
}
|
|
133
145
|
/**
|
|
134
146
|
* 平移一条关系对象
|
|
135
147
|
*
|
|
136
148
|
* 在原 Attachment 上组合 createGeometry,保留依赖查找与 instanceof 所需的对象身份
|
|
137
|
-
*
|
|
149
|
+
* 仅在作用域里没有时间对象时调用,避免关系对象既跟端点走又被重复平移
|
|
138
150
|
*/
|
|
139
|
-
function offsetAttachment(attachment,
|
|
140
|
-
|
|
141
|
-
return;
|
|
151
|
+
function offsetAttachment(attachment, dx, dy) {
|
|
152
|
+
const firstOffset = !attachment.placementOffset;
|
|
142
153
|
attachment.placementOffset = { x: (attachment.placementOffset?.x ?? 0) + dx, y: (attachment.placementOffset?.y ?? 0) + dy };
|
|
154
|
+
if (!firstOffset)
|
|
155
|
+
return;
|
|
143
156
|
const createGeometry = attachment.createGeometry;
|
|
144
157
|
attachment.createGeometry = (context) => {
|
|
145
158
|
const geometry = createGeometry.call(attachment, context);
|
|
146
|
-
if (
|
|
159
|
+
if (context.layoutOnly)
|
|
147
160
|
return geometry;
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
: { x: region.x + dx, y: region.y + dy, w: region.w, h: region.h, line: region.line, track: region.track };
|
|
161
|
+
const { x, y } = attachment.placementOffset;
|
|
162
|
+
const shift = (region) => ({ ...region, x: region.x + x, y: region.y + y });
|
|
151
163
|
return {
|
|
152
164
|
regions: geometry.regions.map(shift),
|
|
153
165
|
occupancy: geometry.occupancy?.map(shift),
|
|
154
166
|
paint(painter) {
|
|
155
167
|
// attachment 的绘制大多用闭包存储了,所以只能代理 painter
|
|
156
|
-
geometry.paint(new TranslatingPainter(painter,
|
|
168
|
+
geometry.paint(new TranslatingPainter(painter, x, y));
|
|
157
169
|
},
|
|
158
170
|
};
|
|
159
171
|
};
|