-
Notifications
You must be signed in to change notification settings - Fork 3
/
processNameInline.ts
112 lines (97 loc) · 3.8 KB
/
processNameInline.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
import { WithErrorValue } from "../../parser/std/util";
import { ErrorMessage } from "../../parser/cst/error";
import { ____Declaration } from "../../node/el/controls";
import $nameInline from "../sentenceChildrenParser/rules/$nameInline";
import { initialEnv } from "../sentenceChildrenParser/env";
import { SentenceChildEL } from "../../node/cst/inline";
import { toSentenceTextRanges, SentenceEnv, SentenceTextRange } 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 processNameInline = (
elToBeModified: std.StdEL | std.__EL,
sentenceEnv: SentenceEnv,
sentenceEnvsStruct: SentenceEnvsStruct,
pointerEnvsStruct: PointerEnvsStruct,
): (
WithErrorValue<{
declarations: ____Declaration[],
}>
) => {
const errors: ErrorMessage[] = [];
const declarations: ____Declaration[] = [];
for (let i = 0; i < elToBeModified.children.length; i++) {
const result = $nameInline.match(
i,
(elToBeModified.children as SentenceChildEL[]),
initialEnv({ target: "" }),
);
if (result.ok) {
const { nameSquareParentheses, following, pointerRanges } = result.value.value;
if (pointerRanges) getScope(pointerRanges, pointerEnvsStruct);
errors.push(...result.value.errors);
const name = nameSquareParentheses.content.text();
const followingStartPos = following ? {
sentenceIndex: sentenceEnv.index,
textOffset: sentenceEnv.textRageOfEL(nameSquareParentheses)?.[1] ?? 0,
} : null;
const scope = toSentenceTextRanges(
pointerRanges?.targetContainerIDRanges ?? null,
sentenceEnvsStruct,
followingStartPos,
);
if (scope.length === 0) {
errors.push(new ErrorMessage(
"No scope found",
[
pointerRanges?.range?.[0] ?? 0,
pointerRanges?.range?.[1] ?? 0,
],
));
}
const nameTextRange = sentenceEnv.textRageOfEL(nameSquareParentheses.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_${nameTextRange[0]}_${nameTextRange[1]}`;
const declaration = new ____Declaration({
declarationID,
type: "Keyword",
name,
value: null,
scope: scope,
nameSentenceTextRange,
range: nameSquareParentheses.content.range,
children: [name],
});
declarations.push(declaration);
nameSquareParentheses.content.children.splice(
0,
nameSquareParentheses.content.children.length,
declaration,
);
}
}
return {
value: { declarations },
errors,
};
};