dopp.d 9.0 KB

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