scanner

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2016 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package scanner generates tokens for a CSS2/3 input.

It is a CSS2 scanner with bits of a CSS3 scanner in it.

To use it, create a new scanner for a given CSS string and call Next() until the token returned has type scanner.EOF or scanner.Error:

s := scanner.New(myCSS)
for {
	token := s.Next()
	if token.Type == scanner.TokenEOF || token.Type == scanner.TokenError {
		break
	}
	// Do something with the token...
}

Following the CSS3 specification, an error can only occur when the scanner finds an unclosed quote or unclosed comment. In these cases the text becomes "untokenizable". Everything else is tokenizable and it is up to a parser to make sense of the token stream (or ignore nonsensical token sequences).

Note: the scanner doesn't perform lexical analysis or, in other words, it doesn't care about the token context. It is intended to be used by a lexer or parser.

Index

Constants

This section is empty.

Variables

View Source
var AtKeyword = Type{3}

AtKeyword token type is for things like @import. The .Value has the @ removed.

View Source
var BOM = Type{22}

BOM token type refers to Byte Order Marks.

View Source
var CDC = Type{12}

CDC token type represents the --> string.

View Source
var CDO = Type{11}

CDO token type represents the <!-- string.

View Source
var Comment = Type{14}

Comment token type is for comments. The internals of the comment will be in the .Value, with no additional processing.

View Source
var DashMatch = Type{17}

DashMatch token type refers to |=.

View Source
var Delim = Type{21}

Delim token type refers to a character which CSS does not otherwise know how to process as any of the above.

View Source
var Dimension = Type{8}

Dimension token type is for dimensions. No further parsing is done on the dimension, which may be bad since we could break in into number and unit.

View Source
var EOF = Type{1}

EOF token type is the end of the string.

View Source
var Error = Type{0}

Error token type is returned when there are errors in the parse.

CSS tries to avoid these; these are mostly unclosed strings and other things with delimiters.

View Source
var Function = Type{15}

Function token type refers to a function invocation, like "rgb(". The .Value does not have the parenthesis on it.

View Source
var Hash = Type{5}

Hash token type is for things like colors: #fff. The value does not contain the #.

View Source
var Ident = Type{2}

Ident token type for identifiers.

View Source
var Includes = Type{16}

Includes token type refers to ~=.

View Source
var Number = Type{6}

Number token type is for numbers that are not percentages or dimensions.

View Source
var Percentage = Type{7}

Percentage token type is for percentages. The .Value does not include the %.

View Source
var PrefixMatch = Type{18}

PrefixMatch token type refers to ^=.

View Source
var S = Type{13}

S token type is for whitespace. The original space content will be in .Value.

View Source
var String = Type{4}

String token type is for double- or single-quote delimited strings. The strings have been processed to their values and do not contain the quotes.

View Source
var SubstringMatch = Type{20}

SubstringMatch token type refers to *=.

View Source
var SuffixMatch = Type{19}

SuffixMatch token type refers to $=.

View Source
var URI = Type{9}

URI token type is for URIs. The .Value will be the processed URI.

View Source
var UnicodeRange = Type{10}

UnicodeRange token type is for Unicode ranges.

Functions

func Fuzz

func Fuzz(data []byte) int

Types

type Scanner

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

Scanner scans an input and emits tokens following the CSS3 specification.

func New

func New(input string) *Scanner

New returns a new CSS scanner for the given input.

func (*Scanner) Next

func (s *Scanner) Next() *Token

Next returns the next token from the input.

At the end of the input the token type is EOF.

If the input can't be tokenized the token type is Error. This occurs in case of unclosed quotation marks or comments.

type Token

type Token struct {
	Type   Type
	Value  string
	Line   int
	Column int
}

Token represents a token and the corresponding string.

func (*Token) Emit

func (t *Token) Emit(w io.Writer) (err error)

Emit will write a string representation of the given token to the target io.Writer. An error will be returned if you either try to emit Error or EOF, or if the Writer returns an error.

Emit will make many small writes to the io.Writer.

Emit assumes you have not set the token's .Value to an invalid value for many of these; for instance, if you manually take a Number token and set its .Value to "sometext", you will emit something that is not a number.

func (*Token) String

func (t *Token) String() string

String returns a string representation of the token.

type Type

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

Type is an integer that identifies the type of the token. Only the types defined as variables in the package may be used.

func (Type) GoString

func (t Type) GoString() string

GoString returns a string representation of the token type.

func (Type) String

func (t Type) String() string

String returns a string representation of the token type.

Jump to

Keyboard shortcuts

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