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

Consider Token Categories in Lookahead Functions Calculation #962

Closed
bd82 opened this issue May 25, 2019 · 1 comment
Closed

Consider Token Categories in Lookahead Functions Calculation #962

bd82 opened this issue May 25, 2019 · 1 comment

Comments

@bd82
Copy link
Member

bd82 commented May 25, 2019

Token Categories should be taken into account when performing lookahead calculation.

For example:

            const A = createToken({ name: "A" })
            
            const B = createToken({
                                   name: "B",
                                   // `B` is a typeof `A`
                                   categories: A 
                             })
            const C = createToken({ name: "C" })
            const D = createToken({ name: "D" })

            class CategoriesLookaheadBugParser extends Parser {
                constructor() {
                    super([A, B, C, D])
                    this.performSelfAnalysis()
                }

                public main = this.RULE("main", () => {
                    this.OR([
                        // 'alt1' is `BC`
                       // 'alt2' is `AD`
                      // However `B` can also match `A` due to the categories definition.
                     // So we need to look **two** tokens ahead
                    //  to decide between these two alternatives.
                        { ALT: () => this.SUBRULE(this.alt1) },
                        { ALT: () => this.SUBRULE(this.alt2) }
                    ])
                })

                public alt1 = this.RULE("alt1", () => {
                    this.CONSUME(B)
                    this.CONSUME(C)
                })

                public alt2 = this.RULE("alt2", () => {
                    this.CONSUME(A)
                    this.CONSUME(D)
                })
            }
@bd82 bd82 changed the title Consider Token Categories when computing Lookahead Functions Consider Token Categories in Lookahead Functions Calculation May 25, 2019
@bd82
Copy link
Member Author

bd82 commented May 25, 2019

This new capability will also allow to detect ambiguities that involve token categories.
e.g:

So while this change is more of a bug fix, it would be released in a new minor version as it could cause
previously "working" (incorrectly) grammars to fail during startup as previously missed errors would now be detected.

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

No branches or pull requests

1 participant