Skip to content
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

Add function to compute lookahead #107

Open
alecthomas opened this issue Sep 6, 2020 · 8 comments
Open

Add function to compute lookahead #107

alecthomas opened this issue Sep 6, 2020 · 8 comments

Comments

@alecthomas
Copy link
Owner

Repetitions will be unbounded. Everything else should be computable.

@ceymard
Copy link
Contributor

ceymard commented Sep 6, 2020

Are you talking about "positive lookahead" ? Because I was about to file an issue about that, as I'm unsure there is something like that.

It would be particularly useful along with "!" (which is starting to actually work with any kind of pattern)

type MyTest struct {
  SomeCoolRule *SubRule `=!('.' String) @@`
} 

This would match SubRule but ONLY if SubRule doesn't start with . 'some-string'

@alecthomas
Copy link
Owner Author

This was solely about automatically determining what the required lookahead value for a particular grammar is.

What's the use case for this? Participle already performs lookahead automatically, so normally given multiple cases in a disjunction this kind of behaviour will occur automatically. ie. you'd typically have

type MyTest struct {
  A *string `"." @String`
  B *SubRule `| @@`
}

@ceymard
Copy link
Contributor

ceymard commented Sep 6, 2020

I can't really think of anything other than readability right now. I'm not even sure I have ever used a look ahead in any of the grammars I've written.

@mechaneg
Copy link

Am I right, that look ahead value is computed automatically now ?

If so, why this disjunction example fails ?

input: "one two"
Error: "unexpected token "two"".
if I'll reorder fields Single and Many in Foo definition everything works fine for both "one" and "one two" inputs

type Foo struct {
	Single *Single `parser:"@@  |"`
	Many   *Many   `parser:"@@"`
}

type Single struct {
	Val string `parser:"@Ident"`
}

type Many struct {
	Vals []string `parser:"@Ident Ident"`
}

@alecthomas
Copy link
Owner Author

It is not computed automatically, this ticket is still open.

@mechaneg
Copy link

Ok, got it.
Anyway, I tried to increase look ahead value during parser initialization and my previous example still fails.
What's wrong with it?

@alecthomas
Copy link
Owner Author

alecthomas commented Oct 13, 2020 via email

@mccolljr
Copy link
Contributor

The addition of Union might make this a bit more complicated. The expr3 example technically requires infinite lookahead to be unambiguous for all possible inputs. In practice a sufficiently large number will have the same result for all reasonable inputs but its something to consider if this feature is still desired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants