-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
Add a spanned
method for automatically wrapping the parser result in a Span
#640
Comments
I've considered this a few times, but in practice it's far from a convention to represent the two as a tuple: folks often create a I am tempted to make such a |
In my opinion, a library taking a position in cases like this usually makes it easier to solve problems and reduces fragmentation, so I'm all in favor of an official solution. |
What worries me about this is that such a |
IMO that's a good thing, it encourages and makes it easier to do good error reporting, since you can access the span right then and there. Besides, it's opt-in only for those who use the method. |
Is there any case where Spanned is something else than a token or element, and a span? In the current version of chumsky i also don't see an easy way to create a custom spanned type in a tokenizer that is then consumed by a parser, since the Input trait is sealed, and the |
The problem with a trait is that you then end up needing to specify the implementor you want, adding more type annotations (and potentially confusing type errors) One option might be to tie it to the pub trait Span {
type Spanned<T>;
fn make_spanned<T>(self, item: T) -> Self::Spanned<T>;
}
...
pub struct Spanned<T, S = SimpleSpan>(T, S);
impl Span for SimpleSpan {
type Spanned<T> = Spanned<T>;
fn make_spanned<T>(self, item: T) -> Self::Spanned<T> { Spanned(item, self) }
} Then a How do you both feel about this? |
I really like this! |
I third this, trying to migrate from |
In #713 I've made a first pass attempt at this. Do you have any thoughts @TheOnlyTails, @MeGaGiGaGon, and @Hedgehogo? |
You could make a separate trait that would contain the associated |
I don't think that inference is likely to be powerful enough to infer the output type in enough cases that it would get very annoying.
|
I don't think just a tuple works for us, as it's too weak a simplification to be worthy of a combinator. So the choice is between three solutions: built in chumsky Yes, it can, in case the user would like to somehow optimize spans for memory by pulling missing span information from If the answer to this question is no, then there is practically no applicability of own |
The current implementation allows the user to specify their own spanning type. |
But it is useless as long as it does not allow to impose constraints on |
I will describe how the implementation should look like, at which it is possible to realize the situation described above. We need a separate trait that allows us to match a span - trait ???: Span {
type Spanned<T>;
} But such a trait does not allow to impose restrictions on T, and this is necessary to get the missing information from it in case of memory-optimized trait ????<T>: Span {
type Spanned;
} This is actually a mapping of the form (T, S) -> Sp. It can be written in Rust in a different way: trait ???<S: Span> {
type Spanned;
} In this form it looks as if some types can have their own Spanned, and since in a condensed form how to get Span depends on the type, it looks logical. It also makes it easy to pick up the name - trait WithSpanned<S: Span> {
type Spanned;
fn new_spanned(self, span: S) -> Self::Spanned;
}
impl<T> WithSpanned<SimpleSpan> for T {
type Spanned = Spanned<T>;
fn new_spanned(self, span: SimpleSpan) -> Self::Spanned {
Spanned(self, span)
}
} |
This would be a very simple utility method I feel is missing right now, as a shorthand:
It's not essential or anything, but it'll definitely be nice to have.
The text was updated successfully, but these errors were encountered: