/// <summary> /// (BNF) ruleset : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*; /// </summary> /// <returns></returns> private CssRuleSet ParseRuleSet() { char ch; CssRuleSet ruleSet = new CssRuleSet(); ParseSelectors: while (true) { try { CssSelector selector = this.ParseSelector(); if (selector == null) { break; } ruleSet.Selectors.Add(selector); } catch (ParseException ex) { this.errors.Add(ex); while (this.Read(out ch)) { // restabalize on next rulset switch (ch) { case ',': { // continue parsing rest of Selectors goto ParseSelectors; } case '{': { goto ParseDeclarations; } //case ':':// keep going case ';': case '}': { throw new SyntaxError("Invalid selector list", this.reader.FilePath, this.reader.Line, this.reader.Column); } } } } } ParseDeclarations: while (true) { try { CssDeclaration declaration = this.ParseDeclaration(); if (declaration == null) { break; } ruleSet.Declarations.Add(declaration); } catch (ParseException ex) { this.errors.Add(ex); while (this.Read(out ch)) { // restabalize on next declaration switch (ch) { case '{': { throw new SyntaxError("Invalid ruleset", this.reader.FilePath, this.reader.Line, this.reader.Column); } //case ':':// keep going case ';': { // continue parsing rest of delcarations goto ParseDeclarations; } case '}': { // no more declarations return ruleSet; } } } } } return ruleSet; }
/* * /// <summary> * /// (BNF) block : '{' S* [ any | block | ATKEYWORD S* | ';' S* ]* '}' S*; * /// </summary> * /// <returns></returns> * private CssBlock ParseBlock() * { * CssBlock block = new CssBlock(); * int start = this.Position;// start with current char * * char ch; * while (this.Read(out ch)) * { * switch (ch) * { * case '@': * { * // copy anything before * string value = this.Copy(start); * if (value != null && !String.IsNullOrEmpty(value = value.Trim())) * { * CssString any = new CssString(); * any.Value = value; * block.Values.Add(any); * } * * // parse inner block * CssAtRule atRule = this.ParseAtRule(); * block.Values.Add(atRule); * * // reset start with current char * start = this.Position; * break; * } * case '{': * { * // copy anything before * string value = this.Copy(start); * if (value != null && !String.IsNullOrEmpty(value = value.Trim())) * { * CssString any = new CssString(); * any.Value = value; * block.Values.Add(any); * } * * // parse inner block * CssBlock innerBlock = this.ParseBlock(); * block.Values.Add(innerBlock); * * // reset start with current char * start = this.Position; * break; * } * case '}': * { * // copy anything before * string value = this.Copy(start); * if (value != null && !String.IsNullOrEmpty(value = value.Trim())) * { * CssString any = new CssString(); * any.Value = value; * block.Values.Add(any); * } * * return block; * } * } * } * * throw new UnexpectedEndOfFile("Unclosed block", this.reader.FilePath, this.reader.Line, this.reader.Column); * } */ #endregion Block #region RuleSet /// <summary> /// (BNF) ruleset : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*; /// </summary> /// <returns></returns> private CssRuleSet ParseRuleSet() { char ch; CssRuleSet ruleSet = new CssRuleSet(); ParseSelectors: while (true) { try { CssSelector selector = this.ParseSelector(); if (selector == null) { break; } ruleSet.Selectors.Add(selector); } catch (ParseException ex) { this.errors.Add(ex); while (this.Read(out ch)) { // restabalize on next rulset switch (ch) { case ',': { // continue parsing rest of Selectors goto ParseSelectors; } case '{': { goto ParseDeclarations; } //case ':':// keep going case ';': case '}': { throw new SyntaxError("Invalid selector list", this.reader.FilePath, this.reader.Line, this.reader.Column); } } } } } ParseDeclarations: while (true) { try { CssDeclaration declaration = this.ParseDeclaration(); if (declaration == null) { break; } ruleSet.Declarations.Add(declaration); } catch (ParseException ex) { this.errors.Add(ex); while (this.Read(out ch)) { // restabalize on next declaration switch (ch) { case '{': { throw new SyntaxError("Invalid ruleset", this.reader.FilePath, this.reader.Line, this.reader.Column); } //case ':':// keep going case ';': { // continue parsing rest of delcarations goto ParseDeclarations; } case '}': { // no more declarations return(ruleSet); } } } } } return(ruleSet); }