CLI for the Fjord Config Format
at master 11 kB view raw
1using System; 2using System.Collections.Generic; 3using System.Runtime.CompilerServices; 4using libfcf; 5 6namespace FCF; 7 8class Program { 9 static void PrintError(InvalidTokenTypeException e, string sourceFilePath) { 10 string[] filearr = File.ReadAllLines(sourceFilePath); 11 12 Console.ForegroundColor = ConsoleColor.Red; 13 Console.Write("error"); 14 Console.ForegroundColor = ConsoleColor.White; 15 Console.Write($": {e.Message}"); 16 Console.WriteLine(); 17 18 19 20 if(e.token.lineStart > 0) { 21 Console.ForegroundColor = ConsoleColor.Blue; 22 Console.Write(e.token.lineStart); 23 Console.Write(" | "); 24 25 foreach(var j in filearr[e.token.lineStart - 1]) { 26 Console.ForegroundColor = ConsoleColor.Gray; 27 Console.Write(j); 28 } 29 Console.WriteLine(); 30 } 31 32 Console.ForegroundColor = ConsoleColor.Blue; 33 Console.Write(e.token.lineStart + 1); 34 Console.Write(" | "); 35 36 int jIndex = -1; 37 foreach(var j in filearr[e.token.lineStart]) { 38 jIndex ++; 39 if(jIndex >= e.token.charStart && jIndex < e.token.charEnd) { 40 Console.ForegroundColor = ConsoleColor.Red; 41 } else { 42 Console.ForegroundColor = ConsoleColor.Gray; 43 } 44 Console.Write(j); 45 } 46 Console.WriteLine(); 47 48 Console.ForegroundColor = ConsoleColor.Blue; 49 for(var j = 0; j < e.token.lineStart.ToString().Length; j++) { 50 Console.Write(" "); 51 } 52 Console.Write(" | "); 53 54 for(int j = 0; j < e.token.charStart; j++) { 55 Console.Write(" "); 56 } 57 Console.ForegroundColor = ConsoleColor.Blue; 58 for(var j = 0; j < e.token.charEnd - e.token.charStart; j++) { 59 Console.Write("^"); 60 } 61 Console.WriteLine(); 62 63 64 65 Console.ForegroundColor = ConsoleColor.Blue; 66 Console.Write(e.token.lineStart + 2); 67 Console.Write(" | "); 68 69 foreach(var j in filearr[e.token.lineStart + 1]) { 70 Console.ForegroundColor = ConsoleColor.Gray; 71 Console.Write(j); 72 } 73 Console.WriteLine(); 74 } 75 76 static void Main(string[] args) 77 { 78 if (args.Length < 1) 79 { 80 Console.WriteLine("No argument provided"); 81 return; 82 } 83 84 switch(args[0].ToLower()) 85 { 86 case "test": 87 string subPath = "tests/"; 88 string categoryPath = "serializer/"; 89 List<string> serializeTests = new List<string>(); 90 91 serializeTests.Add("simple"); 92 serializeTests.Add("simplecondensed"); 93 94 bool verbose = false; 95 if(args.Length >= 2) { 96 if(args[1] == "verbose") { 97 verbose = true; 98 } 99 } 100 101 Console.ForegroundColor = ConsoleColor.Blue; 102 Console.WriteLine("Serializer Tests"); 103 104 int i = 0; 105 foreach(string test in serializeTests) 106 { 107 i++; 108 109 var d = Parser.DeserializeObjectFromFile(subPath + categoryPath + test + ".fc"); 110 string resultJSON = File.ReadAllText(subPath + categoryPath + test + "_result.json"); 111 string resultFCF = File.ReadAllText(subPath + categoryPath + test + "_result.fc"); 112 113 Console.ForegroundColor = ConsoleColor.White; 114 Console.Write($"Test {i} '{test}': "); 115 if (Serializer.SerializeObject(d) != resultFCF) 116 { 117 Console.ForegroundColor = ConsoleColor.Red; 118 Console.Write($"FCF Failed"); 119 if(verbose) { 120 Console.ForegroundColor = ConsoleColor.White; 121 Console.WriteLine(); 122 Console.WriteLine("Got:"); 123 Console.WriteLine(Serializer.SerializeObject(d)); 124 Console.WriteLine("Expected:"); 125 Console.WriteLine(resultFCF); 126 } 127 } else 128 { 129 Console.ForegroundColor = ConsoleColor.Green; 130 Console.Write($"FCF Succeeded"); 131 } 132 133 Console.ForegroundColor = ConsoleColor.White; 134 Console.Write(", "); 135 136 if (Serializer.SerializeObjectToJson(d) != resultJSON) 137 { 138 Console.ForegroundColor = ConsoleColor.Red; 139 Console.Write($"JSON Failed"); 140 if(verbose) { 141 Console.ForegroundColor = ConsoleColor.White; 142 Console.WriteLine(); 143 Console.WriteLine("Got:"); 144 Console.WriteLine(Serializer.SerializeObjectToJson(d)); 145 Console.WriteLine("Expected:"); 146 Console.WriteLine(resultJSON); 147 } 148 } 149 else 150 { 151 Console.ForegroundColor = ConsoleColor.Green; 152 Console.Write($"JSON Succeeded"); 153 } 154 Console.WriteLine(); 155 } 156 157 categoryPath = "tokenizer/"; 158 List<string> tokenizerTest = new List<string>(); 159 160 tokenizerTest.Add("fullconfig"); 161 tokenizerTest.Add("doubleid"); 162 tokenizerTest.Add("idstring"); 163 164 Console.ForegroundColor = ConsoleColor.Blue; 165 Console.WriteLine("\nTokenizer Tests"); 166 167 i = 0; 168 foreach(string test in tokenizerTest) { 169 i++; 170 Token[] tokens = Tokenizer.TokenizeFromFile(subPath + categoryPath + test + ".fc"); 171 string[] expected = File.ReadAllLines(subPath + categoryPath + test + ".txt"); 172 173 List<KeyValuePair<string, string>> problems = new List<KeyValuePair<string, string>>(); 174 175 int tokenIdx = -1; 176 foreach(var token in tokens) { 177 tokenIdx++; 178 179 string output = ""; 180 if(token.GetType() == typeof(TokenString)) { 181 TokenString t = (TokenString)token; 182 output += $"{tokenIdx} {token} \"{t.value}\""; 183 } else { 184 dynamic t = (dynamic)token; 185 output += $"{tokenIdx} {token} '{t.value}'"; 186 } 187 188 bool problem = false; 189 if(tokenIdx < expected.Length) { 190 if(output != expected[tokenIdx]) { 191 problems.Add(new KeyValuePair<string, string>(output, expected[tokenIdx])); 192 problem = true; 193 } 194 } 195 196 if(verbose) { 197 if(problem) { 198 Console.ForegroundColor = ConsoleColor.Red; 199 } else { 200 Console.ForegroundColor = ConsoleColor.White; 201 } 202 Console.WriteLine(output); 203 } 204 } 205 206 Console.ForegroundColor = ConsoleColor.White; 207 Console.Write($"Test {i} '{test}': "); 208 if(problems.Count > 0) { 209 Console.ForegroundColor = ConsoleColor.Red; 210 Console.WriteLine("Failed"); 211 foreach(KeyValuePair<string, string> p in problems) { 212 Console.ForegroundColor = ConsoleColor.Red; 213 Console.WriteLine($" Got: {p.Key}"); 214 Console.ForegroundColor = ConsoleColor.Green; 215 Console.WriteLine($" Expected: {p.Value}"); 216 } 217 } else { 218 Console.ForegroundColor = ConsoleColor.Green; 219 Console.WriteLine("Succeeded"); 220 } 221 } 222 223 224 break; 225 case "validate": 226 if(args.Length < 2) 227 { 228 Console.WriteLine("'Validate' requries a filepath attached"); 229 return; 230 } 231 232 try 233 { 234 Parser.DeserializeObjectFromFile(args[1]); 235 Console.WriteLine("File is valid!"); 236 } 237 catch(InvalidTokenTypeException e) 238 { 239 PrintError(e, args[1]); 240 } 241 break; 242 case "jsonify": 243 if (args.Length < 2) 244 { 245 Console.WriteLine("'jsonify' requries a filepath attached"); 246 return; 247 } 248 249 try 250 { 251 var d = Parser.DeserializeObjectFromFile(args[1]); 252 Console.WriteLine(Serializer.SerializeObjectToJson(d)); 253 } 254 catch (InvalidTokenTypeException e) 255 { 256 PrintError(e, args[1]); 257 } 258 break; 259 case "parse": 260 if (args.Length < 2) 261 { 262 Console.WriteLine("'parse' requries a filepath attached"); 263 return; 264 } 265 266 try 267 { 268 var d = Parser.DeserializeObjectFromFile(args[1]); 269 270 Console.WriteLine(Serializer.SerializeObject(d)); 271 } 272 catch (InvalidTokenTypeException e) 273 { 274 PrintError(e, args[1]); 275 } 276 break; 277 case "tokenize": 278 if (args.Length < 2) 279 { 280 Console.WriteLine("'tokenize' requries a filepath attached"); 281 return; 282 } 283 284 Token[] ts = Tokenizer.TokenizeFromFile(args[1]); 285 286 int tIdx = -1; 287 foreach(Token token in ts) { 288 tIdx ++; 289 string output = ""; 290 if(token.GetType() == typeof(TokenString)) { 291 TokenString t = (TokenString)token; 292 output += $"{tIdx} {token} \"{t.value}\""; 293 } else { 294 dynamic t = (dynamic)token; 295 output += $"{tIdx} {token} '{t.value}'"; 296 } 297 Console.WriteLine(output); 298 } 299 break; 300 default: 301 Console.WriteLine("Invalid command"); 302 break; 303 } 304 } 305}