1 module dmacro;
2 
3 import colorize;
4 import std.stdio;
5 import std.string;
6 import std.algorithm;
7 import std.file;
8 import std.array;
9 
10 import applogger;
11 
12 class DMUnfinishedException : Exception {
13     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null) pure nothrow @nogc @safe
14     {
15         super(msg, file, line, nextInChain);
16     }
17 }
18 
19 
20 /// generate a dictionary from parsed DMacro code
21 string[][string] parseDM(string s) {
22     
23     string buffer;
24     int state = 0;
25     string[][string] fB;
26     
27     string k;
28     string[] v;
29 
30     buffer = strip(buffer);
31 
32     foreach (char sw; s)
33     {
34         if (sw == '(' && state == 0) {
35             state = 1;
36             k = buffer.strip;
37             buffer = "";
38         } else if (sw == ')' && state == 1) {
39 
40             if (buffer.length != 0) v = v ~ buffer.strip;
41             state = 0;
42             buffer = "";
43             fB[k] = v;
44             k = "";
45             v = [];
46 
47         } else if (sw == ',' && state == 1) {
48 
49             v = v ~ buffer.strip;
50             buffer = "";
51             
52 
53         } else if (sw == '"' && state == 1) {
54 
55             state = 999;
56 
57         } else if (sw == '"' && state == 999) state = 1;
58         else if (sw == '#' && state == 0) {
59             state = 9993;
60         } else if (sw == '\n' && state == 9993)  { state = 0; buffer = ""; }
61         else {
62             buffer ~= sw;
63         }
64     }
65     if (state == 999) {
66         errorMessage("Error: unfinished string.");
67         throw new DMUnfinishedException("Unfinished string");
68     } else if (state == 1) {
69         errorMessage("Error: Unfinished macro name at end of file.");
70         throw new DMUnfinishedException("Unfinished `macro_name'");
71     }
72     return fB;
73 }