Esempio n. 1
0
        private bool IsDefine_impl(DefineObject define)
        {
            bool flag = false;

            if (define is DefineString)
            {
                flag = this.m_script.ContainDefine(((DefineString)define).Define);
            }
            else
            {
                DefineOperate operate = (DefineOperate)define;
                bool          flag2   = this.IsDefine_impl(operate.Left);
                if (flag2 && !operate.and)
                {
                    flag = true;
                }
                else if (!flag2 && operate.and)
                {
                    flag = false;
                }
                else if (operate.and)
                {
                    flag = flag2 && this.IsDefine_impl(operate.Right);
                }
                else
                {
                    flag = flag2 || this.IsDefine_impl(operate.Right);
                }
            }
            if (define.Not)
            {
                flag = !flag;
            }
            return(flag);
        }
Esempio n. 2
0
        private DefineObject GetOneDefine()
        {
            Token token = this.ReadToken();
            bool  flag  = false;

            if (token.Type == Scorpio.Compiler.TokenType.Not)
            {
                flag  = true;
                token = this.ReadToken();
            }
            if (token.Type == Scorpio.Compiler.TokenType.LeftPar)
            {
                DefineObject obj2 = this.ReadDefine();
                this.ReadRightParenthesis();
                obj2.Not = flag;
                return(obj2);
            }
            if (token.Type != Scorpio.Compiler.TokenType.Identifier)
            {
                throw new ParserException("宏定义判断只支持 字符串", token);
            }
            return(new DefineString(token.Lexeme.ToString())
            {
                Not = flag
            });
        }
Esempio n. 3
0
        private bool IsDefine_impl(DefineObject define)
        {
            bool ret = false;

            if (define is DefineString)
            {
                ret = m_script.ContainDefine(((DefineString)define).Define);
            }
            else
            {
                DefineOperate oper = (DefineOperate)define;
                bool          left = IsDefine_impl(oper.Left);
                if (left && !oper.and)
                {
                    ret = true;
                }
                else if (!left && oper.and)
                {
                    ret = false;
                }
                else if (oper.and)
                {
                    ret = left && IsDefine_impl(oper.Right);
                }
                else
                {
                    ret = left || IsDefine_impl(oper.Right);
                }
            }
            if (define.Not)
            {
                ret = !ret;
            }
            return(ret);
        }
Esempio n. 4
0
 private bool IsDefine_impl(DefineObject define) {
     bool ret = false;
     if (define is DefineString) {
         ret = m_script.ContainDefine(((DefineString)define).Define);
     } else {
         DefineOperate oper = (DefineOperate)define;
         bool left = IsDefine_impl(oper.Left);
         if (left && !oper.and) {
             ret = true;
         } else if (!left && oper.and) {
             ret = false;
         } else if (oper.and) {
             ret = left && IsDefine_impl(oper.Right);
         } else {
             ret = left || IsDefine_impl(oper.Right);
         }
     }
     if (define.Not) { ret = !ret; }
     return ret;
 }
Esempio n. 5
0
 public bool and;                //是否是并且操作
 public DefineOperate(DefineObject left, DefineObject right, bool and) {
     this.Left = left;
     this.Right = right;
     this.and = and;
 }
Esempio n. 6
0
 public bool and;                //是否是并且操作
 public DefineOperate(DefineObject left, DefineObject right, bool and)
 {
     this.Left  = left;
     this.Right = right;
     this.and   = and;
 }
