-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnml.ts
294 lines (290 loc) · 9.77 KB
/
nml.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
type span = [number,number];
interface NML_document {
type: "document",
span: span,
documenttitle: NML_documenttitle|null,
nodes: Array<object>|null,
}
interface NML_nmlnode {
type: "nmlnode",
span: span,
nodetitle: NML_nodetitle,
nodes: NML_nodes,
}
type NML_nodes = Array<NML_node>
type NML_node = NML_nmlnode|NML_block|NML_paragraph
interface NML_paragraph {
type: "paragraph",
span: span,
contents: Array<NML_contents>
}
interface NML_contents {
type: "contents",
span: span,
contents: Array<NML_text|NML_inline>,
}
interface NML_block {
type: "block",
span: span,
embtype: NML_embtype,
code: NML_code,
}
interface NML_inline {
type: "inline",
span: span,
embtype: NML_embtype,
code: NML_code,
}
interface NML_code {
type: "code",
span: span,
text: string,
}
interface NML_documenttitle {
type: "documenttitle",
span: span,
text: string,
}
interface NML_nodetitle {
type: "nodetitle",
span: span,
text: string,
}
interface NML_text {
type: "text",
span: span,
text: string,
}
interface NML_embtype {
type: "embtype",
span: span,
module: string,
args: Array<Record<string,string>>,
}
class NMLtool {
private code:string;
private filename:string;
private fRead:Function;
tokenizerstates:Array<string>;
indentSpace:number;
parseTree:NML_document;
constructor(filename: string) {
this.indentSpace = 4; // インデントのスペースの個数
this.filename = filename;
{ // Define the fRead function
// @ts-ignore
if (typeof require!="undefined") {
// @ts-ignore
const fs:any = require('fs');
this.fRead = function (filename): string {
return fs.readFileSync(filename,'utf8').replace(/\r\n/g,"\n");
}
}
else {
this.fRead = function (filename): string {
let hr:any = new XMLHttpRequest()
hr.open("GET",filename,false);
hr.send(null);
if (hr.status==200||hr.status==304) {
return hr.responseText.replace(/\r\n/g,"\n");
}
else {
throw "err "+filename;
}
}
}
}
this.code = this.fRead(filename);
//console.log(this.code)
}
getLineAndCol(i:number): object {
let j:number = 0;
let line:number = 1;
let col:number = 1;
while (j<i) {
if (this.code[j]=="\n") {
line++;
col = 0;
}
else {
col++;
}
j++;
}
return [line,col];
}
parseTree_document():NML_document {
console.log(this.code)
let i:number = 0;
let maxIndent:number = 0;
this.parseTree ={ type:"document",span:[i,this.code.length],documenttitle:null,nodes:null };
if (this.code[0]=="#") {
if (this.code[1]!=" ") { throw `error ${this.getLineAndCol(i).toString()}: タイトルの\"#\"の後ろには空白が必要です` }
i = 2;
let doctitle_text = "";
let nodes_text = "";
// @ts-ignore
let doctitle_span:span = [i,null];
// @ts-ignore
let nodes_span:span = [null,this.code.length];
while (true) {
if (i==this.code.length) {throw `error ${this.getLineAndCol(i).toString()}: ドキュメントの内容がありません`}
if (this.code[i]!="\n") {
doctitle_text+=this.code[i];
i++;
doctitle_span[1] = i;
}
else { i++;break; }
}
nodes_span[0] = i;
while (i<this.code.length) {
nodes_text+=this.code[i];
i++;
}
this.parseTree.documenttitle = this.parseTree_documenttitle(doctitle_span);
this.parseTree.nodes = this.parseTree_nodes(nodes_span,0);
}
else {
throw `error ${this.getLineAndCol(i).toString()}: ドキュメントのタイトルがありません`;
}
console.log(this.parseTree)
return this.parseTree;
}
parseTree_checkIndent(i:number) {
let flag = true;
for (let j:number=0;j<this.indentSpace;j++) {
if (this.code[i+j]!=" ") { flag=false;break; }
}
return flag;
}
parseTree_nodes(nodes_span:span,indent:number):NML_nodes {
let nodes:NML_nodes = [];
let i:number = nodes_span[0];
let indentstat:boolean = false; // インデントを受け付けるか (nmlnodeとblockの時は受け付ける)
let lineinfo:Array<["indents"|"nmlnode"|"block"|"empty"|"contents"|"eos",number]> = [];
while (i<nodes_span[1]) {
if (this.code[i-1]=="\n") { // 行頭
if (indentstat&&this.parseTree_checkIndent(i+indent*this.indentSpace)) { // インデント有り
lineinfo.push(["indents",i]);
}
else { // インデント無し
indentstat = false;
if (this.code[i+indent*this.indentSpace]=="#") { // タイトル -> nmlnode
indentstat = true;
lineinfo.push(["nmlnode",i]);
}
else if (this.code[i+indent*this.indentSpace]=="&") { // ブロック -> block
indentstat = true;
lineinfo.push(["block",i]);
}
else if (this.code[i+indent*this.indentSpace]=="\n") { // 空行
lineinfo.push(["empty",i]);
}
else { // 本文 -> content
lineinfo.push(["contents",i]);
}
}
}
i++;
}
lineinfo.push(["eos",i+1]);
console.log(lineinfo)
let j:number = 0;
while (j<lineinfo.length) {
if (lineinfo[j][0]=="nmlnode") {
let k:number = j+1;
if (lineinfo[k][0]=="indents") {
while (lineinfo[k][0]=="indents") {k++;}
nodes.push(this.parseTree_nmlnode([lineinfo[j][1]+indent*this.indentSpace,lineinfo[j+1][1]-1],[lineinfo[j+1][1],lineinfo[k][1]-1],indent));
j = k-1;
}
else {
throw `error ${this.getLineAndCol(i).toString()}: タイトル以下の内容がありません`
}
}
if (lineinfo[j][0]=="block") {
let k:number = j+1;
if (lineinfo[k][0]=="indents") {
while (lineinfo[k][0]=="indents") {k++;}
nodes.push(this.parseTree_block([lineinfo[j][1]+indent*this.indentSpace,lineinfo[j+1][1]-1],[lineinfo[j+1][1],lineinfo[k][1]-1],indent));
j = k-1;
}
else {
throw `error ${this.getLineAndCol(i).toString()}: ブロックのコードがありません`
}
}
if (lineinfo[j][0]=="contents") {
let paragraph:NML_paragraph = {type:"paragraph",span:[lineinfo[j][1],lineinfo[j+1][1]-1],contents:[]};
while (lineinfo[j][0]=="contents") {
paragraph.contents.push(this.parseTree_content([lineinfo[j][1]+indent*this.indentSpace,lineinfo[j+1][1]-1],indent));
paragraph.span[1] = lineinfo[j+1][1]-1;
j++;
}
j--;
nodes.push(paragraph);
}
j++;
}
return nodes
}
parseTree_content(content_span:span,indent:number):NML_contents { // inline無し暫定版
let text:string = "";
let i:number = content_span[0];
while (i<content_span[1]) {
if (this.code[i-1]=="\n") {
i += indent*this.indentSpace;
}
text += this.code[i];
i++;
}
return {
type: "contents",
span: content_span,
text: text,
}
}
parseTree_block(embtype_span:span,code_span:span,indent:number):NML_block {
return {
type: "block",
span: [embtype_span[0],code_span[1]],
embtype: this.parseTree_embtype(embtype_span),
code: this.parseTree_code(code_span,indent+1),
}
}
parseTree_nmlnode(nodetitle_span:span,nodes_span:span,indent:number):NML_nmlnode {
return {
type: "nmlnode",
span: [nodetitle_span[0],nodes_span[1]],
nodetitle: this.parseTree_nodetitle(nodetitle_span),
nodes: this.parseTree_nodes(nodes_span,indent+1),
}
}
parseTree_inline() {
}
parseTree_code(code_span:span,indent:number):NML_code {
let text:string = "";
let i:number = code_span[0];
while (i<code_span[1]) {
if (this.code[i-1]=="\n") {
i += indent*this.indentSpace;
}
text += this.code[i];
i++;
}
return {
type: "code",
span: code_span,
text: text,
}
}
parseTree_embtype(embtype_span:span) {
}
parseTree_documenttitle(doctitle_span:span):NML_documenttitle {
return {type:"documenttitle",span:doctitle_span,text:this.code.slice(...doctitle_span)}
}
parseTree_nodetitle(nodetitle_span:span):NML_nodetitle {
return {type:"nodetitle",span:nodetitle_span,text:this.code.slice(...nodetitle_span)}
}
parseTree_trimIndent() {
}
}