-
Notifications
You must be signed in to change notification settings - Fork 0
/
wml.js
711 lines (604 loc) · 17.1 KB
/
wml.js
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Main entry point when the user clicks compile
function compile() {
var inArea = document.getElementById('idtextarea');
var outDiv = document.getElementById('iddiv');
var inText;
// get input text
if (inArea.value) inText=inArea.value;
else inText = inText.innerHTML;
setDebugText(inText);
// parse and return the output
var outText = ASTtoString(startParsing(inText));
// display result, letting the browser interpret it as HTML
outDiv.innerHTML = outText;
}
// These two functions are for your use, to display debug information.
// Clear existing debug text and display s
function setDebugText(s) {
var dbgDiv = document.getElementById('iddebug');
while (dbgDiv.firstChild)
dbgDiv.removeChild(dbgDiv.firstChild);
addDebugText(s);
}
// Append s to existing debug text
function addDebugText(s) {
var dbgDiv = document.getElementById('iddebug');
var outNL = s.split(/\n/);
for (var i=0;i<outNL.length;i++) {
dbgDiv.appendChild(document.createTextNode(outNL[i]));
dbgDiv.appendChild(document.createElement('br'));
}
}
// Within here you should call your code to parse the input string.
// Whatever this function returns will be shown at the top of the page.
function startParsing(inText) {
return parse(inText);
}
// Within here you should call your code to convert your AST to a string.
function ASTtoString(a) {
return printAST(a);
//return '<i style="color:red;">...time...running...out...must...start...assignment...</i>';
}
//---------------Question 1----------------
//
//I will use Regex object for recognizing the tokens
var TSTART = new RegExp("^ *{{");
var TEND = new RegExp("^ *}}");
var PIPE = new RegExp("^ *\\|");
var OUTERTEXT = new RegExp(/.+?(?={{|{:|:}|$)/); //!TSTART,DSTART, can be PSTART TODO do not catch PSTART
var INNERTEXT = new RegExp(/.+?(?=\||{{|{:|}})/); //match everything but TSTART (PSTART is included),DSTART, PIPE, TEND using positive lookahead
var DSTART = new RegExp("^ *{:");
var DEND = new RegExp("^ *:}");
var INNERDTEXT = new RegExp(/.+?(?=\||{{|{:|:})/); //! TSTART, DSTART, PIPE, PSTART, DEND
var PSTART = new RegExp("^ *{{{");
var PEND = new RegExp("^ *}}}");
var PNAME = new RegExp(/.+?(?=\||}}})/); //anything but PIPE and PEND
//TODO, include new line
//Q1 tests//
/*
var str = "{: hello | you | Hi there <b> {{{ you }}} </b>:}";
var str2 = " hello | you | Hi there <b> {{{ you }}} this {{ t }} </b>:}";
//var reg = new RegExp("/.+?(?={{{/");
var reg2 = new RegExp(/.+?(?=\||{{|{:|:})/);
found = reg2.exec(str2);
//var found = str2.match(reg);
console.log(found);
*/
//----------------Question 2----------------
//
// Test function
function scanit(s) {
var sout = "";{
while (s) {
var t = scan (s ,TOKENSET);
sout += t.tokenvalue;
s = s.substr(t.tokenvalue.length);
}
}
return sout;
}
var TOKENSET = {
PSTART: true,
PEND: true,
TSTART: true,
TEND: true,
PIPE: true,
DSTART: true,
DEND: true,
INNERTEXT: true,
INNERDTEXT: true,
PNAME: true,
OUTERTEXT: true
}
//return the first token object
function scan(string, TOKENSET){
for (var possibleToken in TOKENSET){
var result = null;
var re = eval(possibleToken);
result = re.exec(string);
if (result != null){
return {token:possibleToken, tokenvalue:result[0]};
}
}
}
//More tests
//console.log(scan(str2,TOKENSET));
var testset = {
str1 : "{: foo | test | {{test}} {{{t}}} t:}",
str2 : "{{ test | {{{bar}}}}}",
str3 : "{:says | x | y | {{{x}}} says say | {{{y}}} }} :}",
str4 : "{{ {{ getfunc | say }} | there }}",
str5 : "{:factorial | n | {{#ifeq | {{{n}}} | 0 | 1 | {{times | {{{n }}} | {{factorial | {{#expr | {{{n}}} -1 }} }} }} }} :}"
}
//----------Question 3---------------
//Parse in AST, one node per grammar rule applied.
//Each node have name (rule name - non terminal) and fields for each (non terminal) child objecet, including tokens
//where the content depends on the input string (OUTER/INNERTEXT, PNAME, DNAME).
//Where choice is possible, unused fields should have the value null
//First, scan tokens
//Those functions use scan and return AST
function parseOuter(s){
//The parameter is in fact an object that contains a string so it is passed by reference to modify it within any parse function
var Outset = {
TSTART : true,
DSTART : true,
OUTERTEXT : true
}
if (!s.str){ //Check for end of string
return null;
}
var t = removeToken(s,Outset);
switch (t.token) {
case "OUTERTEXT":
return {
name: "outer",
OUTERTEXT: t.tokenvalue,
templateinvocation: null,
templatedef: null,
next: parseOuter(s)
}
case "TSTART": //Template invocation
return {
name: "outer",
OUTERTEXT: null,
templateinvocation: parseTemplateInvocation(s),
templatedef: null,
next: parseOuter(s)
}
case "DSTART": //Definition invocation
return {
name: "outer",
OUTERTEXT: null,
templateinvocation: null,
templatedef: parseTemplateDef(s),
next: parseOuter(s)
}
}
}
function parseTemplateInvocation(s){
// return template invocation ADT
//<templateinvocation> ::= TSTART <itext> <targs> TEND
var Inset = {
PSTART : true,
TSTART : true,
DSTART : true,
PIPE : true,
TEND : true,
INNERTEXT : true,
}
if (!s.str){ //Check for end of string
return null;
}
var rvalue = {
name : "templateinvocation",
itext : null,
targs : null
}
//Since we assume not empty and legal syntax, first part will be itext up to PIPE or TEND
var ans = parseIText(s);
rvalue.itext = ans.node;
//var nextToken = ans.nextToken; //last token get from the string, Should be TEND or PIPE
switch(ans.nextToken){
case "TEND":
break;
case "PIPE":
rvalue.targs = parseTArgs(s);
break;
}
return rvalue;
}
function parseTemplateDef(s){
//Similar to templateinvocation
var Inset = {
PSTART : true,
TSTART : true,
DSTART : true,
PIPE : true,
DEND : true,
INNERDTEXT : true,
}
if (!s.str){ //Check for end of string
return null;
}
var rvalue = {
name : "templatedef",
dtext : null,
dargs : null
}
//Since we assume not empty and legal syntax, first part will be itext up to PIPE or DEND
var ans = parseDText(s);
rvalue.dtext = ans.node;
//var nextToken = ans.nextToken; //last token get from the string, Should be TEND or PIPE
switch(ans.nextToken){
case "DEND":
break;
case "PIPE":
rvalue.dargs = parseDArgs(s);
break;
}
return rvalue;
}
function parseTParam(s){
if (!s.str){ //Check for end of string
return null;
}
var rvalue = {
name : "tparam",
PNAME : null
}
var tset = {
PNAME : true
}
var t = removeToken(s,tset);
rvalue.PNAME = t.tokenvalue;
removeToken(s, {PEND : true}); //remove the token PEND since it will not be usefull (No error checking, expecting good input)
return rvalue;
}
function parseIText(s){
var Inset = {
PSTART : true,
TSTART : true,
DSTART : true,
PIPE : true,
TEND : true,
INNERTEXT : true
}
if (!s.str){ //Check for end of string
return null;
}
var rvalue = {
name : "itext",
INNERTEXT : null,
templateinvocation : null,
templatedef : null,
tparam : null,
next : null
}
var t = null;
t = removeToken(s, Inset);
switch (t.token) {
case "INNERTEXT": //Set itext INNERTEXT and parse next itext
rvalue.INNERTEXT = t.tokenvalue;
break;
case "TSTART":
rvalue.templateinvocation = parseTemplateInvocation(s);
break;
case "DSTART":
rvalue.templatedef = parseTemplateDef(s);
break;
case "PSTART":
rvalue.tparam = parseTParam(s);
break;
case "PIPE":
return {
node: null,
nextToken: t.token
}
case "TEND":
return {
node: null,
nextToken: t.token
}
}
var ans = parseIText(s); //parse the next itext
var nextToken = null;
if(ans.nextToken == "PIPE" || ans.nextToken == "TEND"){
nextToken = ans.nextToken
}
rvalue.next = ans.node;
return {
node: rvalue,
nextToken: nextToken //should be either PIPE or TEND
}
}
function parseDText(s){
var InDset = {
PSTART: true,
TSTART: true,
DSTART: true,
PIPE: true,
DEND: true,
INNERDTEXT: true
}
if (!s.str) { //Check for end of string
return null;
}
var rvalue = {
name: "dtext",
INNERDTEXT: null,
templateinvocation: null,
templatedef: null,
tparam: null,
next: null
}
var t = null;
t = removeToken(s, InDset);
switch (t.token) {
case "INNERDTEXT": //Set itext INNERTEXT and parse next itext
rvalue.INNERDTEXT = t.tokenvalue;
break;
case "TSTART":
rvalue.templateinvocation = parseTemplateInvocation(s);
break;
case "DSTART":
rvalue.templatedef = parseTemplateDef(s);
break;
case "PSTART":
rvalue.tparam = parseTParam(s);
break;
case "PIPE":
return {
node: null,
nextToken: t.token
}
case "DEND":
return {
node: null,
nextToken: t.token
}
}
var ans = parseDText(s); //parse the next itext
var nextToken = null;
if (ans.nextToken == "PIPE" || ans.nextToken == "DEND") {
nextToken = ans.nextToken
}
rvalue.next = ans.node;
return {
node: rvalue,
nextToken: nextToken //should be either PIPE or TEND
}
}
function parseTArgs(s){
if (!s.str){ //Check for end of string
return null;
}
var rvalue = {
name : "targs",
itext : null,
next : null
}
//Since we assume not empty and legal syntax, first part will be itext up to PIPE or TEND
var ans = parseIText(s);
rvalue.itext = ans.node;
var t = ans.nextToken; //last token get from the string, Should be TEND or PIPE
switch(ans.nextToken){
case "TEND":
break;
case "PIPE":
rvalue.next = parseTArgs(s);
break;
}
return rvalue;
}
function parseDArgs(s){
if (!s.str){ //Check for end of string
return null;
}
var rvalue = {
name : "dargs",
dtext : null,
next : null
}
//Since we assume not empty and legal syntax, first part will be itext up to PIPE or TEND
var ans = parseDText(s);
rvalue.dtext = ans.node;
var t = ans.nextToken; //last token get from the string, Should be TEND or PIPE
switch(ans.nextToken){
case "TEND":
break;
case "PIPE":
rvalue.next = parseDArgs(s);
break;
}
return rvalue;
}
function removeToken(s, tset){ //take a string and a tokenset
var t = scan(s.str, tset);
s.str = s.str.substr(t.tokenvalue.length);
return t;
}
function parse(s){
//Define the global string that will be consumed token by token
var consume = {str: s};
return parseOuter(consume);
}
//<<<<<<< HEAD
var teststr = "outer 1 {{ invoc2 | param2 }} outer2";
//=======
//helper
function printASTIndent(node, tabVal){
if(typeof tabVal === 'undefined'){
tabVal = 0;
}
var tabs = "";
for(var i = 0; i < tabVal; i++){
tabs= tabs.concat(" ");
}
var result = "";
for(var param in node){
if (node.hasOwnProperty(param)){
var curNode;
if(typeof node[param] === 'object' && node[param] !== null){
curNode ='\n' + printASTIndent(node[param], tabVal+1);
result +=tabs + param +":" + curNode;
}
else{
curNode = node[param];
result +=tabs + param +":" + curNode + '\n';
}
}
}
return result;
}
//var teststr = "outer 1 {{invoc {{ invoc2 | param2 }}| param }} outer2";
//var teststr = "{:definition|arg1 {{{you}}} :}";
//>>>>>>> cee6dc78f8c96736655e0d2c876a712a93f6aa69
var teststr = "{{invocation||||||}}"
var done = parse(teststr);
//console.log(done);
console.log("Print after this");
var pstr = printAST(done);
console.log(pstr);
console.log(printAST(parse(pstr)));
//console.log(printASTIndent(done, 4));
//TODO investigate line feed and trim
//-----------------Question 4---------------------
function printAST(a){
return printOuter(a);
}
function printOuter(a){
var rvalue = "";
for (var rule in a) {
if (!a[rule]) {
continue; //property is null, next property
}
switch (rule) {
case 'OUTERTEXT':
rvalue += a[rule];
break;
case 'templateinvocation':
rvalue += printTInvoc(a[rule]);
break;
case 'templatedef':
rvalue += printTDef(a[rule]);
break;
case 'next':
rvalue += printOuter(a[rule]);
}
}
return rvalue;
}
function printTInvoc(a){
var rvalue = "";
for (var rule in a) {
if (!a[rule]) {
continue; //property is null, next property
}
switch (rule) {
case 'itext':
rvalue += printIText(a[rule]);
break;
case 'targs':
rvalue += printTArgs(a[rule]);
break;
}
}
return "{{" + rvalue + "}}";
}
function printTDef(a){
var rvalue = "";
for (var rule in a) {
if (!a[rule]) {
continue; //property is null, next property
}
switch (rule) {
case 'dtext':
rvalue += printDText(a[rule]);
break;
case 'dargs':
rvalue += printDArgs(a[rule]);
break;
}
}
return "{:"+rvalue+":}";
}
function printDText(a){
var rvalue = "";
for (var rule in a) {
if (!a[rule]) {
continue; //property is null, next property
}
switch (rule) {
case 'INNERDTEXT':
rvalue += a[rule];
break;
case 'templateinvocation':
rvalue += printTInvoc(a[rule]);
break;
case 'templatedef':
rvalue += printTDef(a[rule]);
break;
case 'tparam':
rvalue += printTParam(a[rule]);
break;
case 'next':
rvalue += printDText(a[rule]);
break;
}
}
return rvalue;
}
function printDArgs(a){
var rvalue = "";
for (var rule in a) {
if (!a[rule]) {
continue; //property is null, next property
}
switch (rule) {
case 'dtext':
rvalue += printDText(a[rule]);
break;
case 'next':
rvalue += printDArgs(a[rule]);
break;
}
}
return "|" + rvalue;
}
function printIText(a){
var rvalue = "";
for(var rule in a){
if(!a[rule]){
continue; //property is null, next property
}
switch(rule){
case 'INNERTEXT':
rvalue+=a[rule];
break;
case 'templateinvocation':
rvalue+= printTInvoc(a[rule]);
break;
case 'templatedef':
rvalue+= printTDef(a[rule]);
break;
case 'tparam':
rvalue+= printTParam(a[rule]);
break;
case 'next':
rvalue+= printIText(a[rule]);
break;
}
}
return rvalue;
}
function printTParam(a){
var rvalue = "";
for(var rule in a){
if(!a[rule]){
continue; //property is null, next property
}
switch(rule){
case 'PNAME':
rvalue+=a[rule];
break;
}
}
return "{{{"+rvalue+"}}}";
}
function printTArgs(a){
var rvalue = "";
for(var rule in a){
if(!a[rule]){
continue; //property is null, next property
}
switch(rule){
case 'itext':
rvalue+=printIText(a[rule]);
break;
case 'next':
rvalue+=printTArgs(a[rule]);
break;
}
}
return "|"+rvalue;
}