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

Warn if an always-match clause is not the last alternative #576

Open
hildjj opened this issue Nov 25, 2024 · 3 comments
Open

Warn if an always-match clause is not the last alternative #576

hildjj opened this issue Nov 25, 2024 · 3 comments

Comments

@hildjj
Copy link
Contributor

hildjj commented Nov 25, 2024

Consider:

foo = "b"* / "foo" 

This is unlikely to produce the output that the author intended, since the "foo" will never be checked. I wonder if we can detect this pattern and at least warn on it?

@Mingun
Copy link
Member

Mingun commented Nov 25, 2024

It is definitely possible. The idea that for each node you should calculate statistic -- how much characters node will consume when matched? It is better to calculate the minimum and maximum. Here we only need the minimum, and the maximum may be needed in other analyzes. If alternative have a branch with minimum=0, then other alternatives will not be tried.

Actually, I already wrote such pass (but not used it in any project)

@hildjj
Copy link
Contributor Author

hildjj commented Nov 25, 2024

This probably also applies to lookaheads. !("b"*) always fails, and &("b"*) always succeeds.

@Mingun
Copy link
Member

Mingun commented Nov 26, 2024

Maybe. Also, I realized, that the proposed solution will catch only part of situations. . always match everything, but it also always consume 1 character when matched. Actually we need static analysis which will create a representation of what inputs are possible at certain points. Then we would catch even such situations:

start1
  = [a-z]
  / [bcd] // never matched because the previous alternative matches all what this matches
start2
  = 'prefix'
  / 'prefix-literal' // never matched because the previous alternative matches all what this matches

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

2 participants