Пример #1
0
        public static void IgnoreWhiteSpaceAndComments(this IPreprocessorReader rdr, bool stayOnSameLine)
        {
            char ch;
            bool lineChangeFound;

            while (!rdr.EOF)
            {
                ch = rdr.Peek();

                if (stayOnSameLine && (ch == '\r' || ch == '\n'))
                {
                    break;
                }

                if (char.IsWhiteSpace(ch))
                {
                    rdr.IgnoreWhile(stayOnSameLine ? WhiteNonEolChars : WhiteChars);
                    continue;
                }

                if (!rdr.IgnoreComments(out lineChangeFound))
                {
                    break;
                }
                if (lineChangeFound && stayOnSameLine)
                {
                    break;
                }
            }
        }
Пример #2
0
        public static void IgnoreWhiteSpaceAndCommentsToNextLine(this IPreprocessorReader rdr)
        {
            char ch;
            bool lineChangeFound;

            while (!rdr.EOF)
            {
                ch = rdr.Peek();

                if (ch == '\n')
                {
                    rdr.Ignore(1);
                    break;
                }

                if (char.IsWhiteSpace(ch))
                {
                    rdr.Ignore(1);
                    continue;
                }

                if (!rdr.IgnoreComments(out lineChangeFound))
                {
                    break;
                }
                if (lineChangeFound)
                {
                    break;
                }
            }
        }
Пример #3
0
        public static string ReadAndIgnoreStringLiteral(this IPreprocessorReader rdr)
        {
            // This function assumes the next character to read is either " or '.

            char quote = rdr.Peek();

            rdr.Ignore(1);

            char ch;
            var  sb = new StringBuilder();

            sb.Append(quote);

            while (!rdr.EOF)
            {
                ch = rdr.Peek();
                if (ch == quote)
                {
                    sb.Append(ch);
                    rdr.Ignore(1);

                    if (quote == '"' && rdr.Peek() == '"')
                    {
                        // Double "" means keep going
                        sb.Append('"');
                        rdr.Ignore(1);
                    }
                    else
                    {
                        break;
                    }
                }
                else if (ch == '\\')
                {
                    sb.Append('\\');
                    rdr.Ignore(1);
                    if (!rdr.EOF)
                    {
                        sb.Append(rdr.Peek());
                        rdr.Ignore(1);
                    }
                }
                else
                {
                    sb.Append(ch);
                    rdr.Ignore(1);
                }
            }

            return(sb.ToString());
        }
Пример #4
0
        public static string ReadAndIgnoreNestableContent(this IPreprocessorReader rdr, string endToken)
        {
            var    sb = new StringBuilder();
            string str;

            rdr.IgnoreWhiteSpaceAndComments(false);

            while (!rdr.EOF)
            {
                str = rdr.PeekToken(false);
                if (string.IsNullOrEmpty(str))
                {
                    continue;
                }

                if (str == endToken)
                {
                    rdr.Ignore(str.Length);
                    break;
                }
                else if (str == "(")
                {
                    sb.Append(str);
                    rdr.Ignore(str.Length);
                    sb.Append(rdr.ReadAndIgnoreNestableContent(")"));
                    sb.Append(")");
                }
                else if (str == "{")
                {
                    sb.Append(str);
                    rdr.Ignore(str.Length);
                    sb.Append(rdr.ReadAndIgnoreNestableContent("}"));
                    sb.Append("}");
                }
                else if (str == "[")
                {
                    sb.Append(str);
                    rdr.Ignore(str.Length);
                    sb.Append(rdr.ReadAndIgnoreNestableContent("]"));
                    sb.Append("]");
                }
                else
                {
                    sb.Append(str);
                    rdr.Ignore(str.Length);
                }
            }

            return(sb.ToString());
        }
Пример #5
0
 public PreprocessorParams(IPreprocessorReader reader, IPreprocessorWriter writer, string fileName,
                           IEnumerable <string> parentFiles, FileContext serverContext, ContentType contentType, string stopAtIncludeFile)
 {
     this.reader   = reader;
     this.writer   = writer;
     this.fileName = fileName;
     if (parentFiles != null)
     {
         this.parentFiles = parentFiles.ToArray();
     }
     this.fileContext       = serverContext;
     this.contentType       = contentType;
     this.stopAtIncludeFile = stopAtIncludeFile;
 }
Пример #6
0
        /// <summary>
        /// Preprocesses a document.
        /// </summary>
        /// <param name="reader">The document to be read.</param>
        /// <param name="writer">The output stream for the preprocessed document.</param>
        /// <param name="fileName">The name of the file being preprocessed.</param>
        /// <param name="parentFiles">A list of files which are to be considered parent files (to avoid cyclical #includes)</param>
        /// <param name="fileContext">The type of file.</param>
        /// <returns>True if the preprocessor changed a part of the document and the document should be re-run through this function again.
        /// False if the document has finished preprocessing.</returns>
        public PreprocessorResult Preprocess(IPreprocessorReader reader, IPreprocessorWriter writer, string fileName,
                                             IEnumerable <string> parentFiles, FileContext fileContext, string stopAtIncludeFile = null,
                                             IEnumerable <PreprocessorDefine> stdlibDefines = null)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return(Preprocess(new PreprocessorParams(reader, writer, fileName, parentFiles, fileContext,
                                                     ContentType.File, stopAtIncludeFile)
            {
                isMainSource = true,
                stdlibDefines = stdlibDefines
            }));
        }
