jpfun 1.0.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.
Files changed (51) hide show
  1. package/README.md +1 -1
  2. package/dist/functions/bar/index.js +3 -0
  3. package/dist/functions/default.js +2 -1
  4. package/dist/functions/gliss/index.js +81 -7
  5. package/dist/functions/grace/index.d.ts +1 -8
  6. package/dist/functions/grace/index.js +20 -48
  7. package/dist/functions/key/index.js +1 -0
  8. package/dist/functions/note/index.d.ts +5 -2
  9. package/dist/functions/note/index.js +158 -13
  10. package/dist/functions/note/source-transform.d.ts +8 -2
  11. package/dist/functions/note/source-transform.js +25 -11
  12. package/dist/functions/page/index.d.ts +1 -36
  13. package/dist/functions/page/index.js +4 -4
  14. package/dist/functions/set/index.js +2 -2
  15. package/dist/functions/stack/index.js +6 -4
  16. package/dist/functions/temporal.d.ts +3 -0
  17. package/dist/functions/text/index.js +2 -2
  18. package/dist/functions/up/index.js +9 -7
  19. package/dist/functions/voice/connections.js +2 -2
  20. package/dist/functions/voice/lyrics.d.ts +2 -0
  21. package/dist/functions/voice/lyrics.js +4 -4
  22. package/dist/functions/voice/voice.js +2 -2
  23. package/dist/functions/voice/voices.d.ts +2 -1
  24. package/dist/functions/voice/voices.js +2 -2
  25. package/dist/index.d.ts +6 -34
  26. package/dist/index.js +6 -34
  27. package/dist/jpfun.min.js +41 -41
  28. package/dist/layout/engine.js +13 -21
  29. package/dist/layout/model.d.ts +1 -1
  30. package/dist/layout/types.d.ts +2 -1
  31. package/dist/lowering/accidentalMemory.d.ts +33 -0
  32. package/dist/lowering/accidentalMemory.js +84 -0
  33. package/dist/lowering/loweringContext.js +4 -0
  34. package/dist/parser/grammarType.d.ts +6 -1
  35. package/dist/parser/parse-utils/string-utils.d.ts +1 -1
  36. package/dist/parser/parse-utils/string-utils.js +1 -1
  37. package/dist/parser/parserContext.d.ts +25 -25
  38. package/dist/parser/parserContext.js +54 -146
  39. package/dist/parser/preprocess.js +10 -23
  40. package/dist/parser/syntax.d.ts +9 -0
  41. package/dist/parser/syntax.js +109 -0
  42. package/dist/pipeline.js +3 -2
  43. package/dist/playback/compile.js +4 -6
  44. package/dist/playback/event.d.ts +1 -2
  45. package/dist/playback/event.js +2 -3
  46. package/dist/render/index.d.ts +1 -0
  47. package/dist/render/index.js +1 -0
  48. package/grammar.md +1 -0
  49. package/package.json +1 -1
  50. package/dist/functions/voice/index.d.ts +0 -2
  51. package/dist/functions/voice/index.js +0 -2
@@ -108,6 +108,7 @@ export function layoutDocument(result, context, options = {}) {
108
108
  };
