jpfun 0.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 (104) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +134 -0
  3. package/dist/diagnostic.d.ts +27 -0
  4. package/dist/diagnostic.js +141 -0
  5. package/dist/functions/ASTtypes.d.ts +115 -0
  6. package/dist/functions/ASTtypes.js +187 -0
  7. package/dist/functions/bar/index.d.ts +2 -0
  8. package/dist/functions/bar/index.js +180 -0
  9. package/dist/functions/beam/auto.d.ts +4 -0
  10. package/dist/functions/beam/auto.js +202 -0
  11. package/dist/functions/beam/index.d.ts +2 -0
  12. package/dist/functions/beam/index.js +54 -0
  13. package/dist/functions/beam/layout.d.ts +9 -0
  14. package/dist/functions/beam/layout.js +233 -0
  15. package/dist/functions/box/index.d.ts +2 -0
  16. package/dist/functions/box/index.js +202 -0
  17. package/dist/functions/br/index.d.ts +2 -0
  18. package/dist/functions/br/index.js +83 -0
  19. package/dist/functions/dash/index.d.ts +2 -0
  20. package/dist/functions/dash/index.js +61 -0
  21. package/dist/functions/default.d.ts +2 -0
  22. package/dist/functions/default.js +32 -0
  23. package/dist/functions/div/index.d.ts +50 -0
  24. package/dist/functions/div/index.js +190 -0
  25. package/dist/functions/dot/index.d.ts +2 -0
  26. package/dist/functions/dot/index.js +160 -0
  27. package/dist/functions/grace/index.d.ts +84 -0
  28. package/dist/functions/grace/index.js +464 -0
  29. package/dist/functions/key/index.d.ts +2 -0
  30. package/dist/functions/key/index.js +40 -0
  31. package/dist/functions/note/accidentals.d.ts +10 -0
  32. package/dist/functions/note/accidentals.js +60 -0
  33. package/dist/functions/note/index.d.ts +2 -0
  34. package/dist/functions/note/index.js +284 -0
  35. package/dist/functions/note/noteNameFSM.d.ts +8 -0
  36. package/dist/functions/note/noteNameFSM.js +192 -0
  37. package/dist/functions/over/index.d.ts +2 -0
  38. package/dist/functions/over/index.js +307 -0
  39. package/dist/functions/page/index.d.ts +20 -0
  40. package/dist/functions/page/index.js +63 -0
  41. package/dist/functions/set/index.d.ts +2 -0
  42. package/dist/functions/set/index.js +55 -0
  43. package/dist/functions/stack/index.d.ts +2 -0
  44. package/dist/functions/stack/index.js +164 -0
  45. package/dist/functions/tempo/index.d.ts +2 -0
  46. package/dist/functions/tempo/index.js +37 -0
  47. package/dist/functions/text/index.d.ts +2 -0
  48. package/dist/functions/text/index.js +65 -0
  49. package/dist/functions/tie/index.d.ts +2 -0
  50. package/dist/functions/tie/index.js +301 -0
  51. package/dist/functions/voice/index.d.ts +3 -0
  52. package/dist/functions/voice/index.js +714 -0
  53. package/dist/index.d.ts +17 -0
  54. package/dist/index.js +17 -0
  55. package/dist/layout/default.d.ts +12 -0
  56. package/dist/layout/default.js +27 -0
  57. package/dist/layout/engine.d.ts +47 -0
  58. package/dist/layout/engine.js +410 -0
  59. package/dist/layout/model.d.ts +73 -0
  60. package/dist/layout/model.js +422 -0
  61. package/dist/layout/page.d.ts +27 -0
  62. package/dist/layout/page.js +121 -0
  63. package/dist/layout/types.d.ts +197 -0
  64. package/dist/layout/types.js +1 -0
  65. package/dist/lowering/loweringContext.d.ts +126 -0
  66. package/dist/lowering/loweringContext.js +625 -0
  67. package/dist/lowering/track.d.ts +61 -0
  68. package/dist/lowering/track.js +35 -0
  69. package/dist/lowering/types.d.ts +176 -0
  70. package/dist/lowering/types.js +82 -0
  71. package/dist/parser/grammarType.d.ts +81 -0
  72. package/dist/parser/grammarType.js +1 -0
  73. package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
  74. package/dist/parser/parse-utils/brace-utils.js +29 -0
  75. package/dist/parser/parse-utils/call-utils.d.ts +24 -0
  76. package/dist/parser/parse-utils/call-utils.js +178 -0
  77. package/dist/parser/parse-utils/label-utils.d.ts +4 -0
  78. package/dist/parser/parse-utils/label-utils.js +17 -0
  79. package/dist/parser/parse-utils/length-utils.d.ts +3 -0
  80. package/dist/parser/parse-utils/length-utils.js +20 -0
  81. package/dist/parser/parse-utils/note-utils.d.ts +10 -0
  82. package/dist/parser/parse-utils/note-utils.js +111 -0
  83. package/dist/parser/parserContext.d.ts +93 -0
  84. package/dist/parser/parserContext.js +542 -0
  85. package/dist/parser/preprocess.d.ts +6 -0
  86. package/dist/parser/preprocess.js +193 -0
  87. package/dist/parser/types.d.ts +31 -0
  88. package/dist/parser/types.js +2 -0
  89. package/dist/pipeline.d.ts +36 -0
  90. package/dist/pipeline.js +51 -0
  91. package/dist/render/canvas.d.ts +19 -0
  92. package/dist/render/canvas.js +121 -0
  93. package/dist/render/index.d.ts +5 -0
  94. package/dist/render/index.js +5 -0
  95. package/dist/render/recording.d.ts +46 -0
  96. package/dist/render/recording.js +76 -0
  97. package/dist/render/svg.d.ts +18 -0
  98. package/dist/render/svg.js +126 -0
  99. package/dist/render/text.d.ts +6 -0
  100. package/dist/render/text.js +17 -0
  101. package/dist/render/types.d.ts +86 -0
  102. package/dist/render/types.js +1 -0
  103. package/grammar.md +123 -0
  104. package/package.json +58 -0
@@ -0,0 +1,111 @@
1
+ import { Diagnostic } from "../../diagnostic.js";
2
+ export const NoteNameMap = {
3
+ "C": 0,
4
+ "C#": 1, "Db": 1,
5
+ "D": 2,
6
+ "D#": 3, "Eb": 3,
7
+ "E": 4,
8
+ "F": 5,
9
+ "F#": 6, "Gb": 6,
10
+ "G": 7,
11
+ "G#": 8, "Ab": 8,
12
+ "A": 9,
13
+ "A#": 10, "Bb": 10,
14
+ "B": 11,
15
+ "1": 0,
16
+ "2": 2,
17
+ "3": 4,
18
+ "4": 5,
19
+ "5": 7,
20
+ "6": 9,
21
+ "7": 11
22
+ };
23
+ const LETTER_ORDER = ["C", "D", "E", "F", "G", "A", "B"];
24
+ const MAJOR_SCALE_OFFSETS = [0, 2, 4, 5, 7, 9, 11];
25
+ // 输入 C4 D#3 B3b G, A 等音符字符串,输出 MIDI 数值
26
+ // 要求非常严格:必须 [大写字母][升降号#bn只能一个][绝对八度]
27
+ export function tonality2Midi(tonality, defaultOctave) {
28
+ // 解析音名
29
+ const note = tonality[0];
30
+ let acc = "";
31
+ let i = 1;
32
+ // 解析升降号
33
+ if (i < tonality.length && (tonality[i] === '#' || tonality[i] === 'b' || tonality[i] === 'n')) {
34
+ acc = tonality[i];
35
+ i++;
36
+ }
37
+ // 组合音名和升降号
38
+ let noteValue = NoteNameMap[note + acc];
39
+ if (noteValue === undefined)
40
+ throw Diagnostic.error.InvalidTonality(tonality, { start: 0, end: i });
41
+ // 解析八度
42
+ if (i < tonality.length) {
43
+ defaultOctave = parseInt(tonality.slice(i), 10);
44
+ if (isNaN(defaultOctave))
45
+ throw Diagnostic.error.InvalidTonality(tonality, { start: i, end: tonality.length });
46
+ }
47
+ return (defaultOctave + 1) * 12 + noteValue;
48
+ }
49
+ // ignoreNatural: 是否忽略还原号对升降号的重置作用。默认为true,即默认自然音符不重置acc
50
+ // initOffset: 该小节累加的升降号偏移量 仅在ignoreNatural=false时有意义
51
+ export function acc2Offset(acc, ignoreNatural = true, initOffset = 0) {
52
+ for (const ch of acc) {
53
+ if (ch === "#")
54
+ initOffset++;
55
+ else if (ch === "b")
56
+ initOffset--;
57
+ else if (ch === "n" && !ignoreNatural)
58
+ initOffset = 0;
59
+ }
60
+ return initOffset;
61
+ }
62
+ export function resolveNoteMidi(name, acc, octave, keySignature) {
63
+ const noteValue = NoteNameMap[name];
64
+ if (noteValue === undefined)
65
+ return null;
66
+ const offset = acc2Offset(acc);
67
+ if (name >= "1" && name <= "7") {
68
+ return tonality2Midi(keySignature, 4) + noteValue + offset + octave * 12;
69
+ }
70
+ return (octave + 1) * 12 + noteValue + offset;
71
+ }
72
+ function wrapPitchClass(value) {
73
+ return ((value % 12) + 12) % 12;
74
+ }
75
+ function normalizeAccidentalOffset(value) {
76
+ const wrapped = wrapPitchClass(value);
77
+ return wrapped > 6 ? wrapped - 12 : wrapped;
78
+ }
79
+ function offsetToAccidental(offset) {
80
+ if (offset === 0)
81
+ return "";
82
+ if (offset > 0)
83
+ return "#".repeat(offset);
84
+ return "b".repeat(-offset);
85
+ }
86
+ // 把绝对字母音高转换成“相对当前调性的简谱显示信息”。
87
+ // 这里保留字母拼写带来的调内级数信息,因此像 key=D 时的 C 会被解释成 b7,而不是 #6。
88
+ export function resolveLetterNameToJianpu(name, acc, octave, keySignature) {
89
+ const tonicLetter = keySignature[0];
90
+ const tonicMidi = tonality2Midi(keySignature, 4);
91
+ const tonicPitchClass = wrapPitchClass(tonicMidi);
92
+ const notePitchClass = wrapPitchClass(resolveNoteMidi(name, acc, octave, keySignature) ?? NaN);
93
+ const tonicIndex = LETTER_ORDER.indexOf(tonicLetter);
94
+ const noteIndex = LETTER_ORDER.indexOf(name);
95
+ if (tonicIndex < 0 || noteIndex < 0 || Number.isNaN(notePitchClass)) {
96
+ return null;
97
+ }
98
+ const degreeIndex = (noteIndex - tonicIndex + LETTER_ORDER.length) % LETTER_ORDER.length;
99
+ const expectedPitchClass = wrapPitchClass(tonicPitchClass + MAJOR_SCALE_OFFSETS[degreeIndex]);
100
+ const accidentalOffset = normalizeAccidentalOffset(notePitchClass - expectedPitchClass);
101
+ const actualMidi = resolveNoteMidi(name, acc, octave, keySignature);
102
+ if (actualMidi === null) {
103
+ return null;
104
+ }
105
+ const octaveOffset = (actualMidi - tonicMidi - MAJOR_SCALE_OFFSETS[degreeIndex] - accidentalOffset) / 12;
106
+ return {
107
+ renderName: String(degreeIndex + 1),
108
+ renderAcc: offsetToAccidental(accidentalOffset),
109
+ renderOctave: octaveOffset,
110
+ };
111
+ }
@@ -0,0 +1,93 @@
1
+ import { deSugarAtomFunction, deSugarRelationFunction, LengthValue, SourceSpan } from "./types.js";
2
+ import { Diagnostic } from "../diagnostic.js";
3
+ import { GrammarCallNode, GrammarNode, type SyntaxAnalysis } from "./grammarType.js";
4
+ import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue } from "../functions/ASTtypes.js";
5
+ export declare const DEFAULT_FONT_SIZE = 22;
6
+ export declare const DEFAULT_OCTAVE = 4;
7
+ export declare class ParserContext {
8
+ /** 根解析器为 0,内容参数和大括号子解析器依次加一 */
9
+ readonly scopeDepth: number;
10
+ /** 源代码 */
11
+ readonly source: string;
12
+ /** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
13
+ readonly syntax: SyntaxAnalysis;
14
+ /** 解析过程中产生的诊断信息,包含错误和警告 */
15
+ diagnostics: Diagnostic[];
16
+ /** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
17
+ variables: Record<string, any>;
18
+ /** 文档级声明(只准有一个)在所有子解析器之间共享的变量 */
19
+ documentDeclarations: Record<string, any>;
20
+ /** 函数定义查找表 */
21
+ functions: Map<string, ASTFunctionClass>;
22
+ /** 语法糖识别函数列表,分为两轮:第一轮原子级去糖:将文本转为函数调用,或特殊标记 */
23
+ deSugarAtomFns: deSugarAtomFunction[];
24
+ /** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
25
+ deSugarRelationFns: deSugarRelationFunction[];
26
+ /** .labelable() 返回的承载节点会被加入其中,供标签绑定使用 */
27
+ labelableNodes: ASTFunctionNode[];
28
+ nodes: ASTNodeBase[];
29
+ constructor(ctx: ParserContext | {
30
+ source: string;
31
+ diagnostics?: Diagnostic[];
32
+ variables?: Map<string, any>;
33
+ functions?: Map<string, ASTFunctionClass>;
34
+ labelableNodes?: ASTFunctionNode[];
35
+ toConsume?: ASTNodeBase[];
36
+ commentSpans?: SourceSpan[];
37
+ });
38
+ static getDeSugarFns(classes: Iterable<ASTFunctionClass>): [
39
+ deSugarAtomFunction[],
40
+ deSugarRelationFunction[]
41
+ ];
42
+ pushNode(node: ASTNodeBase): void;
43
+ get fontSize(): number;
44
+ set fontSize(size: number | string);
45
+ get strict(): boolean;
46
+ set strict(value: boolean);
47
+ registerFunctions(functionClasses: ASTFunctionClass[]): void;
48
+ /**
49
+ * 扫描 GrammarNode 并构造 AST;不产出 syntax
50
+ * 用于最终渲染
51
+ */
52
+ parse(start?: number, end?: number): ASTNodeBase[];
53
+ /**
54
+ * 词法层:容错扫描 GrammarNode 并收集 syntax
55
+ * 用于防抖时的编辑器高亮和补全
56
+ */
57
+ parseSyntax(start?: number, end?: number): SyntaxAnalysis;
58
+ private addSyntax;
59
+ /** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
60
+ private recordSyntax;
61
+ /**
62
+ * 第二轮去糖 & 函数节点创建
63
+ * 有副作用 会修改 nodes 和 labelableNodes 等上下文属性
64
+ * 对于需要后面Node的语法糖(没有终止符),可以调用此函数,具体做法为:
65
+ * 1. 先用构造函数开启子上下文
66
+ * 2. 再用新的上下文解析剩余的nodes
67
+ * 3. 选取需要的,利用老的ctx构建函数节点
68
+ * 4. 将新ctx的有用信息合并回老的ctx
69
+ * 之所以这么麻烦,是因为创建语法糖对应节点时需要保留当前上下文
70
+ *
71
+ * 对于有终止符的直接搜索后面的终止符即可。扫过的地方普通文本会再次被合并
72
+ */
73
+ makeNodes(nodes: (GrammarNode | number)[], i?: number, end?: number): number;
74
+ /**
75
+ * 识别核心语法: 大括号/函数调用/标签,其余尝试语法糖,失败的字符以索引的形式存在于返回值中,供第二轮识别终止符或合并为 TextNode
76
+ * 即使是关系型的语法糖,这一阶段也要将依赖的字符提取为 GrammarSugarNode,供下一阶段处理
77
+ * 必须将第一阶段语法糖识别放到grammar解析中,比如 `@fn()`,引号若作为语法糖会导致后面的 `@` 无效
78
+ * syntaxOnly 是词法层模式:用户语法错误只记录诊断不抛出(保留 partial call),并顺带记录 syntax;
79
+ * 它不得改变“识别出什么结构”,否则两条路径会认不同的语言
80
+ */
81
+ parseGrammar(p: number, end: number, syntaxOnly?: boolean): (GrammarNode | number)[];
82
+ parseCallNode(callNode: GrammarCallNode): ASTFunctionNode;
83
+ /**
84
+ * 根据参数类型解析参数值,解析过程中会记录诊断信息
85
+ * funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
86
+ * 解析成功返回值 解析失败返回null
87
+ */
88
+ parseArgWithType(argStart: number, argEnd: number, type?: paramType, funcStart?: number): paramValue | null;
89
+ length2px(length: LengthValue): number;
90
+ }
91
+ export declare function skipSpaces(s: string, i: number, end: number): number;
92
+ /** 反向跳过非换行空白,越界返回 -1 */
93
+ export declare function skipSpacesBack(s: string, i: number, start?: number): number;