parser

package
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2024 License: BSD-3-Clause Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ColorKeywords maps color names to RGBA values
	ColorKeywords = map[string]Color{}
)

Functions

func IsLiteral added in v0.0.10

func IsLiteral(token Token, char string) bool

IsLiteral returns true if token is a literal [char].

func ParseNth

func ParseNth(input []Token) *[2]int

Parse <An+B> (see <http://drafts.csswg.org/csswg/css-syntax-3/#anb>), as found in `:nth-child()` and related selector pseudo-classes.

Returns [a, b] or nil

func Serialize

func Serialize(l []Token) string

func SplitOnComma added in v0.0.10

func SplitOnComma(tokens []Token) [][]Token

Split a list of tokens on commas, ie on [LiteralToken].

Types

type AtKeyword added in v0.0.10

type AtKeyword struct {
	// contains filtered or unexported fields
}

func (AtKeyword) Kind added in v0.0.10

func (AtKeyword) Kind() Kind

func (AtKeyword) Pos added in v0.0.10

func (v AtKeyword) Pos() Pos

type AtRule

type AtRule struct {
	AtKeyword string
	QualifiedRule
}

func (AtRule) Pos added in v0.0.10

func (t AtRule) Pos() Pos

type Color

type Color struct {
	Type ColorType
	RGBA RGBA
}

func ParseColor

func ParseColor(token Token) Color

Parse a color value as defined in `CSS Color Level 3 <http://www.w3.org/TR/css3-color/>`. Returns :

  • zero Color if the input is not a valid color value. (No error is returned.)
  • CurrentColor for the *currentColor* keyword
  • RGBA color for every other values (including keywords, HSL && HSLA.) The alpha channel is clipped to [0, 1] but red, green, or blue can be out of range (eg. “rgb(-10%, 120%, 0%)“ is represented as “(-0.1, 1.2, 0, 1)“.

func ParseColorString

func ParseColorString(color string) Color

ParseColorString tokenize the input before calling `ParseColor`.

func (Color) IsNone

func (c Color) IsNone() bool

type ColorType

type ColorType uint8
const (
	// ColorInvalid is an empty or invalid color specification.
	ColorInvalid ColorType = iota
	// ColorCurrentColor represents the special value "currentColor"
	// which need document context to be resolved.
	ColorCurrentColor
	// ColorRGBA is a standard rgba color.
	ColorRGBA
)

type Comment

type Comment struct {
	// contains filtered or unexported fields
}

func (Comment) Kind added in v0.0.10

func (Comment) Kind() Kind

func (Comment) Pos added in v0.0.10

func (v Comment) Pos() Pos

type Compound added in v0.0.10

type Compound interface {
	Pos() Pos
	// contains filtered or unexported methods
}

Compound is a compound CSS chunk, like a declaration or a qualified rule.

func ParseBlocksContents added in v0.0.10

func ParseBlocksContents(input []Token, skipWhitespace bool) []Compound

ParseBlocksContents parses a block’s contents.

This is used e.g. for the QualifiedRule content of a style rule or '@page' rule, or for the 'style' attribute of an HTML element.

In contexts that don’t expect any at-rule and/or qualified rule, all AtRule and QualifiedRule objects should simply be rejected as invalid.

func ParseBlocksContentsString added in v0.0.10

func ParseBlocksContentsString(css string) []Compound

ParseBlocksContentsString tokenizes `css` and calls `ParseBlocksContents`.

func ParseDeclarationList

func ParseDeclarationList(input []Token, skipComments, skipWhitespace bool) []Compound

Parse a `declaration list` (which may also contain at-rules). This is used e.g. for the `QualifiedRule.content` of a style rule or “@page“ rule, or for the “style“ attribute of an HTML element.

In contexts that don’t expect any at-rule, all `AtRule` objects should simply be rejected as invalid.

If `skipComments`, ignore CSS comments at the top-level of the list. If `skipWhitespace`, ignore whitespace at the top-level of the list. Whitespace is still preserved in the `Declaration.value` of declarations and the `AtRule.prelude` and `AtRule.content` of at-rules.

func ParseDeclarationListString

func ParseDeclarationListString(css string, skipComments, skipWhitespace bool) []Compound

ParseDeclarationListString tokenizes `css` and calls `ParseDeclarationList`.

func ParseOneDeclaration

func ParseOneDeclaration(input []Token) Compound

Parse a single `declaration`, returning a ParseError or a Declaration

This is used e.g. for a declaration in an `@supports <http://drafts.csswg.org/csswg/css-conditional/#at-supports>. Any whitespace or comment before the “:“ colon is dropped.

func ParseRuleList

func ParseRuleList(input []Token, skipComments, skipWhitespace bool) []Compound

Parse a non-top-level `rule list`.

This is used for parsing the `AtRule.content` of nested rules like “@media“. This differs from `ParseStylesheet` in that top-level “<!--“ and “-->“ tokens are not ignored.

If [skipComments] is true, ignores CSS comments at the top-level of the list.

If [skipWhitespace] is true, ignores whitespace at the top-level of the list. Whitespace are still preserved in the `QualifiedRule.Prelude` and the `QualifiedRule.Content` of rules.

func ParseStylesheet

func ParseStylesheet(input []Token, skipComments, skipWhitespace bool) []Compound

Parse a stylesheet from tokens.

This is used e.g. for a “<style>“ HTML element. This differs from `ParseRuleList` in that top-level “<!--“ && “-->“ tokens are ignored. This is a legacy quirk for the “<style>“ HTML element.

func ParseStylesheetBytes

func ParseStylesheetBytes(input []byte, skipComments, skipWhitespace bool) []Compound

ParseStylesheetBytes tokenizes `input` and calls `ParseStylesheet`.

type CurlyBracketsBlock

type CurlyBracketsBlock listVal

func (CurlyBracketsBlock) Kind added in v0.0.10

func (CurlyBracketsBlock) Kind() Kind

func (CurlyBracketsBlock) Pos added in v0.0.10

func (v CurlyBracketsBlock) Pos() Pos

type Declaration

type Declaration struct {
	Name  string
	Value []Token

	Important bool
	// contains filtered or unexported fields
}

func (Declaration) Pos added in v0.0.10

func (t Declaration) Pos() Pos

type Dimension added in v0.0.10

type Dimension struct {
	Unit string
	// contains filtered or unexported fields
}

func NewDimension added in v0.0.10

func NewDimension(nb Number, dim string) Dimension

func (Dimension) Int added in v0.0.10

func (tk Dimension) Int() int

Int assumes a numeric token

func (Dimension) IsInt added in v0.0.10

func (tk Dimension) IsInt() bool

IsInt returns true for numerical token with integer value.

func (Dimension) Kind added in v0.0.10

func (Dimension) Kind() Kind

func (Dimension) Pos added in v0.0.10

func (v Dimension) Pos() Pos

type FunctionBlock

type FunctionBlock struct {
	Name string
	// contains filtered or unexported fields
}

func NewFunctionBlock added in v0.0.10

func NewFunctionBlock(pos Pos, name string, arguments []Token) FunctionBlock

func (FunctionBlock) Kind added in v0.0.10

func (FunctionBlock) Kind() Kind

func (FunctionBlock) Pos added in v0.0.10

func (v FunctionBlock) Pos() Pos

type Hash added in v0.0.10

type Hash struct {
	// contains filtered or unexported fields
}

func (Hash) Kind added in v0.0.10

func (Hash) Kind() Kind

func (Hash) Pos added in v0.0.10

func (v Hash) Pos() Pos

type Ident added in v0.0.10

type Ident struct {
	// contains filtered or unexported fields
}

func NewIdent added in v0.0.10

func NewIdent(v string, pos Pos) Ident

func (Ident) Kind added in v0.0.10

func (Ident) Kind() Kind

func (Ident) Pos added in v0.0.10

func (v Ident) Pos() Pos

type Kind added in v0.0.10

type Kind uint8
const (
	KLitteral Kind = iota
	KParseError
	KComment
	KWhitespace
	KIdent
	KAtKeyword
	KHash
	KString
	KURL
	KUnicodeRange
	KNumber
	KPercentage
	KDimension
	KParenthesesBlock
	KSquareBracketsBlock
	KCurlyBracketsBlock
	KFunctionBlock
)

func (Kind) String added in v0.0.10

func (k Kind) String() string

type Literal added in v0.0.10

type Literal struct {
	// contains filtered or unexported fields
}

Either a delimiter or raw value

func NewLiteral added in v0.0.10

func NewLiteral(v string, pos Pos) Literal

func (Literal) Kind added in v0.0.10

func (Literal) Kind() Kind

func (Literal) Pos added in v0.0.10

func (v Literal) Pos() Pos

type Number added in v0.0.10

type Number struct {
	// contains filtered or unexported fields
}

func NewNumber added in v0.0.10

func NewNumber(v utils.Fl, pos Pos) Number

func (Number) Int added in v0.0.10

func (tk Number) Int() int

Int assumes a numeric token

func (Number) IsInt added in v0.0.10

func (tk Number) IsInt() bool

IsInt returns true for numerical token with integer value.

func (Number) Kind added in v0.0.10

func (Number) Kind() Kind

func (Number) Pos added in v0.0.10

func (v Number) Pos() Pos

type ParenthesesBlock

type ParenthesesBlock listVal

compound values

func (ParenthesesBlock) Kind added in v0.0.10

func (ParenthesesBlock) Kind() Kind

func (ParenthesesBlock) Pos added in v0.0.10

func (v ParenthesesBlock) Pos() Pos

type ParseError

type ParseError struct {
	Message string
	// contains filtered or unexported fields
}

special token used for parse errors

func (ParseError) Kind

func (ParseError) Kind() Kind

func (ParseError) Pos added in v0.0.10

func (v ParseError) Pos() Pos

type Percentage added in v0.0.10

type Percentage struct {
	// contains filtered or unexported fields
}

func (Percentage) Int added in v0.0.10

func (tk Percentage) Int() int

Int assumes a numeric token

func (Percentage) IsInt added in v0.0.10

func (tk Percentage) IsInt() bool

IsInt returns true for numerical token with integer value.

func (Percentage) Kind added in v0.0.10

func (Percentage) Kind() Kind

func (Percentage) Pos added in v0.0.10

func (v Percentage) Pos() Pos

type Pos added in v0.0.2

type Pos struct {
	Line, Column int
}

Pos is the position of a token in the input CSS file

type QualifiedRule

type QualifiedRule struct {
	Prelude, Content []Token
	// contains filtered or unexported fields
}

func (QualifiedRule) Pos added in v0.0.10

func (t QualifiedRule) Pos() Pos

type RGBA

type RGBA struct {
	R, G, B, A utils.Fl
}

values in [-1, 1]

func (RGBA) IsNone

func (color RGBA) IsNone() bool

func (RGBA) RGBA

func (c RGBA) RGBA() (r, g, b, a uint32)

RGBA returns the alpha-premultiplied red, green, blue and alpha values for the color. Each value ranges within [0, 0xffff], but is represented by a uint32 so that multiplying by a blend factor up to 0xffff will not overflow.

An alpha-premultiplied color component c has been scaled by alpha (a), so has valid values 0 <= c <= a.

func (RGBA) Unpack

func (color RGBA) Unpack() (r, g, b, a utils.Fl)

type SquareBracketsBlock

type SquareBracketsBlock listVal

func (SquareBracketsBlock) Kind added in v0.0.10

func (SquareBracketsBlock) Kind() Kind

func (SquareBracketsBlock) Pos added in v0.0.10

func (v SquareBracketsBlock) Pos() Pos

type String added in v0.0.10

type String struct {
	// contains filtered or unexported fields
}

func (String) Kind added in v0.0.10

func (String) Kind() Kind

func (String) Pos added in v0.0.10

func (v String) Pos() Pos

type Token

type Token interface {
	Pos() Pos
	Kind() Kind
	// contains filtered or unexported methods
}

Token is a CSS component value, used to build declarations

func ParseFunction added in v0.0.10

func ParseFunction(functionToken_ Token) (string, []Token)

ParseFunction parses functional notation.

Return “(name, args)“ if the given token is a function with comma or space-separated arguments. Return zero values otherwise.

func ParseOneComponentValue

func ParseOneComponentValue(input []Token) Token

Parse a single `component value`. This is used e.g. for an attribute value referred to by “attr(foo length)“.

func RemoveWhitespace added in v0.0.10

func RemoveWhitespace(tokens []Token) []Token

Remove any Whitespace and Comment tokens from the list.

func Tokenize

func Tokenize(css []byte, skipComments bool) []Token

Tokenize parses a list of component values.

If `skipComments` is true, it ignores CSS comments: the return values (and recursively its blocks and functions) will not contain any `Comment` object.

type TokensIter added in v0.0.10

type TokensIter struct {
	// contains filtered or unexported fields
}

func NewIter added in v0.0.10

func NewIter(tokens []Token) *TokensIter

func (TokensIter) HasNext added in v0.0.10

func (it TokensIter) HasNext() bool

func (*TokensIter) Next added in v0.0.10

func (it *TokensIter) Next() (t Token)

Next returns the Next token or nil at the end

func (*TokensIter) NextSignificant added in v0.0.10

func (it *TokensIter) NextSignificant() Token

NextSignificant returns the next significant (neither whitespace or comment) token, or nil

type URL added in v0.0.10

type URL struct {
	// contains filtered or unexported fields
}

func (URL) Kind added in v0.0.10

func (URL) Kind() Kind

func (URL) Pos added in v0.0.10

func (v URL) Pos() Pos

type UnicodeRange added in v0.0.10

type UnicodeRange struct {
	Start, End uint32
	// contains filtered or unexported fields
}

func (UnicodeRange) Kind added in v0.0.10

func (UnicodeRange) Kind() Kind

func (UnicodeRange) Pos added in v0.0.10

func (v UnicodeRange) Pos() Pos

type Whitespace added in v0.0.10

type Whitespace struct {
	// contains filtered or unexported fields
}

func NewWhitespace added in v0.0.10

func NewWhitespace(v string, pos Pos) Whitespace

func (Whitespace) Kind added in v0.0.10

func (Whitespace) Kind() Kind

func (Whitespace) Pos added in v0.0.10

func (v Whitespace) Pos() Pos

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL