Skip to content

add position to text pos #30

@faassen

Description

@faassen

xmlparser errors return a TextPos, which Stream::gen_text_pos mentions is expensive to calculate. But I'd like to use a library like Ariadne or Miette to report parser errors. Both of these are offset based, i.e. a position.

I figured I'd just get the stream from the tokenizer when there's an error, using stream().pos(). I have to obtain the stream when there's an error, because stream() generates a copy, and I don't want a stale copy. But since Tokenizer is an iterator and is moved when I start iteration, I can't access it:

for token in tokenizer {
     match token {
        ...
       Err(e) => {
             let position = tokenizer.stream().pos(); // not allowed
        }
    }
}

The easiest way to fix this would be to simply retain the position in TextPos when it is calculated. Would you accept a PR that implements this?

For my use case, I'd actually prefer it if the TextPos calculation doesn't happen at all, as I don't need this to happen, but I don't see a way to do this without breaking backwards compatibilty.

Without this I think the only way forward is for me to write some really ugly code to calculate the position back from the text pos and the input as I don't see another way to obtain it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions