Inside a parser, how do I "back up" one character? #344

Open
opened 2026-01-29 14:34:19 +00:00 by claunia · 1 comment
Owner

Originally created by @deanebarker on GitHub (Jan 26, 2020).

Assuming I've found my pattern, I also want to include the character before if it's a space. So I want to do something like this:

if(slice.PeekCharExtra(-1).IsSpace())
{
   slice.PrevChar();   // Note: this does not exist
}

I saw the Start property on StringSlice, but I can't figure out what character index I'm on now, so that didn't seem to help.

Originally created by @deanebarker on GitHub (Jan 26, 2020). Assuming I've found my pattern, I also want to include the character before _if it's a space_. So I want to do something like this: ``` if(slice.PeekCharExtra(-1).IsSpace()) { slice.PrevChar(); // Note: this does not exist } ``` I saw the `Start` property on `StringSlice`, but I can't figure out what character index I'm on now, so that didn't seem to help.
claunia added the question label 2026-01-29 14:34:19 +00:00
Author
Owner

@xoofx commented on GitHub (Jan 31, 2020):

Assuming I've found my pattern, I also want to include the character before if it's a space. So I want to do something like this:

I don't think we are able to handle this case easily. Once a parser has parsed a slice, it will transform it to its internal representation (and we are not fully 100% roundtrip ready, as we don't store all stuffs that a parser could discard e.g trailing spaces)...
So the easiest solution would be to trigger your parser on space and your characters, and match from that instead.

@xoofx commented on GitHub (Jan 31, 2020): > Assuming I've found my pattern, I also want to include the character before if it's a space. So I want to do something like this: I don't think we are able to handle this case easily. Once a parser has parsed a slice, it will transform it to its internal representation (and we are not fully 100% roundtrip ready, as we don't store all stuffs that a parser could discard e.g trailing spaces)... So the easiest solution would be to trigger your parser on space and your characters, and match from that instead.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#344