sass-lexer
Adapted from scss-parser.
const tokenize = ; // takes a stringconst tokens = ; // get the next tokentokensnext; // => array // look aheadtokens; // => array // check for end of filetokens; // => boolean // throw an error with line & columntokens;
The next
and peek
methods return null
if no more tokens exist.
Tokens are arrays of the following shape:
0: string
type1: string
value2: number
length3: number
start line4: number
start column
You can track the character offset yourself:
let offset = 0;while !tokens const tok = tokensnext; // // do your thing here... // // now, update the character offset. offset += tok2;
The values of tok[1].length
and tok[2]
are not always equal, so be sure
to use tok[2]
when calculating the character offset.
Token types
atrule
(does not include@
in the value)color_hex
(does not include#
in the value)comment
(does not include//
or/*
or*/
in the value)identifier
number
(does not include units likepx
in the value)operator
punctuation
space
(mix ofand
\t
and\n
in the value)string
(does not include"
or'
in the value)variable
(does not include$
in the value)
Parser utils
const utils = ; // Return true if `tok` is truthy and `tok[0]` equals the given string.utils; // Return true if `tok` is truthy and `tok[1]` equals the given string.utils; // Return true if `tok` is truthy and `tok[0]` is not the given string.utils; // Return true if `tok` is truthy and `tok[1]` is not the given string.utils; // Throw an error if `tok` is falsy or `tok[0]` is not the given string.utils; // Throw an error if `tok` is falsy or `tok[1]` is not the given string.utils;