-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
832 lines (721 loc) · 25.2 KB
/
Copy pathparser.cpp
File metadata and controls
832 lines (721 loc) · 25.2 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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#include "parser.h"
#include "..\helpers\helpers.h"
#include "..\helpers\errors.h"
#include "..\constants\constants.h"
#include "..\ast\ast.h"
#include<iostream>
#include<string.h>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;
#define LOG_TOKEN(msg) std::cout << __LINE__ << " Current Token : " << tokens[index].to_string();\
std::cout << "\t-MSG:" << msg << std::endl;
Parser::Parser(std::vector<Token> _tokens, std::vector<Expression>* _returnExprs, std::vector<Statement*>* _stmnts) : tokens(_tokens), returnExpressions(_returnExprs), stmnts(_stmnts), index(0),logger("Parser"){}
void Parser::parse(){
_parse();
}
void Parser::_parse(){
std::cout << "Parsing..." << std::endl;
while(hasNextToken()){
Token tk = tokens[index];
LOG_TOKEN("Parser Loop Token");
if(tk.type == END_OF_FILE) break;
if(tk.type >= TOKEN_KEYWORD_START && tk.type <= TOKEN_KEYWORD_END) handleKeyword(returnExpressions);
if(tk.type == IDENTIFIER) handleIdentifier();
index++;
}
std::cout << "******* Tokens Parsed without Errors ******* " << std::endl;
}
void Parser::skipCircularBracketStart(){
if(cnext() && gnext().type == CIRCLEBRACKETSTART) index++;
}
void Parser::skipCircularBracketEnd(){
if(cnext() && gnext().type == CIRCLEBRACKETSTART) index++;
}
void Parser::skipSemiColon(){
if(cnext() && gnext().type == SEMI) index++;
}
/*
Returns has next token or not.
*/
bool Parser::cnext(){
return index < tokens.size() - 1;
}
/*
Returns next token or throws Error.
*/
Token Parser::gnext(){
if(cnext()){
return tokens[index+1];
}else{
Token current = tokens[index];
fire_syntax_error("Syntax Error next element required after '" + current.data + "'", current.columnno, current.lineno, current.file_path);
}
}
/*
Returns next token and increments index or throws Error.
*/
Token Parser::ignext(){
Token tk = gnext();
index++;
return tk;
}
/*
type :- type of Token we expect to be the next.
*/
bool Parser::expect(int type){
if(!cnext()) return false;
return tokens[index+1].type == type;
}
void Parser::addExpression(Expression exp, std::vector<Expression>* returnExprs = nullptr){
if(returnExprs == nullptr) returnExpressions->push_back(exp);
else returnExprs->push_back(exp);
}
void Parser::addStatement(Statement exp, std::vector<Statement*>* stmntSptrs = nullptr){
if(stmntSptrs == nullptr) stmntSptrs->push_back(&exp);
else stmntSptrs->push_back(&exp);
}
void Parser::handleKeyword(std::vector<Expression>* returnExprs = nullptr){
Token tk = tokens[index];
std::string skeyword = tk.data;
LOG_TOKEN("Keyword Token");
if (tk.type == K_PUBLIC || tk.type == K_PRIVATE || tk.type == K_PROTECTED){
switch (gnext().type) //gnext() throws error if there no next Token
{
case K_CLASS:
index++;
handleKeyword(returnExprs);
break;
default:
fire_syntax_error("Expected 'class' after access modifier got '" + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
break;
}
}
if (skeyword == "class"){
//Previous token mostly will be an access modifier
access_modifier_type access;
if(index != 0){
Token tk = tokens[index-1];
switch (tk.type)
{
case K_PUBLIC:
access = ACCESS_PUBLIC;
break;
case K_PRIVATE:
access = ACCESS_PRIVATE;
break;
case K_PROTECTED:
access = ACCESS_PROTECTED;
break;
default:
break;
}
}else{
//Default would be Private
access = ACCESS_PRIVATE;
}
/*
ACCESS_MODIFIER class IDENTIFIER > parent_classes {
A) ---- assignments or declarations ----
B) ---- functions ----
init(){}
clear(){}
custom functions
C) ---- complex statements (for latter) ----
}
*/
LOG_TOKEN("Keyword.class Token before Identifier");
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = IDENTIFIER
LOG_TOKEN("Keyword.class Token after Identifier");
ClassDef cdef(tokens[index].data, access);
LOG_TOKEN("Keyword.class Token before '('");
if(!expect(CIRCLEBRACKETSTART)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '(' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CIRCLEBRACKETSTART
/* Parse Arguments */
cdef.params.clear(); //As Params are not supported for now TODO()
LOG_TOKEN("Keyword.class Token before ')'");
if(!expect(CIRCLEBRACKETEND)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected ')' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CIRCLEBRACKETEND
LOG_TOKEN("Keyword.class Token before Superclasses '>' ");
/* Parse Superclasses */
if(cnext() && !expect(CURLYBRACKETSTART)){
//If next token is not '{' there has to be superclass assigned
if(!expect(LGREATER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '>' got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = LGREATER
LOG_TOKEN("Keyword.class Token before Superclasses");
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected a superclass identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = IDENTIFIER or Data Type
std::string superclass_name = tokens[index].data;
cdef.superclasses.push_back(superclass_name);
std::cout << "Saved superclass '" << superclass_name << std::endl;
while (cnext() && !expect(LINE_END) && !expect(CURLYBRACKETSTART))
{
bool commaNeeded = index+2 < tokens.size() && tokens[index+2].type == IDENTIFIER;
if(commaNeeded){
if(!expect(COMMA)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected a ',' before superclass name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //token = ','
}
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected a superclass identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = IDENTIFIER or Data Type
superclass_name = tokens[index].data;
cdef.superclasses.push_back(superclass_name);
std::cout << "Saved superclass '" << superclass_name << std::endl;
}
}
if(expect(LINE_END)){
//function body is on next line
index++;
}
LOG_TOKEN("Keyword.class Token before '{'");
if(!expect(CURLYBRACKETSTART)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '{' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CURLYBRACKETSTART
if(expect(LINE_END)){
//function body is on next line
index++;//current token = LINE_END
}
/* Parse Class Body */
while(cnext() && !expect(CURLYBRACKETEND)){
LOG_TOKEN("Keyword.class Token from While before increment");
index++;
//Parse each line of function body
if(tokens[index].type == LINE_END){
std::cout<<"Skipping Line ending\n";
//function body is on next line
continue;
}
handleKeyword(&cdef.body);
std::cout << index << "] Parsed Exp > Exps : " << cdef.body.size() << std::endl;
LOG_TOKEN("Keyword.class Token from While after increment");
cdef.tree();
}
if(expect(LINE_END)){
//function body is on next line
index++;
}
LOG_TOKEN("Keyword.class Token before '}'");
if(!expect(CURLYBRACKETEND)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '}' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CURLYBRACKETEND
LOG_TOKEN("Keyword.class Token completion");
std::cout << "class() : PARSED" << std::endl;
cdef.tree();
}
/* VARIABLE DECLARATION OR ASSIGNMENT */
if (skeyword == "int"){
/// int IDENTIFIER '=' expr
LOG_TOKEN("Keyword.int Token before Identifier");
std::cout << "int() -> " << tokens[index].data << std::endl;
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
std::string name = tokens[index].data;
Statement::Structs::Assign intAssign(name);
Statement::Structs::Assign intTest(name);
LOG_TOKEN("Keyword.int Token after Identifier");
LOG_TOKEN("Keyword.int Token before Equal");
if(!expect(EQUAL)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '=' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
LOG_TOKEN("Keyword.int Token after Equal.1");
index++;
LOG_TOKEN("Keyword.int Token after Equal.2");
//Parse Expr
Expression exp = parseExpr();
intAssign.exp = exp;
LOG_TOKEN("Keyword.int Token after parseExpr");
addExpression(exp, returnExprs);
std::cout << "int() -> " << "PARSING COMPLETED" << std::endl;
return;
}
if (skeyword == "string"){
/// string IDENTIFIER '=' expr
LOG_TOKEN("Keyword.string Token before Identifier");
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
std::string name = tokens[index].data;
LOG_TOKEN("Keyword.string Token after Identifier");
if(!expect(EQUAL)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '=' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
LOG_TOKEN("Keyword.string Token after Equal.1");
index++;
LOG_TOKEN("Keyword.string Token after Equal.2");
//Parse Expr
Expression exp = parseExpr();
LOG_TOKEN("Keyword.string Token after parseExpr");
addExpression(exp, returnExprs);
std::cout << "string() -> " << "PARSING COMPLETED" << std::endl;
return;
}
if (skeyword == "bool"){
/// bool IDENTIFIER '=' expr
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = IDENTIFIER
if(!expect(EQUAL)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '=' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = EQUAL
index++; //current token = EXPRESSION
//Parse Expr
Expression exp = parseExpr();
addExpression(exp, returnExprs);
std::cout << "bool() -> " << "PARSING COMPLETED" << std::endl;
}
if (skeyword == "float"){
/// float IDENTIFIER '=' expr
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
if(!expect(EQUAL)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '=' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
index++;
//Parse Expr TODO()
}
if (skeyword == "return"){
index++;// currentToken = return
std::vector<Expression> exprs;
while(!expect(LINE_END) && cnext()){
Expression e = (parseExpr());
if(!e.isnull) exprs.push_back(e);
else{
Token current = tokens[index];
if(!current.type == IDENTIFIER){
fire_syntax_error("Expected an expression or identifier, got '" + current.data + "'", current.columnno, current.lineno, current.file_path);
}else{
// Expression ex(TYPE_IDENTIFIER);
// exprs.push_back() TODO()
}
}
index++;
}
std::cout << "Return statement parsed.\n";
return;
// return
// return IDENTIFIER
}
if (skeyword == "for"){
/*
important ->
object itr(...)
increment i++
for i, x in itr([x]):
.......
for x in [x]:
.......
*/
bool traverseMode = false;
std::string name1(""), name2("");
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
name1 = tokens[index].data;
if(index+2 < tokens.size() && tokens[index+2].type == IDENTIFIER){
if(!expect(COMMA)){
fire_syntax_error("Expected ',' before '" + tokens[index+2].data + "' got '" + gnext().data + "'", gnext().columnno, gnext().lineno, gnext().file_path);
}else{
traverseMode = true;
index++;// token = ','
index++;// token = IDENTIFIER
name2 = tokens[index].data;
}
}
if(!expect(K_IN)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected 'in' got '" + gnext().data + "'", gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;//token = in
if(traverseMode){
//expect traverse(array)
}else{
//expect range(x1, x2)
//expect range(x2)
//expect array
}
//index++;//
//Temp loop to skip iterator part
while(cnext() && !expect(CURLYBRACKETSTART) && !expect(LINE_END)){
index++;
}
if(!expect(CURLYBRACKETSTART)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '{' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CURLYBRACKETSTART
/* Parse For Loop Body */
//Temp loop to skip body/block part
while(cnext() && !expect(CURLYBRACKETEND)){
index++;
}
if(!expect(CURLYBRACKETEND)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '}' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CURLYBRACKETEND
LOG_TOKEN("Keyword.for PARSED");
}
if (skeyword == "while"){
/*
while exp:
.......
while true : [int i] ->
.......
*/
}
if (skeyword == "when"){
/*
when(int i){
exp1: {...}
exp2: {...}
else: {...}
}
when(int i){ [int i] ->
it.x.doY() == exp2: {...}
it.isZ: {...}
exp1: {...}
else: {...}
}
*/
}
if (skeyword == "fun"){
///1) fun IDENTIFIER '(' ')' > RETURN_TYPE '{' expr '}'
///2) fun IDENTIFIER '(' ')' > RETURN_TYPE
/// '{' expr '}'
///3) fun IDENTIFIER '(' ')' '{' expr '}' //if void type no return type required
LOG_TOKEN("Keyword.fun Token before Identifier");
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = IDENTIFIER
LOG_TOKEN("Keyword.fun Token after Identifier");
FunctionDef fdef(tokens[index].data);
LOG_TOKEN("Keyword.fun Token before '('");
if(!expect(CIRCLEBRACKETSTART)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '(' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CIRCLEBRACKETSTART
/* Parse Arguments */
fdef.params.clear(); //As Params are not supported for now TODO()
LOG_TOKEN("Keyword.fun Token before ')'");
if(!expect(CIRCLEBRACKETEND)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected ')' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CIRCLEBRACKETEND
LOG_TOKEN("Keyword.fun Token before Return Type");
/* Parse Return Type */
if(cnext() && !expect(CURLYBRACKETSTART)){
//If next token is not '{' there has to be '> RETURN_TYPE'
if(!expect(LGREATER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '>' got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = LGREATER
LOG_TOKEN("Keyword.fun Token before Return Identifier");
if(!expect(IDENTIFIER) && !expect(K_INT) && !expect(K_STRING) && !expect(K_BOOL)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected return type got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = IDENTIFIER or Data Type
Token return_type_token = tokens[index];
fdef.return_type = return_type_token.data;
}else fdef.return_type = "void";
if(expect(LINE_END)){
//function body is on next line
index++;
}
LOG_TOKEN("Keyword.fun Token before '{'");
if(!expect(CURLYBRACKETSTART)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '{' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CURLYBRACKETSTART
if(expect(LINE_END)){
//function body is on next line
index++;//current token = LINE_END
}
/* Parse Function Body */
while(cnext() && !expect(CURLYBRACKETEND)){
LOG_TOKEN("Keyword.fun Token from While before increment");
index++;
//Parse each line of function body
if(expect(LINE_END)){
std::cout<<"Skipping Line ending\n";
//function body is on next line
index++;
continue;
}
handleKeyword(&fdef.body);
std::cout << index << "] Parsed Exp > Exps : " << fdef.body.size() << std::endl;
LOG_TOKEN("Keyword.fun Token from While after increment");
}
if(expect(LINE_END)){
//function body is on next line
index++;
}
LOG_TOKEN("Keyword.fun Token before '}'");
if(!expect(CURLYBRACKETEND)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected '}' or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //current token = CURLYBRACKETEND
LOG_TOKEN("Keyword.fun Token completion");
std::cout << "function() : PARSED" << std::endl;
fdef.tree();
}
if (skeyword == "if"){
//if (exp) { exps }
}
if (skeyword == "else"){
}
if (skeyword == "delete"){
//del [IDENTIFIER](s)
LOG_TOKEN("Keyword.delete Token before Identifier");
std::vector<tp_identifier> names;
int i = -1;
while (!expect(LINE_END) && cnext())
{
i++; //Required to decide whether a Comma is needed or not.
//Incase of first iteration and last there should be 'no' comma
//Eg: del num, r
if(i!=0){
if(!expect(COMMA) && index+2 < tokens.size() && tokens[index+2].type == IDENTIFIER){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected a ',' before Idenntifier got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //token = ','
}
if(!expect(IDENTIFIER)){
if(!cnext()) gnext(); //Throws error in the method itself
fire_syntax_error("Expected Identifier or name got " + gnext().data, gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
names.push_back(tokens[index].data);
LOG_TOKEN("Keyword.delete Token after while iteration.")
}
LOG_TOKEN("Keyword.delete Token after completion")
std::cout << "Delete (" << names.size() << ")" << std::endl;
}
if (skeyword == "as"){
//[IDENTIFIER as IDENTIFIER
}
if (skeyword == "using"){
//UNKNOWN SYNTAX
}
}
//TODO(Use Return type as pointers)
Expression Parser::parseExpr(int id){
id++;
LOG_TOKEN("ParseExpr Token on called");
if(tokens[index].type == LINE_END){
fire_syntax_error("Parser cannot start from line ending.", gnext().columnno, gnext().lineno, gnext().file_path);
}
//STRING EXPRESSION PARSING
if(tokens[index].type == STRING){
StringExprPoint str = parseStringExpr();
Expression expstr(STRING_EXP);
expstr.stringExp = str;
return expstr;
}
//BOOL EXPRESSION PARSING
if(tokens[index].type == K_FALSE || tokens[index].type == K_TRUE ){
BoolExprPoint _bool = parseBoolExpr();
Expression expbool(BOOL_EXP);
expbool.boolExp = _bool;
return expbool;
}
//INTEGER EXPRESSION PARSING
if(tokens[index].type == INTEGER){
NumberExprPoint num = parseIntExpr();
Expression expnum(NUMBER_EXP);
expnum.numExp = num;
return expnum;
}
Expression nullVal;
return nullVal;
}
StringExprPoint Parser::parseStringExpr(int id){
if (tokens[index].type != STRING){
Token current = tokens[index];
fire_illegal_argument_error("Token at current index is not of type String but String was required got '"+current.data+"'", current.columnno, current.lineno, current.file_path);
}
std::string left = tokens[index].data;
if(gnext().type == LINE_END || gnext().type == END_OF_FILE){
StringExprPoint leftExp(left);
leftExp.s2 = "";
leftExp.str_op = STR_ADD;
leftExp.sHasExpr = false;
std::cout << "Returning String Expression with simple value." << std::endl;
return leftExp;
}
if(!expect(PLUS) && !expect(STAR)){
fire_syntax_error("Expected '+' '*' got '" + gnext().data + "'", gnext().columnno, gnext().lineno, gnext().file_path);
}
index++; //token = '+' or '*'
index++;
char op = tokens[index].data[0];
StringExprPoint right;
while(tokens[index].type != LINE_END && cnext()){
right = parseStringExpr();
index++;
}
if(op == '+'){
StringExprPoint exp(left);
exp.se2 = &right;
exp.str_op = STR_ADD;
exp.sHasExpr = true;
std::cout << "Returning String Expression with complex value." << std::endl;
return exp;
}
//NOT CURRENTLY SUPPORTED
if(op == '*'){
if(true){//right->type != NUMBER_EXP){
index-=2;//TODO
fire_syntax_error("Expected an int got '", gnext().columnno, gnext().lineno, gnext().file_path); //+ right.type);
}
// StringExprPoint expM(left);
// exp.multiplier = right->child.numExp;
// exp.op = STR_MUL;
// exp.hasExpr = true;
// std::cout << "Returning String Expression with value ";
// exp.tree();
// std::cout << std::endl;
// return expM;
}
}
BoolExprPoint Parser::parseBoolExpr(int id){
if (tokens[index].type != K_FALSE && tokens[index].type != K_TRUE){
Token current = tokens[index];
fire_illegal_argument_error("Token at current index is not of type Boolean but Boolean was required, got '"+current.data+"'", current.columnno, current.lineno, current.file_path);
}
BoolExprPoint left = string_to_bool(tokens[index].data);
if (left.isnull){
fire_syntax_error("Expected boolean ('true' 'false') value got '" + tokens[index].data + "'", gnext().columnno, gnext().lineno, gnext().file_path);
}
if(gnext().type == LINE_END){
BoolExprPoint exp(left);
std::cout << "Returning Boolean Expression with value " << exp.value << std::endl;
return exp;
}else{
fire_syntax_error("Operations not allowed on Boolean type.", gnext().columnno, gnext().lineno, gnext().file_path);
}
}
NumberExprPoint Parser::parseIntExpr(int id){
LOG_TOKEN("ParseINTExpr Token on called");
if (tokens[index].type != INTEGER){
Token current = tokens[index];
fire_illegal_argument_error("Token at current index is not of type Integer but Integer was required, got '"+current.data+"'", current.columnno, current.lineno, current.file_path);
}
std::cout << id << "] parseExpr() -> " << tokens[index].data << std::endl;
int left = string_to_int(tokens[index].data);
if(gnext().type == LINE_END){
NumberExprPoint leftNumExp(left);
leftNumExp.n2 = 0;
leftNumExp.ne2 = nullptr;
leftNumExp.op = OP_ADD;
leftNumExp.hasExpr = false;
std::cout << "Returning Integer Expression with simple value." << std::endl;
return leftNumExp;
}
if(!expect(PLUS) && !expect(MINUS) && !expect(STAR) && !expect(RSLASH)){
fire_syntax_error("Expected '+' '-' '*' '/' got '" + gnext().data + "'", gnext().columnno, gnext().lineno, gnext().file_path);
}
index++;
LOG_TOKEN("ParseINTExpr Token after expect operator");
num_operation op = get_op(tokens[index].type);
index++;
LOG_TOKEN("ParseINTExpr Token increment");
NumberExprPoint right;
while(tokens[index].type != LINE_END && cnext()){
right = parseIntExpr(id);
if(tokens[index].type == LINE_END) break;
index++;
LOG_TOKEN("ParseINTExpr Token after While Increment");
}
LOG_TOKEN("ParseINTExpr Token after While.completed");
if(right.isnull){
fire_syntax_error("Expected numeriacal expression got 'null'", gnext().columnno, gnext().lineno, gnext().file_path);
}
int n2 = 0;
if (op == OP_MUL) n2 = 1;
NumberExprPoint exp(left);
exp.n2 = n2;
exp.ne2 = &right;
exp.hasExpr = true;
exp.op = op;
std::cout << "Returning Integer Expression with complex value.";
exp.tree();
std::cout << std::endl;
return exp;
}
/* Get num_operation for token type. */
num_operation Parser::get_op(int type){
switch(type){
case PLUS: return OP_ADD;
case MINUS: return OP_SUB;
case RSLASH: return OP_DIV;
case STAR: return OP_MUL;
default:
std::stringstream ss;
ss << type;
fire_syntax_error("Cannot convert type(=" + ss.str() +") to num_operation type.", gnext().columnno, gnext().lineno, gnext().file_path);
}
}
void Parser::handleIdentifier(){
Token tk = tokens[index];
std::string sidentifier = tk.data;
std::cout << "We Identifier : " << sidentifier << std::endl;
}
//REMOVE
bool Parser::hasNextToken(){
return index < tokens.size() - 1;
}