109
109
  context = {
110
110
  ...context,
111
+ solverOptions: options,
111
112
  layoutSubdomain(owner, columns, root, attachments) {
112
113
  // 横排时内层子域先排版并认领关系,这里只取剩下的
113
114
  const line = layoutLocalLine(columns, context);
@@ -595,7 +596,7 @@ function layoutLocalLine(columns, context) {
595
596
  for (const node of column)
596
597
  prepareLayoutHost(node, context);
597
598
  }
598
- const line = prepareHorizontalLine(columns, columns[0][0].layoutLine, {}, true);
599
+ const line = prepareHorizontalLine(columns, columns[0][0].layoutLine, context.solverOptions, true);
599
600
  let left = Infinity;
600
601
  let right = -Infinity;
601
602
  for (const column of line.layout(Infinity)) {
@@ -670,18 +671,14 @@ function layoutSubdomainBlock(columns, { width, view }, root, attachments, conte
670
671
  return { height: solved.height, positions, items };
671
672
  }
672
673
  /**
673
- * 关系附件按同时包含全部端点的复合体分组,由内到外逐层登记
674
+ * 关系附件按首端点所在的各层复合体建立候选索引,由内到外逐层登记
674
675
  *
675
- * 端点都在正文的附件不进分组;子域从这里认领端点全部落在自己成员上的关系,不论它写在哪里
676
+ * 端点都在正文的附件不进索引;子域从这里取候选,再认领端点全部落在自己成员上的关系,不论它写在哪里
676
677
  */
677
678
  function groupRelations(attachments) {
678
679
  const groups = new Map();
679
680
  for (const attachment of attachments) {
680
- const [first, ...rest] = attachment.endPoints ?? [];
681
- for (let owner = first?.foldedInto; owner; owner = owner.foldedInto) {
682
- const candidate = owner;
683
- if (!rest.every(point => encloses(candidate, point)))
684
- continue;
681
+ for (let owner = attachment.endPoints?.[0]?.foldedInto; owner; owner = owner.foldedInto) {
685
682
  const group = groups.get(owner);
686
683
  if (group)
687
684
  group.push(attachment);
@@ -691,13 +688,6 @@ function groupRelations(attachments) {
691
688
  }
692
689
  return groups;
693
690
  }
694
- function encloses(owner, node) {
695
- for (let host = node.foldedInto; host; host = host.foldedInto) {
696
- if (host === owner)
697
- return true;
698
- }
699
- return false;
700
- }
701
691
  /** 子域是刚性块:按参照成员的位移平移块内测得的几何 */
702
692
  function moveSubdomainItem(item, layoutOnly = false) {
703
693
  const measured = layoutOnly ? item.layout ?? item : item;
@@ -831,10 +821,9 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
831
821
  if (host.box.x + host.box.w > right)
832
822
  right = host.box.x + host.box.w;
833
823
  }
834
- const decoration = decorationExtents.get(host);
835
- const top = decoration?.top ?? 0;
824
+ const { top, height } = hostSpan(host);
836
825
  const hostTop = host.box.y + top - baseContext.getVisualAxis(line, host.track);
837
- include(host.track, hostTop, hostTop + (host.box.h - top + (decoration?.bottom ?? 0)));
826
+ include(host.track, hostTop, hostTop + height);
838
827
  }
839
828
  }
840
829
  for (const range of occupancy[line] ?? []) {
@@ -920,11 +909,14 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
920
909
  });
921
910
  return { items, needsRelayout };
922
911
  }
923
- /** 固有占用只需轴局部边界,不必创建全局矩形 */
924
- function includeHostExtent(extents, host) {
912
+ /** 主体完整占用相对盒顶的上沿与高度;调用方各自换算到轴局部或行坐标,不必创建全局矩形 */
913
+ function hostSpan(host) {
925
914
  const decoration = decorationExtents.get(host);
926
915
  const top = decoration?.top ?? 0;
927
- const height = host.box.h - top + (decoration?.bottom ?? 0);
916
+ return { top, height: host.box.h - top + (decoration?.bottom ?? 0) };
917
+ }
918
+ function includeHostExtent(extents, host) {
919
+ const { top, height } = hostSpan(host);
928
920
  const axisTop = top - host.box.visualAxis;
929
921
  includeTrackExtent(extents, host.track, axisTop, axisTop + height);
930
922
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 基于弹簧的有时长物体排版模型 原理参考 apps/docs/src/content/docs/docs/developer/layout.md
2
+ * 基于弹簧的有时长物体排版模型 原理参考 apps/docs/src/content/docs/docs/developer/layout-algorithm.mdx
3
3
  * 基本使用: 借助 layoutElement() 构建 LayoutElement[][],调用 layout() 函数进行排版
4
4
  */
5
5
  import type { HorizontalSpringConfig, LayoutBox, TimeLineEvent } from "./types.js";
@@ -3,7 +3,7 @@ import type { Extent, Track } from "../lowering/track.js";
3
3
  import type { Fraction } from "../fraction.js";
4
4
  import type { TemporalNodeBase, VisualTemporalNode } from "../functions/temporal.js";
5
5
  import type { SourceSpan } from "../parser/types.js";
6
- import type { HorizontalLayoutHook } from "./model.js";
6
+ import type { HorizontalLayoutHook, SolverOptions } from "./model.js";
7
7
  import type { LoweringAttachment, LoweringContent, PageConfig } from "../lowering/types.js";
8
8
  export type { Extent, PageConfig };
9
9
  /** 轴对齐矩形,是所有排版几何的公共部分 */
@@ -59,6 +59,7 @@ export interface LayoutPrepareContext {
59
59
  * 由 layoutDocument 在主体准备阶段注入;调用方创建的上下文没有它。
60
60
  */
61
61
  layoutSubdomain?(owner: TemporalNodeBase, columns: readonly (readonly VisualTemporalNode[])[], root: Track, attachments: readonly LoweringAttachment[]): SubdomainLayout;
62
+ solverOptions?: SolverOptions;
62
63
  }
63
64
  /** 子域块的尺寸与成员位置,坐标以块的左上角为原点 */
64
65
  export interface SubdomainLayout {
@@ -0,0 +1,33 @@
1
+ import type { Track } from "./track.js";
2
+ /**
3
+ * 一个音在记忆中的位置:key 为「级数:八度」;bar 只作小节的身份;seq 是读谱顺序;
4
+ * prior 是读到的半音偏移。升降半音补记时按 (bar, key) 分组,按 seq 找第一个
5
+ */
6
+ export interface AccidentalReading {
7
+ key: string;
8
+ bar: object;
9
+ seq: number;
10
+ prior: number;
11
+ }
12
+ /**
13
+ * 小节内变音记忆,作用按时间先后:后发生的音读得到先发生的写入,同一时刻的音互不影响。
14
+ * 处理顺序不等于时间顺序(如 ^ 从后往前处理),所以一列里的读写和清空先登记,列末按时刻结算
15
+ */
16
+ export declare class AccidentalMemory {
17
+ private readonly memory;
18
+ private readonly events;
19
+ private readonly staged;
20
+ private readonly now;
21
+ private reads;
22
+ /** 在列内时刻 time 处理 run。一列默认都在时刻 0,倚音成员在宿主之前或之后;嵌套时逐级细分,缺省的级为 0 */
23
+ at<T>(time: number, run: () => T): T;
24
+ /** 登记一个音:结算时读到前文,resolve 返回要写入的半音偏移,不写返回 undefined */
25
+ read(track: Track, key: string, resolve: (reading: AccidentalReading) => number | undefined): void;
26
+ /** 小节线清本轨及其子孙轨道(stack 的分支轨道跨小节复用);转调不传 track,全部清空 */
27
+ clear(track?: Track): void;
28
+ /** 列末按时刻结算;同一时刻按处理顺序执行 */
29
+ commit(): void;
30
+ private flush;
31
+ /** 当前小节的记忆;清空后会换成新的 Map,因此也作为小节的身份 */
32
+ private scope;
33
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * 小节内变音记忆,作用按时间先后:后发生的音读得到先发生的写入,同一时刻的音互不影响。
3
+ * 处理顺序不等于时间顺序(如 ^ 从后往前处理),所以一列里的读写和清空先登记,列末按时刻结算
4
+ */
5
+ export class AccidentalMemory {
6
+ memory = new Map(); // 每个声部本小节的变音:声部轨道 → (键 → 半音偏移,# 为 +1、n 为 0)
7
+ events = []; // 本列登记的读写与清空
8
+ staged = []; // 当前时刻的写入,时刻推进时才提交
9
+ now = [];
10
+ reads = 0;
11
+ /** 在列内时刻 time 处理 run。一列默认都在时刻 0,倚音成员在宿主之前或之后;嵌套时逐级细分,缺省的级为 0 */
12
+ at(time, run) {
13
+ this.now.push(time);
14
+ try {
15
+ return run();
16
+ }
17
+ finally {
18
+ this.now.pop();
19
+ }
20
+ }
21
+ /** 登记一个音:结算时读到前文,resolve 返回要写入的半音偏移,不写返回 undefined */
22
+ read(track, key, resolve) {
23
+ const owner = voiceOf(track);
24
+ this.events.push({ time: [...this.now], run: () => {
25
+ const bar = this.scope(owner);
26
+ const offset = resolve({ key, bar, seq: this.reads++, prior: bar.get(key) ?? 0 });
27
+ if (offset !== undefined)
28
+ this.staged.push([owner, key, offset]);
29
+ } });
30
+ }
31
+ /** 小节线清本轨及其子孙轨道(stack 的分支轨道跨小节复用);转调不传 track,全部清空 */
32
+ clear(track) {
33
+ this.events.push({ time: [...this.now], run: () => {
34
+ if (!track)
35
+ return this.memory.clear();
36
+ for (const owner of this.memory.keys())
37
+ if (within(owner, track))
38
+ this.memory.delete(owner);
39
+ } });
40
+ }
41
+ /** 列末按时刻结算;同一时刻按处理顺序执行 */
42
+ commit() {
43
+ const events = this.events.splice(0).sort((a, b) => compareTime(a.time, b.time));
44
+ events.forEach(({ time, run }, i) => {
45
+ if (i > 0 && compareTime(events[i - 1].time, time))
46
+ this.flush();
47
+ run();
48
+ });
49
+ this.flush();
50
+ }
51
+ flush() {
52
+ for (const [owner, key, offset] of this.staged.splice(0))
53
+ this.scope(owner).set(key, offset);
54
+ }
55
+ /** 当前小节的记忆;清空后会换成新的 Map,因此也作为小节的身份 */
56
+ scope(owner) {
57
+ let map = this.memory.get(owner);
58
+ if (!map)
59
+ this.memory.set(owner, map = new Map());
60
+ return map;
61
+ }
62
+ }
63
+ /** 列内时刻逐级比较,缺省的级视为 0 */
64
+ function compareTime(a, b) {
65
+ for (let i = 0; i < a.length || i < b.length; i++) {
66
+ const diff = (a[i] ?? 0) - (b[i] ?? 0);
67
+ if (diff)
68
+ return diff;
69
+ }
70
+ return 0;
71
+ }
72
+ /** 记忆按声部归属:分组里的分支各成声部;不在父轨分组里的子轨(如倚音的私有根轨)只是排版子域,延续父轨的声部 */
73
+ function voiceOf(track) {
74
+ while (track.parent && !track.parent.groups.some(group => group.members.includes(track)))
75
+ track = track.parent;
76
+ return track;
77
+ }
78
+ function within(track, root) {
79
+ for (let current = track; current; current = current.parent) {
80
+ if (current === root)
81
+ return true;
82
+ }
83
+ return false;
84
+ }
@@ -2,6 +2,7 @@ import { Diagnostic } from "../diagnostic.js";
2
2
  import { Fraction } from "../fraction.js";
3
3
  import { ANCHOR_KEY, DEFAULT_BPM, DEFAULT_PROGRAM, DEFAULT_TONALITY, DEFAULT_VELOCITY, } from "../functions/temporal.js";
4
4
  import { Track } from "./track.js";
5
+ import { AccidentalMemory } from "./accidentalMemory.js";
5
6
  /**
6
7
  * 把 AST 展开并固化为时间列、attachment 和页面配置
7
8
  *
@@ -94,6 +95,7 @@ export class LoweringContext {
94
95
  velocity: DEFAULT_VELOCITY,
95
96
  keySignature: DEFAULT_TONALITY,
96
97
  program: DEFAULT_PROGRAM,
98
+ accidentals: new AccidentalMemory(),
97
99
  };
98
100
  // 力度和音色按音轨各自流动,新分叉的声部继承分叉处父轨的值
99
101
  const velocities = new Map();
@@ -115,6 +117,8 @@ export class LoweringContext {
115
117
  velocities.set(node.track, state.velocity);
116
118
  programs.set(node.track, state.program);
117
119
  }
120
+ // 整列登记完再按时间先后结算变音,同一时刻的音互不影响
121
+ state.accidentals.commit();
118
122
  // 同列声部一起开始,只在整列固化后累加一次偏移
119
123
  if (insertion)
120
124
  timeOffset.add(insertion);
@@ -4,7 +4,11 @@ export type GrammarNode = GrammarBraceNode | GrammarLabelNode | GrammarCallNode
4
4
  export interface GrammarNodeBase {
5
5
  kind: "brace" | "label" | "call" | "sugar";
6
6
  span: SourceSpan;
7
- /** 语法高亮类型;缺省为 operator,复合语法糖可直接给出多段 SyntaxToken */
7
+ /**
8
+ * 语法高亮类型;缺省为 operator,复合语法糖可直接给出多段 SyntaxToken
9
+ * 多段须按源码顺序、互不重叠,且落在 deSugarAtom 本次消费的 [start, next) 内(可超出 span,如 `H.title:` 后的正文);
10
+ * 解析器既不排序也不校验,违反时编辑器高亮会出错
11
+ */
8
12
  syntaxKind?: SyntaxTokenKind | SyntaxToken[];
9
13
  }
10
14
  export interface GrammarBraceNode extends GrammarNodeBase {
@@ -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[];
@@ -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 quote(value: string): string;
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 quote(value) {
37
+ export function quoteSourceString(value) {
38
38
  return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
39
39
  }
@@ -1,7 +1,8 @@
1
1
  import { deSugarAtomFunction, deSugarRelationFunction, LengthValue, SourceSpan } from "./types.js";
2
2
  import { Diagnostic } from "../diagnostic.js";
3
- import { GrammarCallNode, GrammarCallNodeRaw, GrammarNode, type SyntaxAnalysis } from "./grammarType.js";
3
+ import { GrammarCallNode, GrammarCallNodeRaw, GrammarNode } from "./grammarType.js";
4
4
  import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue } from "../functions/ASTtypes.js";
5
+ export declare const BOOL_RE: RegExp;
5
6
  export declare const DEFAULT_FONT_SIZE = 18;
6
7
  export declare const DEFAULT_NUMBER_WEIGHT = 700;
7
8
  export declare class ParserContext {
@@ -11,14 +12,13 @@ export declare class ParserContext {
11
12
  default: paramValue;
12
13
  description: string;
13
14
  }>;
14
- /** 根解析器为 0,内容参数和大括号子解析器依次加一 */
15
+ /**
16
+ * 根解析器为 0,内容参数和大括号子解析器依次加一
17
+ * 分组只存在于 AST 路径,导致语法糖并不会和函数调用一样开辟新的层级,因此只有 0(顶层)是可信的,非零的具体数值不可依赖
18
+ */
15
19
  readonly scopeDepth: number;
16
20
  /** 源代码 */
17
21
  readonly source: string;
18
- /** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
19
- readonly syntax: SyntaxAnalysis;
20
- /** syntax.tokens 前缀注释区间的扫描游标 */
21
- private commentIndex;
22
22
  /** 解析过程中产生的诊断信息,包含错误和警告 */
23
23
  diagnostics: Diagnostic[];
24
24
  /** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
@@ -36,6 +36,8 @@ export declare class ParserContext {
36
36
  /** 当前正在实例化的显式函数调用节点,供构造函数按需读取原始参数区间 */
37
37
  callNode?: GrammarCallNodeRaw;
38
38
  nodes: ASTNodeBase[];
39
+ /** 等待右操作数的中缀语法糖,见 deferRightOperand */
40
+ private readonly pendingRight;
39
41
  constructor(ctx: ParserContext | {
40
42
  source: string;
41
43
  diagnostics?: Diagnostic[];
@@ -43,7 +45,6 @@ export declare class ParserContext {
43
45
  functions?: Map<string, ASTFunctionClass>;
44
46
  labelableNodes?: (ASTFunctionNode | null)[];
45
47
  toConsume?: ASTNodeBase[];
46
- commentSpans?: SourceSpan[];
47
48
  });
48
49
  static getDeSugarFns(classes: Iterable<ASTFunctionClass>): [
49
50
  deSugarAtomFunction[],
@@ -53,42 +54,41 @@ export declare class ParserContext {
53
54
  setVariable(name: string, value: paramValue | undefined): void;
54
55
  registerFunctions(functionClasses: ASTFunctionClass[]): void;
55
56
  /**
56
- * 扫描 GrammarNode 并构造 AST;不产出 syntax
57
- * 用于最终渲染
57
+ * 扫描 GrammarNode 并构造 AST,用于最终渲染
58
+ * 编辑器语法视图不构造 AST,见 syntax.ts 的 collectSyntax
58
59
  */
59
60
  parse(start?: number, end?: number): ASTNodeBase[];
60
- /**
61
- * 词法层:容错扫描 GrammarNode 并收集 syntax
62
- * 用于防抖时的编辑器高亮和补全
63
- */
64
- parseSyntax(start?: number, end?: number): SyntaxAnalysis;
65
- private addSyntax;
66
- /** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
67
- private recordSyntax;
68
61
  /**
69
62
  * 向前寻找最后一个非文本节点下标;
70
63
  * 若末尾是标签,则将“被标注节点..标签”原位打包为 ASTBraceNode
71
64
  */
72
65
  findLastOperandIndex(): number;
73
- /** 摘出末尾最后一个操作数(含其后置标签) */
74
- takeLeftOperand(): ASTNodeBase | null;
75
- /** 解析右侧剩余序列,将首个非文本节点交给 combine 合成后连同后续节点写回 this.nodes */
76
- consumeRightOperand(nodes: (GrammarNode | number)[], at: number, combine: (right: ASTNodeBase) => ASTNodeBase): number | null;
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;
77
77
  /**
78
78
  * 第二轮去糖 & 函数节点创建
79
79
  * 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
80
80
  */
81
- makeNodes(nodes: (GrammarNode | number)[], i?: number, end?: number): number;
81
+ makeNodes(nodes: (GrammarNode | number)[]): void;
82
82
  /** 追加文本,与紧邻的上一个文本节点合并;新建区间对象,避免改写语法节点的 span */
83
83
  private pushText;
84
84
  /**
85
85
  * 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
86
86
  * 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
87
87
  * 必须将第一阶段语法糖识别放到grammar解析中,比如 `@fn()`,引号若作为语法糖会导致后面的 `@` 无效
88
- * syntaxOnly 是词法层模式:用户语法错误只记录诊断不抛出(保留 partial call),并顺带记录 syntax;
89
- * 它不得改变“识别出什么结构”,否则两条路径会认不同的语言
88
+ * observe 在每个节点识别后、校验前收到该节点及当前上下文;返回 true 即接管该节点:其语法错误只记录诊断不抛出(保留 partial call),
89
+ * 供编辑器在输入途中继续工作。不传或返回 false 时照常抛出。observe 不得改变“识别出什么结构”,否则两条路径会认不同的语言
90
90
  */
91
- parseGrammar(p: number, end: number, syntaxOnly?: boolean): (GrammarNode | number)[];
91
+ parseGrammar(p: number, end: number, observe?: (node: GrammarNode, ctx: ParserContext) => boolean): (GrammarNode | number)[];
92
92
  parseCallNode(callNode: GrammarCallNode): ASTFunctionNode;
93
93
  /**
94
94
  * 根据参数类型解析参数值,解析过程中会记录诊断信息