-
-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(css_parser): CSS Parser charset #268
- Loading branch information
1 parent
04e6319
commit aed440a
Showing
23 changed files
with
809 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::parser::CssParser; | ||
use crate::syntax::parse_error::expected_string; | ||
use crate::syntax::parse_string; | ||
use biome_css_syntax::CssSyntaxKind::*; | ||
use biome_css_syntax::{CssSyntaxKind, TextRange, 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_charset_rule(p: &mut CssParser) -> bool { | ||
p.at(T![@]) && p.nth_at(1, T![charset]) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn parse_at_charset_rule(p: &mut CssParser) -> ParsedSyntax { | ||
if !is_at_charset_rule(p) { | ||
return Absent; | ||
} | ||
|
||
let m = p.start(); | ||
|
||
p.bump(T![@]); | ||
p.bump(T![charset]); | ||
|
||
let kind = match parse_string(p).or_recover( | ||
p, | ||
&ParseRecovery::new(CSS_BOGUS, CHARTSET_RECOVERY_SET).enable_recovery_on_line_break(), | ||
expected_string, | ||
) { | ||
Ok(encoding) if !encoding.kind(p).is_bogus() => { | ||
if eat_or_recover_close_token(p, encoding) { | ||
CSS_AT_CHARSET_RULE | ||
} else { | ||
CSS_BOGUS_AT_RULE | ||
} | ||
} | ||
_ => { | ||
p.expect(T![;]); | ||
CSS_BOGUS_AT_RULE | ||
} | ||
}; | ||
|
||
Present(m.complete(p, kind)) | ||
} | ||
|
||
const CHARTSET_RECOVERY_SET: TokenSet<CssSyntaxKind> = token_set![T![;]]; | ||
|
||
#[inline] | ||
fn eat_or_recover_close_token(p: &mut CssParser, encoding: CompletedMarker) -> bool { | ||
if p.eat(T![;]) { | ||
true | ||
} else { | ||
if let Ok(m) = ParseRecovery::new(CSS_BOGUS, CHARTSET_RECOVERY_SET) | ||
.enable_recovery_on_line_break() | ||
.recover(p) | ||
{ | ||
let diagnostic = expected_string( | ||
p, | ||
TextRange::new(encoding.range(p).start(), m.range(p).end()), | ||
); | ||
p.error(diagnostic); | ||
} | ||
|
||
p.expect(T![;]); | ||
|
||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
mod charset; | ||
|
||
use crate::parser::CssParser; | ||
use crate::syntax::at_rule::charset::{is_at_charset_rule, parse_at_charset_rule}; | ||
use biome_css_syntax::T; | ||
use biome_parser::prelude::ParsedSyntax::Absent; | ||
use biome_parser::prelude::*; | ||
|
||
#[inline] | ||
pub(crate) fn at_at_rule(p: &mut CssParser) -> bool { | ||
p.at(T![@]) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn parse_at_rule(p: &mut CssParser) -> ParsedSyntax { | ||
if !at_at_rule(p) { | ||
return Absent; | ||
} | ||
|
||
if is_at_charset_rule(p) { | ||
parse_at_charset_rule(p) | ||
} else { | ||
Absent | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/biome_css_parser/tests/css_test_suite/error/at_rule/at_rule_charset_error.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@charset dsadsa; | ||
@charset ; | ||
@charset "iso-8859-15" | ||
@charset "UTF-8" 12321321; | ||
@charset "UTF-8" 12321321 | ||
@charset | ||
|
Oops, something went wrong.