-
Notifications
You must be signed in to change notification settings - Fork 3
/
processNameList.ts
173 lines (142 loc) · 6.7 KB
/
processNameList.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
import { WithErrorValue } from "../../parser/std/util";
import { ErrorMessage } from "../../parser/cst/error";
import { __Parentheses, __Text, ____Declaration } from "../../node/el/controls";
import $nameListHead from "../sentenceChildrenParser/rules/$nameListHead";
import { initialEnv } from "../sentenceChildrenParser/env";
import { SentenceChildEL } from "../../node/cst/inline";
import { SentenceEnv, SentenceTextRange, toSentenceTextRanges } from "../../node/container/sentenceEnv";
import * as std from "../../law/std";
import { SentenceEnvsStruct } from "../getSentenceEnvs";
import getScope from "../pointerEnvs/getScope";
import { PointerEnvsStruct } from "../pointerEnvs/getPointerEnvs";
export const processNameList = (
headSentenceEnv: SentenceEnv,
sentenceEnvsStruct: SentenceEnvsStruct,
pointerEnvsStruct: PointerEnvsStruct,
): (
WithErrorValue<____Declaration[]>
) => {
const errors: ErrorMessage[] = [];
const declarations: ____Declaration[] = [];
const result = $nameListHead.match(
0,
(headSentenceEnv.el.children as SentenceChildEL[]),
initialEnv({ target: "" }),
);
if (result.ok) {
const { pointerRanges } = result.value.value;
if (pointerRanges) getScope(pointerRanges, pointerEnvsStruct);
const scope = toSentenceTextRanges(
pointerRanges.targetContainerIDRanges,
sentenceEnvsStruct,
);
if (scope.length === 0) {
errors.push(new ErrorMessage(
"No scope found",
[
pointerRanges?.range?.[0] ?? 0,
pointerRanges?.range?.[1] ?? 0,
],
));
}
for (const nameContainer of headSentenceEnv.container.children) {
const paragraphItemSentence = nameContainer.el.children.find(std.isParagraphItemSentence);
if (!paragraphItemSentence || paragraphItemSentence.children.length === 0) continue;
if (paragraphItemSentence.children.every(std.isSentence)) {
const sentence = paragraphItemSentence.children[0];
if (sentence.children.length <= 1) continue;
const sentenceEnv = sentenceEnvsStruct.sentenceEnvByEL.get(sentence);
if (!sentenceEnv) continue;
const nameChild = sentence.children[0];
if (!(nameChild instanceof __Parentheses && nameChild.attr.type === "square")) continue;
const name = nameChild.content.text();
const afterNameChild = sentence.children[1];
if (!(afterNameChild instanceof __Text)) continue;
const afterNameMatch = /^とは、/.exec(afterNameChild.text());
if (!afterNameMatch) continue;
const lastChild = sentence.children[sentence.children.length - 1];
if (!(lastChild instanceof __Text)) continue;
const lastMatch = /をいう。$/.exec(lastChild.text());
if (!lastMatch) continue;
const nameTextRange = sentenceEnv.textRageOfEL(nameChild.content);
if (!nameTextRange) {
errors.push(new ErrorMessage(
"nameTextRange is null",
[
pointerRanges?.range?.[0] ?? 0,
pointerRanges?.range?.[1] ?? 0,
],
));
continue;
}
const nameSentenceTextRange: SentenceTextRange = {
start: {
sentenceIndex: sentenceEnv.index,
textOffset: nameTextRange[0],
},
end: {
sentenceIndex: sentenceEnv.index,
textOffset: nameTextRange[1],
},
};
const declarationID = `decl-sentence_${sentenceEnv.index}-text_${nameSentenceTextRange.start.textOffset}_${nameSentenceTextRange.end.textOffset}`;
const declaration = new ____Declaration({
declarationID,
type: "Keyword",
name,
value: sentence.text().slice(nameChild.text().length + afterNameMatch[0].length, -lastMatch[0].length),
scope: scope,
nameSentenceTextRange,
range: nameChild.content.range,
children: [name],
});
declarations.push(declaration);
nameChild.content.children.splice(
0,
nameChild.content.children.length,
declaration,
);
} else if (paragraphItemSentence.children.every(std.isColumn)) {
if (paragraphItemSentence.children.length !== 2) continue;
const [nameColumn, defColumn] = paragraphItemSentence.children;
if (nameColumn.children.length !== 1) continue;
const nameSentence = nameColumn.children[0];
if (!std.isSentence(nameSentence)) continue;
const nameSentenceEnv = sentenceEnvsStruct.sentenceEnvByEL.get(nameSentence);
if (!nameSentenceEnv) continue;
const name = nameSentenceEnv.text;
const nameSentenceTextRange: SentenceTextRange = {
start: {
sentenceIndex: nameSentenceEnv.index,
textOffset: 0,
},
end: {
sentenceIndex: nameSentenceEnv.index,
textOffset: name.length,
},
};
const declarationID = `decl-sentence_${nameSentenceEnv.index}-text_${nameSentenceTextRange.start.textOffset}_${nameSentenceTextRange.end.textOffset}`;
const declaration = new ____Declaration({
declarationID,
type: "Keyword",
name,
value: defColumn.children.map(c => sentenceEnvsStruct.sentenceEnvByEL.get(c as std.Sentence)?.text ?? "").join(""),
scope: scope,
nameSentenceTextRange,
range: nameSentence.range,
children: ([...nameSentence.children] as (string | std.Ruby | std.Sup | std.Sub | std.__EL)[]),
});
declarations.push(declaration);
nameSentence.children.splice(
0,
nameSentence.children.length,
declaration,
);
}
}
}
return {
value: declarations,
errors,
};
};