Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        public MsgFile(MsgFileLocation filename, string extraindent)
        {
            if (filename == null)
            {
                throw new ArgumentNullException(nameof(filename));
            }

            if (!filename.Path.Contains(".msg"))
            {
                throw new ArgumentException($"'{filename}' is not a valid .msg file name.", nameof(filename));
            }

            this.msgFileLocation = filename;
            this.extraindent     = extraindent;

            if (resolver == null)
            {
                resolver = new Dictionary <string, Dictionary <string, List <ResolvedMsg> > >();
            }

            classname = filename.basename;
            Package   = filename.package;

            //Parse for the Namespace
            Namespace += "." + filename.package;
            Name       = filename.package + "." + classname;
            Namespace  = Namespace.Trim('.');

            if (!resolver.Keys.Contains(Package))
            {
                resolver.Add(Package, new Dictionary <string, List <ResolvedMsg> >());
            }
            if (!resolver[Package].ContainsKey(classname))
            {
                resolver[Package].Add(classname, new List <ResolvedMsg> {
                    new ResolvedMsg {
                        OtherType = Namespace + "." + classname, Definer = this
                    }
                });
            }
            else
            {
                resolver[Package][classname].Add(new ResolvedMsg {
                    OtherType = Namespace + "." + classname, Definer = this
                });
            }

            var lines = new List <string>(File.ReadAllLines(filename.Path));

            lines = lines.Where(st => (!st.Contains('#') || st.Split('#')[0].Length != 0)).ToList();
            for (int i = 0; i < lines.Count; i++)
            {
                lines[i] = lines[i].Split('#')[0].Trim();
            }

            this.lines = lines.Where(s => s.Trim().Length > 0).ToList();
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        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");
        }
Exemplo n.º 5
0
        public void Should_ProduceCorrectMD5Sums_When_GivenRosDefaultActionFiles(MsgFileLocation stubFile, string[] lines,
                                                                                 Dictionary <string, string> md5sums)
        {
            // Parse action file
            var actionFile = new ActionFile(stubFile, lines);

            actionFile.ParseAndResolveTypes();

            // Compare MD5 sums
            Assert.Equal(md5sums["Goal"], MD5.Sum(actionFile.GoalMessage));
            Assert.Equal(md5sums["ActionGoal"], MD5.Sum(actionFile.GoalActionMessage));
            Assert.Equal(md5sums["Result"], MD5.Sum(actionFile.ResultMessage));
            Assert.Equal(md5sums["ActionResult"], MD5.Sum(actionFile.ResultActionMessage));
            Assert.Equal(md5sums["Feedback"], MD5.Sum(actionFile.FeedbackMessage));
            Assert.Equal(md5sums["ActionFeedback"], MD5.Sum(actionFile.FeedbackActionMessage));
        }
Exemplo n.º 6
0
        //specifically for SRV halves
        public MsgFile(MsgFileLocation filename, bool isRequest, List <string> lines, string extraIndent)
        {
            this.msgFileLocation = filename;
            this.extraindent     = extraIndent;
            this.lines           = lines;

            if (resolver == null)
            {
                resolver = new Dictionary <string, Dictionary <string, List <ResolvedMsg> > >();
            }

            serviceMessageType = isRequest ? ServiceMessageType.Request : ServiceMessageType.Response;
            // Parse The file name to get the classname
            classname = filename.basename;
            // Parse for the Namespace
            Namespace += "." + filename.package;
            Name       = filename.package + "." + classname;
            classname += (isRequest ? "Request" : "Response");
            Namespace  = Namespace.Trim('.');
            Package    = filename.package;
            if (!resolver.Keys.Contains(Package))
            {
                resolver.Add(Package, new Dictionary <string, List <ResolvedMsg> >());
            }
            if (!resolver[Package].ContainsKey(classname))
            {
                resolver[Package].Add(classname, new List <ResolvedMsg> {
                    new ResolvedMsg {
                        OtherType = Namespace + "." + classname, Definer = this
                    }
                });
            }
            else
            {
                resolver[Package][classname].Add(new ResolvedMsg {
                    OtherType = Namespace + "." + classname, Definer = this
                });
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create a non SRV MsgsFile from a list of strings. Use suffix to prepend a string to the classname.
        /// </summary>
        public MsgFile(MsgFileLocation filename, List <string> lines, string suffix)
        {
            if (filename == null)
            {
                throw new ArgumentNullException(nameof(filename));
            }

            this.msgFileLocation = filename;

            if (resolver == null)
            {
                resolver = new Dictionary <string, Dictionary <string, List <ResolvedMsg> > >();
            }

            classname = filename.basename + suffix;
            Package   = filename.package;

            //Parse for the Namespace
            Namespace += "." + filename.package;
            Name       = filename.package + "." + classname;
            Namespace  = Namespace.Trim('.');

            if (!resolver.Keys.Contains(Package))
            {
                resolver.Add(Package, new Dictionary <string, List <ResolvedMsg> >());
            }
            if (!resolver[Package].ContainsKey(classname))
            {
                resolver[Package].Add(classname, new List <ResolvedMsg>());
            }
            resolver[Package][classname].Add(new ResolvedMsg {
                OtherType = Namespace + "." + classname, Definer = this
            });
            Debug.Assert(resolver[Package][classname].Count <= 1);

            this.lines = lines.Where(s => s.Trim().Length > 0).ToList();
        }
Exemplo n.º 8
0
 public MsgFile(MsgFileLocation filename)
     : this(filename, "")
 {
 }
Exemplo n.º 9
0
 public MsgFile(MsgFileLocation filename, bool isRequest, List <string> lines)
     : this(filename, isRequest, lines, string.Empty)
 {
 }
Exemplo n.º 10
0
 public ActionFile(MsgFileLocation filename, string[] lines)
 {
     InitializeActionFile(filename, lines);
 }
Exemplo n.º 11
0
 public ActionFile(MsgFileLocation filename)
 {
     // Read in action file
     string[] lines = File.ReadAllLines(filename.Path);
     InitializeActionFile(filename, lines);
 }
Exemplo n.º 12
0
        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();
            }
        }