question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: an analog of Positioned from Sprache?

See original GitHub issue

In Sprache, you provided IPositionAware and Positioned to make a parse result, well, aware of the position it’s parsed. I see this feature useful for giving a precise position of some syntax construct in post-parse checks (like, “this variable right here wasn’t declared” vs. the same without being able to report a position to the user, so they would have to search that place for themselves).

There is Result<T>.Location, but I don’t see how I could apply that to the resulting value via combinators. Could I achive it here, and which way you’d advice to do it best? (Or if maybe I’m looking for the wrong thing, and the thing mentioned should be done another way.)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
nblumhardtcommented, Jul 7, 2019

Hi!

There’s no built-in combinator; I think it would be reasonably easy to write one, using similar tactics to Sprache’s implementation - keen to explore how it might look.

If you want to drop this into your own project I think it’s roughly:

interface ILocated
{
    TextSpan Location { get; set; }
}

static TextParser<T> WithLocation<T>(this TextParser<T> parser)
    where T: ILocated
{
    return i => {
        var inner = parser(i);
        if (!inner.HasValue) return inner;
        inner.Value.Location = inner.Location;
        return inner;
    };
}

(Sketched in browser, no idea whether or not this will compile as-is 😉)

HTH, Nick

1reaction
nblumhardtcommented, Nov 21, 2022

Hi @JoeMGomes - unfortunately no time to dig in properly but hopefully this helps:

The start index of the match within the input will be inner.Location.First().Position.Absolute.

The exclusive end index will be inner.Remainder.First().Position.Absolute.

In the second case it’s also possible that the remainder token list will be empty, which would mean “matched until end of stream”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the word for 'language' in German 'sprache' but 'taal ...
It comes from the Middle Low German word for 'German', 'Düdesch', which in Modern High German is 'Deutsch', but 2500 years ago was...
Read more >
The Syntax of Question Particles
Julien (2001), Lee (2005, 2008) and Simpson & Wu (2002) argue that final particles are derived by TP-‐movement to a Topic or Focus...
Read more >
Prosodic Prominence in Polar Questions and Exclamatives
This study investigates prosodic prominence in string-identical verb-first exclamatives and questions in German. It presents results from ...
Read more >
overt analogue criterion
Position of the definiens. principle that a zero morpheme may not be postulated in a language unless there is an overt morpheme that...
Read more >
German Word Order in Main Clauses (Hauptsätze)
German ears prefer pronouns to precede nouns wherever possible, even when the noun is the subject in "third position". Thus "Der Mann rasiert...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found