Exemple #1
0
        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);
        }
Exemple #2
0
 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;
 }
Exemple #3
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();
            }
        }
Exemple #4
0
        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();
            }
        }
Exemple #5
0
        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;
        }