Пример #7
0
        public static string PeekIdentifier(this IPreprocessorReader rdr)
        {
            var first = true;

            return(rdr.PeekUntil(ch =>
            {
                if (first)
                {
                    first = false;
                    return ch.IsWordChar(true);
                }
                else
                {
                    return ch.IsWordChar(false);
                }
            }));
        }
Пример #8
0
        public static string PeekToken(this IPreprocessorReader rdr, bool stayOnSameLine)
        {
            bool lineChange;

            if (rdr.IgnoreComments(out lineChange))
            {
                if (lineChange && stayOnSameLine)
                {
                    return(null);
                }
                return(string.Empty);
            }

            var ch = rdr.Peek();

            if ((ch == '\r' || ch == '\n') && stayOnSameLine)
            {
                return(null);
            }

            if (ch == ' ' || ch == '\t')
            {
                return(rdr.PeekUntil(c => c == ' ' || c == '\t'));
            }

            if (ch.IsWordChar(true))
            {
                return(rdr.PeekIdentifier());
            }

            if (char.IsDigit(ch))
            {
                var gotDecimal = false;
                return(rdr.PeekUntil(c =>
                {
                    if (char.IsDigit(c))
                    {
                        return true;
                    }
                    if (c == '.')
                    {
                        if (gotDecimal)
                        {
                            return false;
                        }
                        gotDecimal = true;
                        return true;
                    }
                    return false;
                }));
            }

            if (ch == '\"' || ch == '\'')
            {
                var sb = new StringBuilder();

                var lastCh = '\0';
                var first  = true;
                var gotEnd = false;
                sb.Append(rdr.PeekUntil(c =>
                {
                    if (gotEnd)
                    {
                        return(false);
                    }

                    if (first)
                    {
                        first = false;
                        return(true);
                    }

                    if (c == ch && lastCh != '\\')
                    {
                        gotEnd = true;
                        return(true);
                    }

                    if (c == '\\' && lastCh == '\\')
                    {
                        lastCh = '\0';
                    }
                    else
                    {
                        lastCh = c;
                    }

                    return(true);
                }));
                return(sb.ToString());
            }

            if (ch == '#')
            {
                var index = -1;
                return(rdr.PeekUntil(c =>
                {
                    index++;
                    if (index == 0)
                    {
                        return c == '#';
                    }
                    else if (index == 1)
                    {
                        return c.IsWordChar(true);
                    }
                    else
                    {
                        return c.IsWordChar(false);
                    }
                }));
            }

            return(ch.ToString());
        }
Пример #9
0
        public static bool IgnoreComments(this IPreprocessorReader rdr, out bool lineChangeFound, bool multiLineOnly)
        {
            lineChangeFound = false;
            if (rdr.Peek() == '/')
            {
                var str = rdr.Peek(2);
                if (str == "/*")
                {
                    rdr.Ignore(2);
                    //rdr.IgnoreUntil(c => c != '/' && c != '*' && c != '\r' && c != '\n');
                    rdr.IgnoreUntil(_multiLineCommentBreakChars);

                    char ch;
                    while (!rdr.EOF)
                    {
                        ch = rdr.Peek();
                        if (ch == '*')
                        {
                            if (rdr.Peek(2) == "*/")
                            {
                                rdr.Ignore(2);
                                return(true);
                            }
                            else
                            {
                                rdr.Ignore(1);
                            }
                        }
                        else if (ch == '/')
                        {
                            bool lineChange;
                            if (rdr.IgnoreComments(out lineChange, true))
                            {
                                if (lineChange)
                                {
                                    lineChangeFound = true;
                                }
                            }
                            else
                            {
                                rdr.Ignore(1);
                            }
                        }
                        else if (ch == '\r' || ch == '\n')
                        {
                            lineChangeFound = true;
                            rdr.Ignore(1);
                        }
                        //rdr.IgnoreUntil(c => c != '/' && c != '*' && c != '\r' && c != '\n');
                        rdr.IgnoreUntil(_multiLineCommentBreakChars);
                    }
                    return(true);
                }
                else if (str == "//" && multiLineOnly == false)
                {
                    //rdr.IgnoreUntil(c => c != '\r' && c != '\n');
                    rdr.IgnoreUntil(LineEndChars);
                    return(true);
                }
            }
            return(false);
        }
Пример #10
0
 public static bool IgnoreComments(this IPreprocessorReader rdr, out bool lineChangeFound)
 {
     return(rdr.IgnoreComments(out lineChangeFound, false));
 }
Пример #11
0
        public static bool IgnoreComments(this IPreprocessorReader rdr)
        {
            bool lineChange;

            return(rdr.IgnoreComments(out lineChange, false));
        }