benjamin-hodgson / Pidgin

A lightweight and fast parsing library for C#.
https://www.benjamin.pizza/Pidgin/
MIT License
883 stars 68 forks source link

SourcePosDelta => MatchPos #131

Closed RomanSoloweow closed 2 years ago

RomanSoloweow commented 2 years ago

Hi, recently, I needed to have complete information about the result of the parser: StartLine, EndLine, StartColumn, EndColumnt, StartOffset, EndOffset, Lenght.

I could get the result in the way shown in the screenshot, but that doesn't seem like an elegant solution to me. This will require some minor internal changes, but I think you could implement it out of the box.

image

benjamin-hodgson commented 2 years ago

What you have there looks sensible to me. If you want to improve the code you could wrap the pair of CurrentPos and CurrentOffset into something reusable,

Parser<TToken, (int, SourcePos)> FullPositionInfo { get; }
    = Map(
        ValueTuple.Create,
        CurrentOffset,
        CurrentPos
    );

It’s a fairly straightforward application of the library, so I don’t think we need to add any special support to the API, but in general I’m not against adding convenience methods such as FullPositionInfo so I’d accept a PR!