-
Notifications
You must be signed in to change notification settings - Fork 3
/
$otherLine.ts
149 lines (146 loc) · 6.6 KB
/
$otherLine.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
import factory from "../factory";
import $indents from "./$indents";
import { OtherLine } from "../../../node/cst/line";
import { $_, $_EOL } from "./lexical";
import $sentencesArray from "./$sentencesArray";
import { WithErrorRule } from "../util";
import { Control, Sentences } from "../../../node/cst/inline";
import $xml from "./$xml";
import { newStdEL } from "../../../law/std";
import * as std from "../../../law/std";
import { EL } from "../../../node/el";
import $squareAttr from "./$squareAttr";
export const keepLeadingSpacesControl = ":keep-leading-spaces:";
export const ignoreTitleControl = ":ignore-title:";
/**
* The parser rule for {@link OtherLine} that represents a line of other types. Please see the source code for the detailed syntax, and the [test code](https://github.com/yamachig/Lawtext/blob/main/core/src/parser/cst/rules/$otherLine.spec.ts) for examples.
*/
export const $otherLine: WithErrorRule<OtherLine> = factory
.withName("otherLine")
.sequence(s => s
.and(() => $indents, "indentsStruct")
.and(r => r
.zeroOrMore(r => r
.sequence(s => s
// eslint-disable-next-line no-irregular-whitespace
.and(r => r
.sequence(s => s
.and(r => r.regExp(/^:[^:\r\n]+:/), "value")
.action(({ value, range }) => ({ value, range: range() }))
)
, "control")
.and(r => r
.sequence(s => s
.and(r => r
.asSlice(r => r
.choice(c => c
.or(s => s
.assert(({ control }) => control.value === keepLeadingSpacesControl)
)
.or(() => $_)
)
)
, "value")
.action(({ value, range }) => ({ value, range: range() }))
)
, "trailingSpace")
.action(({ control, trailingSpace }) => new Control(
control.value,
control.range,
trailingSpace.value,
trailingSpace.range,
))
)
)
, "controls")
.and(r => r
.zeroOrOne(r => r
.choice(c => c
.orSequence(s => s
// .andOmit(r => r.assert(({ controls }) => controls.some(c => c.control === ":xml:")))
.and(() => $xml, "xml")
.andOmit(r => r.nextIs(() => $_EOL))
.action(({ range, xml }) => {
const [start, end] = xml.value.range ?? range();
const el = xml.value;
const capturedEl = (
std.isLine(el) || std.isQuoteStruct(el) || std.isArithFormula(el) || std.isRuby(el) || std.isSup(el) || std.isSub(el) || std.isControl(el)
)
? el
: new EL("__CapturedXML", {}, [el], [start, end]);
return {
value: [
new Sentences(
"",
[start, start],
[],
[newStdEL("Sentence", {}, [capturedEl], [start, end])]
)
],
errors: xml.errors,
};
})
)
.or(() => $sentencesArray)
.orSequence(s => s
.and(r => r
.oneOrMore(r => r
.sequence(s => s
.and(() => $squareAttr, "entry")
.and(() => $_, "trailingSpace")
.action(({ entry, trailingSpace, range }) => {
const r = range();
entry.value.trailingSpace = trailingSpace;
if (entry.value.entryRange) {
entry.value.trailingSpaceRange = [
entry.value.entryRange[1],
r[1],
];
}
return {
value: entry.value,
errors: entry.errors,
};
})
)
)
, "attrEntries")
.action(({ attrEntries, range }) => {
const r = range();
const errors = [...attrEntries.map(e => e.errors).flat()];
return {
value: [
new Sentences(
"",
[r[0], r[0]],
attrEntries.map(e => e.value).flat(),
[],
)
],
errors,
};
})
)
)
)
, "columns")
.and(() => $_EOL, "lineEndText")
.action(({ range, indentsStruct, controls, columns, lineEndText }) => {
const errors = [
...indentsStruct.errors,
...(columns?.errors ?? []),
];
return {
value: new OtherLine({
range: range(),
indentTexts: indentsStruct.value.indentTexts,
controls,
sentencesArray: columns?.value ?? [],
lineEndText,
}),
errors,
};
})
)
;
export default $otherLine;