pub enum Kind {
Show 24 variants
Eof = 0,
Whitespace = 1,
Comment = 2,
CdcOrCdo = 3,
Number = 4,
Dimension = 5,
BadString = 6,
BadUrl = 7,
Ident = 8,
Function = 9,
AtKeyword = 10,
Hash = 11,
String = 12,
Url = 13,
Delim = 16,
Colon = 17,
Semicolon = 18,
Comma = 19,
LeftSquare = 20,
RightSquare = 21,
LeftParen = 22,
RightParen = 23,
LeftCurly = 24,
RightCurly = 25,
}
Expand description
Kind represents the token “Type”, categorised mostly by the token types within the CSS Syntax spec.
Maintaining parity with the spec makes it easier to reason about logica round the parser, despite it being possible to group a bunch of these tokens into a single “delimiter” token.
Importantly, Kind
is represented as u8
and must only use the 5 low bits, because the upper 3 bits get used to
house details about each kind, that a token would be interested in learning about.
Variants§
Eof = 0
Represents the <eof-token> defined in CSS. While CSS stipulates that this token is never produced by a tokenizer, this Lexer will produce <eof-token>s if the underlying source has been fully consumed.
Whitespace = 1
Represents the <whitespace-token> defined in CSS.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ───────╭──┤│
├─ "\t" ──────┤
╰─ <newline> ─╯
<whitespace-token>
│├─╭─ <whitespace> ─╮─┤│
╰────────────────╯
While CSS stipulates that this token represents collapsed whitespace, it is possible for Lexer
to produce multiple consecutive Kind::Whitespace tokens if the
Feature::SeparateWhitespace runtime feature is enabled. In this case,
<whitespace-token>
becomes:
<whitespace-token>
│├──╮─╭─ " " ───────╮─╭──┤│
│ ╰─────────────╯ │
├─╭─ "\t" ──────╮─┤
│ ╰─────────────╯ │
╰─╭─ <newline> ─╮─╯
╰─────────────╯
Comment = 2
Represents the <comment> defined in CSS. While CSS stipulates comment tokens are not produced during tokenization, they are for this Lexer as they’re needed in order to preserve them.
<comment>
╭──────────────────────────────────────────╮
│├─ "/*" ─╯-╭─ (anything but "*" followed by "/") ─╮─╰─ "*/" ─┤│
╰──────────────────────────────────────╯
It is possible for Lexer to produce Kind::Whitespace tokens that begin //
if the
Feature::SingleLineComments runtime feature is enabled. In this mode,
<comment>
becomes:
<comment>
╭──────────────────────────────────────────╮
│├──╮─ "/*" ─╯-╭─ (anything but "*" followed by "/") ─╮─╰─ "*/" ─╭─┤│
│ ╰──────────────────────────────────────╯ │
│ ╭───────────────────────────╮ │
╰─ "//" ───────╯-╭─ (anything but "\n") ─╮─╰─ "\n" ──────────╯
╰───────────────────────╯
CdcOrCdo = 3
Represents both the <cdc-token> and <cdo-token>s defined in CSS. While CSS separates these tokens, they’re only useful representations at the top-level stylesheet, anywhere else they represent a parse error, and it’s a little pointless to define two tokens types for what amounts to a parse error.
<cdo-token>
│├─ "<!--" ─┤│
<cdc-token>
│├─ "-->" ─┤│
<cdc-or-cdo-token> (Not part of the CSS specification)
│├──╮─ <cdo-token> ─╭──┤│
╰─ <crc-token> ─╯
Number = 4
Represents the <number-token>.
<number-token>
╭─ "+" ─╮
│├─├───────┤───╭─ [digit] ─╮─ "." ─╭─ [digit] ─╮──╭───╮──────────────────────────────────╭──┤│
╰─ "-" ─╯ │ ╰───────────╯ ╰───────────╯ │ │ ╭─ "+" ─╮ │
├───────── ╭─ [digit] ─╮─────────────┤ ├─ "e" ─╭─├───────┤──╭─ [digit] ─╮─╯
│ ╰───────────╯ │ ╰─ "E" ─╯ ╰─ "-" ─╯ ╰───────────╯
╰──── "." ─╭─ [digit] ─╮─────────────╯
╰───────────╯
Dimension = 5
Represents the <dimension-token>.
Here we deviate from the spec slightly, which has both <dimension-token> and <percentage-token>.
<percentage-token>
represents a dimension with a %
symbol, but having this as a separate token results in more
work in the parser for little gain in the Lexer. So instead this lexer does not have a <percentage-token>
and
instead folds the grammar for it inside of <dimension-token>
.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ───────╭──┤│
├─ "\t" ──────┤
╰─ <newline> ─╯
<hexdigit>
│├─ [ 0-9, A-F, a-f ] ─┤│
<escape>
│├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
<ident-token>
╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
│├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
╰───────────────────────────────────────╯
<number-token>
╭─ "+" ─╮
│├─├───────┤─╮─╭─ [digit] ─╮─ "." ─╭─ [digit] ─╮──╭───╮──────────────────────────────────╭──┤│
╰─ "-" ─╯ │ ╰───────────╯ ╰───────────╯ │ │ ╭─ "+" ─╮ │
├───────── ╭─ [digit] ─╮─────────────┤ ├─ "e" ─╭─├───────┤──╭─ [digit] ─╮─╯
│ ╰───────────╯ │ ╰─ "E" ─╯ ╰─ "-" ─╯ ╰───────────╯
╰──── "." ─╭─ [digit] ─╮─────────────╯
╰───────────╯
<dimension-token>
│├─ <number-token> ─ <ident-token> ─┤│
<dimension-token> // Refined for this lexer, not true to the standard.
│├─ <number-token> ─╮─ <ident-token> ─╭──┤│
╰────── "%" ──────╯
BadString = 6
Represents the <bad-string-token>. This token is a failure to fully lex the <string-token>.
BadUrl = 7
Represents the <bad-url-token>. This token is a failure to fully lex the <url-token>.
Ident = 8
Represents the <ident-token>.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ─────╭──┤│
├─ "\t" ────┤
╰─ newline ─╯
<hexdigit>
│├─ [ 0-9, A-F, a-f ] ─┤│
<escape>
│├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
<ident-token>
╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
│├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
╰───────────────────────────────────────╯
Function = 9
Represents the <function-token>.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ───────╭──┤│
├─ "\t" ──────┤
╰─ <newline> ─╯
<hexdigit>
│├─ [ 0-9, A-F, a-f ] ─┤│
<escape>
│├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
<ident-token>
╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
│├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
╰───────────────────────────────────────╯
<function-token>
│├─ <ident-token> ─ "(" ─┤│
AtKeyword = 10
Represents the <at-keyword-token>.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ───────╭──┤│
├─ "\t" ──────┤
╰─ <newline> ─╯
<hexdigit>
│├─ [ 0-9, A-F, a-f ] ─┤│
<escape>
│├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
<ident-token>
╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
│├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
╰───────────────────────────────────────╯
<at-keyword-token>
│├─ "@" ─ <ident-token> ─┤│
Hash = 11
Represents the <hash-token>.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ───────╭──┤│
├─ "\t" ──────┤
╰─ <newline> ─╯
<hexdigit>
│├─ [ 0-9, A-F, a-f ] ─┤│
<escape>
│├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
<hash-token>
│├─ "#" ──╭─╮─ [a-z, A-Z, 0-9, "_", "-", non-ASCII] ─╭─╮─┤│
│ ╰─────────────── <escape> ───────────────╯ │
╰────────────────────────────────────────────╯
String = 12
Represents the <string-token>.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<escape>
│├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
<string-token>
╭───────────────────────────────────╮
│├─╮─ """ ─╯─╭─╮─ [not """, "\", newline] ─╭─╮─╰── """ ─╭─┤│
│ │ ├──────── <escape> ─────────┤ │ │
│ │ ╰───── "\" ─ <newline> ─────╯ │ │
│ ╰───────────────────────────────╯ │
│ ╭───────────────────────────────────╮ │
╰─ "'" ─╯─╭─╮─ [not """, "\", newline] ─╭─╮─╰── "'" ─╯
│ ├──────── <escape> ─────────┤ │
│ ╰───── "\" ─ <newline> ─────╯ │
╰───────────────────────────────╯
Url = 13
Represents the <url-token>.
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ───────╭──┤│
├─ "\t" ──────┤
╰─ <newline> ─╯
<whitespace-token>
│├─╭─ <whitespace> ─╮─┤│
╰────────────────╯
<ws*>
╭──────────────────────────╮
│├─╯─╭─ <whitespace-token> ─╮─╰─┤│
╰──────────────────────╯
<hexdigit>
│├─ [ 0-9, A-F, a-f ] ─┤│
<escape>
│├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
<ident-token>
╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
│├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
╰───────────────────────────────────────╯
<url-token>
╭───────────────────────────────────────────────────────────────────╮
│├─ <ident-token "url"> ─ "(" ─ <ws*> ─╯─╭─╮─ [not """ "'" "(" ")" "\" <whitespace> or non-printable] ─╭─╮─╰─ <ws*> ─ ")" ─┤│
│ ╰──────────────────────── <escape> ─────────────────────────╯ │
╰───────────────────────────────────────────────────────────────╯
Delim = 16
Represents the <delim-token>. The <delim-token>
has a value composed of a single code point.
<delim-token>
│├─ [codepoint] ─┤│
Colon = 17
Represents the <colon-token>.
<colon-token>
│├─ ":" ─┤│
Semicolon = 18
Represents the <semicolon-token>.
<semicolon-token>
│├─ ";" ─┤│
Comma = 19
Represents the <comma-token>.
<comma-token>
│├─ "," ─┤│
LeftSquare = 20
Represents the <[-token>.
<[-token>
│├─ "[" ─┤│
RightSquare = 21
Represents the <]-token>.
<]-token>
│├─ "]" ─┤│
LeftParen = 22
Represents the <(-token>.
<(-token>
│├─ "(" ─┤│
RightParen = 23
Represents the <)-token>.
<)-token>
│├─ ")" ─┤│
LeftCurly = 24
Represents the <{-token>.
<{-token>
│├─ "{" ─┤│
RightCurly = 25
Represents the <}-token>.
<}-token>
│├─ "}" ─┤│
Trait Implementations§
Source§impl Ord for Kind
impl Ord for Kind
Source§impl PartialEq<Kind> for SourceCursor<'_>
impl PartialEq<Kind> for SourceCursor<'_>
Source§impl PartialOrd for Kind
impl PartialOrd for Kind
impl Copy for Kind
impl Eq for Kind
impl StructuralPartialEq for Kind
Auto Trait Implementations§
impl Freeze for Kind
impl RefUnwindSafe for Kind
impl Send for Kind
impl Sync for Kind
impl Unpin for Kind
impl UnwindSafe for Kind
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
] or
a color-specific method, such as [OwoColorize::green
], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
] or
a color-specific method, such as [OwoColorize::on_yellow
], Read more