use crate::parser::CssParser;
use crate::syntax::at_rule::parse_error::expected_any_namespace_url;
use crate::syntax::value::url::{is_at_url_function, parse_url_function};
use crate::syntax::{is_at_string, parse_regular_identifier, parse_string};
use biome_css_syntax::CssSyntaxKind::*;
use biome_css_syntax::{CssSyntaxKind, T};
use biome_parser::parse_recovery::ParseRecovery;
use biome_parser::parsed_syntax::ParsedSyntax::Present;
use biome_parser::prelude::ParsedSyntax::Absent;
use biome_parser::prelude::*;
use biome_rowan::SyntaxKind;
#[inline]
pub(crate) fn is_at_namespace_at_rule(p: &mut CssParser) -> bool {
p.at(T![namespace])
}
#[inline]
pub(crate) fn parse_namespace_at_rule(p: &mut CssParser) -> ParsedSyntax {
if !is_at_namespace_at_rule(p) {
return Absent;
}
let m = p.start();
p.bump(T![namespace]);
if !is_at_namespace_url(p) {
parse_regular_identifier(p).ok();
}
let kind = match parse_namespace_url(p).or_recover(
p,
&NamespaceUrlParseRecovery,
expected_any_namespace_url,
) {
Ok(m) => {
if m.kind(p).is_bogus() {
CSS_BOGUS_AT_RULE
} else {
CSS_NAMESPACE_AT_RULE
}
}
Err(_) => CSS_BOGUS_AT_RULE,
};
p.expect(T![;]);
Present(m.complete(p, kind))
}
struct NamespaceUrlParseRecovery;
impl ParseRecovery for NamespaceUrlParseRecovery {
type Kind = CssSyntaxKind;
type Parser<'source> = CssParser<'source>;
const RECOVERED_KIND: Self::Kind = CSS_BOGUS;
fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool {
p.at(T![;]) || p.has_nth_preceding_line_break(1)
}
}
#[inline]
pub(crate) fn is_at_namespace_url(p: &mut CssParser) -> bool {
is_at_url_function(p) || is_at_string(p)
}
#[inline]
pub(crate) fn parse_namespace_url(p: &mut CssParser) -> ParsedSyntax {
if !is_at_namespace_url(p) {
return Absent;
}
if is_at_url_function(p) {
parse_url_function(p)
} else {
parse_string(p)
}
}