Skip to content

Commit

Permalink
feat(js_parser): support metavariables (#3548)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Jul 31, 2024
1 parent 52175f2 commit c9ecdac
Show file tree
Hide file tree
Showing 93 changed files with 1,656 additions and 523 deletions.
12 changes: 6 additions & 6 deletions crates/biome_css_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions crates/biome_css_factory/src/generated/syntax_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl FormatRule<AnyCssDeclarationOrRule> for FormatAnyCssDeclarationOrRule {
AnyCssDeclarationOrRule::AnyCssRule(node) => node.format().fmt(f),
AnyCssDeclarationOrRule::CssBogus(node) => node.format().fmt(f),
AnyCssDeclarationOrRule::CssDeclarationWithSemicolon(node) => node.format().fmt(f),
AnyCssDeclarationOrRule::CssGritMetavariable(node) => node.format().fmt(f),
AnyCssDeclarationOrRule::CssMetavariable(node) => node.format().fmt(f),
}
}
}
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/css/any/media_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ impl FormatRule<AnyCssMediaQuery> for FormatAnyCssMediaQuery {
match node {
AnyCssMediaQuery::AnyCssMediaTypeQuery(node) => node.format().fmt(f),
AnyCssMediaQuery::CssBogusMediaQuery(node) => node.format().fmt(f),
AnyCssMediaQuery::CssGritMetavariable(node) => node.format().fmt(f),
AnyCssMediaQuery::CssMediaConditionQuery(node) => node.format().fmt(f),
AnyCssMediaQuery::CssMetavariable(node) => node.format().fmt(f),
}
}
}
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/css/any/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl FormatRule<AnyCssSelector> for FormatAnyCssSelector {
AnyCssSelector::CssBogusSelector(node) => node.format().fmt(f),
AnyCssSelector::CssComplexSelector(node) => node.format().fmt(f),
AnyCssSelector::CssCompoundSelector(node) => node.format().fmt(f),
AnyCssSelector::CssGritMetavariable(node) => node.format().fmt(f),
AnyCssSelector::CssMetavariable(node) => node.format().fmt(f),
}
}
}
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/css/any/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl FormatRule<AnyCssValue> for FormatAnyCssValue {
AnyCssValue::CssColor(node) => node.format().fmt(f),
AnyCssValue::CssCustomIdentifier(node) => node.format().fmt(f),
AnyCssValue::CssDashedIdentifier(node) => node.format().fmt(f),
AnyCssValue::CssGritMetavariable(node) => node.format().fmt(f),
AnyCssValue::CssIdentifier(node) => node.format().fmt(f),
AnyCssValue::CssMetavariable(node) => node.format().fmt(f),
AnyCssValue::CssNumber(node) => node.format().fmt(f),
AnyCssValue::CssRatio(node) => node.format().fmt(f),
AnyCssValue::CssString(node) => node.format().fmt(f),
Expand Down
12 changes: 0 additions & 12 deletions crates/biome_css_formatter/src/css/auxiliary/grit_metavariable.rs

This file was deleted.

10 changes: 10 additions & 0 deletions crates/biome_css_formatter/src/css/auxiliary/metavariable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::prelude::*;
use biome_css_syntax::CssMetavariable;
use biome_rowan::AstNode;
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssMetavariable;
impl FormatNodeRule<CssMetavariable> for FormatCssMetavariable {
fn fmt_fields(&self, node: &CssMetavariable, f: &mut CssFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
}
}
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/css/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub(crate) mod font_feature_values_block;
pub(crate) mod font_feature_values_item;
pub(crate) mod function;
pub(crate) mod generic_delimiter;
pub(crate) mod grit_metavariable;
pub(crate) mod import_anonymous_layer;
pub(crate) mod import_named_layer;
pub(crate) mod import_supports;
Expand All @@ -50,6 +49,7 @@ pub(crate) mod media_not_condition;
pub(crate) mod media_or_condition;
pub(crate) mod media_type;
pub(crate) mod media_type_query;
pub(crate) mod metavariable;
pub(crate) mod named_namespace_prefix;
pub(crate) mod namespace;
pub(crate) mod nested_qualified_rule;
Expand Down
80 changes: 40 additions & 40 deletions crates/biome_css_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,46 +1747,6 @@ impl IntoFormat<CssFormatContext> for biome_css_syntax::CssGenericProperty {
)
}
}
impl FormatRule<biome_css_syntax::CssGritMetavariable>
for crate::css::auxiliary::grit_metavariable::FormatCssGritMetavariable
{
type Context = CssFormatContext;
#[inline(always)]
fn fmt(
&self,
node: &biome_css_syntax::CssGritMetavariable,
f: &mut CssFormatter,
) -> FormatResult<()> {
FormatNodeRule::<biome_css_syntax::CssGritMetavariable>::fmt(self, node, f)
}
}
impl AsFormat<CssFormatContext> for biome_css_syntax::CssGritMetavariable {
type Format<'a> = FormatRefWithRule<
'a,
biome_css_syntax::CssGritMetavariable,
crate::css::auxiliary::grit_metavariable::FormatCssGritMetavariable,
>;
fn format(&self) -> Self::Format<'_> {
#![allow(clippy::default_constructed_unit_structs)]
FormatRefWithRule::new(
self,
crate::css::auxiliary::grit_metavariable::FormatCssGritMetavariable::default(),
)
}
}
impl IntoFormat<CssFormatContext> for biome_css_syntax::CssGritMetavariable {
type Format = FormatOwnedWithRule<
biome_css_syntax::CssGritMetavariable,
crate::css::auxiliary::grit_metavariable::FormatCssGritMetavariable,
>;
fn into_format(self) -> Self::Format {
#![allow(clippy::default_constructed_unit_structs)]
FormatOwnedWithRule::new(
self,
crate::css::auxiliary::grit_metavariable::FormatCssGritMetavariable::default(),
)
}
}
impl FormatRule<biome_css_syntax::CssIdSelector>
for crate::css::selectors::id_selector::FormatCssIdSelector
{
Expand Down Expand Up @@ -2896,6 +2856,46 @@ impl IntoFormat<CssFormatContext> for biome_css_syntax::CssMediaTypeQuery {
)
}
}
impl FormatRule<biome_css_syntax::CssMetavariable>
for crate::css::auxiliary::metavariable::FormatCssMetavariable
{
type Context = CssFormatContext;
#[inline(always)]
fn fmt(
&self,
node: &biome_css_syntax::CssMetavariable,
f: &mut CssFormatter,
) -> FormatResult<()> {
FormatNodeRule::<biome_css_syntax::CssMetavariable>::fmt(self, node, f)
}
}
impl AsFormat<CssFormatContext> for biome_css_syntax::CssMetavariable {
type Format<'a> = FormatRefWithRule<
'a,
biome_css_syntax::CssMetavariable,
crate::css::auxiliary::metavariable::FormatCssMetavariable,
>;
fn format(&self) -> Self::Format<'_> {
#![allow(clippy::default_constructed_unit_structs)]
FormatRefWithRule::new(
self,
crate::css::auxiliary::metavariable::FormatCssMetavariable::default(),
)
}
}
impl IntoFormat<CssFormatContext> for biome_css_syntax::CssMetavariable {
type Format = FormatOwnedWithRule<
biome_css_syntax::CssMetavariable,
crate::css::auxiliary::metavariable::FormatCssMetavariable,
>;
fn into_format(self) -> Self::Format {
#![allow(clippy::default_constructed_unit_structs)]
FormatOwnedWithRule::new(
self,
crate::css::auxiliary::metavariable::FormatCssMetavariable::default(),
)
}
}
impl FormatRule<biome_css_syntax::CssNamedNamespacePrefix>
for crate::css::auxiliary::named_namespace_prefix::FormatCssNamedNamespacePrefix
{
Expand Down
58 changes: 2 additions & 56 deletions crates/biome_css_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,8 @@ impl<'src> CssLexer<'src> {
self.advance(1);
self.consume_byte(T!["$="])
}
UNI if self.options.is_grit_metavariable_enabled()
&& self.is_grit_metavariable_start() =>
{
self.consume_grit_metavariable()
UNI if self.options.is_metavariable_enabled() && self.is_metavariable_start() => {
self.consume_metavariable(GRIT_METAVARIABLE)
}
IDT | UNI | BSL if self.is_ident_start() => self.consume_identifier(),

Expand Down Expand Up @@ -1320,58 +1318,6 @@ impl<'src> CssLexer<'src> {
_ => false,
}
}

/// Check if the lexer starts a grit metavariable
fn is_grit_metavariable_start(&mut self) -> bool {
let current_char = self.current_char_unchecked();
if current_char == 'μ' {
let current_char_length = current_char.len_utf8();
// μ[a-zA-Z_][a-zA-Z0-9_]*
if matches!(
self.byte_at(current_char_length),
Some(b'a'..=b'z' | b'A'..=b'Z' | b'_')
) {
return true;
}

// μ...
if self.byte_at(current_char_length) == Some(b'.')
&& self.byte_at(current_char_length + 1) == Some(b'.')
&& self.byte_at(current_char_length + 2) == Some(b'.')
{
return true;
}
}
false
}

/// Consume a grit metavariable(μ[a-zA-Z_][a-zA-Z0-9_]*|μ...)
/// https://github.com/getgrit/gritql/blob/8f3f077d078ccaf0618510bba904a06309c2435e/resources/language-metavariables/tree-sitter-css/grammar.js#L388
fn consume_grit_metavariable(&mut self) -> CssSyntaxKind {
debug_assert!(self.is_grit_metavariable_start());

// SAFETY: We know the current character is μ.
let current_char = self.current_char_unchecked();
self.advance(current_char.len_utf8());

if self.current_byte() == Some(b'.') {
// SAFETY: We know that the current token is μ...
self.advance(3);
} else {
// μ[a-zA-Z_][a-zA-Z0-9_]*
self.advance(1);
while let Some(chr) = self.current_byte() {
match chr {
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' => {
self.advance(1);
}
_ => break,
}
}
}

GRIT_METAVARIABLE
}
}

