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 + "."); } }