-
Notifications
You must be signed in to change notification settings - Fork 3
/
$preamble.ts
117 lines (103 loc) · 4.18 KB
/
$preamble.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { factory } from "../factory";
import { Line, LineType, OtherLine } from "../../../node/cst/line";
import { $blankLine, makeIndentBlockWithCaptureRule, WithErrorRule } from "../util";
import { isParagraph, isParagraphItemTitle, newStdEL } from "../../../law/std";
import * as std from "../../../law/std";
import CST from "../toCSTSettings";
import { Control } from "../../../node/cst/inline";
import { ErrorMessage } from "../../cst/error";
import $paragraphItem, { $noControlAnonymParagraph, paragraphItemToLines } from "./$paragraphItem";
import { rangeOfELs } from "../../../node/el";
export const preambleControl = ":preamble:";
/**
* The renderer for {@link std.Preamble}. Please see the source code for the detailed syntax, and the [test code](https://github.com/yamachig/Lawtext/blob/main/core/src/parser/std/rules/$preamble.spec.ts) for examples.
*/
export const preambleToLines = (preamble: std.Preamble, indentTexts: string[]): Line[] => {
const lines: Line[] = [];
lines.push(new OtherLine({
range: null,
indentTexts,
controls: [
new Control(
preambleControl,
null,
"",
null,
),
],
sentencesArray: [],
lineEndText: CST.EOL,
}));
const childrenIndentTexts = [...indentTexts, CST.INDENT];
for (const paragraph of preamble.children) {
if (paragraph.children.filter(isParagraphItemTitle).some(el => el.text() !== "")) {
lines.push(...paragraphItemToLines(paragraph, childrenIndentTexts, { defaultTag: "Paragraph" }));
} else {
lines.push(...paragraphItemToLines(paragraph, childrenIndentTexts, { noControl: true }));
}
}
return lines;
};
const $preambleChildrenBlock = makeIndentBlockWithCaptureRule(
"$preambleChildrenBlock",
(factory
.choice(c => c
.orSequence(s => s
.and(() => $paragraphItem("Paragraph"), "paragraphItem")
.andOmit(r => r.assert(({ paragraphItem }) => isParagraph(paragraphItem.value)))
)
.or(() => $noControlAnonymParagraph)
)
),
);
/**
* The parser rule for {@link std.Preamble}. Please see the source code for the detailed syntax, and the [test code](https://github.com/yamachig/Lawtext/blob/main/core/src/parser/std/rules/$preamble.spec.ts) for examples.
*/
export const $preamble: WithErrorRule<std.Preamble> = factory
.withName("preamble")
.sequence(s => s
.and(r => r
.oneMatch(({ item }) => {
if (
item.type === LineType.OTH
&& item.line.type === LineType.OTH
// && item.virtualIndentDepth === 0
&& item.line.sentencesArray.length === 0
&& item.line.controls.length === 1
&& item.line.controls.some(c => c.control === preambleControl)
) {
return item;
} else {
return null;
}
})
, "headLine")
.and(r => r.zeroOrMore(() => $blankLine))
.and(r => r
.zeroOrOne(() => $preambleChildrenBlock)
, "childrenBlock")
.action(({ headLine, childrenBlock }) => {
const children: std.Preamble["children"] = [];
const errors: ErrorMessage[] = [];
if (childrenBlock) {
children.push(...childrenBlock.value.map(c => c.value as std.Paragraph));
errors.push(...childrenBlock.value.map(c => c.errors).flat());
errors.push(...childrenBlock.errors);
for (let i = 0; i < children.length; i++) {
children[i].attr.Num = `${i + 1}`;
}
}
const pos = headLine.line.indentsEndPos;
const range = rangeOfELs(children) ?? (pos !== null ? [pos, pos] : null);
if (range && pos !== null) {
range[0] = pos;
}
const preamble = newStdEL("Preamble", {}, children, range);
return {
value: preamble,
errors,
};
})
)
;
export default $preamble;