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()); }
private StyleObject ReadFunction(IStyleSheetLexer lexer, string functionName) { List <StyleObject> functionArgs = new List <StyleObject>(); bool exitLoop = false; while (!exitLoop && lexer.Read(out IStyleSheetLexerToken token)) { switch (token.Type) { case StyleSheetLexerTokenType.Value: functionArgs.Add(new StyleObject(token.Value)); break; case StyleSheetLexerTokenType.Function: functionArgs.Add(ReadFunction(lexer, token.Value)); break; case StyleSheetLexerTokenType.FunctionArgumentsStart: break; case StyleSheetLexerTokenType.FunctionArgumentSeparator: break; case StyleSheetLexerTokenType.FunctionArgumentsEnd: exitLoop = true; break; default: throw new UnexpectedTokenException($"Unexpected token: {token.Type}."); } } StyleObject returnValue = PropertyUtilities.EvaluateFunction(functionName, functionArgs.ToArray(), options.FileReader); if (returnValue.Type == StyleObjectType.Image) { disposableResources.Add(returnValue.GetImage()); } return(returnValue); }
private ISelector ReadSelector(IStyleSheetLexer lexer) { return(Selector.FromLexer(lexer)); }
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()); }