public void WriteLarvae(Dictionary <string, Larva> items) { string ns = Larvae.GetElement(ElementType.Namespace); foreach (KeyValuePair <string, Larva> pair in items) { Larva l = pair.Value; if (l.Mode == LarvaMode.Skip || l.Declaration.Count == 0) { continue; } SetOutputFile(l.Name); if (ns != null) { AddDeclarationText("package " + ns + ";"); } AddDeclarationText(Larvae.GetElement(ElementType.IncludeDeclarationTop)); AddDeclarationText(Larvae.GetElement(ElementType.UserIncludeDeclarationTop)); // output declaration foreach (string d in l.Declaration) { AddDeclarationText(d); } SaveToFile(); } }
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 void Generate(string outputFile) { List <string> output = new List <string>(); foreach (KeyValuePair <string, Larva> pair in Items) { Larva l = pair.Value; if (l.Mode == LarvaMode.Skip) { continue; } switch (l.Type) { case LarvaType.Struct: GenerateStruct(l); break; case LarvaType.Static: GenerateStatic(l); break; case LarvaType.Enum: GenerateEnum(l); break; } } Generator.Current.Generate(); }
public string GetDeclaration(Larva larva) { string m = larva.Type == LarvaType.Struct ? Larvae.GetElement(ElementType.MethodDeclaration) : Larvae.GetElement(ElementType.StaticMethodDeclaration); // check does this method need to be virtual // check does base class contain the same method bool fromBase = false; Larva b = Larvae.GetLarva(larva.BaseName, true); while (b != null) { if (b.Methods.Contains(Name)) { fromBase = true; } b = Larvae.GetLarva(b.BaseName, true); } bool toChild = false; foreach (KeyValuePair <string, Larva> p in Larvae.Items) { if (p.Value.BaseName == larva.Name && p.Value.Methods.Contains(Name)) { toChild = true; break; } } m = Generator.Current.GetVirtualModificator(fromBase, toChild) + m; m = Variables.ReplaceAll(m, larva).Replace("%larva%", larva.Name).Replace("%method%", Name); return(m); }
static public Larva GetLarva(string fullName, bool existingOnly = false) { if (fullName != null && Items.ContainsKey(fullName)) { return Items[fullName]; } else { // try to add current namespace if name is without . (maybe from local namespace) if (fullName != null && !fullName.Contains('.')) { string realyFullName = GetFullName(fullName); if (Items.ContainsKey(realyFullName)) { return Items[realyFullName]; } } if (existingOnly) { return null; } Larva l = new Larva(); l.Name = l.FullName = fullName; l.Type = LarvaType.Custom; Items[fullName] = l; return l; } }
static public Larva GetLarva(string fullName, bool existingOnly = false) { if (fullName != null && Items.ContainsKey(fullName)) { return(Items[fullName]); } else { // try to add current namespace if name is without . (maybe from local namespace) if (fullName != null && !fullName.Contains('.')) { string realyFullName = GetFullName(fullName); if (Items.ContainsKey(realyFullName)) { return(Items[realyFullName]); } } if (existingOnly) { return(null); } Larva l = new Larva(); l.Name = l.FullName = fullName; l.Type = LarvaType.Custom; Items[fullName] = l; return(l); } }
static void GenerateStatic(Larva larva) { // generate struct string staticDefinition = GetElement(ElementType.StaticDefinition); TextHelper.ReplaceExpressions(ref staticDefinition, larva); string body = larva.GetStaticBody(); TextHelper.ReplaceField(ref staticDefinition, "%body%", body); larva.Declaration.Add(staticDefinition); // generate field definition foreach (Part p in larva.Parts) { larva.Definitions.Add(p.GetStaticFieldDefinition()); } // generate method definition foreach (string name in larva.Methods) { Method m = MethodFactory.GetMethod(name); larva.Definitions.Add(m.GetDefinition(larva)); } }
public string GetDefinition(Larva larva) { string m = larva.Type == LarvaType.Struct ? Larvae.GetElement(ElementType.MethodDefinition) : Larvae.GetElement(ElementType.StaticMethodDefinition); m = Variables.ReplaceAll(m, larva).Replace("%larva%", larva.Name).Replace("%method%", Name); return(m); }
public void WriteLarvae(Dictionary <string, Larva> items) { foreach (KeyValuePair <string, Larva> pair in items) { Larva l = pair.Value; // output declaration foreach (string d in l.Declaration) { AddDeclarationText(d); } } }
static void GenerateEnum(Larva larva) { string enumDefinition = GetElement(ElementType.EnumDefinition); TextHelper.ReplaceExpressions(ref enumDefinition, larva); string enumBody = larva.GetEnumBody(); TextHelper.ReplaceField(ref enumDefinition, "%body%", enumBody); larva.Declaration.Add(enumDefinition); }
public Larva GetLarva() { Larva l; string type = GetNextToken(); TypeInfo td = TypeFactory.Get(type, true); if (td == null) { if (!type.Contains('.')) { type = Larvae.GetFullName(type); } l = Larvae.GetLarva(type, true); if (l == null) { Console.WriteLine("Error! Type '" + type + "' not found"); return(null); } return(l); } else { string fullType = type; List <Larva> subLarvae = null; if (td.Length > 1) { subLarvae = new List <Larva>(); for (int i = 1; i < td.Length; i++) { Larva sl = GetLarva(); fullType += "." + sl.FullName; subLarvae.Add(sl); } } l = Larvae.GetLarva(fullType); l.Type = LarvaType.Custom; l.TypeInfo = td; l.SubLarvae = subLarvae; } return(l); }
static void GenerateStruct(Larva larva) { // generate struct string structDefinition = larva.BaseName == null?GetElement(ElementType.StructDefinition) : GetElement(ElementType.StructDefinitionWithBase); TextHelper.ReplaceExpressions(ref structDefinition, larva); string body = larva.GetStructBody(); TextHelper.ReplaceField(ref structDefinition, "%body%", body); larva.Declaration.Add(structDefinition); larva.Definitions.Add(larva.GetConstructorDefinition()); // generate method definition foreach (string name in larva.Methods) { Method m = MethodFactory.GetMethod(name); larva.Definitions.Add(m.GetDefinition(larva)); } }
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 Part(Larva parent) { Parent = parent; }
public string ReplaceAll(string text, Larva larva) { while (true) { // check for expression Match match = Regex.Match(text, @"%(~[^%]+)?%", RegexOptions.IgnoreCase); // Here we check the Match instance. if (match.Success) { string v = match.Captures[0].Value; string group = null; if (v != "%%") { group = v.Substring(2, v.Length - 3); } string fields = ""; // replace with base larva fields Larva bl = larva; while (true) { if (bl.BaseName == null) { break; } bl = larva.BaseLarva; if (bl == null) { break; } foreach (Part p in bl.Parts) { // skip part if part from different group or with skip mode if ((group != null && group != p.Larva.TypeInfo.Group) || p.Mode == PartMode.Skip) { continue; } string sp = ReplacePart(p); if (sp != "") { fields += sp + Environment.NewLine; } } } // replace with larva fields foreach (Part p in larva.Parts) { // skip part if part from different group or with skip mode if ((group != null && group != p.Larva.TypeInfo.Group) || p.Mode == PartMode.Skip) { continue; } string sp = ReplacePart(p); if (sp != "") { fields += sp + Environment.NewLine; } } text = TextHelper.ReplaceField(ref text, v, fields); } else { break; } } // replace with variables foreach (Variable v in Items) { string name = "%" + v.Name + "%"; if (text.Contains(name)) { string value = v.Value; value = v.Variables.ReplaceAll(value, larva); TextHelper.ReplaceExpressions(ref value, larva); TextHelper.ReplaceField(ref text, name, value); } } return(text); }
public string GetDefinition(Larva larva) { string m = larva.Type == LarvaType.Struct ? Larvae.GetElement(ElementType.MethodDefinition) : Larvae.GetElement(ElementType.StaticMethodDefinition); m = Variables.ReplaceAll(m, larva).Replace("%larva%", larva.Name).Replace("%method%", Name); return m; }
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); }
public string GetDeclaration(Larva larva) { string m = larva.Type == LarvaType.Struct ? Larvae.GetElement(ElementType.MethodDeclaration) : Larvae.GetElement(ElementType.StaticMethodDeclaration); // check does this method need to be virtual // check does base class contain the same method bool fromBase = false; Larva b = Larvae.GetLarva(larva.BaseName, true); while (b != null) { if (b.Methods.Contains(Name)) { fromBase = true; } b = Larvae.GetLarva(b.BaseName, true); } bool toChild = false; foreach (KeyValuePair<string, Larva> p in Larvae.Items) { if (p.Value.BaseName == larva.Name && p.Value.Methods.Contains(Name)) { toChild = true; break; } } m = Generator.Current.GetVirtualModificator(fromBase, toChild) + m; m = Variables.ReplaceAll(m, larva).Replace("%larva%", larva.Name).Replace("%method%", Name); return m; }
static void GenerateStruct(Larva larva) { // generate struct string structDefinition = larva.BaseName == null ? GetElement(ElementType.StructDefinition) : GetElement(ElementType.StructDefinitionWithBase); TextHelper.ReplaceExpressions(ref structDefinition, larva); string body = larva.GetStructBody(); TextHelper.ReplaceField(ref structDefinition, "%body%", body); larva.Declaration.Add(structDefinition); larva.Definitions.Add(larva.GetConstructorDefinition()); // generate method definition foreach (string name in larva.Methods) { Method m = MethodFactory.GetMethod(name); larva.Definitions.Add(m.GetDefinition(larva)); } }
public string ReplaceAll(string text, Larva larva) { while (true) { // check for expression Match match = Regex.Match(text, @"%(~[^%]+)?%", RegexOptions.IgnoreCase); // Here we check the Match instance. if (match.Success) { string v = match.Captures[0].Value; string group = null; if (v != "%%") { group = v.Substring(2, v.Length - 3); } string fields = ""; // replace with base larva fields Larva bl = larva; while (true) { if (bl.BaseName == null) { break; } bl = larva.BaseLarva; if (bl == null) { break; } foreach (Part p in bl.Parts) { // skip part if part from different group or with skip mode if ((group != null && group != p.Larva.TypeInfo.Group) || p.Mode == PartMode.Skip) { continue; } string sp = ReplacePart(p); if (sp != "") { fields += sp + Environment.NewLine; } } } // replace with larva fields foreach (Part p in larva.Parts) { // skip part if part from different group or with skip mode if ((group != null && group != p.Larva.TypeInfo.Group) || p.Mode == PartMode.Skip) { continue; } string sp = ReplacePart(p); if (sp != "") { fields += sp + Environment.NewLine; } } text = TextHelper.ReplaceField(ref text, v, fields); } else { break; } } // replace with variables foreach (Variable v in Items) { string name = "%" + v.Name + "%"; if (text.Contains(name)) { string value = v.Value; value = v.Variables.ReplaceAll(value, larva); TextHelper.ReplaceExpressions(ref value, larva); TextHelper.ReplaceField(ref text, name, value); } } return text; }