yari-css-parser
Fault-tolerant CSS parser producing a typed, fully-located AST. Parses complete stylesheets, at-rules (@media, @keyframes, @import…), rule sets, complex selectors (pseudo-classes, pseudo-elements, combinators, attribute selectors, nth-patterns), and property value expressions. Never throws on malformed input.
Installation
Add the dependency to your build file. Replace VERSION with the desired release.
// Gradle (Groovy DSL)
implementation 'com.easyparsingapi:yari-css-parser:VERSION'
// Maven
<dependency>
<groupId>com.easyparsingapi</groupId>
<artifactId>yari-css-parser</artifactId>
<version>VERSION</version>
</dependency>
CssParser — Entry Points
All methods are static. CssParser is not instantiable.
| parseUnit(String) | → AstResult<Css> | Parse a complete stylesheet; returns the AST plus the full token list |
| parseUnit(List<Token>) | → AstResult<Css> | Parse a pre-tokenised stylesheet |
| parseProperties(String) | → AstResult<Css> | Parse a bare declaration list (no selector), e.g. an inline style value |
| parseProperties(List<Token>) | → AstResult<Css> | Parse a pre-tokenised declaration list |
CssConfig — Parser Configuration
Holds the lexer/parser configuration and implements ApiParser.Config. The CssParser entry points create it automatically, so you do not normally construct or pass one yourself.
| new CssConfig() | Default configuration (used internally by CssParser) |
Css — Root AST Node
Root of a parsed CSS document. Implements AstUnit.
| getNodes() | → List<CssNode> | Top-level nodes (rule sets, at-rules, comments…) |
| astComments() | → List<AstComment> | All comments in the parsed document |
| astCommentsOf(AstNode, Position…) | → List<AstComment> | Comments associated with a given node at the specified position |
Rule Sets & At-Rules
Top-level nodes produced by the parser. Each node implements CssNode and carries an exact SourceLocation.
| RuleSet | A selector + declaration block. getSelector() → CssSelector, getBlock() → Block |
|
| Block | An ordered list of property declarations. getNodes() → List<CssNode> |
|
| Property | A CSS property declaration. getName() → Identifier, getValue() → CssNode |
|
| AtRule | A CSS at-rule. getName() → AtRuleName, getSignature() → CssNode, hasSignature() → boolean |
|
| AtRuleName | The name after @. getName() → Identifier |
Selectors
All selector nodes implement CssSelector. Complex selectors are composed from simpler ones by the parser.
| ListSelector | Comma-separated selector list | |
| QualifiedSelector | Compound selector (e.g. div.btn:hover) |
|
| CombinatorSelector | Two selectors joined by a combinator (descendant, child >, adjacent +, sibling ~). getType() → CombinatorSelector.Type |
|
| ElementSelector | Type selector (e.g. div). getName() → String |
|
| ClassSelector | Class selector (.btn). getClassName() → Identifier |
|
| IdSelector | ID selector (#header). getId() → Identifier |
|
| AttributeSelector | Attribute selector ([href^='https']) |
|
| PseudoClassSelector | Pseudo-class (:hover). getName() → Identifier |
|
| PseudoElementSelector | Pseudo-element (::before). getName() → Identifier |
|
| PseudoFunctionSelector | Functional pseudo (:nth-child(2n+1)). getSignature() → Signature |
|
| PrefixSelector | Vendor-prefix selector | |
| NamespaceSelector | Namespace selector (ns|element) |
|
| Universal | Universal selector * |
|
| Nesting | CSS nesting selector & |
Values & Expressions
Value nodes represent the right-hand side of CSS property declarations and the arguments of at-rule signatures and functions.
| Identifier | A CSS identifier. getValue() → String |
|
| Literal | A CSS literal value (number, string, color…). getValue() → String, getType() → Literal.Type |
|
| Function | A CSS function call (rgb(), calc()…). getName() → Identifier, getSignature() → Signature |
|
| Infix | An infix expression. getLeftOperand() → CssNode, getOperator() → Operator, getRightOperand() → CssNode |
|
| Prefix | A prefix expression | |
| ListValue | A space- or comma-separated value list. getSeparator() → Separator |
|
| ListParameter | A comma-separated parameter list in a function call | |
| Range | A media-query range expression (width >= 600px) |
|
| NthPattern | An nth-* pattern (2n+1) |
|
| Parenthesis | A parenthesised expression | |
| Important | The !important flag |
Error Recovery
The parser emits a CssError node and resumes parsing whenever it encounters
unrecognisable input. The rest of the stylesheet is still parsed to completion, so you always
get the maximum number of recognisable nodes from a broken or partially-malformed source.
Css css = CssParser.parseUnit("""
.btn { color: red; }
@media (width >= 600px) {
.btn { font-size: 1rem; }
}
""").unit();
css.astStream()
.filter(n -> n instanceof CssError)
.forEach(n -> System.out.println("error: " + n.getSourceLocation()));
For the full code-level reference, see the README on GitHub and Javadoc under yari-css-parser/src/main/java/.