Example #1
0
        /// <summary>
        /// Both reads new #defines labels and processes them on the line.
        /// </summary>
        private string ProcessDefines(string curLine, Log log)
        {
            // Skip empty lines (after removing comments and doing defines)
            if (string.IsNullOrWhiteSpace(curLine))
            {
                return(string.Empty);
            }

            ///////// Process new #Defines /////////
            // lets see if the first word is a #define
            if (curLine.Contains("#define "))
            {
                Match def = Regex.Match(curLine,
                                        @"^\s*\#(?:define)\s*" +
                                        @"(?<name>[a-z_][a-z0-9_]*)" +
                                        @"(?:\(\s*" +
                                        @"(?:(?<params>[a-z_][a-z0-9_]*)(?:\s*,\s*(?!\)))?)+" +
                                        @"\s*\))?" +
                                        @"\s*(?<main>.*?)\s*" +
                                        @"(?://.*)?$");
                if (!def.Groups["name"].Success | !def.Groups["main"].Success)
                {
                    log.Error("unrecognized #define '{0}'", curLine);
                    //return null;
                }
                Define define = new Define {
                    name = def.Groups["name"].Value
                };
                if (def.Groups["params"].Success)
                {
                    define.defParams = new string[def.Groups["params"].Captures.Count];
                    for (int i = 0; i < define.defParams.Length; i++)
                    {
                        define.defParams[i] = def.Groups["params"].Captures[i].Value;
                    }
                }

                if (def.Groups["main"].Success)
                {
                    define.data = def.Groups["main"].Value; // you have NonSerializedAttribute idea what are you doing 676
                }
                // if the label does not start and end with a "_" then throw a warning
                if (define.name[0] != '_' || define.name[define.name.Length - 1] != '_')
                {
                    log.Warning("To prevent unintended usage the #define '{0}' should begin and end with an underscore.", define.name);
                }

                // check for duplicates
                foreach (var d in defines)
                {
                    int dParamCt      = (d.defParams == null) ? 0 : d.defParams.Length;
                    int defineParamCt = (define.defParams == null) ? 0 : define.defParams.Length;
                    // if name and param count match then we have a duplicate
                    if (d.name == define.name &&
                        (d.defParams == null ? 0 : d.defParams.Length) == (define.defParams == null ? 0 : define.defParams.Length))
                    {
                        log.Warning("Duplicate #define found for '{0}', replacing with new value.", define.name);
                        defines.Remove(d);
                        break;
                    }
                }

                defines.Add(define);
                curLine = "";
                //return null;
            }

            /////////   Replace Defines   /////////
            // Lets check this line to see if there are any defines in it.
            // We go in reverse order in case there are any nested #defines
            for (int i = defines.Count - 1; i >= 0; i--)
            {
                Define define = defines[i];
                if (define.defParams == null)
                {
                    curLine = curLine.Replace(define.name, define.data); // curLine = Regex.Replace(curLine, define.name, define.data);
                }
                else
                {
                    // Given: #define dog(op1) dog with op1 bones   THEN    dog(mouse) ---> rat+mouse
                    // Then: A dog(six) will be happy.
                    // Results In:  a dog with six bones will be happy.

                    // Step 1) find "dog(six)" and replace "op1" in "dog with op1 bones" with "six"
                    // Step 2) replace "dog(six)" in row and replace with "dog with six bones"

                    string items     = (define.defParams.Length - 1).ToString();
                    string searchFor = define.name + @"\((?<1>[^\r\n,\)\(]+?)(?:,(?<1>[^\r\n,\)\(]+?)){" + items + @"}\)";

                    curLine = Regex.Replace(curLine, searchFor, delegate(Match m)
                    {
                        int curParamNum = 0;
                        string val      = define.data;
                        foreach (Capture item in m.Groups[1].Captures)
                        {
                            val = val.Replace(define.defParams[curParamNum++], item.Value);
                        }
                        return(val);
                    });
                }
            }

            return(curLine);
        }
