private void InitializeActionFile(MsgFileLocation filename, string[] lines) { MsgFileLocation = filename; Name = filename.package + "." + filename.basename; className = filename.basename; fileNamespace += "." + filename.package; var parsedAction = ParseActionFile(lines); // Goal Messages GoalMessage = CreateMessageFile(filename, parsedAction.GoalParameters, "Goal"); GoalActionMessage = CreateMessageFile(filename, parsedAction.GoalActionParameters, "ActionGoal"); GoalActionMessage.ActionMessageType = ActionMessageType.Goal; // Result Messages ResultMessage = CreateMessageFile(filename, parsedAction.ResultParameters, "Result"); ResultActionMessage = CreateMessageFile(filename, parsedAction.ResultActionParameters, "ActionResult"); ResultActionMessage.ActionMessageType = ActionMessageType.Result; // Feedback Messages FeedbackMessage = CreateMessageFile(filename, parsedAction.FeedbackParameters, "Feedback"); FeedbackActionMessage = CreateMessageFile(filename, parsedAction.FeedbackActionParameters, "ActionFeedback"); FeedbackActionMessage.ActionMessageType = ActionMessageType.Feedback; }
public static SingleType WhatItIs(MsgFile parent, string s, string extraindent) { string[] pieces = s.Split('/'); string package = null; // sometimes, a string can contain the char '/', such as the following line: // string CONTENT_JSON = "application/json" if (pieces.Length == 2) { if (s.ToLower().Contains("string") && !MsgFile.resolver.ContainsKey(pieces[0])) { goto ResolvingStep; } package = pieces[0]; s = pieces[1]; } ResolvingStep: SingleType st = new SingleType(package, s, extraindent); parent.resolve(st); WhatItIs(parent, st); return(st); }
public MessageTemplateInfo(MsgFile OuterMessage, MsgFile InnerMessage, string OuterMessagePlaceHoder, string InnerMessagePlaceHolder) { this.OuterMessage = OuterMessage; this.InnerMessage = InnerMessage; this.OuterMessagePlaceHoder = OuterMessagePlaceHoder; this.InnerMessagePlaceHolder = InnerMessagePlaceHolder; }
/// <summary> /// Wrapper to create a MsgsFile /// </summary> private MsgFile CreateMessageFile(MsgFileLocation messageLocation, List <string> parameters, string suffix) { var result = new MsgFile(new MsgFileLocation( messageLocation.Path, messageLocation.searchroot), parameters, suffix ); return(result); }
public static void WhatItIs(MsgFile parent, SingleType t) { if (t.IsPrimitve) { t.rostype = t.Type; SingleType.Finalize(parent, t); return; } t.Finalize(parent, t.input.Split(spliter, StringSplitOptions.RemoveEmptyEntries), false); }
public SrvFile(MsgFileLocation filename) { msgfilelocation = filename; // read in srv file var lines = File.ReadAllLines(filename.Path); classname = filename.basename; Namespace += "." + filename.package; Name = filename.package + "." + filename.basename; // def is the list of all lines in the file def = new List <string>(); int mid = 0; bool found = false; var request = new List <string>(); var response = new List <string>(); // Search through for the "---" separator between request and response for ( ; mid < lines.Length; mid++) { lines[mid] = lines[mid].Replace("\"", "\\\""); if (lines[mid].Contains('#')) { lines[mid] = lines[mid].Substring(0, lines[mid].IndexOf('#')); } lines[mid] = lines[mid].Trim(); if (lines[mid].Length == 0) { continue; } def.Add(lines[mid]); if (lines[mid].Contains("---")) { found = true; continue; } if (found) { response.Add(lines[mid]); } else { request.Add(lines[mid]); } } // treat request and response like 2 message files, each with a partial definition and extra stuff tagged on to the classname Request = new MsgFile(new MsgFileLocation(filename.Path.Replace(".srv", ".msg"), filename.searchroot), true, request, "\t"); Response = new MsgFile(new MsgFileLocation(filename.Path.Replace(".srv", ".msg"), filename.searchroot), false, response, "\t"); }
public static void Finalize(MsgFile parent, SingleType thing) { string[] parts = thing.input.Split(' '); thing.rostype = parts[0]; if (!KnownStuff.KnownTypes.ContainsKey(thing.rostype)) { thing.meta = true; } else { parts[0] = KnownStuff.KnownTypes[thing.rostype]; } thing.Finalize(parent, parts, true); }
public static SingleType WhatItIs(MsgFile parent, string s, string extraindent) { string[] pieces = s.Split('/'); string package = null; if (pieces.Length == 2) { package = pieces[0]; s = pieces[1]; } SingleType st = new SingleType(package, s, extraindent); parent.resolve(st); WhatItIs(parent, st); return st; }
/// <summary> /// Loads the template for a single message and replaces all the $placeholders with appropriate content /// </summary> public string GenerateMessageFromTemplate(string template, MsgFile message, MsgFile childMessage) { var properties = message.GenerateProperties(); template = template.Replace("$CLASS_NAME", message.classname); template = template.Replace("$$PROPERTIES", properties); template = template.Replace("$ISMETA", message.meta.ToString().ToLower()); template = template.Replace("$MSGTYPE", fileNamespace.Replace("Messages.", "") + "/" + message.classname); template = template.Replace("$MESSAGEDEFINITION", "@\"" + message.Definition + "\""); template = template.Replace("$HASHEADER", message.HasHeader.ToString().ToLower()); template = template.Replace("$NULLCONSTBODY", ""); template = template.Replace("$EXTRACONSTRUCTOR", ""); template = template.Replace("$MD5SUM", MD5.Sum(message)); // Set the base class of the message var actionClasses = new List <string> { "InnerActionMessage", "GoalActionMessage<$ACTION_GENERIC>", "ResultActionMessage<$ACTION_GENERIC>", "FeedbackActionMessage<$ACTION_GENERIC>" }; var actionClass = actionClasses[(int)message.ActionMessageType]; actionClass = actionClass.Replace("$ACTION_GENERIC", childMessage != null ? childMessage.Name : ""); template = template.Replace("$ACTION_CLASS", actionClass); string deserializationCode = ""; string serializationCode = ""; string randomizationCode = ""; string equalizationCode = ""; for (int i = 0; i < message.Stuff.Count; i++) { deserializationCode += message.GenerateDeserializationCode(message.Stuff[i], 1); serializationCode += message.GenerateSerializationCode(message.Stuff[i], 1); randomizationCode += message.GenerateRandomizationCode(message.Stuff[i], 1); equalizationCode += message.GenerateEqualityCode(message.Stuff[i], 1); } template = template.Replace("$SERIALIZATIONCODE", serializationCode); template = template.Replace("$DESERIALIZATIONCODE", deserializationCode); template = template.Replace("$RANDOMIZATIONCODE", randomizationCode); template = template.Replace("$EQUALITYCODE", equalizationCode); return(template); }
public static void Resolve(MsgFile parent, SingleType st) { if (st.Type == null) { KnownStuff.WhatItIs(parent, st); } if (st.IsPrimitve) { return; } List <string> prefixes = new List <string>(new[] { "", "std_msgs", "geometry_msgs", "actionlib_msgs" }); if (st.Type.Contains("/")) { string[] pieces = st.Type.Split('/'); st.Package = pieces[0]; st.Type = pieces[1]; } prefixes[0] = !string.IsNullOrEmpty(st.Package) ? st.Package : parent.Package; foreach (string p in prefixes) { if (resolver.Keys.Contains(p)) { if (resolver[p].ContainsKey(st.Type)) { if (resolver[p][st.Type].Count == 1) { st.Package = p; st.Definer = resolver[p][st.Type][0].Definer; } else if (resolver[p][st.Type].Count > 1) { throw new ArgumentException($"Could not resolve: {st.Type}"); } } } } }
public void Finalize(MsgFile parent, string[] s, bool isliteral) { backup = new string[s.Length]; Array.Copy(s, backup, s.Length); bool isconst = false; IsLiteral = isliteral; string type = s[0]; string name = s[1]; string otherstuff = ""; if (name.Contains('=')) { string[] parts = name.Split('='); isconst = true; name = parts[0]; otherstuff = " = " + parts[1]; } if (name == parent.Name.Split(".").Last() || !MsgFileLocation.IsValidCSharpIdentifier(name) && name.Length > 0) { if (IsCSharpKeyword(name)) { name = "@" + name; } else if (MsgFileLocation.IsValidCSharpIdentifier(name) && name == parent.Name.Split(".").Last()) { name = "_" + name; } else { throw new ArgumentException(String.Format("Variable '{0}' from '{1}' is not a compatible C# identifier name\n\tAll variable names must conform to C# Language Specifications (refer to this StackOverflow answer: https://stackoverflow.com/a/950651/4036588)\n", name, parent.msgFileLocation.Path)); } } for (int i = 2; i < s.Length; i++) { otherstuff += " " + s[i]; } if (otherstuff.Contains('=')) { isconst = true; } if (!IsArray) { if (otherstuff.Contains('=') && type.Equals("string", StringComparison.CurrentCultureIgnoreCase)) { otherstuff = otherstuff.Replace("\\", "\\\\"); otherstuff = otherstuff.Replace("\"", "\\\""); string[] split = otherstuff.Split('='); otherstuff = split[0] + " = " + split[1].Trim() + ""; } if (otherstuff.Contains('=') && type == "bool") { otherstuff = otherstuff.Replace("0", "false").Replace("1", "true"); } if (otherstuff.Contains('=') && type == "byte") { otherstuff = otherstuff.Replace("-1", "255"); } Const = isconst; bool wantsconstructor = !KnownStuff.IsPrimitiveType(this); if (otherstuff.Contains("=")) { string[] chunks = otherstuff.Split('='); ConstValue = chunks[chunks.Length - 1].Trim(); if (type.Equals("string", StringComparison.OrdinalIgnoreCase)) { otherstuff = chunks[0] + " = \"" + chunks[1].Trim() + "\""; wantsconstructor = false; } } string prefix = "", suffix = ""; if (isconst) { // why can't strings be constants? //if (!type.Equals("string", StringComparison.OrdinalIgnoreCase)) //{ if (KnownStuff.IsPrimitiveType(this)) { prefix = "const "; } else { prefix = "static readonly "; } wantsconstructor = false; //} } string t = KnownStuff.GetNamespacedType(this, type); if (otherstuff.Contains('=')) { if (wantsconstructor) { if (type == "string") { suffix = " = \"\""; } else { suffix = " = new " + type + "()"; } } else { suffix = KnownStuff.GetConstTypesAffix(type); } } else { if (type == "string") { suffix = " = \"\""; } else if (wantsconstructor) { suffix = " = new " + prefix + t + "()"; } } output = lowestindent + "public " + prefix + t + " " + name + otherstuff + suffix + ";"; } else { if (length.Length > 0) { IsLiteral = true; } if (otherstuff.Contains('=')) { string[] split = otherstuff.Split('='); otherstuff = split[0] + " = (" + type + ")" + split[1]; } string t = KnownStuff.GetNamespacedType(this, type); if (length.Length > 0) { output = lowestindent + "public " + t + "[] " + name + otherstuff + " = new " + type + "[" + length + "];"; } else { output = lowestindent + "public " + "" + t + "[] " + name + otherstuff + ";"; } } Type = type; parent.resolve(this); if (!KnownStuff.KnownTypes.ContainsKey(rostype)) { meta = true; } Name = name.Length == 0 ? otherstuff.Trim() : name; if (Name.Contains('=')) { Name = Name.Substring(0, Name.IndexOf("=")).Trim(); } }
public void Finalize(MsgFile parent, string[] s, bool isliteral) { backup = new string[s.Length]; Array.Copy(s, backup, s.Length); bool isstatic = false; IsLiteral = isliteral; string type = s[0]; string name = s[1]; string otherstuff = ""; if (name.Contains('=')) { string[] parts = name.Split('='); isstatic = true; name = parts[0]; otherstuff = " = " + parts[1]; } if (IsCSharpKeyword(name)) { name = "@" + name; } for (int i = 2; i < s.Length; i++) { otherstuff += " " + s[i]; } if (otherstuff.Contains('=')) { isstatic = true; } if (!IsArray) { if (otherstuff.Contains('=') && type.Equals("string", StringComparison.CurrentCultureIgnoreCase)) { otherstuff = otherstuff.Replace("\\", "\\\\"); otherstuff = otherstuff.Replace("\"", "\\\""); string[] split = otherstuff.Split('='); otherstuff = split[0] + " = " + split[1].Trim() + ""; } if (otherstuff.Contains('=') && type == "bool") { otherstuff = otherstuff.Replace("0", "false").Replace("1", "true"); } if (otherstuff.Contains('=') && type == "byte") { otherstuff = otherstuff.Replace("-1", "255"); } Static = isstatic; bool wantsconstructor = true; if (otherstuff.Contains("=")) { string[] chunks = otherstuff.Split('='); StaticValue = chunks[chunks.Length - 1].Trim(); if (type.Equals("string", StringComparison.OrdinalIgnoreCase)) { otherstuff = chunks[0] + " = \"" + chunks[1].Trim() + "\""; wantsconstructor = false; } } string prefix = "", suffix = ""; if (isstatic) { if (!type.Equals("string", StringComparison.OrdinalIgnoreCase)) { prefix = "static "; wantsconstructor = false; } } string t = KnownStuff.GetNamespacedType(this, type); if (otherstuff.Contains('=')) { if (wantsconstructor) { if (type == "string") { suffix = " = \"\""; } else { suffix = " = new " + type + "()"; } } else { suffix = KnownStuff.GetConstTypesAffix(type); } } else { if (type == "string") { suffix = " = \"\""; } else { suffix = " = new " + prefix + t + "()"; } } output = lowestindent + "public " + prefix + t + " " + name + otherstuff + suffix + ";"; } else { if (length.Length > 0) { IsLiteral = true; } if (otherstuff.Contains('=')) { string[] split = otherstuff.Split('='); otherstuff = split[0] + " = (" + type + ")" + split[1]; } string t = KnownStuff.GetNamespacedType(this, type); if (length.Length > 0) { output = lowestindent + "public " + t + "[] " + name + otherstuff + " = new " + type + "[" + length + "];"; } else { output = lowestindent + "public " + "" + t + "[] " + name + otherstuff + ";"; } } Type = type; parent.resolve(this); if (!KnownStuff.KnownTypes.ContainsKey(rostype)) { meta = true; } Name = name.Length == 0 ? otherstuff.Trim() : name; if (Name.Contains('=')) { Name = Name.Substring(0, Name.IndexOf("=")).Trim(); } }
public void refinalize(MsgFile parent, string REALTYPE) { bool isstatic = false; Type = REALTYPE; string name = backup[1]; string otherstuff = ""; if (name.Contains('=')) { string[] parts = name.Split('='); isstatic = true; name = parts[0]; otherstuff = " = " + parts[1]; } if (IsCSharpKeyword(name)) { name = "@" + name; } for (int i = 2; i < backup.Length; i++) { otherstuff += " " + backup[i]; } if (otherstuff.Contains('=')) { isstatic = true; } parent.resolve(this); if (!IsArray) { if (otherstuff.Contains('=') && Type.Equals("string", StringComparison.CurrentCultureIgnoreCase)) { otherstuff = otherstuff.Replace("\\", "\\\\"); otherstuff = otherstuff.Replace("\"", "\\\""); string[] split = otherstuff.Split('='); otherstuff = split[0] + " = \"" + split[1].Trim() + "\""; } if (otherstuff.Contains('=') && Type == "bool") { otherstuff = otherstuff.Replace("0", "false").Replace("1", "true"); } if (otherstuff.Contains('=') && Type == "byte") { otherstuff = otherstuff.Replace("-1", "255"); } Static = isstatic; bool wantsconstructor = false; if (otherstuff.Contains("=")) { string[] chunks = otherstuff.Split('='); StaticValue = chunks[chunks.Length - 1].Trim(); if (Type.Equals("string", StringComparison.OrdinalIgnoreCase)) { otherstuff = chunks[0] + " = \"" + chunks[1].Trim().Replace("\"", "") + "\""; } } else if (!Type.Equals("String")) { wantsconstructor = true; } string prefix = "", suffix = ""; if (isstatic) { if (!Type.Equals("string", StringComparison.OrdinalIgnoreCase)) { prefix = "const "; } } if (otherstuff.Contains('=')) { if (wantsconstructor) { if (Type == "string") { suffix = " = \"\""; } else { suffix = " = new " + Type + "()"; } } else { suffix = KnownStuff.GetConstTypesAffix(Type); } } string t = KnownStuff.GetNamespacedType(this, Type); output = lowestindent + "public " + prefix + t + " " + name + otherstuff + suffix + ";"; } else { if (length.Length != 0) { IsLiteral = true; //type != "string"; } if (otherstuff.Contains('=')) { string[] split = otherstuff.Split('='); otherstuff = split[0] + " = (" + Type + ")" + split[1]; } string t = KnownStuff.GetNamespacedType(this, Type); if (length.Length != 0) { output = lowestindent + "public " + t + "[] " + name + otherstuff + " = new " + t + "[" + length + "];"; } else { output = lowestindent + "public " + "" + t + "[] " + name + otherstuff + ";"; } } if (!KnownStuff.KnownTypes.ContainsKey(rostype)) { meta = true; } Name = name.Length == 0 ? otherstuff.Split('=')[0].Trim() : name; }