-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.d
718 lines (590 loc) · 14.4 KB
/
parsers.d
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
707
708
709
710
711
712
713
714
715
716
717
718
module sard.parsers;
/**
This file is part of the "SARD"
@license The MIT License (MIT) Included in this distribution
@author Zaher Dirkey <zaherdirkey at yahoo dot com>
*/
/**
Generate the runtime objects, it use the current Collector
*/
import std.stdio;
import std.conv;
import std.array;
import std.string;
import std.stdio;
import std.uni;
import std.datetime;
import sard.utils;
import sard.classes;
import sard.objects;
import sard.standards;
import minilib.sets;
/**---------------------------*/
/** Controls
/**---------------------------*/
enum Ctl: int
{
None = 0,
Token,//* Token like Identifier, Keyword or Number
Operator,//
Start, //* Start parsing
Stop, //* Stop parsing
Declare, //* Declare a class of object
// Let, //* Same as assign but make it as lexical scope, this variable will be seen from descent/child objects
Assign, //* Assign to object/variable used as :=
Next, //* End Params, Comma ,
End, //* End Statement Semicolon ;
OpenBlock, //* {
CloseBlock, //* }
OpenParams, //* (
CloseParams, //* )
OpenPreprocessor, //* <?
ClosePreprocessor, //* ?>
OpenArray, //* [
CloseArray //* ]
}
//TokenType
enum Type : int
{
None,
Identifier,
Number,
Color,
String,
Escape, //Maybe Strings escape
Comment
}
class SymbolicObject: BaseObject
{
public bool IsSymbol;
}
/**
* This will used in the tokenizer
*/
//TODO maybe struct not a class
class Control: SymbolicObject
{
string name;
Ctl code;
int level;
string description;
this(){
super();
}
this(string aName, Ctl aCode, string aDescription = "")
{
this();
name = aName;
code = aCode;
description = aDescription;
}
}
class Controls: NamedObjects!Control
{
Control findControl(Ctl control)
{
Control result = null;
foreach(itm; this) {
if (control == itm.code) {
result = itm;
break;
}
}
return result;
}
//getControl like find but raise exception
Control getControl(Ctl control){
if (count == 0)
error("No controls is added" ~ to!string(control));
Control result = findControl(control);
if (!result)
error("Control not found " ~ to!string(control));
return result;
}
Control add(string name, Ctl code, string description = "")
{
Control c = new Control(name, code, description);
super.add(c);
return c;
}
}
/**---------------------------*/
/** Operators
/**---------------------------*/
enum Associative {Left, Right};
class Operator: SymbolicObject
{
public:
string name; //Sign like + or -
string title;
// int precedence;//TODO it is bad idea, we need more intelligent way to define the power level of operators
Associative associative;
string description;
public:
debug(log_nodes) {
override void debugWrite(int level){
super.debugWrite(level);
writeln("operator: " ~ name);
}
}
}
class Operators: NamedObjects!Operator
{
public:
Operator findByTitle(string title)
{
foreach(itm; this)
{
if (icmp(title, itm.title) == 0)
return itm;
}
return null;
}
}
/**---------------------------*/
/** Symbol
/**---------------------------*/
class Symbol: SymbolicObject
{
public:
string name; //Sign like + or -
this(string aName)
{
name = aName;
}
}
class Symbols: NamedObjects!Symbol
{
public:
Symbol add(string name)
{
Symbol c = new Symbol(name);
super.add(c);
return c;
}
}
/**---------------------------*/
/** Token
/**---------------------------*/
struct Token
{
public:
Ctl control;
int type;
string value;
@disable this();
this(Ctl c, int t, string v)
{
type = t;
value = v;
control = c;
}
}
/**---------------------------*/
/** Tokenizer
/**---------------------------*/
/**
*
* Small object scan one type of token
*
*/
class Tokenizer: BaseObject
{
private:
Lexer _lexer;
public @property Lexer lexer() {
return _lexer;
} ;
protected:
//Return true if it done, next will auto detect it detect
abstract void scan(const string text, int started, ref int column, ref bool resume);
abstract bool accept(const string text, int column);
//This function call when switched to it
void switched() {
//Maybe reseting buffer or something
}
public:
this(){
super();
}
}
/**---------------------------*/
/** Lexer
/**---------------------------*/
class Lexer: Objects!Tokenizer {
private:
protected:
Scanner _scanner;
public @property Scanner scanner() { return _scanner; } ;
Tokenizer _current; //current tokenizer
public @property Tokenizer current() { return _current; } ;
Symbols _symbols;
@property public Symbols symbols() { return _symbols; }
Controls _controls;
@property public Controls controls() { return _controls; }
Operators _operators;
@property public Operators operators () { return _operators; }
Parser _parser;
public @property Parser parser() { return _parser; };
public @property Parser parser(Parser value) { return _parser = value; }
protected:
Tokenizer detectTokenizer(const string text, int column)
{
Tokenizer result = null;
if (column >= text.length) // compare > in pascal
{
//do i need to switchTokenizer?
//return null; //no tokenizer for empty line or EOL
//result = null; nothing to do already nil
}
else
{
foreach(itm; this)
{
if (itm.accept(text, column))
{
result = itm;
break;
}
}
if (result is null)
error("Tokenizer not found: " ~ text[column]);
}
switchTokenizer(result);
return result;
}
void switchTokenizer(Tokenizer nextTokenizer)
{
if (_current != nextTokenizer)
{
_current = nextTokenizer;
if (_current !is null)
_current.switched();
}
}
Tokenizer findClass(const ClassInfo tokenizerClass)
{
int i = 0;
foreach(itm; this) {
if (itm.classinfo == tokenizerClass) {
return itm;
}
i++;
}
return null;
}
//This find the class and switch to it
Tokenizer selectTokenizer(ClassInfo tokenizerClass)
{
Tokenizer t = findClass(tokenizerClass);
if (t is null)
error("Tokenizer not found");
switchTokenizer(t);
return t;
}
public:
override void beforeAdd(Tokenizer tokenizer)
{
super.beforeAdd(tokenizer);
tokenizer._lexer = this;
}
this(){
super();
_symbols = new Symbols();
_controls = new Controls();
_operators = new Operators();
}
~this(){
destroy(_symbols);
destroy(_controls);
destroy(_operators);
}
bool trimSymbols = true; //ommit send open and close tags when setToken
abstract bool isEOL(char vChar);
abstract bool isWhiteSpace(char vChar, bool vOpen= true);
abstract bool isSymbol(char vChar);
abstract bool isControl(char vChar);
abstract bool isOperator(char vChar);
abstract bool isNumber(char vChar, bool vOpen = true);
bool isKeyword(string keyword){
return false;
}
bool isIdentifier(char vChar, bool vOpen = true)
{
bool r = !isWhiteSpace(vChar) && !isControl(vChar) && !isOperator(vChar) &&!isSymbol(vChar);
if (vOpen)
r = r && !isNumber(vChar, vOpen);
return r;
}
final void scanLine(const string text, const int line, ref int column)
{
int len = text.length;
bool resume = false;
while (column < len) //use <= in pascal
{
int oldColumn = column;
Tokenizer oldTokenizer = current;
try
{
if (current is null) //resume the line to current/last tokenizer
detectTokenizer(text, column);
else
resume = true;
current.scan(text, column, column, resume);
if (!resume)
switchTokenizer(null);
if ((oldColumn == column) && (oldTokenizer == _current))
error("Feeder in loop with: " ~ _current.classinfo.nakename); //TODO: be careful here
}
catch(Exception e) {
throw new ParserException(e.msg, line, column);
}
}
}
void start(){
if (parser is null)
error("Parser should be not null");
parser.start();
}
void stop(){
parser.stop();
}
}
enum Action
{
Pop, //Pop the current Collector
Pass //resend the control char to the next Collector
}
alias Set!Action Actions;
/**
* @class Instruction
*/
/**
* @class Collector
* list if controller
*/
abstract class Collector: BaseObject
{
private:
protected:
Controller controller;
Parser _parser;
public @property Parser parser() { return _parser; };
void internalPost(){
}
Controller createController() {
return null;
}
public:
this(){
debug(log_compile) writeln("new collecter");
}
this(Parser aParser)
{
this();
_parser = aParser;
controller = createController();
reset();
}
~this(){
destroy(controller);
debug(log_compile) writeln("kill collecter");
}
//IsInitial: check if the next object will be the first one, usefule for Assign and Declare
@property bool isInitial()
{
return false;
}
abstract void reset();
protected abstract void doToken(Token token);
protected abstract void doControl(Control control);
final void setToken(Token token){
doToken(token);
}
final void setControl(Control control){
doControl(control);
if (!(controller is null))
controller.setControl(control);
}
}
/**
* @class Controller
*/
abstract class Controller: BaseObject
{
protected:
Collector collector;
public:
this(Collector aCollector){
super();
collector = aCollector;
}
abstract void setControl(Control control);
}
/**
* @class Parser
*
*/
class Parser: Stack!Collector
{
protected:
Actions actions;
Collector nextCollector;
bool isKeyword(string identifier){
return false;
}
public:
void setToken(Token token){
}
void setControl(Control control){
}
void setOperator(Operator operator){
}
void setWhiteSpaces(string whitespaces){
}
void start(){
if (current is null)
error("At last you need one collector pushed");
}
void stop(){
}
//No pop, but when finish Parser will pop it
void setAction(Actions aActions = [], Collector aNextCollector = null)
{
actions = aActions;
nextCollector = aNextCollector;
}
}
/**---------------------------*/
/** Scanner
/**---------------------------*/
class Scanner: Objects!Lexer
{
private:
bool _active;
public @property bool active() { return _active; }
string _ver;
public @property string ver() { return _ver; }
string _charset;
public @property string charset() { return _charset; }
//TODO: use env to wrap the code inside <?sard ... ?>,
//the current one must detect ?> to stop scanning and pop
//but the other lexer will throw none code to output provider
int _line;
public @property int line() { return _line; };
protected:
Parser _parser;
public @property Parser parser() { return _parser; }
Lexer current; //current lexer
protected:
override void beforeAdd(Lexer lexer)
{
super.beforeAdd(lexer);
lexer._scanner = this;
}
void doStart() {
}
void doStop() {
}
abstract Parser createParser();
public:
this()
{
super();
}
~this(){
}
public:
void scanLine(const string text, const int line)
{
if (!active)
error("Should be started first");
_line = line;
int column = 0;
//int column = 1; when convert to pascal
current.scanLine(text, line, column);
}
void scan(const string[] lines)
{
start();
int i = 0;
while(i < lines.count())
{
scanLine(lines[i] ~ "\n", i + 1);//TODO i hate to add \n it must be included in the lines itself
i++;
}
stop();
}
/*void scan(InputStream stream)
{
char[] line;
start();
int i = 0;
while (stream.eof) {
line = stream.readLine(); //TODO readLineW
scanLine(to!string(line), i);
i++;
}
stop();
}
*/
void scan(const string text)
{
string[] lines = text.split("\n");
scan(lines);
}
/* void scanFile(string filename)
{
BufferedFile stream = new BufferedFile(filename);
scope(exit) destroy(stream);
try {
scanStream(stream);
}
finally {
stream.close();
}
}*/
void scanFile(string filename)
{
auto file = File(filename, "r");
auto lines = file.byLine();
start();
int i = 0;
foreach(char[] line; lines)
{
line = line ~ "\n";
scanLine(to!string(line) , i); //TODO i hate to add \n it must be included in the lines itself
i++;
}
stop();
}
/* void scanStream(Stream stream)
{
scan(stream);
}
*/
void start()
{
if (_active)
error("Already opened");
if (count == 0)
error("There is no lexers added");
_active = true;
current = this[0]; //First one
_parser = createParser();
current.parser = _parser;
current.start();
doStart();
}
void stop()
{
if (!_active)
error("Already closed");
doStop();
current.stop();
current.parser = null;
destroy(_parser);
current = null;
_active = false;
}
}