Example #2
0
        /// <summary>
        /// Both reads new #defines labels and processes them on the line.
        /// </summary>
        private string ProcessDefines(string curLine, Log log)
        {
            // Skip empty lines (after removing comments and doing defines)
            if (string.IsNullOrWhiteSpace(curLine))
                return string.Empty;

            ///////// Process new #Defines /////////
            // lets see if the first word is a #define
            if (curLine.Contains("#define "))
            {
                Match def = Regex.Match(curLine,
                    @"^\s*\#(?:define)\s*" +
                    @"(?<name>[a-z_][a-z0-9_]*)" +
                    @"(?:\(\s*" +
                    @"(?:(?<params>[a-z_][a-z0-9_]*)(?:\s*,\s*(?!\)))?)+" +
                    @"\s*\))?" +
                    @"\s*(?<main>.*?)\s*" +
                    @"(?://.*)?$");
                if (!def.Groups["name"].Success | !def.Groups["main"].Success)
                {
                    log.Error("unrecognized #define '{0}'", curLine);
                    //return null;
                }
                Define define = new Define { name = def.Groups["name"].Value };
                if (def.Groups["params"].Success)
                {
                    define.defParams = new string[def.Groups["params"].Captures.Count];
                    for (int i = 0; i < define.defParams.Length; i++)
                        define.defParams[i] = def.Groups["params"].Captures[i].Value;
                }

                if (def.Groups["main"].Success)
                    define.data = def.Groups["main"].Value; // you have NonSerializedAttribute idea what are you doing 676

                // if the label does not start and end with a "_" then throw a warning
                if (define.name[0] != '_' || define.name[define.name.Length - 1] != '_')
                    log.Warning("To prevent unintended usage the #define '{0}' should begin and end with an underscore.", define.name);

                // check for duplicates
                foreach (var d in defines)
                {
                    int dParamCt = (d.defParams == null) ? 0 : d.defParams.Length;
                    int defineParamCt = (define.defParams == null) ? 0 : define.defParams.Length;
                    // if name and param count match then we have a duplicate
                    if (d.name == define.name &&
                        (d.defParams == null ? 0 : d.defParams.Length) == (define.defParams == null ? 0 : define.defParams.Length))
                    {
                        log.Warning("Duplicate #define found for '{0}', replacing with new value.", define.name);
                        defines.Remove(d);
                        break;
                    }
                }

                defines.Add(define);
                curLine = "";
                //return null;
            }

            /////////   Replace Defines   /////////
            // Lets check this line to see if there are any defines in it.
            // We go in reverse order in case there are any nested #defines
            for (int i = defines.Count - 1; i >= 0; i--)
            {
                Define define = defines[i];
                if (define.defParams == null)
                    curLine = curLine.Replace(define.name, define.data); // curLine = Regex.Replace(curLine, define.name, define.data);
                else
                {
                    // Given: #define dog(op1) dog with op1 bones   THEN    dog(mouse) ---> rat+mouse   
                    // Then: A dog(six) will be happy.
                    // Results In:  a dog with six bones will be happy.

                    // Step 1) find "dog(six)" and replace "op1" in "dog with op1 bones" with "six"
                    // Step 2) replace "dog(six)" in row and replace with "dog with six bones"

                    string items = (define.defParams.Length - 1).ToString();
                    string searchFor = define.name + @"\((?<1>[^\r\n,\)\(]+?)(?:,(?<1>[^\r\n,\)\(]+?)){" + items + @"}\)";

                    curLine = Regex.Replace(curLine, searchFor, delegate (Match m)
                    {
                        int curParamNum = 0;
                        string val = define.data;
                        foreach (Capture item in m.Groups[1].Captures)
                            val = val.Replace(define.defParams[curParamNum++], item.Value);
                        return val;
                    });
                }
            }

            return curLine;
        }