impl<'src> ReLexer<'src> for CssLexer<'src> {
Expand Down
10 changes: 5 additions & 5 deletions crates/biome_css_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct CssParserOptions {

/// Enables parsing of Grit metavariables.
/// Defaults to `false`.
pub grit_metavariable: bool,
pub grit_metavariables: bool,
}

impl CssParserOptions {
Expand All @@ -49,8 +49,8 @@ impl CssParserOptions {
}

/// Enables parsing of Grit metavariables.
pub fn allow_grit_metavariables(mut self) -> Self {
self.grit_metavariable = true;
pub fn allow_metavariables(mut self) -> Self {
self.grit_metavariables = true;
self
}

Expand All @@ -60,8 +60,8 @@ impl CssParserOptions {
}

/// Checks if parsing of Grit metavariables is enabled.
pub fn is_grit_metavariable_enabled(&self) -> bool {
self.grit_metavariable
pub fn is_metavariable_enabled(&self) -> bool {
self.grit_metavariables
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/biome_css_parser/src/syntax/at_rule/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::parser::CssParser;
use crate::syntax::at_rule::feature::parse_any_query_feature;
use crate::syntax::block::parse_conditional_block;
use crate::syntax::{
is_at_grit_metavariable, is_at_identifier, is_nth_at_identifier, parse_grit_metavariable,
is_at_identifier, is_at_metavariable, is_nth_at_identifier, parse_metavariable,
parse_regular_identifier,
};
use biome_css_syntax::CssSyntaxKind::*;
Expand Down Expand Up @@ -80,8 +80,8 @@ impl ParseSeparatedList for MediaQueryList {
fn parse_any_media_query(p: &mut CssParser) -> ParsedSyntax {
if is_at_media_type_query(p) {
parse_any_media_type_query(p)
} else if is_at_grit_metavariable(p) {
parse_grit_metavariable(p)
} else if is_at_metavariable(p) {
parse_metavariable(p)
} else if is_at_any_media_condition(p) {
let m = p.start();
parse_any_media_condition(p).ok(); // TODO handle error
Expand Down
Loading

0 comments on commit c9ecdac

Please sign in to comment.