pub struct Feature { /* private fields */ }
Expand description
A set of runtime feature flags which can be enabled individually or in combination, which will change the way individual Lexer produces Tokens.
To build multiple features, use the bitwise OR operator.
§Example
use css_lexer::*;
let features = Feature::SingleLineComments | Feature::SeparateWhitespace;
let mut lexer = Lexer::new_with_features("// foo", features);
Implementations§
Source§impl Feature
impl Feature
Sourcepub const SingleLineComments: Feature
pub const SingleLineComments: Feature
With this flag enabled the Lexer will produce Tokens with
Kind::Comment when it encounters two consecutative SOLIDUS characters (//
), the
Token will have a length up to the next newline (\n
) character. The contents between the two
SOLIDUS (//
) characters and the \n
will be consumed by this token, so no tokens will be produced for the
contents of the comment.
If this flag is not enabled, encountering something that looks like a single line commet will produce two
Kind::Delim tokens for the two SOLIDUS (//
) characters, and any number of other tokens
depending on the contents of the comment, per the CSS specification.
A comment with two leading SOLIDUS characters is not valid CSS syntax, but might be considered valid syntax in other CSS-alike languages for example SASS.
With this feature turned off comments are tokenized per the CSS specification:
<comment>
╭──────────────────────────────────────────╮
│├─ "/*" ─╯-╭─ (anything but "*" followed by "/") ─╮─╰─ "*/" ─┤│
╰──────────────────────────────────────╯
With this feature turned on comments are tokenized with the additional grammar:
<comment>
╭──────────────────────────────────────────╮
│├──╮─ "/*" ─╯-╭─ (anything but "*" followed by "/") ─╮─╰─ "*/" ─╭─┤│
│ ╰──────────────────────────────────────╯ │
│ ╭───────────────────────────╮ │
├─ "//" ───────╯-╭─ (anything but "\n") ─╮─╰─ "\n" ──────────╯
╰─ ╰───────────────────────╯
§Example
use css_lexer::*;
let mut lexer = Lexer::new("// foo");
assert_eq!(lexer.advance(), Kind::Delim); // The first `/`
assert_eq!(lexer.advance(), Kind::Delim); // The second `/`
assert_eq!(lexer.advance(), Kind::Whitespace);
assert_eq!(lexer.advance(), Kind::Ident); // The "foo" in the comment
lexer = Lexer::new_with_features("// foo", Feature::SingleLineComments);
let token = lexer.advance();
assert_eq!(token, Kind::Comment); // The whole comment "// foo"
assert_eq!(token, CommentStyle::Single);
Sourcepub const SeparateWhitespace: Feature
pub const SeparateWhitespace: Feature
The CSS Spec mentions that whitespace tokens should be combined into a single Whitespace token. This means a single whitespace token can contain a cominbation of newlines, tabs, and space characters. This is often fine as whitespace is generally ignored during parsing, however for certain IDE features it might be important to tokenize discrete whitespace Tokens, each with their own discrete whitespace. Enabling this flag will enforce that the Lexer outputs these discrete tokens. In other words with this feature enabled, multiple contiguous whitespace tokens may be returned from subsequent calls to Lexer::advance(), but with this feature off this will never be the case (as whitespace is collapsed into a single Token).
With this feature turned off whitespace-tokens are tokenized per the CSS specification:
<newline>
│├──╮─ "\n" ───╭──┤│
├─ "\r\n" ─┤
├─ "\r" ───┤
╰─ "\f" ───╯
<whitespace>
│├──╮─ " " ───────╭──┤│
├─ "\t" ──────┤
╰─ <newline> ─╯
<whitespace-token>
│├─╭─ <whitespace> ─╮─┤│
╰────────────────╯
With this feature turned on whitespace-tokens are tokenized with the additional grammar:
<whitespace-token>
│├──╮─╭─ " " ───────╮─╭──┤│
│ ╰─────────────╯ │
├─╭─ "\t" ──────╮─┤
│ ╰─────────────╯ │
╰─╭─ <newline> ─╮─╯
╰─────────────╯
§Example
use css_lexer::*;
let mut lexer = Lexer::new("\n\thello world");
{
// This token will be collapsed Whitespace.
let token = lexer.advance();
assert_eq!(token, Kind::Whitespace);
// The Whitespace is comprised of many bits:
assert_eq!(token, Whitespace::Newline | Whitespace::Tab);
}
assert_eq!(lexer.advance(), Kind::Ident);
{
let token = lexer.advance();
assert_eq!(token, Kind::Whitespace);
assert_eq!(token, Whitespace::Space);
}
assert_eq!(lexer.advance(), Kind::Ident);
lexer = Lexer::new_with_features("\n\thello world", Feature::SeparateWhitespace);
{
// This token will be discrete Whitespace, just the `\n`.
let token = lexer.advance();
assert_eq!(token, Kind::Whitespace);
// The Whitespace is comprised of a single bit:
assert_eq!(token, Whitespace::Newline);
}
{
// This token will be discrete Whitespace, just the `\t`.
let token = lexer.advance();
assert_eq!(token, Kind::Whitespace);
// The Whitespace is comprised of a single bit:
assert_eq!(token, Whitespace::Tab);
}
assert_eq!(lexer.advance(), Kind::Ident);
{
let token = lexer.advance();
assert_eq!(token, Kind::Whitespace);
assert_eq!(token, Whitespace::Space);
}
assert_eq!(lexer.advance(), Kind::Ident);
Sourcepub const fn all_bits() -> Self
pub const fn all_bits() -> Self
Returns a bitmask that contains all values.
This will include bits that do not have any flags.
Use ::all_flags()
if you only want to use flags.
Sourcepub const fn is_all_bits(&self) -> bool
pub const fn is_all_bits(&self) -> bool
Returns true
if the bitmask contains all values.
This will check for bits == !0
,
use .is_all_flags()
if you only want to check for all flags
Sourcepub const fn is_all_flags(&self) -> bool
pub const fn is_all_flags(&self) -> bool
Returns true
if the bitmask contains all flags.
This will fail if any unused bit is set,
consider using .truncate()
first.
Sourcepub const fn all() -> Self
👎Deprecated: Please use the ::all_bits()
constructor
pub const fn all() -> Self
::all_bits()
constructorReturns a bitmask that contains all values.
This will include bits that do not have any flags.
Use ::all_flags()
if you only want to use flags.
Sourcepub const fn is_all(&self) -> bool
👎Deprecated: Please use the .is_all_bits()
method
pub const fn is_all(&self) -> bool
.is_all_bits()
methodReturns true
if the bitmask contains all values.
This will check for bits == !0
,
use .is_all_flags()
if you only want to check for all flags
Sourcepub const fn full() -> Self
👎Deprecated: Please use the ::all_flags()
constructor
pub const fn full() -> Self
::all_flags()
constructorReturns a bitmask that contains all flags.
Sourcepub const fn is_full(&self) -> bool
👎Deprecated: Please use the .is_all_flags()
method
pub const fn is_full(&self) -> bool
.is_all_flags()
methodReturns true
if the bitmask contains all flags.
This will fail if any unused bit is set,
consider using .truncate()
first.
Sourcepub const fn truncate(&self) -> Self
pub const fn truncate(&self) -> Self
Returns a bitmask that only has bits corresponding to flags
Sourcepub const fn intersects(&self, other: Self) -> bool
pub const fn intersects(&self, other: Self) -> bool
Returns true
if self
intersects with any value in other
,
or if other
does not contain any values.
This is equivalent to (self & other) != 0 || other == 0
.
Trait Implementations§
Source§impl BitAndAssign for Feature
impl BitAndAssign for Feature
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&=
operation. Read moreSource§impl BitOrAssign for Feature
impl BitOrAssign for Feature
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read moreSource§impl BitXorAssign for Feature
impl BitXorAssign for Feature
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^=
operation. Read moreSource§impl Ord for Feature
impl Ord for Feature
Source§impl PartialOrd for Feature
impl PartialOrd for Feature
impl Copy for Feature
impl Eq for Feature
impl StructuralPartialEq for Feature
Auto Trait Implementations§
impl Freeze for Feature
impl RefUnwindSafe for Feature
impl Send for Feature
impl Sync for Feature
impl Unpin for Feature
impl UnwindSafe for Feature
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