123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import dopp;
- // Parser checks tokens for correct program
- //export void parse(Token[] tokens){
- export string parse(Token[] tokens){
- string result;
- auto i = 0;
- while(i < tokens.length){
- /+
- if(tokens[i].type == TokenType.Keyword && tokens[i].lexeme == "if"){
- if(i + 2 < tokens.length && tokens[i+1].type == TokenType.Symbol && tokens[i+1].lexeme == "("){ // if begins
- writeln("Found 'if' statement");
- i++; // goto (
- // here parsing inside ( .. ) can be extended
- while(i < tokens.length && tokens[i].lexeme != ")"){
- i++;
- }
-
- if(i < tokens.length && tokens[i].lexeme == ")"){
- i++; // goto )
-
- if(i < tokens.length && tokens[i].lexeme == "{"){ // block of code
- writeln("Opening block for if statement");
-
- // here parsing inside { .. } can be extended
- while(i < tokens.length && tokens[i].lexeme != "}"){
- i++;
- }
-
- if(i < tokens.length && tokens[i].lexeme == "}"){
- writeln("Closing block for if statement");
- i++;
- }else{
- writeln("Error: Missing closing brace for if block");
- return;
- }
-
- }else{
- writeln("Error: Expected opening brace after if condition");
- return;
- }
-
- }else{
- writeln("Error: Missing closing parenthesis for if condition");
- return;
- }
-
- }else{
- writeln("Error: Invalid if statement syntax");
- return;
- }
- }
- +/
-
- if( (tokens[i].type != TokenType.New_Line) &&
- (tokens[i].type != TokenType.Indent_Incr) &&
- (tokens[i].type != TokenType.Indent_Decr) &&
- (tokens[i].type != TokenType.Round_Bracket) &&
- ( (i + 1) < tokens.length ) && (tokens[i + 1].lexeme != ";") &&
- (tokens[i + 1].type != TokenType.New_Line) &&
- (tokens[i + 1].type != TokenType.Indent_Incr) &&
- (tokens[i + 1].type != TokenType.Indent_Decr) &&
- (tokens[i + 1].type != TokenType.Round_Bracket) ){
- result ~= tokens[i].lexeme ~ " "; // add whitespace between lexemes, but not after "\n" new_line, ";", "{", "}", "(", ")"
-
- }else{
- result ~= tokens[i].lexeme; // for just show all text
- }
-
- i++; // goto next token
- }
- return result;
- }
|