Exemple #1
0
        /// <summary>
        /// (BNF) stylesheet : [ CDO | CDC | S | statement ]*;
        /// </summary>
        /// <returns>CSS StyleSheet parse tree</returns>
        private CssStyleSheet ParseStyleSheet()
        {
            CssStyleSheet styleSheet = new CssStyleSheet();

            using (this.reader = new LineReader(this.fileContent, this.source, CssParser.ReadFilters))
            {
                this.reader.NormalizeWhiteSpace = true;

#if DEBUG
                System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
#endif

                char ch;
                while (this.Read(out ch))
                {
                    switch (ch)
                    {
                    case '\uFEFF': // BOM (UTF byte order mark)
                    case '\t':     //TAB
                    case '\n':     //LF
                    case '\r':     //CR
                    case ' ':      //Space
                    {
                        // skip whitespace
                        continue;
                    }

                    case '<':
                    {
                        // CDO (Char Data Open?)
                        if (!this.Read(out ch) || ch != '-' ||
                            !this.Read(out ch) || ch != '-')
                        {
                            throw new SyntaxError("Expected \"<!--\"", this.reader.FilePath, this.reader.Line, this.reader.Column);
                        }
                        continue;
                    }

                    case '-':
                    {
                        // CDC (Char Data Close?)
                        if (!this.Read(out ch) || ch != '-' ||
                            !this.Read(out ch) || ch != '>')
                        {
                            throw new SyntaxError("Expected \"-->\"", this.reader.FilePath, this.reader.Line, this.reader.Column);
                        }
                        continue;
                    }

                    default:
                    {
                        try
                        {
                            CssStatement statement = this.ParseStatement();
                            styleSheet.Statements.Add(statement);
                        }
                        catch (ParseException ex)
                        {
                            this.errors.Add(ex);

                            while (this.Read(out ch) && ch != '}')
                            {
                                // restabilize on next statement
                            }
                        }
                        continue;
                    }
                    }
                }

#if DEBUG
                watch.Stop();
                Console.WriteLine("CSS parse duration: {0} ms for {1} chars", watch.ElapsedMilliseconds, this.reader.Length);
#endif
            }

            this.reader = null;
            this.source = null;

            return(styleSheet);
        }
Exemple #2
0
        /// <summary>
        /// (BNF) stylesheet : [ CDO | CDC | S | statement ]*;
        /// </summary>
        /// <returns>CSS StyleSheet parse tree</returns>
        private CssStyleSheet ParseStyleSheet()
        {
            CssStyleSheet styleSheet = new CssStyleSheet();
            using (this.reader = new LineReader(this.fileContent, this.source, CssParser.ReadFilters))
            {
                this.reader.NormalizeWhiteSpace = true;

#if DEBUG
                System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
#endif

                char ch;
                while (this.Read(out ch))
                {
                    switch (ch)
                    {
                        case '\uFEFF': // BOM (UTF byte order mark)
                        case '\t': //TAB
                        case '\n': //LF
                        case '\r': //CR
                        case ' ': //Space
                            {
                                // skip whitespace
                                continue;
                            }
                        case '<':
                            {
                                // CDO (Char Data Open?)
                                if (!this.Read(out ch) || ch != '-' ||
                                    !this.Read(out ch) || ch != '-')
                                {
                                    throw new SyntaxError("Expected \"<!--\"", this.reader.FilePath, this.reader.Line, this.reader.Column);
                                }
                                continue;
                            }
                        case '-':
                            {
                                // CDC (Char Data Close?)
                                if (!this.Read(out ch) || ch != '-' ||
                                    !this.Read(out ch) || ch != '>')
                                {
                                    throw new SyntaxError("Expected \"-->\"", this.reader.FilePath, this.reader.Line, this.reader.Column);
                                }
                                continue;
                            }
                        default:
                            {
                                try
                                {
                                    CssStatement statement = this.ParseStatement();
                                    styleSheet.Statements.Add(statement);
                                }
                                catch (ParseException ex)
                                {
                                    this.errors.Add(ex);

                                    while (this.Read(out ch) && ch != '}')
                                    {
                                        // restabilize on next statement
                                    }
                                }
                                continue;
                            }
                    }
                }

#if DEBUG
                watch.Stop();
                Console.WriteLine("CSS parse duration: {0} ms for {1} chars", watch.ElapsedMilliseconds, this.reader.Length);
#endif
            }

            this.reader = null;
            this.source = null;

            return styleSheet;
        }