-
-
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
Invalid error span for eoi #704
Comments
Interesting, that seems odd. Any chance you could cut bits off until you have a minimum testable example that exhibits the behaviour? |
@zesterer let parser = just(T!["as"])
.map_with(
|t, e| (t, e.span()),
)
.then(
just(T!["text"])
.recover_with(via_parser(any().or_not().map(|_| T!["error"])))
.map_with(|t, e| (t, e.span())),
)
.map_with(|(t1, t2), e| (t1, t2, e.span())); For input: "as" tokens: [("as", 0..2)] eoi: 2..2 Result is: output: Some(("as", 0..2), ("error", 0..2), 0..2)
errs: found end of input at 0..2 expected "text" |
Do you have something I can compile and run locally? This seems to depend on a lot of external things. |
@zesterer This should run out of the box: use chumsky::{
input::{SpannedInput, ValueInput},
prelude::{Parser as ChumskyParser, *},
};
use extra::Err;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Token(pub String);
trait Lexer<'src, Output>:
ChumskyParser<'src, &'src str, Output, Err<Rich<'src, char>>> + Clone
{
}
impl<
'src,
Output,
T: ChumskyParser<'src, &'src str, Output, extra::Err<Rich<'src, char>>> + Clone,
> Lexer<'src, Output> for T
{
}
trait Parser<'src, Output>:
ChumskyParser<
'src,
SpannedInput<Token, SimpleSpan, &'src [(Token, SimpleSpan)]>,
Output,
Err<Rich<'src, Token>>,
> + Clone
{
}
impl<
'src,
Output,
T: ChumskyParser<
'src,
SpannedInput<Token, SimpleSpan, &'src [(Token, SimpleSpan)]>,
Output,
extra::Err<Rich<'src, Token>>,
> + Clone,
> Parser<'src, Output> for T
{
}
fn lexer<'a>() -> impl Lexer<'a, Vec<(Token, SimpleSpan)>> {
any::<&'a str, Err<Rich<'a, char>>>()
.filter(|c: &char| !c.is_whitespace())
.repeated()
.at_least(1)
.collect::<String>()
.map_with(|s, e| (Token(s), e.span()))
.then_ignore(any().filter(|c: &char| c.is_whitespace()).repeated())
.repeated()
.collect::<Vec<(Token, SimpleSpan)>>()
.then_ignore(end())
}
fn parser<'a>() -> impl Parser<'a, Vec<(Token, SimpleSpan)>> {
just(Token("as".to_string()))
.map_with(|t, e| (t, e.span()))
.then(
just(Token("text".to_string()))
.recover_with(via_parser(
any().or_not().map(|_| Token("error".to_string())),
))
.map_with(|t, e| (t, e.span())),
)
.map(|(t1, t2)| vec![t1, t2])
}
fn main() {
let input = "as";
let tokens = lexer().parse(input).unwrap();
let end = tokens.last().unwrap().1.end;
let spanned_input = tokens.spanned(SimpleSpan::new(end, end));
let result = parser().parse(spanned_input).into_result();
println!("{:?}", result);
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm using my own lexer that returns vector of spanned tokens like this:
For input like this:
I've noticed an incorrect span for error when compiler looks something like this:
Parsing results in:
Error has a span of
0..4
but It should have 4..4The text was updated successfully, but these errors were encountered: