public void ParseTest() { const string expected = "block1begin\n" + "name1 =val1\n" + "block2begin\n" + "name2 =val2\n" + "BLOCKend\n" + "name3 =val3\n" + "BLOCKend\n"; const string expected3 = "block1begin\n" + "name1 =val1\n" + "block2begin\n" + " val2 " + " val " + "BLOCKend\n" + "name3 =val3\n" + "block3begin\n" + "BLOCKend\n" + "BLOCKend\n"; string res = ""; ClausewitzParser parser = new ClausewitzParser(block => { res += block + "begin\n"; }, () => { res += "BLOCKend\n"; }, (name, val) => { res += name + " =" + val + "\n"; }, val => { res += " " + val + " "; }); parser.Parse("TestData\\ClausewitzTest1.txt"); Assert.AreEqual(expected, res); res = ""; parser.Parse("TestData\\ClausewitzTest2.txt"); Assert.AreEqual(expected, res); res = ""; parser.Parse("TestData\\ClausewitzTest3.txt"); Assert.AreEqual(expected3, res); }
private static void ExctractTechTree(string path) { _blocks.Clear(); _blocks.Add("", new Block()); Stack <Block> stack = new Stack <Block>(); stack.Push(_blocks[""]); ClausewitzParser parser = new ClausewitzParser( name => { stack.Peek().Siblings.Add(name); if (!_blocks.ContainsKey(name)) { _blocks.Add(name, new Block()); } stack.Push(_blocks[name]); }, () => { stack.Pop(); }, (name, val) => { stack.Peek().Vars[name] = val; }, val => { }); foreach (string filename in Directory.GetFiles(path)) { if (filename.EndsWith(@"\old_nuclear_tech.txt")) { continue; } parser.Parse(filename); } }
private static void ExtractEffects(string path) { EffectStates state = EffectStates.Unknown; MultiMap <string, string> effects = new MultiMap <string, string>(); ClausewitzParser parser = new ClausewitzParser( name => { if (name == "effects") { if (state != EffectStates.Unknown) { throw new ClauzewitzSyntaxException("effects block inside " + state.ToString()); } state = EffectStates.Effects; } else if (name == "command") { if (state != EffectStates.Effects) { throw new ClauzewitzSyntaxException("command block inside " + state.ToString()); } state = EffectStates.Command; } }, () => { if (state == EffectStates.Command) { state = EffectStates.Effects; } else if (state == EffectStates.Effects) { state = EffectStates.Unknown; } }, (name, val) => { if (state != EffectStates.Command) { return; } effects.Add(name, val); }, val => { }); foreach (string filename in Directory.GetFiles(path)) { if (filename.EndsWith(@"\old_nuclear_tech.txt")) { continue; } parser.Parse(filename); } Console.WriteLine("Found the following effects:"); foreach (string key in effects.Keys) { Console.WriteLine(key + ":"); foreach (string val in effects.ValueList(key)) { Console.WriteLine("\t" + val); } } }