private void ReadStream(Stream stream) { using (IStyleSheetLexer lexer = new StyleSheetLexer(stream)) { IRuleset currentRuleset = null; string currentPropertyName = string.Empty; while (!lexer.EndOfStream) { IStyleSheetLexerToken token = lexer.Peek(); switch (token.Type) { case StyleSheetLexerTokenType.DeclarationEnd: rulesets.Add(currentRuleset); break; case StyleSheetLexerTokenType.PropertyName: currentPropertyName = token.Value; break; case StyleSheetLexerTokenType.Value: case StyleSheetLexerTokenType.Function: { StyleObject[] propertyValues = ReadPropertyValues(lexer); IProperty property = null; try { property = Property.Create(currentPropertyName, propertyValues); } catch (Exception ex) { if (!options.IgnoreInvalidProperties) { throw ex; } } if (property != null) { currentRuleset.AddProperty(property); } } continue; case StyleSheetLexerTokenType.Tag: case StyleSheetLexerTokenType.Class: case StyleSheetLexerTokenType.Id: currentRuleset = new Ruleset(ReadSelector(lexer)); continue; } // Consume the current token. lexer.Read(out _); } } }
private StyleObject[] ReadPropertyValues(IStyleSheetLexer lexer) { List <StyleObject> values = new List <StyleObject>(); bool exitLoop = false; while (!exitLoop && !lexer.EndOfStream) { IStyleSheetLexerToken token = lexer.Peek(); switch (token.Type) { case StyleSheetLexerTokenType.Value: lexer.Read(out _); // Eat the token values.Add(new StyleObject(token.Value)); break; case StyleSheetLexerTokenType.Function: lexer.Read(out _); // Eat the token values.Add(ReadFunction(lexer, token.Value)); break; case StyleSheetLexerTokenType.PropertyEnd: lexer.Read(out _); // Eat the token exitLoop = true; break; case StyleSheetLexerTokenType.DeclarationEnd: // Don't eat the token, so that it can be seen by ReadStream. // Encountering this token means the style sheet is malformed, but we want to handle it gracefully. exitLoop = true; break; } } return(values.ToArray()); }
public static ISelector FromLexer(IStyleSheetLexer lexer) { SelectorBuilder builder = new SelectorBuilder(); bool exitLoop = false; while (!exitLoop && !lexer.EndOfStream) { IStyleSheetLexerToken token = lexer.Peek(); switch (token.Type) { case StyleSheetLexerTokenType.Class: builder.AddClass(token.Value); break; case StyleSheetLexerTokenType.PseudoClass: builder.AddPseudoClass(token.Value); break; case StyleSheetLexerTokenType.PseudoElement: builder.AddPseudoElement(token.Value); break; case StyleSheetLexerTokenType.Id: builder.AddId(token.Value); break; case StyleSheetLexerTokenType.Tag: builder.AddTag(token.Value); break; case StyleSheetLexerTokenType.DescendantCombinator: builder.AddDescendantCombinator(); break; case StyleSheetLexerTokenType.ChildCombinator: builder.AddChildCombinator(); break; case StyleSheetLexerTokenType.AdjacentSiblingCombinator: builder.AddAdjacentSiblingCombinator(); break; case StyleSheetLexerTokenType.GeneralSiblingCombinator: builder.AddGeneralSiblingCombinator(); break; case StyleSheetLexerTokenType.SelectorSeparator: builder.AddSelector(); break; default: exitLoop = true; break; } if (!exitLoop) { lexer.Read(out _); } } return(builder.Build()); }