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

Error recovery for unexpected continuation keywords #87

Open
pfitzseb opened this issue Aug 31, 2022 · 1 comment
Open

Error recovery for unexpected continuation keywords #87

pfitzseb opened this issue Aug 31, 2022 · 1 comment
Labels
error messages Better, more actionable diagnostics

Comments

@pfitzseb
Copy link
Member

Consider

julia> JuliaSyntax.parse(JuliaSyntax.GreenNode, "if true; x ? true : elseif true end")[1]
     1:35     │[toplevel]
     1:35     │  [if]
     1:2      │    if
     3:3      │    Whitespace
     4:7      │    true                 ✔
     8:26     │    [block]
     8:8      │      ;
     9:26     │      [if]
     9:9      │        Whitespace
    10:10     │        Identifier       ✔
    11:11     │        Whitespace
    12:12     │        ?
    13:13     │        Whitespace
    14:17     │        true             ✔
    18:18     │        Whitespace
    19:19     │        :
    20:20     │        Whitespace
    21:26     │        [error]           ✘
    21:26     │          elseif         ✔
    27:27     │    Whitespace
    28:31     │    [error]               ✘
    28:31     │      true               ✔
    32:32     │    Whitespace
    33:35     │    end

This special case is fixed by #77 by punting the elseif into the containing block instead:

julia> JuliaSyntax.parse(JuliaSyntax.GreenNode, "if true; x ? true : elseif true end")[1]
     1:35     │[toplevel]
     1:35     │  [if]
     1:2      │    if
     3:3      │    Whitespace
     4:7      │    true                 ✔
     8:19     │    [block]
     8:8      │      ;
     9:19     │      [if]
     9:9      │        Whitespace
    10:10     │        Identifier       ✔
    11:11     │        Whitespace
    12:12     │        ?
    13:13     │        Whitespace
    14:17     │        true             ✔
    18:18     │        Whitespace
    19:19     │        :
    20:19     │        error             ✘
    20:20     │    Whitespace
    21:32     │    [elseif]
    21:26     │      elseif
    27:27     │      Whitespace
    28:31     │      true               ✔
    32:32     │      [block]
    32:32     │        Whitespace
    33:35     │    end

but of course that naive solution only works if there is only one missing or extraneous token, so "if true; x ? true : foo ))))) elseif true end" will break it again.

Generally, this should be solvable by an arbitrarily long look-ahead for continuation keywords, but I really don't like that solution (and it might not even work in all cases).

@c42f
Copy link
Member

c42f commented Sep 13, 2022

Generally, this should be solvable by an arbitrarily long look-ahead for continuation keywords

If we cap the lookahead at some large but not huge value I feel this is acceptable. But recovery and error reporting really is hard: in general it requires both look ahead and look behind to do a good job.

Any parsing algorithm which assumes the source is "well-formed by default" is likely to enter weird states where there's not enough local information available in the program to emit the ideal error. Hence wanting #93 or something similar... (really, I believe parsing of broken text and emitting error messages and warnings are in the "big pile of heuristics" category of software which in general is better learned with a data driven ML approach... but that would be a research project ...)

@c42f c42f added the error messages Better, more actionable diagnostics label Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error messages Better, more actionable diagnostics
Projects
None yet
Development

No branches or pull requests

2 participants