dopp.d 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import toml;
  2. public{
  3. import std.stdio : writeln;
  4. import std.file : read;
  5. import std.array;
  6. import std.string;
  7. import std.uni : isWhite, isAlpha, isAlphaNum;
  8. import std.ascii : isDigit;
  9. import std.conv : to;
  10. import std.algorithm;
  11. import std.sumtype : SumType;
  12. }
  13. import dopp_lexer : tokenize, isKeyword;
  14. import dopp_parser : parse;
  15. TOMLDocument toml_s;
  16. export enum TokenType{
  17. Identifier, // todo split to Type - Function - Variable
  18. Keyword,
  19. Integer,
  20. Float,
  21. String,
  22. Symbol,
  23. Whitespace, // maybe todo add equals and other (=, >=, <=, ==) - needs or not ?
  24. Comment_Line,
  25. New_Line,
  26. Match_Any
  27. }
  28. export struct Token{
  29. TokenType type;
  30. string lexeme;
  31. // for debug
  32. string toString(){
  33. import std.format : format;
  34. //return "Token(type: %d, lexeme: %s)".format(type, lexeme);
  35. //return "Token = type: %d, lexeme: %s\n".format(type, lexeme);
  36. return "Token = type: %d, lexeme: %s\n".format(type, ( (TokenType.New_Line == type) ? `\n` : lexeme) );
  37. }
  38. }
  39. // ./dopp -t == shortened for test (onto single .de -> .d module)
  40. // ./dopp -d "/test" -o "/test2" == input dir path + output dir path (default output dir = input dir)
  41. // ./dopp -f test.de -o test.d == input file name + output file name (default output file name = input file name [but .d instead .de] )
  42. // ./dopp -f "/test/test.de" -o "/result/test.d" == input file path + output file path (default output file name and path = input file name and path [but .d instead .de] )
  43. bool is_valid_argv(ref string[] argv){
  44. ulong num = argv.length; // uint64
  45. if(num != 1 && num != 2 && num != 4){
  46. return false;
  47. }else if(num == 1){ // one argument only (just filename or /path/filename) == shortened ./dopp -f test.de = ./dopp test.de; or -t == shortened for test (onto single .de -> .d module)
  48. if( (argv[0] == "-t") || (argv[0].length >= 4 && argv[0][$-3 .. $] == ".de") ){
  49. return true;
  50. }else{
  51. return false;
  52. }
  53. }else if(num == 2){ // two arguments = input file name/path or input dir path
  54. if(argv[0] != "-d" && argv[0] != "-f"){
  55. return false;
  56. }else if(argv[0] == "-f"){ // must be file
  57. if(argv[1].length >= 4 && argv[1][$-3 .. $] == ".de"){
  58. return true;
  59. }else{
  60. return false;
  61. }
  62. }else{ // dir
  63. if(argv[1].length > 0){
  64. return true;
  65. }else{
  66. return false;
  67. }
  68. }
  69. }else{ // num == 4
  70. string[] argv2 = argv[0 .. 2];
  71. if( is_valid_argv(argv2) ){ // valid first 2
  72. string[] argv4 = argv[2 .. $];
  73. if( argv[2] != "-o" ){ // valid next - last 2
  74. return false;
  75. }else if(argv[0] == "-f"){ // must be file
  76. if(argv[3].length >= 3 && argv[3][$-2 .. $] == ".d"){
  77. return true;
  78. }else{
  79. return false;
  80. }
  81. }else{ // dir
  82. if(argv[3].length > 0){
  83. return true;
  84. }else{
  85. return false;
  86. }
  87. }
  88. }else{
  89. return false;
  90. }
  91. }
  92. }
  93. //alias Config_Value = SumType!(string, int, ubyte, bool);
  94. alias Config_Value = SumType!(string, ubyte, bool);
  95. Config_Value get_config_value(ref TOMLDocument toml_s, string key){
  96. if(key == "indent_matter"){
  97. ubyte default_indent_matter = 4;
  98. if(("indent_matter" in toml_s) != null){
  99. if(toml_s[key].type == TOMLType.INTEGER){
  100. ubyte indent_matter = cast(ubyte) toml_s[key].integer; // long (int64) -> ubyte (uint8) // todo add alias
  101. if(indent_matter > 0){
  102. return Config_Value(indent_matter);
  103. }
  104. }
  105. }
  106. writeln("Warning: use default indent_matter = ", default_indent_matter); // todo add config key-value "no_warn" = false | true for silence
  107. return Config_Value(default_indent_matter);
  108. }else if(key == "indent_out"){
  109. ubyte default_indent_out = 2;
  110. if(("indent_out" in toml_s) != null){
  111. if(toml_s[key].type == TOMLType.INTEGER){
  112. ubyte indent_out = cast(ubyte) toml_s[key].integer; // long (int64) -> ubyte (uint8)
  113. if(indent_out > 0){
  114. return Config_Value(indent_out);
  115. }
  116. }
  117. }
  118. writeln("Warning: use default indent_out = ", default_indent_out);
  119. return Config_Value(default_indent_out);
  120. }else if(key == "indent_type"){ // 0 = whitespaces, 1 = tabs
  121. ubyte default_indent_type = 0;
  122. if(("indent_type" in toml_s) != null){
  123. if(toml_s[key].type == TOMLType.INTEGER){
  124. ubyte indent_type = cast(ubyte) toml_s[key].integer; // long (int64) -> ubyte (uint8)
  125. if(indent_type < 2){ // 0 | 1
  126. return Config_Value(indent_type);
  127. }
  128. }
  129. }
  130. writeln("Warning: use default indent_type = ", default_indent_type);
  131. return Config_Value(default_indent_type);
  132. //}else{}
  133. }
  134. return Config_Value(0);
  135. }
  136. //void main(string[] argv){
  137. int main(string[] argv){
  138. toml_s = parseTOML(cast(string)read("dopp.toml"));
  139. /*
  140. writeln("indent_matter: ", toml_s["indent_matter"]); // 4
  141. //writeln("xyz: ", toml_s["xyz"]); // Address boundary error || Segmentation fault (core dumped)
  142. writeln("indent_matter in: ", "indent_matter" in toml_s); // hex like 7F27C2992040
  143. writeln("indent_matter is null: ", ("indent_matter" in toml_s) == null); // fasle
  144. writeln("indent_matter is TOMLType.INTEGER: ", (toml_s["indent_matter"].type == TOMLType.INTEGER)); // true
  145. writeln("xyz in: ", "xyz" in toml_s); // null
  146. writeln("xyz is null: ", ("xyz" in toml_s) == null); // true
  147. */
  148. auto indent_type = get_config_value(toml_s, "indent_type");
  149. //writeln("config: indent_type = ", indent_type); // 0 // todo add config key-value "no_warn" = false | true for silence
  150. auto indent_matter = get_config_value(toml_s, "indent_matter");
  151. //writeln("config: indent_matter = ", indent_matter); // 4
  152. auto indent_out = get_config_value(toml_s, "indent_out");
  153. //writeln("config: indent_out = ", indent_out); // 2
  154. /*
  155. int i = 0;
  156. foreach(string s; argv[1 .. $]){
  157. writeln(s);
  158. i++;
  159. }
  160. writeln("i = ", i);
  161. */
  162. string[] argv1 = argv[1 .. $]; // argv[0] always = program name ( ./dopp in my case in linux )
  163. bool valid_argv = is_valid_argv(argv1);
  164. // writeln("valid_argv = ", valid_argv); // todo add config key-value "no_warn" = false | true for silence
  165. if(!valid_argv){
  166. return 0;
  167. }
  168. //string source = `if (x > 0) { writeln("Positive"); }`;
  169. /+
  170. string source = q"[
  171. if (x > 0) {
  172. writeln("Pos`it`i've'");
  173. }
  174. ]";
  175. +/
  176. //string str1 = "this is test"; // valid in dlang
  177. //string str1 = "this \"is\" test"; // valid in dlang
  178. //string str1 = "this `is` test"; // valid in dlang
  179. //string str1 = `this "is" test`; // valid in dlang
  180. // //string str1 = `this `is` test`; // not valid in dlang - but valid in my lexer-parser
  181. //string str1 = `this ` ~ "`is`" ~ ` test`; // valid in dlang
  182. // todo add to lexer r" .. ";
  183. // todo add to lexer multiline q"[ .. ]";
  184. // todo add to lexer //string str1 = x"48 65 6C 6C 6F"; // valid in dlang - hex string // Hello
  185. //writeln(str1);
  186. //writeln("hey" ~ '\n' ~ "hello");
  187. string source = q"[
  188. auto y1 = "this is test";
  189. auto y2 = "this \"is\" test";
  190. auto y3 = `this "is" test`; // this is single line comment
  191. auto y4 = `this is test`;
  192. auto y5 = `this `is` test`;
  193. ]"; // "
  194. /+
  195. string source = q"[
  196. auto x = 5;
  197. auto x2 = 5_001;
  198. auto y1 = "this is test";
  199. auto y2 = "this \"is\" test";
  200. auto y3 = `this "is" test`;
  201. auto z = 1;
  202. ]"; // "
  203. +/
  204. /+
  205. string source = q"[
  206. void main(){
  207. auto x = 5;
  208. auto y = ptn x
  209. 1 = 1
  210. 5 = 5 * 2
  211. 9 = 9 * 3
  212. _ = x * 4
  213. }
  214. ]";
  215. +/
  216. auto tokens = tokenize(source, 0, 2); // indent_type = 0 = whitespace; indent_matter = 2 (whitespaces)
  217. writeln(tokens);
  218. auto result = parse(tokens); // indent_out = 2 (whitespaces in output-generated code)
  219. writeln(result);
  220. /+
  221. void main(){
  222. auto x = 5;
  223. if(x == 1){
  224. y = 1;
  225. }else if(x == 5){
  226. y = 5 * 2; // return x * 2;
  227. }else if(x == 9){
  228. y = 9 * 3; // return x * 3;
  229. }else{ // other cases
  230. y = x * 4;
  231. }
  232. }
  233. +/
  234. return 1;
  235. }
  236. unittest{
  237. assert( is_valid_argv([]) == false );
  238. assert( is_valid_argv([""]) == false );
  239. assert( is_valid_argv(["-t"]) == true );
  240. assert( is_valid_argv(["-d"]) == false );
  241. assert( is_valid_argv(["-d", ""]) == false );
  242. assert( is_valid_argv(["-d", "/test"]) == true );
  243. assert( is_valid_argv(["-d", "/test", "-o", "/test2"]) == true );
  244. assert( is_valid_argv(["test.de"]) == true );
  245. assert( is_valid_argv(["-f", "test.de"]) == true );
  246. assert( is_valid_argv(["-f", "test.de", "-o", "test.d"]) == true );
  247. assert( is_valid_argv(["test.txt"]) == false );
  248. assert( is_valid_argv(["-f", "test.txt"]) == false );
  249. assert( is_valid_argv(["-f", "test.txt", "-o", "test.d"]) == false );
  250. assert( is_valid_argv(["-f", "test.de", "-o", "test.txt"]) == false );
  251. }
  252. // ./dopp -t == shortened for test (onto single .de -> .d module)
  253. // ./dopp -d "/test" -o "/test2" == input dir path + output dir path (default output dir = input dir)
  254. // ./dopp -f test.de -o test.d == input file name + output file name (default output file name = input file name [but .d instead .de] )
  255. // ./dopp -f "/test/test.de" -o "/result/test.d" == input file path + output file path (default output file name and path = input file name and path [but .d instead .de] )