public string ReplacePart(Part part) { Larva l = part.Larva; TypeInfo td = part.Larva.TypeInfo; Variable v = Items.Find(x => x.Name == td.Group); if (v != null) { // search for sub larva group if (l.SubLarvae != null) { for (int i = 0; i < l.SubLarvae.Count; i++) { Larva sl = l.SubLarvae[i]; Variable sv = v.Variables.GetVariable(sl.TypeInfo.Group, true); if (sv == null) { break; } v = sv; } } string value = v.Value; TextHelper.ReplaceSystemFields(ref value, part); return value; } return ""; }
public static string Process(string command) { string[] p = command.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries); string[] v = p[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (v[0] == "method") { if (v.Length < 3) { return ""; } VariableList vl = MethodFactory.GetMethod(v[1]).Variables; //Variable variable = vl.GetVariable(v[2]); for (int i = 2; i < v.Length; i++ ) { Variable nv = vl.GetVariable(v[i], true); if (nv == null) { return ""; } vl = nv.Variables; } Larva l = Larvae.GetLarva(p[2], true); if (l == null) { return "<error:larva not found with name " + p[2] + ">"; } Part part = new Part(null); part.Name = p[1]; part.Larva = l; string value = vl.ReplacePart(part); return value; } return ""; }
public static string ReplaceSystemFields(ref string text, Part part) { while (true) { // check for auto_id names Match match = Regex.Match(text, @"%_[_0-9a-zA-Z]+%", RegexOptions.IgnoreCase); // Here we check the Match instance. if (match.Success) { string v = match.Captures[0].Value; string n = part.GetVariable(v); text = text.Replace(v, n); } else { break; } } TextHelper.ReplaceExpressions(ref text, part); while (true) { // check method variable Match match = Regex.Match(text, @"%#[^%]+%", RegexOptions.IgnoreCase); // Here we check the Match instance. if (match.Success) { string v = match.Captures[0].Value; string n = v.Substring(2, v.Length - 3); n = CommandProcessor.Process(n); TextHelper.ReplaceField(ref text, v, n); } else { break; } } return text; }
public ElementType Next() { element = null; string token = GetNextToken(); if (token == null) { return ElementType.EOF; } // check is it global variable ElementType et = ConvertToElementType(token); if (et != ElementType.None) { string operation = GetNextToken(); string value = GetNextToken(); if (Mode == ParserMode.Normal || Mode == ParserMode.ImportRules) { switch (operation) { case "=": Larvae.SetElement(et, value); break; case "+=": value = Larvae.GetElement(et) + value; Larvae.SetElement(et, value); break; default: Log.Error(operation, Error.UnexpectedToken, " expected for \"=\" or \"+=\""); break; } } element = value; return et; } switch (token) { case "static": case "struct": { LarvaType larvaType = token == "struct" ? LarvaType.Struct : LarvaType.Static; // parse struct data string larvaName = GetNextToken(); string fullName = Larvae.GetFullName(larvaName); Larva larva = Larvae.GetLarva(fullName); larva.Name = larvaName; larva.Namespace = Larvae.GetElement(ElementType.Namespace); larva.TypeInfo = larvaType == LarvaType.Struct ? TypeFactory.Get("struct", true) : TypeFactory.Get("static", true); switch (Mode) { case ParserMode.Normal: case ParserMode.ImportLarva: larva.Mode = LarvaMode.Generate; break; default: larva.Mode = LarvaMode.Skip; break; } larva.Type = larvaType; token = GetNextToken(); if (token == ":") { token = GetNextToken(); larva.BaseName = token; token = GetNextToken(); if (token == "base") { token = GetNextToken(); // read base field initialisation if (token != "{") { Log.Error(token, Error.UnexpectedToken, "Expected symbol '{' after base " + larva.Name + " defenition"); return ElementType.Error; } token = GetNextToken(); while (token != "}") { Part p = new Part(larva); larva.BaseParts.Add(p); p.Name = token; token = GetNextToken(); if (token != "=") { Console.WriteLine("Error: Expected symbol '=' in base " + larva.Name + " defenition"); return ElementType.Error; } // initial value token = GetNextToken(); p.InitialValue = token; // must be ended with , or } token = GetNextToken(); if (token != "," && token != "}") { Console.WriteLine("Error: Expected symbol ',' in base " + larva.Name + " defenition"); return ElementType.Error; } // skip , if (token == ",") { token = GetNextToken(); } } token = GetNextToken(); } } if (token != "{") { Log.Error(token, Error.UnexpectedToken, "Expected symbol '{' after struct " + larva.Name + " defenition"); return ElementType.Error; } int ie = GetNextTokenIndex("}"); if (ie == -1) { Log.Error("}", Error.NotFound, "struct closing symbol '}' not found"); return ElementType.Error; } while (currentToken < ie) { Part p = new Part(larva); Larva l = GetLarva(); string t = GetNextToken(); p.Larva = l; p.Name = t; t = GetNextToken(); if (t == "=") { p.InitialValue = GetNextToken(); t = GetNextToken(); } if (t ==":") { t = GetNextToken(); // read modificator if (t == "skip") { p.Mode = PartMode.Skip; } t = GetNextToken(); } if (t != ";") { Log.Error(token, Error.UnexpectedToken, "Unexpected token '" + token +"' field name not found"); return ElementType.Error; } larva.Parts.Add(p); } GetNextToken(); // read !} // check for methods name that we will need to generate if (IsNextToken(":")) { token = GetNextToken(); // read : while (true) { token = GetNextToken(); if (token == ";") { break; } larva.Methods.Add(token); } } element = larva; return ElementType.Struct; } case "enum": { // parse struct data string larvaName = GetNextToken(); string fullName = Larvae.GetFullName(larvaName); Larva larva = Larvae.GetLarva(fullName); larva.Name = larvaName; larva.Namespace = Larvae.GetElement(ElementType.Namespace); larva.TypeInfo = TypeFactory.Get("enum", true); switch (Mode) { case ParserMode.Normal: case ParserMode.ImportLarva: larva.Mode = LarvaMode.Generate; break; default: larva.Mode = LarvaMode.Skip; break; } larva.Type = LarvaType.Enum; token = GetNextToken(); if (token != "{") { Log.Error(token, Error.UnexpectedToken, "Expected symbol '{' after enum " + larva.Name + " defenition"); return ElementType.Error; } while (true) { string t = GetNextToken(); if (t == "}") { break; } Part p = new Part(larva); p.Name = t; p.Larva = larva; larva.Parts.Add(p); t = GetNextToken(); if (t == "=") { p.InitialValue = GetNextToken(); t = GetNextToken(); } if (t == ":") { p.Description = GetNextToken(); t = GetNextToken(); } if (t == "}") { break; } if (t != ",") { Log.Error(token, Error.UnexpectedToken, "Unexpected token '" + token + "' field name not found"); return ElementType.Error; } } element = larva; return ElementType.Enum; } case "import": { token = GetNextToken(); ParserMode parserMode = ParserMode.Unknown; switch (token.ToLower()) { case "larva": parserMode = ParserMode.ImportLarva; break; case "rules": parserMode = ParserMode.ImportRules; break; default: Console.WriteLine("Error. Unknown parser mode: " + token); return ElementType.Error; break; } string fileName = GetNextToken(); if (parserMode == ParserMode.ImportLarva) { Larvae.AddImport(fileName); } // save current namespace string ns = Larvae.GetElement(ElementType.Namespace); Parser p = new Parser(Program.ReadAllText(fileName), parserMode); p.Parse(); // restore namespace Larvae.SetElement(ElementType.Namespace, ns); return ElementType.Import; } case "type": { token = GetNextToken(); int length = 0; try { length = Convert.ToInt32(token); } catch (Exception e) { Console.WriteLine("Can't parse type length: " + e.ToString()); return ElementType.Error; } string group = GetNextToken(); string name = GetNextToken(); token = GetNextToken(); if (token != "=") { Console.WriteLine("Unexpected token '" + token + "'. Expected '='"); return ElementType.Error; } string definition = GetNextToken(); TypeInfo td = TypeFactory.Get(name); td.Length = length; td.Group = group; td.Definition = definition; return ElementType.Type; } case "method": { string methodName = GetNextToken(); Method m = MethodFactory.GetMethod(methodName); VariableList vl = m.Variables; Variable v = null; while (true) { token = GetNextToken(); if (token == "=") { break; } v = vl.GetVariable(token); vl = v.Variables; } token = GetNextToken(); // read variable value v.Value = token; return ElementType.Method; } } element = token; return ElementType.None; }