1 module econf.tokens;
2 
3 import std.string;
4 import std.array;
5 import std.stdio : writeln;
6 
7 enum ETokenType {
8 	STRING,
9 	INTEGER,
10 	FLOAT,
11 	HEXADECIMAL,
12 
13     START_ARRAY,
14     END_ARRAY,
15 	START_OBJECT,
16 	END_OBJECT,
17     ARRAY,
18 	OBJECT,
19 
20 	SET,
21 	IDENTIFIER,
22 
23 	NONE
24 }
25 
26 class EToken {
27 	private ETokenType type;
28 	private string t;
29     EToken[int] childs;
30 	EToken[string] obj_childs;
31 
32     void childsToT() {
33         string temp="[";
34         for(int i=0; i<this.childs.length; i++) {
35             temp~=this.childs[i].getString()~" ";
36         }
37         t=temp.strip()~"]";
38     }
39 
40 	void childsToObj() {
41 		int i=0;
42 		while(i<this.childs.length) {
43 			this.obj_childs[this.childs[i].getString()]=
44 				this.childs[i+1];
45 			i+=2;
46 		}
47 
48         string temp="{";
49         i=0;
50 		while(i<this.childs.length) {
51 			temp~=this.childs[i].getString()~": "~this.childs[i+1].getString()~",\n";
52 			i+=2;
53 		}
54         t=temp.strip()~"}";
55     }
56 
57 	this(string t) {
58 		this.t=t;
59 		if(t=="=") this.type=ETokenType.SET;
60 		else if(t[0]=='\"') {
61             this.type=ETokenType.STRING;
62             this.t=t.replace("\\n","\n").replace("\\t","\t");
63         }
64 		else if(
65 			t[0]=='0'||t[0]=='1'||t[0]=='2'||
66 			t[0]=='3'||t[0]=='4'||t[0]=='5'||
67 			t[0]=='6'||t[0]=='7'||t[0]=='8'||
68 			t[0]=='9'
69 		) {
70 			// INTEGER or FLOAT or HEXADECIMAL
71 			if(t.indexOf('.')!=-1) {
72 				// FLOAT
73 				this.type=ETokenType.FLOAT;
74 			}
75 			else if(t.indexOf('x')!=-1) {
76 				// HEXADECIMAL
77 				this.type=ETokenType.HEXADECIMAL;
78 			}
79 			else {
80 				// INTEGER
81 				this.type=ETokenType.INTEGER;
82 			}
83 		}
84 		else {
85 			// IDENTIFIER, START_ARRAY, END_ARRAY, ARRAY, START_OBJECT,
86 			// END_OBJECT, OBJECT
87             if(t[0]=='[') {
88                 // START_ARRAY or ARRAY
89                 if(t.indexOf(']')!=-1) {this.type=ETokenType.ARRAY;} // ARRAY
90                 else this.type=ETokenType.START_ARRAY; // START_ARRAY
91             }
92             else if(t[0]==']') {
93                 // END_ARRAY
94                 this.type=ETokenType.END_ARRAY;
95             }
96 			else if(t[0]=='{') {
97                 // START_OBJECT or OBJECT
98                 if(t.indexOf('}')!=-1) {this.type=ETokenType.OBJECT;} // OBJECT
99                 else this.type=ETokenType.START_OBJECT; // OBJECT
100             }
101             else if(t[0]=='}') {
102                 // END_OBJECT
103                 this.type=ETokenType.END_OBJECT;
104             }
105 			else this.type=ETokenType.IDENTIFIER; // IDENTIFIER
106 		}
107 	}
108 	string getString() {return this.t;}
109 	string getStr() {return this.t[1..$-1];}
110 	ETokenType getType() {return this.type;}
111 }