dopp_lexer.d 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import dopp;
  2. // helper - is lexeme keyword
  3. export bool isKeyword(string lexeme){
  4. //static immutable keywords = ["if", "else", "while", "for", "return"];
  5. static immutable keywords = ["dlang", "ptn", "ptns"];
  6. return keywords.canFind(lexeme);
  7. }
  8. // lexer - Tokenizer makes tokens from text
  9. //export Token[] tokenize(string source){
  10. export Token[] tokenize(string source, ubyte indent_type, ubyte indent_matter){
  11. Token[] tokens;
  12. bool inside_string = false;
  13. //ubyte inside_string_type = 0; // 0 = ".."; 1 = `..`; 2 = todo
  14. string str_helper = "";
  15. int i = 0;
  16. while(i < source.length){
  17. if(source[i] == '\n'){ // new line
  18. tokens ~= Token(TokenType.New_Line, "\n");
  19. i++;
  20. }else if(source[i].isWhite){ // skip whitespaces
  21. i++;
  22. }else if(source[i].isAlpha || (source[i] == '_') ){ // is unicode alphabetic character or underscore
  23. auto start = i;
  24. while( (i < source.length) && (source[i].isAlphaNum || (source[i] == '_') ) ){
  25. i++;
  26. }
  27. string lexeme = source[start .. i];
  28. tokens ~= Token(lexeme.isKeyword ? TokenType.Keyword : TokenType.Identifier, lexeme);
  29. }else if(source[i].isDigit){ // number
  30. auto start = i;
  31. while( (i < source.length) && (source[i].isDigit || (source[i] == '_') ) ){ // underscore can be inside number like 5_000 etc
  32. i++;
  33. }
  34. if( (i < source.length) && (source[i] == '.') ){ // include dot for float
  35. i++;
  36. while( (i < source.length) && source[i].isDigit){
  37. i++;
  38. }
  39. tokens ~= Token(TokenType.Float, source[start .. i]);
  40. }else{
  41. tokens ~= Token(TokenType.Integer, source[start .. i]);
  42. }
  43. }else if( (inside_string == false) && (source[i] == '"') ){ // Type string ".." begins
  44. auto start = i++; // string begin position
  45. inside_string = true;
  46. while( (i < source.length) && inside_string ){ // goto Type string end position
  47. if( (source[i] == '\\') && ( (i + 1) < source.length ) && (source[i + 1] == '"') ){ // escaped " is not string end
  48. i += 2; // skip 2 symbols
  49. }else if(source[i] == '"'){ // close quote "
  50. inside_string = false;
  51. }else{ // string not ends yet
  52. i++;
  53. }
  54. }
  55. if(i < source.length){ // we count close quote
  56. i++;
  57. }
  58. tokens ~= Token(TokenType.String, source[start .. i]);
  59. }else if( (inside_string == false) && (source[i] == '`') ){ // Type string `..` begins
  60. auto start = i++; // string begin position
  61. inside_string = true;
  62. while( (i < source.length) && inside_string ){ // goto Type string end position
  63. // ` cannot be escaped in `..` string - so we can add ~ "`" for it - because this is not string end
  64. if( (source[i] == '`') && ( (i + 1) < source.length ) && (source[i + 1] != ';') ){ // todo check for '\n' next line for new version lexer-compiler for syntax without ; in lines-commands ends
  65. str_helper ~= source[start .. i] ~ "`" ~ `~ "` ~ "`" ~ `" ~ ` ~ "`"; // ` ~ "`" ~ ` -> ` after dlang compiling
  66. i++;
  67. start = i;
  68. }else if(source[i] == '`'){ // close quote `
  69. inside_string = false;
  70. }else{ // string not ends yet
  71. i++;
  72. }
  73. }
  74. if(i < source.length){ // we count close quote
  75. i++;
  76. }
  77. if(str_helper != ""){
  78. tokens ~= Token(TokenType.String, str_helper ~ source[start .. i]);
  79. str_helper = "";
  80. }else{
  81. tokens ~= Token(TokenType.String, source[start .. i]);
  82. }
  83. }else if( (inside_string == false) && (source[i] == '`') ){ // Type string `..` begins
  84. auto start = i++; // string begin position
  85. while( (i < source.length) && (source[i] != '"') ){ // goto Type string end position
  86. i++;
  87. }
  88. if(i < source.length){
  89. i++;
  90. }
  91. tokens ~= Token(TokenType.String, source[start .. i]);
  92. }else{ // common symbols as tokens
  93. tokens ~= Token(TokenType.Symbol, source[i].to!string);
  94. i++;
  95. }
  96. }
  97. return tokens;
  98. }