Exemple #1
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 #2
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 (IsCSharpKeyword(name))
            {
                name = "@" + name;
            }

            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();
            }
        }