Skip to content

Have at-rule without block handled in two stages as well #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove AtRuleType::OptionalBlock
  • Loading branch information
upsuper committed Sep 1, 2017
commit 85827c0ce009ea71d9dc3467c70ccc9f22beef51
10 changes: 7 additions & 3 deletions src/css-parsing-tests/one_rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
"@foo", ["at-rule", "foo", [], null],

"@foo bar; \t/* comment */", ["at-rule", "foo", [" ", ["ident", "bar"]], null],
" /**/ @foo bar{[(4", ["at-rule", "foo",
" /**/ @foo-with-block bar{[(4", ["at-rule", "foo-with-block",
[" ", ["ident", "bar"]],
[["[]", ["()", ["number", "4", 4, "integer"]]]]
],

"@foo { bar", ["at-rule", "foo", [" "], [" ", ["ident", "bar"]]],
"@foo [ bar", ["at-rule", "foo", [" ", ["[]", " ", ["ident", "bar"]]], null],
"@foo-with-block { bar", ["at-rule", "foo-with-block", [" "], [
" ", ["ident", "bar"]]
],
"@foo [ bar", ["at-rule", "foo",
[" ", ["[]", " ", ["ident", "bar"]]], null
],

" /**/ div > p { color: #aaa; } /**/ ", ["qualified rule",
[["ident", "div"], " ", ">", " ", ["ident", "p"], " "],
Expand Down
8 changes: 5 additions & 3 deletions src/css-parsing-tests/rule_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
"@foo", [["at-rule", "foo", [], null]],

"@charset; @foo", [
["at-rule", "charset", [], null],
["error", "invalid"],
["at-rule", "foo", [], null]
],

"@foo bar; \t/* comment */", [["at-rule", "foo", [" ", ["ident", "bar"]], null]],

" /**/ @foo bar{[(4", [["at-rule", "foo",
" /**/ @foo-with-block bar{[(4", [["at-rule", "foo-with-block",
[" ", ["ident", "bar"]],
[["[]", ["()", ["number", "4", 4, "integer"]]]]
]],

"@foo { bar", [["at-rule", "foo", [" "], [" ", ["ident", "bar"]]]],
"@foo-with-block { bar", [["at-rule", "foo-with-block", [" "], [
" ", ["ident", "bar"]]]
],
"@foo [ bar", [["at-rule", "foo", [" ", ["[]", " ", ["ident", "bar"]]], null]],

" /**/ div > p { color: #aaa; } /**/ ", [["qualified rule",
Expand Down
8 changes: 5 additions & 3 deletions src/css-parsing-tests/stylesheet.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@

"@foo; @charset 4 {}", [
["at-rule", "foo", [], null],
["at-rule", "charset", [" ", ["number", "4", 4, "integer"], " "], []]
["error", "invalid"]
],

"@foo bar; \t/* comment */", [["at-rule", "foo", [" ", ["ident", "bar"]], null]],

" /**/ @foo bar{[(4", [["at-rule", "foo",
" /**/ @foo-with-block bar{[(4", [["at-rule", "foo-with-block",
[" ", ["ident", "bar"]],
[["[]", ["()", ["number", "4", 4, "integer"]]]]
]],

"@foo { bar", [["at-rule", "foo", [" "], [" ", ["ident", "bar"]]]],
"@foo-with-block { bar", [["at-rule", "foo-with-block", [" "], [
" ", ["ident", "bar"]]]
],
"@foo [ bar", [["at-rule", "foo", [" ", ["[]", " ", ["ident", "bar"]]], null]],

" /**/ div > p { color: #aaa; } /**/ ", [["qualified rule",
Expand Down
36 changes: 2 additions & 34 deletions src/rules_and_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ pub enum AtRuleType<P, R> {
///
/// The value is the representation of the "prelude" part of the rule.
WithBlock(P),

/// The at-rule may either have a block or end with a semicolon.
///
/// This is mostly for testing. As of this writing no real CSS at-rule behaves like this.
///
/// The value is the representation of the "prelude" part of the rule.
OptionalBlock(P),
}

/// A trait to provide various parsing of declaration values.
Expand Down Expand Up @@ -124,24 +117,14 @@ pub trait AtRuleParser<'i> {
/// as returned by `RuleListParser::next` or `DeclarationListParser::next`,
/// or `Err(())` to ignore the entire at-rule as invalid.
///
/// This is only called when `parse_prelude` returned `WithBlock` or `OptionalBlock`,
/// and a block was indeed found following the prelude.
/// This is only called when `parse_prelude` returned `WithBlock`, and a block
/// was indeed found following the prelude.
fn parse_block<'t>(&mut self, prelude: Self::Prelude, input: &mut Parser<'i, 't>)
-> Result<Self::AtRule, ParseError<'i, Self::Error>> {
let _ = prelude;
let _ = input;
Err(ParseError::Basic(BasicParseError::AtRuleBodyInvalid))
}

/// An `OptionalBlock` prelude was followed by `;`.
///
/// Convert the prelude into the finished representation of the at-rule
/// as returned by `RuleListParser::next` or `DeclarationListParser::next`.
fn rule_without_block(&mut self, prelude: Self::Prelude) -> Self::AtRule {
let _ = prelude;
panic!("The `AtRuleParser::rule_without_block` method must be overriden \
if `AtRuleParser::parse_prelude` ever returns `AtRuleType::OptionalBlock`.")
}
}

/// A trait to provide various parsing of qualified rules.
Expand Down Expand Up @@ -495,21 +478,6 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
Ok(_) => unreachable!()
}
}
Ok(AtRuleType::OptionalBlock(prelude)) => {
match input.next() {
Ok(&Token::Semicolon) | Err(_) => Ok(parser.rule_without_block(prelude)),
Ok(&Token::CurlyBracketBlock) => {
// FIXME: https://github.com/rust-lang/rust/issues/42508
parse_nested_block::<'i, 't, _, _, _>(input, move |input| parser.parse_block(prelude, input))
.map_err(|e| PreciseParseError {
error: e,
slice: input.slice_from(start.position()),
location: start.source_location(),
})
}
_ => unreachable!()
}
}
Err(error) => {
let end_position = input.position();
match input.next() {
Expand Down
17 changes: 10 additions & 7 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,23 +751,26 @@ impl<'i> AtRuleParser<'i> for JsonParser {

fn parse_prelude<'t>(&mut self, name: CowRcStr<'i>, input: &mut Parser<'i, 't>)
-> Result<AtRuleType<Vec<Json>, Json>, ParseError<'i, ()>> {
Ok(AtRuleType::OptionalBlock(vec![
let mut prelude = vec![
"at-rule".to_json(),
name.to_json(),
Json::Array(component_values_to_json(input)),
]))
];
match_ignore_ascii_case! { &*name,
"media" | "foo-with-block" => Ok(AtRuleType::WithBlock(prelude)),
"charset" => Err(BasicParseError::AtRuleInvalid(name.clone()).into()),
_ => {
prelude.push(Json::Null);
Ok(AtRuleType::WithoutBlock(Json::Array(prelude)))
}
}
}

fn parse_block<'t>(&mut self, mut prelude: Vec<Json>, input: &mut Parser<'i, 't>)
-> Result<Json, ParseError<'i, ()>> {
prelude.push(Json::Array(component_values_to_json(input)));
Ok(Json::Array(prelude))
}

fn rule_without_block(&mut self, mut prelude: Vec<Json>) -> Json {
prelude.push(Json::Null);
Json::Array(prelude)
}
}

impl<'i> QualifiedRuleParser<'i> for JsonParser {
Expand Down