Exemplo n.º 1
0
 public static void DefineFile(string path, IDefineCollection defCol)
 {
     StreamReader sr = new StreamReader(path);
     while (!sr.EndOfStream)
     {
         string line = sr.ReadLine();
         if (line.Length > 0)
         {
             string[] dividedLine = null;// = line.Split(parameterSplitCharacters, parameterUniterCharacters);
             for (int i = 1; i < dividedLine.Length; i++)
             {
                 defCol.Add(dividedLine[i], dividedLine[0]);
             }
         }
     }
     sr.Close();
 }
Exemplo n.º 2
0
        public static void DefineFile(string path, IDefineCollection defCol)
        {
            StreamReader streamReader = new StreamReader(path);

            while (!streamReader.EndOfStream)
            {
                if (streamReader.ReadLine().Length > 0)
                {
                    string[] strArray = (string[])null;

                    for (int index = 1; index < strArray.Length; ++index)
                    {
                        defCol.Add(strArray [index], strArray [0]);
                    }
                }
            }

            streamReader.Close();
        }
Exemplo n.º 3
0
        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;
            }
        }