private IEnumerable <Token> HandleInclude(IList <Token> arguments) { if (arguments.Count == 0 || arguments[0].Type != TokenType.StringLiteral) { // TODO throw new NotImplementedException("Expected a string literal!"); } var includeFileName = arguments[0].Value; includeFileName = includeFileName.Substring(1, includeFileName.Length - 2); // Search include paths foreach (var includePath in state.IncludePaths.Prepend(root)) { var filePath = Path.GetFullPath(Path.Combine(includePath, includeFileName)); if (!File.Exists(filePath)) { continue; } // We found the file to include // Check if it's pragma onced if (state.PragmaOncedFiles.Contains(filePath)) { // Yes, let's skip it return(Enumerable.Empty <Token>()); } // Create a sub-processor at the new root var sourceText = new StreamReader(filePath).AsCharEnumerable(); var tokensToPreProcess = Lexer.Lex(CppTextReader.Process(sourceText)); var pp = new PreProcessor(filePath, state); return(pp.Process(tokensToPreProcess).SkipLast(1)); } // TODO throw new NotImplementedException($"Can't find include '{includeFileName}'!"); }
/// <summary> /// Processes the given source. /// </summary> /// <param name="source">The source characters to process.</param> /// <returns>The processed characters that have no trigraphs or line-continuations.</returns> public static IEnumerable <PositionedChar> Process(IEnumerable <char> source) { var reader = new CppTextReader(source); while (reader.MoveNext()) { yield return(reader.Current); } }