Documentation ¶
Overview ¶
Package css implements a CSS3 compliant scanner and parser. This is meant to be a low-level library for extracting a CSS3 abstract syntax tree from raw CSS text.
This package can be used for building tools to validate, optimize and format CSS text.
Basics ¶
CSS parsing occurs in two steps. First the scanner breaks up a stream of code points (runes) into tokens. These tokens represent the most basic units of the CSS syntax tree such as identifiers, whitespace, and strings. The second step is to feed these tokens into the parser which creates the abstract syntax tree (AST) based on the context of the tokens.
Unlike many language parsers, the abstract syntax tree for CSS saves many of the original tokens in the stream so they can be reparsed at different levels. For example, parsing a @media query will save off the raw tokens found in the {-block so they can be reparsed as a full style sheet. This package doesn't understand the specifics of how to parse different types of at-rules (such as @media queries) so it defers that to the user to handle parsing.
Abstract Syntax Tree ¶
The CSS3 syntax defines a syntax tree of several types. At the top-level there is a StyleSheet. The style sheet is simply a collection of Rules. A Rule can be either an AtRule or a QualifiedRule.
An AtRule is defined as a rule starting with an "@" symbol and an identifier, then it's followed by zero or more component values and finally ends with either a {-block or a semicolon. The block is parsed simply as a collection of tokens and it is up to the user to define the exact grammar.
A QualifiedRule is defined as a rule starting with one or more component values and ending with a {-block.
Inside the {-blocks are a list of declarations. Despite the name, a list of declarations can be either an AtRule or a Declaration. A Declaration is an identifier followed by a colon followed by one or more component values. The declaration can also have it's Important flag set if the last two non-whitespace tokens are a case-insensitive "!important".
ComponentValues are the basic unit inside rules and declarations. A ComponentValue can be either a SimpleBlock, a Function, or a Token. A simple block starts with either a {, [, or (, has zero or more component values, and then ends with the mirror of the starting token (}, ], or )). A Function is an identifier immediately followed by a left parenthesis, then zero or more component values, and then ending with a right parenthesis.
Index ¶
- type AtRule
- type ComponentValue
- type ComponentValueScanner
- type ComponentValues
- type Declaration
- type Declarations
- type Error
- type ErrorList
- type Function
- type Node
- type Parser
- func (p *Parser) ConsumeAtRule(s ComponentValueScanner) *AtRule
- func (p *Parser) ConsumeComponentValue(s ComponentValueScanner) ComponentValue
- func (p *Parser) ConsumeDeclaration(s ComponentValueScanner) *Declaration
- func (p *Parser) ConsumeDeclarations(s ComponentValueScanner) Declarations
- func (p *Parser) ConsumeFunction(s ComponentValueScanner) *Function
- func (p *Parser) ConsumeQualifiedRule(s ComponentValueScanner) *QualifiedRule
- func (p *Parser) ConsumeRules(s ComponentValueScanner, topLevel bool) Rules
- func (p *Parser) ConsumeSimpleBlock(s ComponentValueScanner) *SimpleBlock
- func (p *Parser) ParseComponentValue(s *Scanner) ComponentValue
- func (p *Parser) ParseComponentValues(s *Scanner) ComponentValues
- func (p *Parser) ParseDeclaration(s *Scanner) *Declaration
- func (p *Parser) ParseDeclarations(s *Scanner) Declarations
- func (p *Parser) ParseRule(s *Scanner) Rule
- func (p *Parser) ParseRules(s *Scanner) Rules
- func (p *Parser) ParseStyleSheet(s *Scanner) *StyleSheet
- type Pos
- type Printer
- type QualifiedRule
- type Rule
- type Rules
- type Scanner
- type SimpleBlock
- type StyleSheet
- type Tok
- type Token
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AtRule ¶
type AtRule struct { Name string Prelude ComponentValues Block *SimpleBlock Pos Pos }
AtRule represents a rule starting with an "@" symbol.
type ComponentValue ¶
type ComponentValue interface { Node // contains filtered or unexported methods }
ComponentValue represents a component value.
type ComponentValueScanner ¶
type ComponentValueScanner interface { Current() ComponentValue Scan() ComponentValue Unscan() }
ComponentValueScanner represents a type that can retrieve the next component value.
func NewComponentValueScanner ¶
func NewComponentValueScanner(values ComponentValues) ComponentValueScanner
NewComponentValueScanner returns a scanner for a fixed list of component values. This can be used with nodes which have blocks such as at-rules. For example, a @media query can have a full ruleset inside its block. This block can be further parsed using the consume functions on the Parser.
type ComponentValues ¶
type ComponentValues []ComponentValue
ComponentValues represents a list of component values.
type Declaration ¶
type Declaration struct { Name string Values ComponentValues Important bool Pos Pos }
Declaration represents a name/value pair.
type Declarations ¶
type Declarations []Node
Declarations represents a list of declarations or at-rules.
type Function ¶
type Function struct { Name string Values ComponentValues Pos Pos }
Function represents a function call with a list of arguments.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node represents a node in the CSS3 abstract syntax tree.
type Parser ¶
type Parser struct {
Errors ErrorList
}
Parser represents a CSS3 parser.
func (*Parser) ConsumeAtRule ¶
func (p *Parser) ConsumeAtRule(s ComponentValueScanner) *AtRule
ConsumeAtRule consumes a single at-rule.
func (*Parser) ConsumeComponentValue ¶
func (p *Parser) ConsumeComponentValue(s ComponentValueScanner) ComponentValue
ConsumeComponentValue consumes a single component value. (§5.4.6)
func (*Parser) ConsumeDeclaration ¶
func (p *Parser) ConsumeDeclaration(s ComponentValueScanner) *Declaration
ConsumeDeclaration consumes a single declaration.
func (*Parser) ConsumeDeclarations ¶
func (p *Parser) ConsumeDeclarations(s ComponentValueScanner) Declarations
ConsumeDeclarations consumes a list of declarations.
func (*Parser) ConsumeFunction ¶
func (p *Parser) ConsumeFunction(s ComponentValueScanner) *Function
ConsumeFunction consumes a function.
func (*Parser) ConsumeQualifiedRule ¶
func (p *Parser) ConsumeQualifiedRule(s ComponentValueScanner) *QualifiedRule
ConsumeQualifiedRule consumes a single qualified rule.
func (*Parser) ConsumeRules ¶
func (p *Parser) ConsumeRules(s ComponentValueScanner, topLevel bool) Rules
ConsumeRules consumes a list of rules from a token stream.
func (*Parser) ConsumeSimpleBlock ¶
func (p *Parser) ConsumeSimpleBlock(s ComponentValueScanner) *SimpleBlock
ConsumeSimpleBlock consumes a simple block. (§5.4.7)
func (*Parser) ParseComponentValue ¶
func (p *Parser) ParseComponentValue(s *Scanner) ComponentValue
ParseComponentValue parses a component value.
func (*Parser) ParseComponentValues ¶
func (p *Parser) ParseComponentValues(s *Scanner) ComponentValues
ParseComponentValues parses a list of component values.
func (*Parser) ParseDeclaration ¶
func (p *Parser) ParseDeclaration(s *Scanner) *Declaration
ParseDeclaration parses a name/value declaration.
func (*Parser) ParseDeclarations ¶
func (p *Parser) ParseDeclarations(s *Scanner) Declarations
ParseDeclarations parses a list of declarations and at-rules.
func (*Parser) ParseRules ¶
ParseRule parses a list of rules.
func (*Parser) ParseStyleSheet ¶
func (p *Parser) ParseStyleSheet(s *Scanner) *StyleSheet
ParseStyleSheet parses an input stream into a stylesheet.
type Pos ¶
Pos specifies the line and character position of a token. The Char and Line are both zero-based indexes.
type QualifiedRule ¶
type QualifiedRule struct { Prelude ComponentValues Block *SimpleBlock Pos Pos }
QualifiedRule represents an unnamed rule that includes a prelude and block.
type Rule ¶
type Rule interface { Node // contains filtered or unexported methods }
Rule represents a qualified rule or at-rule.
type Scanner ¶
type Scanner struct { // Errors contains a list of all errors that occur during scanning. Errors []*Error // contains filtered or unexported fields }
Scanner implements a CSS3 standard compliant tokenizer.
This implementation only allows UTF-8 encoding. @charset directives will be ignored.
type SimpleBlock ¶
type SimpleBlock struct { Token *Token Values ComponentValues Pos Pos }
SimpleBlock represents a {-block, [-block, or (-block.
type StyleSheet ¶
type StyleSheet struct {
Rules Rules
}
StyleSheet represents a top-level CSS3 stylesheet.
type Tok ¶
type Tok int
Tok represents a lexical token type.
const ( IdentToken Tok = iota + 1 FunctionToken AtKeywordToken HashToken StringToken BadStringToken URLToken BadURLToken DelimToken NumberToken PercentageToken DimensionToken UnicodeRangeToken IncludeMatchToken DashMatchToken PrefixMatchToken SuffixMatchToken SubstringMatchToken ColumnToken WhitespaceToken CDOToken CDCToken ColonToken SemicolonToken CommaToken LBrackToken RBrackToken LParenToken RParenToken LBraceToken RBraceToken EOFToken )
type Token ¶
type Token struct { // The type of token. Tok Tok // A flag set for ident-like tokens to either "id" or "unrestricted". // Also set for numeric tokens to either "integer" or "number" Type string // The literal value of the token as parsed. Value string // The rune used to close the token. Used for string tokens. Ending rune // The numeric value and unit used for numeric tokens. Number float64 Unit string // Beginning and ending range for a unicode-range token. Start int End int // Position of the token in the source document. Pos Pos }
Token represents a lexical token.