示例#1
0
        private void Preprocess(TextReader reader, string file, DefineCollection defCol,
                                ICollection <string> lines, Stack <bool> ifStack)
        {
            int    stackDepth = ifStack.Count;
            string line;
            bool   include = ifStack.And();

            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length > 0)
                {
                    if (line[0] == '#')
                    {
                        string[] splitLine = StringExtensions.Split(line,
                                                                    parameterSplitCharacters, parameterUniterCharacters);
                        //string[] splitLine = StringHelper.DivideLine(line);

                        switch (splitLine[0].ToLower())
                        {
                        case define:
                            if (include)
                            {
                                if (splitLine.Length > 1)
                                {
                                    if (splitLine[1].Equals(defineFile))
                                    {
                                        string path = IOHelpers.FindFile(file, splitLine[2]);
                                        if (!string.IsNullOrEmpty(path))
                                        {
                                            IOHelpers.DefineFile(path, defCol);
                                        }
                                        else
                                        {
                                            messageHandler.AddFileNotFoundError(file, line, splitLine[2]);
                                        }
                                    }
                                    else
                                    {
                                        int indexBeg = splitLine[1].IndexOf('(');
                                        int indexEnd = splitLine[1].IndexOf(')');
                                        if (indexBeg >= 0 && indexEnd >= 0 && indexBeg < indexEnd)
                                        {
                                            string   parametersList = splitLine[1].Substring(indexBeg + 1, indexEnd - indexBeg - 1);
                                            string[] parameters     = parametersList.Split(',');
                                            string   original       = splitLine[1].Substring(0, indexBeg);
                                            for (int i = 0; i < parameters.Length; i++)
                                            {
                                                parameters[i] = parameters[i].Trim();
                                            }
                                            defCol.Add(original, splitLine[2], parameters);
                                        }
                                        else if (indexBeg == -1 && indexEnd == -1)
                                        {
                                            if (splitLine.Length > 2)
                                            {
                                                defCol.Add(splitLine[1], splitLine[2]);
                                            }
                                            else
                                            {
                                                defCol.Add(splitLine[1], "");
                                            }
                                        }
                                        else
                                        {
                                            messageHandler.AddError(define + " is improperly defined: " + line);
                                        }
                                    }
                                }
                                else
                                {
                                    messageHandler.AddNotEnoughParametersError(file, line, define, 1);
                                }
                            }
                            break;

                        case unDefine:
                            if (include)
                            {
                                if (splitLine.Length > 1)
                                {
                                    defCol.Remove(splitLine[1]);
                                }
                                else
                                {
                                    messageHandler.AddNotEnoughParametersError(file, line, unDefine, 1);
                                }
                            }
                            break;

                        case includeFile:
                            if (include)
                            {
                                if (splitLine.Length > 1)
                                {
                                    string path = IOHelpers.FindFile(file, splitLine[1]);
                                    if (!string.IsNullOrEmpty(path))
                                    {
                                        string moreText = File.ReadAllText(path);
                                        moreText = this.RemoveComments(moreText);
                                        lines.Add("{");
                                        using (StringReader newReader = new StringReader(moreText))
                                        {
                                            Preprocess(newReader, path, defCol, lines, ifStack);
                                            //Preprocess(moreText, path, defCol, lines, ifStack);
                                        }
                                        lines.Add("}");
                                    }
                                    else
                                    {
                                        messageHandler.AddFileNotFoundError(file, line, splitLine[1]);
                                    }
                                }
                                else
                                {
                                    messageHandler.AddNotEnoughParametersError(file, line, includeFile, 1);
                                }
                            }
                            break;

                        case includeBinary:
                            if (include)
                            {
                                if (splitLine.Length > 1)
                                {
                                    string path = IOHelpers.FindFile(file, splitLine[1]);
                                    if (!string.IsNullOrEmpty(path))
                                    {
                                        byte[]        data    = File.ReadAllBytes(path);
                                        StringBuilder newLine = new StringBuilder("CODE");
                                        for (int i = 0; i < data.Length; i++)
                                        {
                                            newLine.Append(data[i].ToHexString(" 0x"));
                                        }
                                        lines.Add(newLine.ToString());
                                    }
                                    else
                                    {
                                        messageHandler.AddFileNotFoundError(file, line, splitLine[1]);
                                    }
                                }
                                else
                                {
                                    messageHandler.AddNotEnoughParametersError(file, line, includeBinary, 1);
                                }
                            }
                            break;

                        case ifDefined:
                            if (splitLine.Length > 1)
                            {
                                ifStack.Push(defCol.ContainsName(splitLine[1]));
                                include = ifStack.And();
                            }
                            else
                            {
                                messageHandler.AddNotEnoughParametersError(file, line, ifDefined, 1);
                            }
                            break;

                        case ifNotDefined:
                            if (splitLine.Length > 1)
                            {
                                ifStack.Push(!defCol.ContainsName(splitLine[1]));
                                include = ifStack.And();
                            }
                            else
                            {
                                messageHandler.AddNotEnoughParametersError(file, line, ifNotDefined, 1);
                            }
                            break;

                        case ifElse:
                            bool top = ifStack.Pop();
                            ifStack.Push(!top);
                            include = ifStack.And();
                            break;

                        case ifEnd:
                            ifStack.Pop();
                            include = ifStack.And();
                            break;

                        case "#org":
                            lines.Add(line.Substring(1));
                            messageHandler.AddWarning("#ORG no longer exists. Use ORG instead.");
                            break;

                        default:
                            messageHandler.AddError(splitLine[0] + " is not usable preprocessor command: " + line);
                            break;
                        }
                    }
                    else if (include)
                    {
                        string[] newLines = line.Split(extraLineSplitters);
                        for (int i = 0; i < newLines.Length; i++)
                        {
                            lines.Add(newLines[i]);
                        }
                    }
                }
            }

            if (ifStack.Count != stackDepth)
            {
                messageHandler.AddWarning("#IFDEF stack unbalanced in file " + file + ".");
            }
        }
        private void PreprocessorDirective(string line, string name, string[] parameters,
                                           string path, int lineNumber)
        {
            switch (name)
            {
            case "ifdef":
                if (parameters.Length > 0)
                {
                    include.Push(defCol.ContainsName(parameters[0]) || predefined.Contains(parameters[0]));
                }
                else
                {
                    messageLog.AddError(path, line, "#ifdef requires 1 parameter");
                }
                break;

            case "ifndef":
                if (parameters.Length > 0)
                {
                    include.Push(!(defCol.ContainsName(parameters[0]) || predefined.Contains(parameters[0])));
                }
                else
                {
                    messageLog.AddError(path, line, "#ifndef requires 1 parameter");
                }
                break;

            case "else":
                if (include.Count > 0)
                {
                    include.Push(!include.Pop());
                }
                else
                {
                    messageLog.AddError(path, line, "#else used without #ifdef or #ifndef.");
                }
                break;

            case "endif":
                if (include.Count > 0)
                {
                    include.Pop();
                }
                else
                {
                    messageLog.AddError(path, line, "#endif used without #ifdef or #ifndef.");
                }
                break;

            case "define":
                if (include.And())
                {
                    if (parameters.Length > 1)
                    {
                        string[] macroParam;
                        string   mname;
                        int      startIndex = parameters[0].IndexOf('(');
                        int      endIndex   = parameters[0].LastIndexOf(')');
                        if (startIndex != -1 && endIndex != -1 && startIndex < endIndex)
                        {
                            string paramString = parameters[0].Substring(
                                startIndex + 1, endIndex - startIndex - 1);
                            macroParam = paramString.Split(macroSeparators, uniters);
                            mname      = parameters[0].Substring(0, startIndex);
                        }
                        else
                        {
                            macroParam = new string[0];
                            mname      = parameters[0];
                        }
                        for (int j = 0; j < macroParam.Length; j++)
                        {
                            macroParam[j] = macroParam[j].Trim();
                        }


                        if (mname.Equals(parameters[1]))
                        {
                            messageLog.AddWarning(path, line, "Defining something as itself.");
                        }
                        else if (!defCol.IsValidName(mname))
                        {
                            messageLog.AddError(path, line, mname + " is not valid name to define.");
                        }
                        else if (predefined.Contains(mname))
                        {
                            messageLog.AddError(path, line, mname + " cannot be redefined.");
                        }
                        else if (reserved.Contains(mname))
                        {
                            messageLog.AddError(path, line, mname + " is reserved.");
                        }
                        else
                        {
                            if (defCol.ContainsName(mname, macroParam))
                            {
                                messageLog.AddWarning(path, line, "Redefining " + mname);
                            }
                            defCol.Add(mname, parameters[1].Trim('"'), macroParam);
                        }
                    }
                    else if (parameters.Length == 1)
                    {
                        defCol.Add(parameters[0], "");
                    }
                    else
                    {
                        messageLog.AddError(path, line, "#define requires 1 parameters");
                    }
                }
                break;

            case "undef":
                if (include.And())
                {
                    if (parameters.Length > 0)
                    {
                        //Check amount of macro parameters
                        defCol.Remove(parameters[0]);
                    }
                    else
                    {
                        messageLog.AddError(path, line, "#undef requires 1 parameters");
                    }
                }
                break;

            case "include":
                if (include.And())
                {
                    if (parameters.Length > 0)
                    {
                        string file = IOHelpers.FindFile(path, parameters[0]);
                        if (file.Length > 0)
                        {
                            if (file.Equals(path))
                            {
                                messageLog.AddError(path, line, "File including itself.");
                            }
                            else
                            {
                                string newAssembly = File.ReadAllText(file);
                                newAssembly = ReplaceComments(newAssembly);

                                if (includedFilesAsNewScope)
                                {
                                    output.AppendLine("{");
                                }
                                using (StringReader reader = new StringReader(newAssembly))
                                {
                                    Preprocess(reader, Path.GetFullPath(file));
                                }
                                if (includedFilesAsNewScope)
                                {
                                    output.AppendLine("}");
                                }
                            }
                        }
                        else
                        {
                            messageLog.AddFileNotFoundError(path, line, parameters[0]);
                        }
                    }
                    else
                    {
                        messageLog.AddError(path, line, "#include requires 1 parameters");
                    }
                }
                break;

            case "incbin":
                if (include.And())
                {
                    if (parameters.Length > 0)
                    {
                        string file = IOHelpers.FindFile(path, parameters[0]);
                        if (file.Length > 0)
                        {
                            byte[] data = File.ReadAllBytes(file);
                            output.Append("CODE");
                            for (int i = 0; i < data.Length; i++)
                            {
                                output.Append(data[i].ToHexString(" 0x"));
                            }
                            output.AppendLine();
                        }
                        else
                        {
                            messageLog.AddFileNotFoundError(path, line, parameters[0]);
                        }
                    }
                }
                break;

            default:
                messageLog.AddError(path, line, name + " is not usable preprocessor command.");
                break;
            }
        }