Exemplo n.º 1
0
        /// <summary>
        /// C#/Script のプリプロセスを行います。
        /// </summary>
        /// <param name="parameters">プリプロセッサの設定。</param>
        /// <param name="sources">ソースプログラム。</param>
        /// <returns></returns>
        public ScriptPreprocessorResults Preprocess(ScriptPreprocessorParameters parameters, params string[] sources)
        {
            this.parameters = parameters;

            this.results = new ScriptPreprocessorResults();
            this.results.ReferencedAssemblies = new string[] { };
            this.results.OutputSources        = new string[sources.Length];
            this.errors = new CompilerErrorCollection();

            for (int i = 0; i < sources.Length; i++)
            {
                this.currentSourceNumber = i;

                string code = sources[i];

                // Delete comments.
                code = PreprocessComment(code);

                // Expand #include directives.
                code = PreprocessInclude(code);

                // Expand macro.
                code = PreprocessFunctionMacro(code);
                code = PreprocessObjectMacro(code);

                // Preprocess reserved macros.
                code = PreprocessReservedMacro(code);

                // Process #using directives.
                code = PreprocessUsing(code);

                this.results.OutputSources[i] = code;
            }

            foreach (PreprocessorError error in this.errors)
            {
                // Modify error information.
                string source = sources[error.SourceNumber];
                int    index  = source.IndexOf(error.ErrorText);
                int    line   = source.Substring(0, index).Split('\n').Length;
                int    column = source.Split('\n')[line].IndexOf(error.ErrorText);
                error.Line   = line + 1;
                error.Column = column;
            }

            if (this.errors.HasErrors)
            {
                throw new CompilerErrorException("C#/Script プリプロセス", this.errors);
            }

            return(this.results);
        }
Exemplo n.º 2
0
        /// <summary>
        /// C#/Script のプリプロセスを行います。
        /// </summary>
        /// <param name="parameters">プリプロセッサの設定。</param>
        /// <param name="sources">ソースファイル。</param>
        /// <returns></returns>
        public ScriptPreprocessorResults PreprocessFromFile(ScriptPreprocessorParameters parameters, params string[] filenames)
        {
            string[] sources = filenames.Select(filename =>
            {
                using (StreamReader sr = new StreamReader(filename))
                {
                    return(sr.ReadToEnd());
                }
            }).ToArray();

            var result = Preprocess(parameters, sources);

            foreach (PreprocessorError error in this.errors)
            {
                error.FileName = filenames[error.SourceNumber];
            }

            return(result);
        }