-
Notifications
You must be signed in to change notification settings - Fork 3
/
$articleGroup.ts
207 lines (185 loc) · 8.59 KB
/
$articleGroup.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { ArticleGroupHeadLine, BlankLine, Line, LineType } from "../../../node/cst/line";
import { newStdEL } from "../../../law/std";
import * as std from "../../../law/std";
import CST from "../toCSTSettings";
import { assertNever, Diff, range } from "../../../util";
import { WithErrorRule } from "../util";
import factory from "../factory";
import { $blankLine, $indentBlock } from "../util";
import { paragraphItemToLines } from "./$paragraphItem";
import { mergeAdjacentTextsWithString } from "../../cst/util";
import $article, { articleToLines } from "./$article";
import { parseNamedNum } from "../../../law/num";
import { appdxItemToLines } from "./$appdxItem";
import { ErrorMessage } from "../../cst/error";
import { Control } from "../../../node/cst/inline";
import { rangeOfELs } from "../../../node/el";
/**
* The renderer for article group ({@link std.ArticleGroup | ArticleGroup}) such as Chapter. 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/$articleGroup.spec.ts) for examples.
*/
export const articleGroupToLines = (el: std.ArticleGroup, indentTexts: string[]): Line[] => {
const lines: Line[] = [];
const ChildItems: Array<Diff<std.ArticleGroup, std.Part> | std.Article | std.Paragraph> = [];
for (const child of el.children) {
if (std.isArticleGroupTitle(child)) {
const titleIndentDepth = indentTexts.length === 0
? std.articleGroupTitleTags.indexOf(child.tag) + 2
: indentTexts.length;
const titleIndentTexts = indentTexts.length === 0
? [...range(0, titleIndentDepth)].map(() => CST.INDENT)
: indentTexts;
lines.push(new ArticleGroupHeadLine({
range: null,
indentTexts: titleIndentTexts,
mainTag: el.tag,
controls: indentTexts.length == 0 ? [] : [
new Control(
":keep-indents:",
null,
"",
null,
)
],
sentenceChildren: mergeAdjacentTextsWithString(child.children),
lineEndText: CST.EOL,
}));
lines.push(new BlankLine({ range: null, lineEndText: CST.EOL }));
} else {
ChildItems.push(child);
}
}
for (const [i, child] of ChildItems.entries()) {
if (i > 0) lines.push(new BlankLine({ range: null, lineEndText: CST.EOL }));
if (child.tag === "Article") {
lines.push(...articleToLines(child, indentTexts));
lines.push(new BlankLine({ range: null, lineEndText: CST.EOL }));
} else if (child.tag === "Paragraph") {
lines.push(...paragraphItemToLines(child, indentTexts, { defaultTag: "Paragraph" }));
lines.push(new BlankLine({ range: null, lineEndText: CST.EOL }));
} else if (std.isArticleGroup(child)) {
lines.push(...articleGroupToLines(child, indentTexts));
lines.push(new BlankLine({ range: null, lineEndText: CST.EOL }));
} else if (std.isAppdxStyle(child)) {
console.error("Unexpected AppdxStyle in MainProvision!");
lines.push(...appdxItemToLines(child, indentTexts));
}
else { assertNever(child); }
}
return lines;
};
/**
* The parser rule for article group ({@link std.ArticleGroup | ArticleGroup}) such as Chapter. 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/$articleGroup.spec.ts) for examples.
*/
export const $articleGroup: WithErrorRule<std.ArticleGroup> = factory
.withName("articleGroup")
.sequence(s => s
.and(r => r
.oneMatch(({ item }) => {
if (
item.type === LineType.ARG
) {
return item;
} else {
return null;
}
})
, "headLine")
.and(r => r
.zeroOrMore(r => r
.sequence(r => r
.andOmit(r => r.zeroOrMore(() => $blankLine))
.and(r => r
.choice(r => r
.or(() => $article)
// .or(() => $paragraphItem)
.orSequence(s => s
.andOmit(r => r
.nextIs(r => r
.oneMatch(({ item, headLine }) => {
if (
item.type === LineType.ARG
&& std.articleGroupTags.indexOf(headLine.line.mainTag) < std.articleGroupTags.indexOf(item.line.mainTag)
) {
return item;
} else {
return null;
}
})
)
)
.and(() => $articleGroup)
)
.or(r => r
.asSlice(r => r
.oneOrMore(r => r
.sequence(s => s
.and(r => r
.choice(c => c
.or(() => $indentBlock)
.orSequence(s => s
.and(r => r.anyOne(), "captured")
.andOmit(r => r.assert(({ captured }) =>
(captured.type === LineType.OTH)
|| (captured.type === LineType.PIT)
))
)
)
)
)
)
)
)
)
)
)
)
, "childrenAndErrors")
.action(({ headLine, childrenAndErrors }) => {
const children: (Diff<std.ArticleGroup, std.Part> | std.Article)[] = [];
const errors: ErrorMessage[] = [];
for (const child of childrenAndErrors) {
if (Array.isArray(child)) {
errors.push(new ErrorMessage(
"$articleGroup: この部分をパースできませんでした。",
[
child[0].virtualRange[0],
child.slice(-1)[0].virtualRange[1],
],
));
} else {
children.push(child.value as Diff<std.ArticleGroup, std.Part>);
errors.push(...child.errors);
}
}
const articleGroup = newStdEL(
headLine.line.mainTag,
);
(articleGroup.children as (typeof articleGroup.children)[number][]).push(newStdEL(
std.articleGroupTitleTags[std.articleGroupTags.indexOf(headLine.line.mainTag)],
{},
headLine.line.title,
headLine.line.titleRange,
));
const num = parseNamedNum(
typeof headLine.line.title[0] === "string"
? headLine.line.title[0]
: headLine.line.title[0]?.text()
);
if (num) {
articleGroup.attr.Num = num;
}
(articleGroup.children as (typeof articleGroup.children)[number][]).push(...children);
const pos = headLine.line.indentsEndPos;
const range = rangeOfELs(articleGroup.children) ?? (pos !== null ? [pos, pos] : null);
if (range && pos !== null) {
range[0] = pos;
}
articleGroup.range = range;
return {
value: articleGroup,
errors,
};
})
)
;
export default $articleGroup;