221V 2 недель назад
Родитель
Сommit
27b5dff993
3 измененных файлов с 27 добавлено и 2 удалено
  1. 2 1
      source/dopp.d
  2. 19 0
      source/dopp_lexer.d
  3. 6 1
      source/dopp_parser.d

+ 2 - 1
source/dopp.d

@@ -28,6 +28,7 @@ export enum TokenType{
   String,
   Symbol,
   Whitespace, // maybe todo add equals and other (=, >=, <=, ==) - needs or not ?
+  Comment_Line,
   New_Line,
   Match_Any
 }
@@ -231,7 +232,7 @@ int main(string[] argv){
   string source = q"[
 auto y1 = "this is test";
 auto y2 = "this \"is\" test";
-auto y3 = `this "is" test`;
+auto y3 = `this "is" test`; // this is single line comment
 auto y4 = `this is test`;
 auto y5 = `this `is` test`;
   ]"; // "

+ 19 - 0
source/dopp_lexer.d

@@ -14,6 +14,7 @@ export bool isKeyword(string lexeme){
 export Token[] tokenize(string source, ubyte indent_type, ubyte indent_matter){
   Token[] tokens;
   bool inside_string = false;
+  bool inside_comment = false;
   //ubyte inside_string_type = 0; // 0 = ".."; 1 = `..`; 2 = todo
   string str_helper = "";
   int i = 0;
@@ -54,6 +55,24 @@ export Token[] tokenize(string source, ubyte indent_type, ubyte indent_matter){
       }
     
     
+    }else if( (inside_string == false) && (inside_comment == false) && (source[i] == '/') && ( (i + 1) < source.length ) && (source[i + 1] == '/') ){ // single line comment "//" begins
+      auto start = i;
+      inside_comment = true;
+      i++;
+      
+      while( (i < source.length) && inside_comment ){ // goto Type single line comment end position
+        if(source[i] == '\n'){ // line end means single line comment ends
+          i++;
+          inside_string = false;
+        
+        }else{ // single line comment not ends yet
+          i++;
+        }
+      }
+      tokens ~= Token(TokenType.Comment_Line, source[start .. i]);
+      tokens ~= Token(TokenType.New_Line, "\n");
+    
+    
     }else if( (inside_string == false) && (source[i] == '"') ){ // Type string ".." begins
       auto start = i++; // string begin position
       inside_string = true;

+ 6 - 1
source/dopp_parser.d

@@ -52,8 +52,13 @@ export string parse(Token[] tokens){
       }
     }
     +/
-    if(tokens[i].lexeme == "="){
+    
+    if(tokens[i].type == TokenType.Comment_Line){
+      result ~= " " ~ tokens[i].lexeme; // add whitespace before single line comment
+    
+    }else if(tokens[i].lexeme == "="){
       result ~= " " ~ tokens[i].lexeme ~ " "; // add whitespace before and after =
+    
     }else{
       result ~= tokens[i].lexeme; // for just show all text
     }