jpfun 0.9.0 → 1.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/README.md +11 -16
- package/dist/diagnostic.d.ts +29 -2
- package/dist/diagnostic.js +5 -4
- package/dist/functions/arpeggio/index.js +2 -3
- package/dist/functions/bar/index.d.ts +3 -0
- package/dist/functions/bar/index.js +21 -3
- package/dist/functions/beam/layout.js +3 -2
- package/dist/functions/box/index.js +1 -0
- package/dist/functions/dash/index.js +5 -2
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +1 -1
- package/dist/functions/div/index.js +4 -2
- package/dist/functions/dyn/index.js +1 -0
- package/dist/functions/gliss/index.js +85 -9
- package/dist/functions/grace/index.d.ts +1 -8
- package/dist/functions/grace/index.js +23 -53
- package/dist/functions/head/index.js +23 -30
- package/dist/functions/key/index.js +5 -7
- package/dist/functions/meter/index.d.ts +1 -0
- package/dist/functions/meter/index.js +3 -0
- 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.d.ts +5 -2
- package/dist/functions/note/index.js +168 -25
- package/dist/functions/note/source-transform.d.ts +23 -2
- package/dist/functions/note/source-transform.js +95 -74
- package/dist/functions/ottava/index.js +1 -0
- package/dist/functions/page/index.d.ts +1 -36
- package/dist/functions/page/index.js +8 -7
- package/dist/functions/rest/index.js +8 -6
- package/dist/functions/set/index.js +2 -2
- package/dist/functions/slide/index.js +1 -0
- package/dist/functions/stack/index.js +6 -4
- package/dist/functions/temporal.d.ts +12 -3
- package/dist/functions/temporal.js +15 -2
- package/dist/functions/text/index.js +2 -2
- package/dist/functions/tie/index.js +28 -4
- package/dist/functions/tremolo/index.js +1 -0
- package/dist/functions/tuplet/index.js +1 -0
- package/dist/functions/up/index.js +12 -12
- package/dist/functions/voice/connections.d.ts +1 -1
- package/dist/functions/voice/connections.js +2 -2
- package/dist/functions/voice/lyrics.d.ts +11 -5
- package/dist/functions/voice/lyrics.js +151 -47
- package/dist/functions/voice/voice.d.ts +6 -2
- package/dist/functions/voice/voice.js +51 -19
- package/dist/functions/voice/voices.d.ts +2 -1
- package/dist/functions/voice/voices.js +4 -3
- package/dist/functions/volta/index.js +1 -0
- package/dist/index.d.ts +69 -3
- package/dist/index.js +69 -3
- package/dist/jpfun.min.js +47 -60
- package/dist/layout/engine.d.ts +4 -3
- package/dist/layout/engine.js +173 -27
- package/dist/layout/model.d.ts +13 -4
- package/dist/layout/model.js +15 -7
- package/dist/layout/page.js +2 -2
- package/dist/layout/path.js +1 -1
- package/dist/layout/types.d.ts +12 -3
- package/dist/lowering/accidentalMemory.d.ts +33 -0
- package/dist/lowering/accidentalMemory.js +84 -0
- package/dist/lowering/loweringContext.js +4 -0
- package/dist/parser/grammarType.d.ts +8 -3
- package/dist/parser/parse-utils/brace-utils.js +7 -17
- package/dist/parser/parse-utils/call-utils.d.ts +3 -1
- package/dist/parser/parse-utils/call-utils.js +17 -32
- package/dist/parser/parse-utils/note-utils.d.ts +4 -0
- package/dist/parser/parse-utils/note-utils.js +8 -12
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +1 -1
- package/dist/parser/parserContext.d.ts +32 -26
- package/dist/parser/parserContext.js +104 -194
- package/dist/parser/preprocess.d.ts +7 -0
- package/dist/parser/preprocess.js +31 -42
- package/dist/parser/syntax.d.ts +9 -0
- package/dist/parser/syntax.js +109 -0
- package/dist/pipeline.d.ts +1 -1
- package/dist/pipeline.js +3 -2
- package/dist/playback/compile.js +5 -7
- package/dist/playback/event.d.ts +1 -2
- package/dist/playback/event.js +2 -3
- package/dist/render/canvas.js +50 -35
- package/dist/render/index.d.ts +2 -1
- package/dist/render/index.js +2 -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 -1
- package/dist/render/svg.js +37 -17
- package/dist/render/text.d.ts +2 -2
- package/dist/render/text.js +7 -6
- package/dist/render/translate.d.ts +3 -1
- package/dist/render/translate.js +6 -0
- package/dist/render/types.d.ts +9 -0
- package/grammar.md +7 -5
- package/package.json +8 -18
- package/dist/converter/midi/convert.d.ts +0 -42
- package/dist/converter/midi/convert.js +0 -682
- 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 -1285
- 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 -19
- package/dist/converter/source.js +0 -53
- package/dist/core.d.ts +0 -24
- package/dist/core.js +0 -24
- package/dist/functions/voice/index.d.ts +0 -2
- package/dist/functions/voice/index.js +0 -2
- package/dist/jpfun.core.min.js +0 -132
- package/dist/jpfun.from-midi.min.js +0 -9
- package/dist/jpfun.from-musicxml.min.js +0 -12
|
@@ -4,8 +4,12 @@ export type GrammarNode = GrammarBraceNode | GrammarLabelNode | GrammarCallNode
|
|
|
4
4
|
export interface GrammarNodeBase {
|
|
5
5
|
kind: "brace" | "label" | "call" | "sugar";
|
|
6
6
|
span: SourceSpan;
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* 语法高亮类型;缺省为 operator,复合语法糖可直接给出多段 SyntaxToken
|
|
9
|
+
* 多段须按源码顺序、互不重叠,且落在 deSugarAtom 本次消费的 [start, next) 内(可超出 span,如 `H.title:` 后的正文);
|
|
10
|
+
* 解析器既不排序也不校验,违反时编辑器高亮会出错
|
|
11
|
+
*/
|
|
12
|
+
syntaxKind?: SyntaxTokenKind | SyntaxToken[];
|
|
9
13
|
}
|
|
10
14
|
export interface GrammarBraceNode extends GrammarNodeBase {
|
|
11
15
|
kind: "brace";
|
|
@@ -42,7 +46,7 @@ export interface CallInfo {
|
|
|
42
46
|
name: string;
|
|
43
47
|
/** 整个调用表达式在源码中的范围 */
|
|
44
48
|
span: SourceSpan;
|
|
45
|
-
/**
|
|
49
|
+
/** 调用名在源码中的范围,包含 `@` 前缀 */
|
|
46
50
|
nameSpan: SourceSpan;
|
|
47
51
|
/** 左括号 `(` 在源码中的范围 */
|
|
48
52
|
openParenSpan: SourceSpan;
|
|
@@ -73,6 +77,7 @@ export interface SyntaxToken {
|
|
|
73
77
|
/**
|
|
74
78
|
* 面向编辑器的源码视图
|
|
75
79
|
* tokens 用于着色,calls 保留调用与参数边界供补全定位;两者都引用原始源码 offset
|
|
80
|
+
* tokens 按起点升序且互不重叠;calls 按起点严格递增,外层调用在内层之前
|
|
76
81
|
*/
|
|
77
82
|
export interface SyntaxAnalysis {
|
|
78
83
|
tokens: SyntaxToken[];
|
|
@@ -1,29 +1,19 @@
|
|
|
1
|
+
import { findClosingQuote } from "./string-utils.js";
|
|
1
2
|
// 找到 } 的位置
|
|
2
3
|
export function readBrace(source, lBracePos, end) {
|
|
3
4
|
// 要求 source[lBracePos]==='{'(外部检验)
|
|
4
5
|
let depth = 1;
|
|
5
|
-
let quote = null;
|
|
6
|
-
let escaped = false;
|
|
7
6
|
for (let i = lBracePos + 1; i < end; i++) {
|
|
8
7
|
const ch = source[i];
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
escaped = true;
|
|
14
|
-
else if (ch === quote)
|
|
15
|
-
quote = null;
|
|
16
|
-
continue;
|
|
8
|
+
if (ch === '"') {
|
|
9
|
+
i = findClosingQuote(source, i, end);
|
|
10
|
+
if (i < 0)
|
|
11
|
+
break;
|
|
17
12
|
}
|
|
18
|
-
if (ch === '"')
|
|
19
|
-
quote = ch;
|
|
20
13
|
else if (ch === "{")
|
|
21
14
|
depth++;
|
|
22
|
-
else if (ch === "}")
|
|
23
|
-
|
|
24
|
-
if (depth === 0)
|
|
25
|
-
return i;
|
|
26
|
-
}
|
|
15
|
+
else if (ch === "}" && --depth === 0)
|
|
16
|
+
return i;
|
|
27
17
|
}
|
|
28
18
|
return -1;
|
|
29
19
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SourceSpan } from "../types.js";
|
|
2
|
-
import type { CallInfo } from "../grammarType.js";
|
|
2
|
+
import type { CallArgumentInfo, CallInfo } from "../grammarType.js";
|
|
3
3
|
/**
|
|
4
4
|
* 尝试在 `source` 的 `atPos` 位置读取一个以 `@` 开头的调用表达式,例如 `@name(...)`
|
|
5
5
|
* @param source 原始文本
|
|
@@ -17,5 +17,7 @@ import type { CallInfo } from "../grammarType.js";
|
|
|
17
17
|
* - 若找到匹配右括号,记录 closeParenSpan;否则在 end 处结束,让上层决定诊断/恢复策略
|
|
18
18
|
*/
|
|
19
19
|
export declare function readCall(source: string, atPos: number, end: number): CallInfo | null;
|
|
20
|
+
/** 实参的小写参数名;位置参数返回 undefined */
|
|
21
|
+
export declare function callArgName(source: string, arg?: CallArgumentInfo): string | undefined;
|
|
20
22
|
export declare function trimRange(source: string, start: number, end: number): SourceSpan;
|
|
21
23
|
export declare function findRightParen(source: string, start: number, end: number): number;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { findClosingQuote } from "./string-utils.js";
|
|
1
2
|
// 函数名:允许字母/下划线,以及符号别名(@/ @. @-)
|
|
2
3
|
const CALL_NAME_CHAR_RE = /[A-Za-z0-9_./-]/;
|
|
3
4
|
/**
|
|
@@ -30,8 +31,6 @@ export function readCall(source, atPos, end) {
|
|
|
30
31
|
const openPos = i;
|
|
31
32
|
let parenDepth = 1;
|
|
32
33
|
let braceDepth = 0;
|
|
33
|
-
let quote = null;
|
|
34
|
-
let escaped = false;
|
|
35
34
|
// 参数相关
|
|
36
35
|
const argInfo = [];
|
|
37
36
|
let lastCommaPos = openPos + 1;
|
|
@@ -62,22 +61,13 @@ export function readCall(source, atPos, end) {
|
|
|
62
61
|
});
|
|
63
62
|
for (i++; i < end; i++) {
|
|
64
63
|
const ch = source[i];
|
|
65
|
-
//
|
|
66
|
-
if (quote) {
|
|
67
|
-
if (escaped)
|
|
68
|
-
escaped = false;
|
|
69
|
-
else if (ch === "\\")
|
|
70
|
-
escaped = true;
|
|
71
|
-
else if (ch === quote)
|
|
72
|
-
quote = null;
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
64
|
+
// 跳过字符串;忽略大括号内的小括号 防止大括号内小括号的不闭合影响外部
|
|
75
65
|
if (ch === '"') {
|
|
76
|
-
|
|
77
|
-
|
|
66
|
+
i = findClosingQuote(source, i, end);
|
|
67
|
+
if (i < 0)
|
|
68
|
+
break;
|
|
78
69
|
}
|
|
79
|
-
|
|
80
|
-
if (ch === "{")
|
|
70
|
+
else if (ch === "{")
|
|
81
71
|
braceDepth++;
|
|
82
72
|
else if (ch === "}")
|
|
83
73
|
braceDepth = Math.max(0, braceDepth - 1);
|
|
@@ -105,6 +95,11 @@ export function readCall(source, atPos, end) {
|
|
|
105
95
|
pushArg(end);
|
|
106
96
|
return callInfo(end);
|
|
107
97
|
}
|
|
98
|
+
/** 实参的小写参数名;位置参数返回 undefined */
|
|
99
|
+
export function callArgName(source, arg) {
|
|
100
|
+
const span = arg?.nameSpan;
|
|
101
|
+
return span && source.slice(span.start, span.end).toLowerCase();
|
|
102
|
+
}
|
|
108
103
|
const SPACE_RE = /\s/;
|
|
109
104
|
export function trimRange(source, start, end) {
|
|
110
105
|
let s = start;
|
|
@@ -118,27 +113,17 @@ export function trimRange(source, start, end) {
|
|
|
118
113
|
// 不管大括号 适用于小括号里传递字符串但没加引号的情况
|
|
119
114
|
export function findRightParen(source, start, end) {
|
|
120
115
|
let parenDepth = 1;
|
|
121
|
-
let quote = null;
|
|
122
|
-
let escaped = false;
|
|
123
116
|
for (let i = start; i < end; i++) {
|
|
124
117
|
const ch = source[i];
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
escaped = true;
|
|
130
|
-
else if (ch === quote)
|
|
131
|
-
quote = null;
|
|
132
|
-
continue;
|
|
118
|
+
if (ch === '"') {
|
|
119
|
+
i = findClosingQuote(source, i, end);
|
|
120
|
+
if (i < 0)
|
|
121
|
+
break;
|
|
133
122
|
}
|
|
134
|
-
if (ch === '"')
|
|
135
|
-
quote = ch;
|
|
136
123
|
else if (ch === "(")
|
|
137
124
|
parenDepth++;
|
|
138
|
-
else if (ch === ")")
|
|
139
|
-
|
|
140
|
-
return i;
|
|
141
|
-
}
|
|
125
|
+
else if (ch === ")" && --parenDepth === 0)
|
|
126
|
+
return i;
|
|
142
127
|
}
|
|
143
128
|
return -1;
|
|
144
129
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export declare const SHARP_NOTE_NAMES: readonly ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
|
2
2
|
export declare const FLAT_NOTE_NAMES: readonly ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
|
|
3
|
+
export declare const NOTE_LETTERS = "CDEFGAB";
|
|
3
4
|
/** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
|
|
4
5
|
export declare const NoteNameMap: Record<string, number>;
|
|
5
6
|
export interface RelativeJianpuPitch {
|
|
@@ -17,4 +18,7 @@ export declare function resolveNoteMidi(name: string, acc: string, octave: numbe
|
|
|
17
18
|
* 基准位置基于已归一化的简谱级数与相对八度,不包含主体的临时升降号。
|
|
18
19
|
*/
|
|
19
20
|
export declare function createDiatonicTranspose(name: string, octave: number, keySignature: string): ((steps: number) => number) | undefined;
|
|
21
|
+
export declare function wrapPitchClass(value: number): number;
|
|
22
|
+
export declare function normalizeAccidentalOffset(value: number): number;
|
|
23
|
+
export declare function offsetToAccidental(offset: number): string;
|
|
20
24
|
export declare function resolveLetterNameToJianpu(name: string, acc: string, octave: number, keySignature: string): RelativeJianpuPitch | null;
|
|
@@ -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,7 +23,6 @@ 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
27
|
// 输入 C4 D#3 Bb3 等调性字符串,输出 MIDI 数值;格式不合法返回 null
|
|
28
28
|
// 要求非常严格:必须 [大写字母][最多一个 #b][绝对八度]
|
|
@@ -90,30 +90,26 @@ export function createDiatonicTranspose(name, octave, keySignature) {
|
|
|
90
90
|
return tonicMidi + MAJOR_SCALE_OFFSETS[((total % 7) + 7) % 7] + shift * 12;
|
|
91
91
|
};
|
|
92
92
|
}
|
|
93
|
-
function wrapPitchClass(value) {
|
|
93
|
+
export function wrapPitchClass(value) {
|
|
94
94
|
return ((value % 12) + 12) % 12;
|
|
95
95
|
}
|
|
96
|
-
function normalizeAccidentalOffset(value) {
|
|
96
|
+
export function normalizeAccidentalOffset(value) {
|
|
97
97
|
const wrapped = wrapPitchClass(value);
|
|
98
98
|
return wrapped > 6 ? wrapped - 12 : wrapped;
|
|
99
99
|
}
|
|
100
|
-
function offsetToAccidental(offset) {
|
|
101
|
-
|
|
102
|
-
return "";
|
|
103
|
-
if (offset > 0)
|
|
104
|
-
return "#".repeat(offset);
|
|
105
|
-
return "b".repeat(-offset);
|
|
100
|
+
export function offsetToAccidental(offset) {
|
|
101
|
+
return offset < 0 ? "b".repeat(-offset) : "#".repeat(offset);
|
|
106
102
|
}
|
|
107
103
|
// 把绝对字母音高转换成“相对当前调性的简谱显示信息”。
|
|
108
104
|
// 这里保留字母拼写带来的调内级数信息,因此像 key=D 时的 C 会被解释成 b7,而不是 #6。
|
|
109
105
|
export function resolveLetterNameToJianpu(name, acc, octave, keySignature) {
|
|
110
|
-
const tonicIndex =
|
|
111
|
-
const noteIndex =
|
|
106
|
+
const tonicIndex = NOTE_LETTERS.indexOf(keySignature[0]);
|
|
107
|
+
const noteIndex = NOTE_LETTERS.indexOf(name);
|
|
112
108
|
const actualMidi = resolveNoteMidi(name, acc, octave, keySignature);
|
|
113
109
|
if (tonicIndex < 0 || noteIndex < 0 || actualMidi === null)
|
|
114
110
|
return null;
|
|
115
111
|
const tonicMidi = tonality2Midi(keySignature);
|
|
116
|
-
const degreeIndex = (noteIndex - tonicIndex +
|
|
112
|
+
const degreeIndex = (noteIndex - tonicIndex + NOTE_LETTERS.length) % NOTE_LETTERS.length;
|
|
117
113
|
const expectedPitchClass = wrapPitchClass(tonicMidi + MAJOR_SCALE_OFFSETS[degreeIndex]);
|
|
118
114
|
const accidentalOffset = normalizeAccidentalOffset(wrapPitchClass(actualMidi) - expectedPitchClass);
|
|
119
115
|
const octaveOffset = (actualMidi - tonicMidi - MAJOR_SCALE_OFFSETS[degreeIndex] - accidentalOffset) / 12;
|
|
@@ -8,4 +8,4 @@ export declare function findClosingQuote(source: string, quotePos: number, end:
|
|
|
8
8
|
/** 剥掉成对的双引号(若有)并解码编码/定界级转义(`\"`、`\\`、`\uXXXX`);其余 `\X` 原样保留给具体函数解析 */
|
|
9
9
|
export declare function parseString(source: string): string | Diagnostic;
|
|
10
10
|
/** 生成双引号包裹的 jpFun 源码,仅转义反斜杠和双引号;与 JSON.stringify 不同,保留真实换行和制表符 */
|
|
11
|
-
export declare function
|
|
11
|
+
export declare function quoteSourceString(value: string): string;
|
|
@@ -34,6 +34,6 @@ export function parseString(source) {
|
|
|
34
34
|
return diagnostic ?? text;
|
|
35
35
|
}
|
|
36
36
|
/** 生成双引号包裹的 jpFun 源码,仅转义反斜杠和双引号;与 JSON.stringify 不同,保留真实换行和制表符 */
|
|
37
|
-
export function
|
|
37
|
+
export function quoteSourceString(value) {
|
|
38
38
|
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
39
39
|
}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { deSugarAtomFunction, deSugarRelationFunction, LengthValue, SourceSpan } from "./types.js";
|
|
2
2
|
import { Diagnostic } from "../diagnostic.js";
|
|
3
|
-
import { GrammarCallNode,
|
|
3
|
+
import { GrammarCallNode, GrammarCallNodeRaw, GrammarNode } from "./grammarType.js";
|
|
4
4
|
import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue } from "../functions/ASTtypes.js";
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const BOOL_RE: RegExp;
|
|
6
|
+
export declare const DEFAULT_FONT_SIZE = 18;
|
|
7
|
+
export declare const DEFAULT_NUMBER_WEIGHT = 700;
|
|
6
8
|
export declare class ParserContext {
|
|
7
9
|
/** 系统变量,供 `@set` 读取 */
|
|
8
10
|
static systemVariables: Record<string, {
|
|
9
11
|
type: paramType;
|
|
10
12
|
default: paramValue;
|
|
13
|
+
description: string;
|
|
11
14
|
}>;
|
|
12
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* 根解析器为 0,内容参数和大括号子解析器依次加一
|
|
17
|
+
* 分组只存在于 AST 路径,导致语法糖并不会和函数调用一样开辟新的层级,因此只有 0(顶层)是可信的,非零的具体数值不可依赖
|
|
18
|
+
*/
|
|
13
19
|
readonly scopeDepth: number;
|
|
14
20
|
/** 源代码 */
|
|
15
21
|
readonly source: string;
|
|
16
|
-
/** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
|
|
17
|
-
readonly syntax: SyntaxAnalysis;
|
|
18
|
-
/** syntax.tokens 前缀注释区间的扫描游标 */
|
|
19
|
-
private commentIndex;
|
|
20
22
|
/** 解析过程中产生的诊断信息,包含错误和警告 */
|
|
21
23
|
diagnostics: Diagnostic[];
|
|
22
24
|
/** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
|
|
@@ -31,7 +33,11 @@ export declare class ParserContext {
|
|
|
31
33
|
deSugarRelationFns: deSugarRelationFunction[];
|
|
32
34
|
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
33
35
|
labelableNodes: (ASTFunctionNode | null)[];
|
|
36
|
+
/** 当前正在实例化的显式函数调用节点,供构造函数按需读取原始参数区间 */
|
|
37
|
+
callNode?: GrammarCallNodeRaw;
|
|
34
38
|
nodes: ASTNodeBase[];
|
|
39
|
+
/** 等待右操作数的中缀语法糖,见 deferRightOperand */
|
|
40
|
+
private readonly pendingRight;
|
|
35
41
|
constructor(ctx: ParserContext | {
|
|
36
42
|
source: string;
|
|
37
43
|
diagnostics?: Diagnostic[];
|
|
@@ -39,7 +45,6 @@ export declare class ParserContext {
|
|
|
39
45
|
functions?: Map<string, ASTFunctionClass>;
|
|
40
46
|
labelableNodes?: (ASTFunctionNode | null)[];
|
|
41
47
|
toConsume?: ASTNodeBase[];
|
|
42
|
-
commentSpans?: SourceSpan[];
|
|
43
48
|
});
|
|
44
49
|
static getDeSugarFns(classes: Iterable<ASTFunctionClass>): [
|
|
45
50
|
deSugarAtomFunction[],
|
|
@@ -49,40 +54,41 @@ export declare class ParserContext {
|
|
|
49
54
|
setVariable(name: string, value: paramValue | undefined): void;
|
|
50
55
|
registerFunctions(functionClasses: ASTFunctionClass[]): void;
|
|
51
56
|
/**
|
|
52
|
-
* 扫描 GrammarNode 并构造 AST
|
|
53
|
-
*
|
|
57
|
+
* 扫描 GrammarNode 并构造 AST,用于最终渲染
|
|
58
|
+
* 编辑器语法视图不构造 AST,见 syntax.ts 的 collectSyntax
|
|
54
59
|
*/
|
|
55
60
|
parse(start?: number, end?: number): ASTNodeBase[];
|
|
56
|
-
/**
|
|
57
|
-
* 词法层:容错扫描 GrammarNode 并收集 syntax
|
|
58
|
-
* 用于防抖时的编辑器高亮和补全
|
|
59
|
-
*/
|
|
60
|
-
parseSyntax(start?: number, end?: number): SyntaxAnalysis;
|
|
61
|
-
private addSyntax;
|
|
62
|
-
/** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
|
|
63
|
-
private recordSyntax;
|
|
64
61
|
/**
|
|
65
62
|
* 向前寻找最后一个非文本节点下标;
|
|
66
63
|
* 若末尾是标签,则将“被标注节点..标签”原位打包为 ASTBraceNode
|
|
67
64
|
*/
|
|
68
65
|
findLastOperandIndex(): number;
|
|
69
|
-
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
66
|
+
/**
|
|
67
|
+
* 摘出末尾最后一个操作数(含其后置标签)
|
|
68
|
+
* 传入 chain 时,若该操作数恰是同一 chain 挂起合成的唯一右操作数,先合成再返回:同类中缀由此左结合
|
|
69
|
+
*/
|
|
70
|
+
takeLeftOperand(chain?: unknown): ASTNodeBase | null;
|
|
71
|
+
/**
|
|
72
|
+
* 中缀语法糖的右操作数是本层剩余部分的首个非文本节点,要等本层解析完才能确定。
|
|
73
|
+
* 这里只挂起:已有节点留作左侧,后续节点解析到新数组,由 makeNodes 收尾时交给 combine;没有右操作数时 combine 收到 undefined
|
|
74
|
+
* 默认右结合;登记 chain 后,下一个同 chain 的中缀可经 takeLeftOperand 提前合成本帧。提前合成的结果不会进入 nodes,combine 不应有登记之类的副作用
|
|
75
|
+
*/
|
|
76
|
+
deferRightOperand(combine: (right?: ASTNodeBase) => ASTNodeBase, chain?: unknown): void;
|
|
73
77
|
/**
|
|
74
78
|
* 第二轮去糖 & 函数节点创建
|
|
75
79
|
* 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
|
|
76
80
|
*/
|
|
77
|
-
makeNodes(nodes: (GrammarNode | number)[]
|
|
81
|
+
makeNodes(nodes: (GrammarNode | number)[]): void;
|
|
82
|
+
/** 追加文本,与紧邻的上一个文本节点合并;新建区间对象,避免改写语法节点的 span */
|
|
83
|
+
private pushText;
|
|
78
84
|
/**
|
|
79
85
|
* 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
|
|
80
86
|
* 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
|
|
81
87
|
* 必须将第一阶段语法糖识别放到grammar解析中,比如 `@fn()`,引号若作为语法糖会导致后面的 `@` 无效
|
|
82
|
-
*
|
|
83
|
-
*
|
|
88
|
+
* observe 在每个节点识别后、校验前收到该节点及当前上下文;返回 true 即接管该节点:其语法错误只记录诊断不抛出(保留 partial call),
|
|
89
|
+
* 供编辑器在输入途中继续工作。不传或返回 false 时照常抛出。observe 不得改变“识别出什么结构”,否则两条路径会认不同的语言
|
|
84
90
|
*/
|
|
85
|
-
parseGrammar(p: number, end: number,
|
|
91
|
+
parseGrammar(p: number, end: number, observe?: (node: GrammarNode, ctx: ParserContext) => boolean): (GrammarNode | number)[];
|
|
86
92
|
parseCallNode(callNode: GrammarCallNode): ASTFunctionNode;
|
|
87
93
|
/**
|
|
88
94
|
* 根据参数类型解析参数值,解析过程中会记录诊断信息
|