forked from cshaa/nml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnml.js
More file actions
329 lines (313 loc) · 8.83 KB
/
Copy pathnml.js
File metadata and controls
329 lines (313 loc) · 8.83 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/** * * * * * * * * * * * * * * * * *
* nml parser 1.0 *
* Release: 0 *
* * * * * * * * * * * * * * * * * * *
* NOTE: *
* Please understand the word *
* "childs" as a shortand for *
* "children". *
* * * * * * * * * * * * * * * * * * */
//TODO add comment support
//FIXME no doctype parsing
window.nml = function(){
this.version = 1;
this.update = 1;
this.release = 0;
this.failonerr = false;
this.namespaces = [];
this.sandbox = [];
this.childs = [];
this.parse = function(str,async){
if(async){
setTimeout(this.parse,0);
}
//Preparation
str = str.trim();
var i = {n:0};//The major cursor
var x = 0; //The minor cursor
var line = 1;
var col = 1;
var run = true;
var structure = [this];
var node;
i.add = function(n){//i++ with line counter
if(!n){n=1;}
while(n-->0&&str.length>i.n){
if(str.length<=i.n&&this.failonerr){throw {line:line,collumn:col,message:"Unexpected end of the document."};}
if(str[i.n]=="\n"){line++;col=1;}
i.n++;col++;
}
}
var skipEmpty = function(){//Skip to the first non-empty character
while(" \n\t".indexOf(str[i.n])+1&&str.length>i.n) {i.add();}
};
var getAttrs = function(){//Parse tag's attributes
skipEmpty();
var attrs = {};
var attr = {};//Buffer
var x = i.n; //Redefine the minor cursor
while(str[i.n]!=">"&&str.substr(i.n,2)!="/>"&&str.length>i.n){//Until the end of the tag
x=i.n;
while(!(" =>".indexOf(str[x])+1)){x++;}
attr.name=str.substring(i.n,x);//Save attribute name
i.add(x-i.n);
skipEmpty();
x=i.n;
if(str[x]=="="){
//String attribute
i.add();
skipEmpty();
x=i.n;
if(str[x]=="'"){
//For single quotes
i.add();x++;//Skip the quote
while(str[x]!="'"&&str.length>x){x++;}
attr.value=str.substring(i.n,x);//Save attribute value
x++;//Skip the quoto
i.add(x-i.n);//Move i to x
}else if(str[x]=='"'){
//For double quotes
i.add();x++;//Skip the quote
while(str[x]!='"'&&str.length>x){x++;}
attr.value=str.substring(i.n,x);//Save attribute value
x++;//Skip the quote
i.add(x-i.n);//Move i to x
}else{
//For value without quotes
while(!(" \n\t>".indexOf(str[x])+1)&&str.length>x){x++;}
attr.value=str.substring(i.n,x);//Save attribute value
i.add(x-i.n);//Move i to x
}
}else{
//Boolean attribute
attr.value=true;//Save attribute value
}
attrs[attr.name]=attr.value;//Write to the array
attr = {};//Clean buffer
skipEmpty();
}
skipEmpty();
return attrs;
};
//Parsing
while(run){
if(str[i.n]=="<"){
//If the document begins with "<"
run = false; //Do not repeat the cycle
i.add();
x = i.n;
while(!(" \n\t/>".indexOf(str[x])+1)&&str.length>x) {x++;}
if(str.substring(i.n,x).toLowerCase()=="!doctype"){
//If the first tag is doctype
i.add(x-i.n+1);
while(str[i.n]!=">"){
//Get the namespaces
skipEmpty();
x=i.n;
while(!(" \n\t,>".indexOf(str[x])+1)){x++;}
if(str.substring(i.n,x)){
this.namespaces[this.namespaces.length]=str.substring(i.n,x);
}
i.add(x-i.n);//Move i to x
}
}else{
this.childs[0] = {};
this.childs[0].tag = {};
if(str.substring(i.n,x).indexOf(":")+1){//With namespace
this.childs[0].tag.name = str.substring(i.n,x).split(":")[1];
this.childs[0].tag.space = str.substring(i.n,x).split(":")[0];
}else{//Without namespace
this.childs[0].tag.name = str.substring(i.n,x);
}
i.add(x-i.n);//Move i to x
this.childs[0].attrs = getAttrs();
if(str[i.n]){
if(str.substring(i.n,i.n+2)=="/>"){
return;
}else{
i.add();
}
}else if(this.failonerr){
throw {line:line,collumn:col,message:"Unexpected end of the document."};
}else{
return;
}
}
i.add();
//BEGIN Main cycle
while(str.length>i.n){
//Add text node
while(str[x]!="<"&&str.length>x){x++;}
if(x!=i.n){structure[structure.length-1].childs[structure[structure.length-1].childs.length]=str.substring(i.n,x); i.add(x-i.n);}
if(str.length<=i.n){
if(this.failonerr){
throw {line:line,collumn:col,message:"Unexpected end of the document, document does not end with the ending tag of the root."};
}else{
return;
}
}
//Parse tag
i.add();
if(str[i.n]=="/"){//Ending tag
i.add();
x = i.n;
while((!(">".indexOf(str[x])+1))&&str.length>x) {x++;}
node = {};
node.tag = {};
if(str.substring(i.n,x).indexOf(":")+1){//With namespace
node.tag.name = str.substring(i.n,x).split(":")[1];
node.tag.space = str.substring(i.n,x).split(":")[0];
}else{//Without namespace
node.tag.name = str.substring(i.n,x);
}
i.add(x-i.n);
if(
structure[structure.length-1].tag.name == node.tag.name &&
structure[structure.length-1].tag.space == node.tag.space
){
structure.length--;
}else{
//begin TODO
if(this.failonerr){
throw {line:line,collumn:col,message:"Bazinga!"};
}else{
return;
}
//end TODO
}
skipEmpty();
if(str[i.n]==">"){
i.add();
}else{
if(this.failonerr){
throw {line:line,collumn:col,message:"Unknown error"};
}
}
}else{
x = i.n;
while((!(" \n\t/>".indexOf(str[x])+1))&&str.length>x) {x++;}
node = {};
node.tag = {};
node.childs = [];
structure[structure.length-1].childs[structure[structure.length-1].childs.length] = node;
if(str.substring(i.n,x).indexOf(":")+1){//With namespace
node.tag.name = str.substring(i.n,x).split(":")[1];
node.tag.space = str.substring(i.n,x).split(":")[0];
}else{//Without namespace
node.tag.name = str.substring(i.n,x);
}
skipEmpty();
//Parse attributes
i.add(x-i.n);//Move i to x
node.attrs = getAttrs();
//End the tag
if(str.substr(i.n,2)=="/>"){
i.add(2);
}else if(str[i.n]){
if(str[i.n]==">"){
structure[structure.length] = node;
i.add();
}else if(this.failonerr){
throw {line:line,collumn:col,message:"Unknown error"};
}
}else{
if(!structure.length){
return;
}else{
if(this.failonerr){
throw {line:line,collumn:col,message:"Unexpected end of the document"};
}else{
return;
}
}
}
}
}
//END Main cycle
}else{
//If the document begins with an invalid character
if(this.failonerr){
throw {line:line,collumn:col,message:"Expected '<' at the beginning of the document."};
}
while(str[x]!="<"&&str.length>x){x++;}//Get the position of a)the "<" char or b)the end of the document
this.childs[0].childs[this.childs[0].childs.length] = str.substring(i.n,x-i.n);//Insert the text into root element
i.add(x-i.n);//Move i to x
if(str.length==x){
run = false; //If x is the end of the document, do not repeat the cycle
}
}
}
var q = 0;
while(this.parse.events.length>q){
this.parse.events[q++]();
}
this.events = [];
};
this.parse.events = [];
this.parse.on = function(){
//TODO
};
this.parse.addEventListener = this.parse.on;
this.parse.rmon = function(){
//TODO
};
this.parse.removeEventListener = this.parse.rmon;
this.toDOM = function(){
/*
TODO Make a way to define namespace identifier!
*/
var ns = "";
if(this.childs[0].tag.space){
ns = "/nml/"+this.childs[0].tag.space;
}
var doc = (new Document()).implementation.createDocument(
ns, //Namespace identifier
this.childs[0].tag.name, //Set root element's tag name
(new Document()).implementation.createDocumentType(
this.namespaces[0], //Add a simple doctype
"", //No DTD, the same way as in HTML5
""
)
);
//BEGIN Main cycle
var f = function(f,e,d,x){
var node;
if(typeof e == "string"){
node = d.createTextNode(e);
x.appendChild(node);
}else{
if(e.tag.space){
node = d.createElementNS("/nml/"+e.tag.space,e.tag.name);
}else{
node = d.createElement(e.tag.name);
}
var i = 0;
for(attr in e.attrs){
node.setAttribute(attr,e.attrs[attr]);
}
x.appendChild(node);
var i = 0;
while(e.childs.length>i){
f(f,e.childs[i],d,node);
i++;
}
}
}
var i = 0;
while(this.childs[0].childs.length>i){
f(f,this.childs[0].childs[i],doc,doc.lastChild);
i++;
}
//END Main cycle
return doc;
};
this.toDOM.events = [];
this.toDOM.on = function(){
//TODO
};
this.toDOM.addEventListener = this.toDOM.on;
this.toDOM.rmon = function(){
//TODO
};
this.toDOM.removeEventListener = this.toDOM.rmon;
};