Esempio n. 7
0
    public static void FindDefines(string directory, Dictionary <string, DefineObject> defines)
    {
        if (!Directory.Exists(directory))
        {
            Debug.LogError("Directory does not exist (" + directory + ")");
            return;
        }



        DirectoryInfo dir = new DirectoryInfo(directory);

        FileInfo[] files = dir.GetFiles("*.cs");
        foreach (FileInfo filePath in files)
        {
            //Debug.Log ("Copying "+filePath.Name+" to "+destination);


            //string result = PreProcessScript (directory+"/"+filePath.Name, directives);

            string path = directory + "/" + filePath.Name;

            if (!File.Exists(path))
            {
                Debug.LogError("File does not exist : " + path);
                continue;
            }


            string text = File.ReadAllText(path);

            //Regex matching (optional //)#define [name] (optional->)//[description]
            Regex defineRegex = new Regex(@"^(//)?#define\s+?(\w+)(?:[^\n]*//(.+))?", RegexOptions.Multiline);

            //Regex matching "somestring" (with the quotation marks)
            Regex nameRegex = new Regex("\"(.*?)\"", RegexOptions.Multiline);

            //Regex matching [something]
            Regex enumRegex = new Regex(@"\[(.*?)\]", RegexOptions.Multiline);

            //Loop through all matches in this file
            foreach (Match match in defineRegex.Matches(text))
            {
                string name = match.Groups[2].Value;
                string key  = name;

                bool firstAdd = false;

                //Create the define object if it has not already been found in another file
                if (!defines.ContainsKey(name))
                {
                    firstAdd = true;
                    defines.Add(name, new DefineObject());
                }

                DefineObject defOb = defines[name];

                //Get the description from group 3
                string desc = match.Groups[3].Value;

                defOb.name = name;

                //Find the optional name value in the description (in the form "MyName") and replace the name got from the key
                Match nameMatch = nameRegex.Match(desc);

                if (nameMatch.Success)
                {
                    defOb.name = nameMatch.Groups[1].Value;
                    desc       = desc.Replace(nameMatch.Value, "");
                }

                //It is enabled if we couldn't find the "//" in the beginning of the string
                bool enabled = match.Groups[1].Value == "";

                //Check if some defines with this name are enabled and some are not
                if (!firstAdd && defOb.enabled != enabled)
                {
                    defOb.inconsistent = true;
                }

                defOb.enabled = enabled;

                //Find the optional enum values (in the form [Value1,Value2,Value3])
                Match enumMatch = enumRegex.Match(desc);

                if (enumMatch.Success)
                {
                    string enumstring = enumMatch.Groups[1].Value;

                    //Split the enum values by ","
                    string[] enums = enumstring.Split(","[0]);

                    //Add a special value to the start of the list
                    List <string> enumList = new List <string> (enums.Length + 1);
                    enumList.Add("Define Disabled");
                    enumList.AddRange(enums);

                    enums = enumList.ToArray();

                    defOb.enumValues = enums;

                    //Remove the enums from the description
                    desc = desc.Replace(enumMatch.Value, "");

                    if (defOb.enabled)
                    {
                        //Figure out which one is selected right now
                        for (int j = 0; j < defOb.enumValues.Length; j++)
                        {
                            if (key == defOb.enumValues[j])
                            {
                                defOb.selectedEnum = j;
                                break;
                            }
                        }
                    }
                }

                //Only add to the brief if it was empty before
                if (defOb.brief == "")
                {
                    defOb.brief = desc;
                }

                //Add to the files list
                defOb.files += (defOb.files == "" ? "" : ", ") + filePath.Name;
            }
        }

        //Search sub-folders
        DirectoryInfo[] children = dir.GetDirectories();
        foreach (DirectoryInfo dirPath in children)
        {
            FindDefines(directory + "/" + dirPath.Name, defines);
        }
    }
Esempio n. 8
0
    public static void ApplyDefines(string directory, Dictionary <string, DefineObject> defines)
    {
        if (!Directory.Exists(directory))
        {
            Debug.LogError("Directory does not exist (" + directory + ")");
            return;
        }



        DirectoryInfo dir = new DirectoryInfo(directory);

        FileInfo[] files = dir.GetFiles("*.cs");
        foreach (FileInfo filePath in files)
        {
            string path = directory + "/" + filePath.Name;

            if (!File.Exists(path))
            {
                Debug.LogError("File does not exist : " + path);
                continue;
            }


            string text = File.ReadAllText(path);

            StringBuilder newScript = new StringBuilder();

            Regex defineRegex = new Regex(@"^(//)?#define\s+?(\w+)(?:[^\n]*//(.+))?", RegexOptions.Multiline);

            //Match[] matches =

            int prevIndex = 0;

            bool changed = false;

            foreach (Match match in defineRegex.Matches(text))
            {
                newScript.Append(text.Substring(prevIndex, match.Index - prevIndex));
                prevIndex = match.Index;

                string name = match.Groups[2].Value;

                if (!defines.ContainsKey(name))
                {
                    newScript.Append(match.Value);
                    continue;
                }

                DefineObject defOb = defines[name];

                bool enabled = match.Groups[1].Value == "";

                if (!enabled && defOb.enabled)
                {
                    prevIndex += 2;
                    changed    = true;
                }
                else if (enabled && !defOb.enabled)
                {
                    newScript.Append("//");
                    changed = true;
                }

                if (defOb.selectedEnum != 0 && defOb.enumValues != null)
                {
                    prevIndex = match.Groups[2].Index + match.Groups[2].Length;
                    newScript.Append("#define ");
                    newScript.Append(defOb.enumValues[defOb.selectedEnum]);
                    changed = true;
                }
            }

            if (changed)
            {
                newScript.Append(text.Substring(prevIndex));

                using (StreamWriter outfile =
                           new StreamWriter(path))
                {
                    outfile.Write(newScript.ToString());
                }
            }
        }

        DirectoryInfo[] children = dir.GetDirectories();
        foreach (DirectoryInfo dirPath in children)
        {
            ApplyDefines(directory + "/" + dirPath.Name, defines);
        }
    }