Skip to content

Return more specific errors from parsing APIs #143

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
May 18, 2017

Conversation

jdm
Copy link
Member

@jdm jdm commented May 5, 2017

This shouldn't be merged until the related Servo and Gecko work is complete, but I figured I would put this up for feedback.


This change is Reviewable

@jdm
Copy link
Member Author

jdm commented May 6, 2017

Since servo/servo#16752 is ready for review, please review this!

Copy link
Member

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks overall pretty great to me.

I'm curious about which non-UnexpectedEof errors you're seeing coming from Tokenizer::next (what other errors can it possibly return?), and I have a few minor nits around.

Probably @SimonSapin wants to take a look, since he was looking into parsing performance and this has a potential impact on that (though probably not too much).

I also wonder if we could (ab)use the UnexpectedToken errors to do a bit less tokenization in Servo...

Awesome job overall!

src/parser.rs Outdated
Ok(_) => {
Err(())
}
//FIXME: swallowing non-UnexpectedEof errors seems wrong, but tests fail otherwise.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh... Could you file an issue with more details, and perhaps an example, and reference it here, just to see if it's something expected or not? Which other errors are reported?

(also, uber-nit: a space after //)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #144.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at #144, It's legit and we should remove the fixme and perhaps add a comment there.

@@ -52,6 +52,9 @@ pub trait DeclarationParser {
/// The finished representation of a declaration.
type Declaration;

///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docs? :)

@@ -89,6 +93,9 @@ pub trait AtRuleParser {
/// The finished representation of an at-rule.
type AtRule;

///
type Error;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document it too

@@ -266,8 +284,8 @@ pub struct RuleListParser<'i: 't, 't: 'a, 'a, P> {
}


impl<'i: 't, 't: 'a, 'a, R, P> RuleListParser<'i, 't, 'a, P>
where P: QualifiedRuleParser<QualifiedRule = R> + AtRuleParser<AtRule = R> {
impl<'i: 't, 't: 'a, 'a, R, P, E> RuleListParser<'i, 't, 'a, P>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, those are a lot of lifetimes.

@jdm
Copy link
Member Author

jdm commented May 8, 2017

Review comments addressed. @SimonSapin when do you think you will be able to look at this?

@@ -157,6 +164,9 @@ pub trait QualifiedRuleParser {
/// The finished representation of a qualified rule.
type QualifiedRule;

///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one seems to still be missing. A copy of the other Error docs is probably ok.

@SimonSapin
Copy link
Member

SimonSapin commented May 10, 2017

IRC conversation:

  • SimonSapin> jdm: looking at Return more specific errors from parsing APIs #143 , I don’t have a precise counter-proposal but I think the set of error types don’t quite make sense
  • jdm> go on
  • SimonSapin> jdm: for PreciseParseError should be the basic thing
  • SimonSapin> jdm: and the generic ParseError::Custom(T) feels like the wrong pattern
  • jdm isn't sure how else to incorporate errors from outside of the library
  • SimonSapin> jdm: the Basic / Custom enum should not exist, instead things should use From conversions as needed
  • jdm> conversions into what?
  • jdm> SimonSapin: keep in mind that BasicParseError exists as a separate type to reduce the number of explicit type annotations that are required
  • jdm> SimonSapin: when I threaded ParseError through the whole API it because extremely frustrating to use
  • jdm> so for the APIs that can't return custom types, it's much easier to return a small set of known errors
  • SimonSapin> jdm: for example instead of returning Result <T, ParseError<'i, E>>, parse_entirely could return Result<T, E> where E: From<RemainingInput>
  • SimonSapin> or E: From<ParseError>
  • jdm> see, that would lead to a lot of ambugious types that would need resolving
  • jdm> and that becomes very gross
  • jdm> actually I might be misinterpreting that
  • jdm> SimonSapin: what is RemainingInput?
  • SimonSapin> jdm: an hypothetical unit type
  • jdm> hmm
  • jdm> SimonSapin: ok, I have a minimal form of selectors and style that I can prototype changes to rust-cssparser against
  • jdm> SimonSapin: so I'll try that out
  • SimonSapin> jdm: minimal form?
  • jdm> SimonSapin: yeah; it has parsing code for background-color, custom properties, and something that uses the selectors/ parsing traits
  • jdm> SimonSapin: rather than having to redo the return types for about 3000 parsing functions
  • SimonSapin> interesting
  • SimonSapin> jdm: alternative idea: no generic E, one ParseError type to rule them all. Add variants/fields to it as needed. For example &'static str that describes what was expected instead of that unexpected token
  • jdm> SimonSapin: could you record this conversation in the PR, please?
  • SimonSapin> sure
  • jdm> SimonSapin: I don't think the one type to rule them all makes sense given how many errors will be added to style/
  • SimonSapin> jdm: I haven’t looked at the servo PR yet. Are there many error types? Why?
  • jdm> SimonSapin: because we want to provide enough information for gecko to use https://dxr.mozilla.org/mozilla-central/source/dom/locales/en-US/chrome/layout/css.properties
  • jdm> SimonSapin: the servo PR provides useful context
  • jdm> (for understanding the rust-cssparser changes)
  • SimonSapin> jdm: do we need this exact set of error cases, or just providing roughly as useful info?
  • jdm> SimonSapin: hard to say; I'm aiming for more information than my first attempt could provide.
  • jdm> which was basically nothing more than "this rule was invalid"

@bors-servo
Copy link
Contributor

☔ The latest upstream changes (presumably #146) made this pull request unmergeable. Please resolve the merge conflicts.

@jdm
Copy link
Member Author

jdm commented May 16, 2017

I've spent some time trying to integrate your ideas about returning E where E: From<...> (I've started out with From<BasicParseError<'i>> as something concrete), and I'm really having trouble making any traction. Between issues with incorporating lifetimes (such as the error types in the traits in rules_and_declarations.rs) and type-inference errors when there are too many layers of generics, I don't see a clear path forward. I would like to move forward with the existing design that actually compiles unless you have a concrete counter-proposal.

@SimonSapin
Copy link
Member

SimonSapin commented May 18, 2017

I’m still convinced that the generic enum ParseError<T> { …, Custom(T) } is not a good pattern, but at the same time if I spent a bunch of time to come up with a concrete counter-proposal I’m likely to hit the same issues as you did and I don’t know that I’ll have any better solution. So let’s call this r+ on the general approach.


Reviewed 5 of 7 files at r2, 2 of 2 files at r4.
Review status: all files reviewed at latest revision, 7 unresolved discussions, some commit checks failed.


src/parser.rs, line 373 at r4 (raw file):

        let byte = self.tokenizer.next_byte();
        if self.stop_before.contains(Delimiters::from_byte(byte)) {
            return Err(BasicParseError::UnexpectedToken(Token::Delim(byte.unwrap_or(0) as char)))

Returning UnexpectedToken here is incorrect. The point of self.stop_before is to provide the abstraction that we’re parsing a subslice of the original stream. Finding the byte/token that marks the end of that sub-stream is the expected end of the stream.

Maybe UnexpectedEof should be renamed EndOfInput?


src/parser.rs, line 610 at r4 (raw file):

        match token {
            Token::Number(NumericValue { ref int_value, .. }) if int_value.is_some() => {
                Ok(int_value.unwrap())

Please use int_value: Some(int_value) in the pattern here to make unwrap unnecessary.


Comments from Reviewable

@nox
Copy link
Contributor

nox commented May 18, 2017

I don't like this design either.

What I would have expected is for cssparser to only define a basic set of errors, and for the Parse trait in Servo to have an associated type Error, like Serde's Serializer trait.

This way each specific parser in Servo would care only about its specific errors, and we wouldn't need to have all errors possible in Servo in a single huge enum.

@SimonSapin
Copy link
Member

I suspect that reconciling lifetime parameters with generic error types would require "associated type constructors" rust-lang/rfcs#1598

@nox
Copy link
Contributor

nox commented May 18, 2017

I see.

@SimonSapin
Copy link
Member

@bors-servo r+


Reviewed 8 of 8 files at r6.
Review status: all files reviewed at latest revision, 5 unresolved discussions.


Comments from Reviewable

@bors-servo
Copy link
Contributor

📌 Commit 40d86b0 has been approved by SimonSapin

@bors-servo
Copy link
Contributor

⌛ Testing commit 40d86b0 with merge 0a70d22...

bors-servo pushed a commit that referenced this pull request May 18, 2017
Return more specific errors from parsing APIs

This shouldn't be merged until the related Servo and Gecko work is complete, but I figured I would put this up for feedback.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/143)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

☀️ Test successful - status-travis
Approved by: SimonSapin
Pushing 0a70d22 to master...

@bors-servo bors-servo merged commit 40d86b0 into servo:master May 18, 2017
jdm added a commit to jdm/rust-cssparser that referenced this pull request May 18, 2017
This reverts commit 0a70d22, reversing
changes made to fc0bdcd.
bors-servo pushed a commit to servo/servo that referenced this pull request Jun 9, 2017
Report more informative CSS errors

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16752)
<!-- Reviewable:end -->
bors-servo pushed a commit that referenced this pull request Jun 9, 2017
Return meaningful error values when parsing

This is #143 with an extra commit added to accommodate changes requested in servo/servo#16752.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/155)
<!-- Reviewable:end -->
bors-servo pushed a commit to servo/servo that referenced this pull request Jun 9, 2017
Report more informative CSS errors

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16752)
<!-- Reviewable:end -->
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Jun 10, 2017
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 683cf352b472110df4b28c191e8850763334134d
JerryShih pushed a commit to JerryShih/gecko-dev that referenced this pull request Jun 12, 2017
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341
aethanyc pushed a commit to aethanyc/gecko-dev that referenced this pull request Jun 12, 2017
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341
Manishearth pushed a commit to Manishearth/gecko-dev that referenced this pull request Jun 15, 2017
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341
jryans pushed a commit to jryans/gecko-dev that referenced this pull request Jun 16, 2017
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this pull request Oct 1, 2019
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341

UltraBlame original commit: 79d8f06313932e8f116100390dd153a1c8f9734b
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this pull request Oct 1, 2019
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341

UltraBlame original commit: 79d8f06313932e8f116100390dd153a1c8f9734b
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this pull request Oct 1, 2019
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341

UltraBlame original commit: 79d8f06313932e8f116100390dd153a1c8f9734b
bhearsum pushed a commit to mozilla-releng/staging-firefox that referenced this pull request May 1, 2025
…s-parse-error); r=SimonSapin

This requires servo/rust-cssparser#143 for the final commit. There's no better way to split that work up, unfortunately, and it's extremely easy to bitrot. I would appreciate if we could expedite reviewing this work.

This is the work necessary to enable https://bugzilla.mozilla.org/show_bug.cgi?id=1352669. It makes sense to merge it separately because it's so much effort to keep it up to date with the ongoing Stylo work.

---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] There are tests for these changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 061cb5f48e5c93a5decf39e530aea4a566e97341
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants