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

Custom error message for missing "end" #1141

Open
kostmo opened this issue Mar 6, 2023 · 3 comments
Open

Custom error message for missing "end" #1141

kostmo opened this issue Mar 6, 2023 · 3 comments
Assignees
Labels
C-Low Hanging Fruit Ideal issue for new contributors. L-Error reporting Reporting language or runtime errors to the player. L-Parsing Parsing the Swarm language from a string into an AST. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Feature A new feature to be added to the game.

Comments

@kostmo
Copy link
Member

kostmo commented Mar 6, 2023

I've found a common mistake in my own swarm-lang code to be a missing end keyword at end of a def. One reason for the frequency of this error is probably that the languages I, and I suspect most others, are most familiar with (e.g. Python and Haskell) do not require this keyword at the end of a function.

The error manifests itself far away (at the end of the file) and as the message "unexpected end of input".
Since it is a common error, could we special-case this error to have a more user-friendly message? Perhaps in the parser we can count to make sure that the number of end keywords matches the number of def keywords.

@kostmo kostmo added Z-Feature A new feature to be added to the game. L-Error reporting Reporting language or runtime errors to the player. labels Mar 6, 2023
@byorgey
Copy link
Member

byorgey commented Mar 7, 2023

Yes, that sounds like it should be feasible. If we work a little harder we can probably even pinpoint the place where the missing end likely occurred --- it will be the first place we try to parse a def inside the body of another def. Currently that is syntactically valid but would fail if we try to typecheck it. Perhaps we could even refactor the parser a bit to make it a syntax error for a def to show up inside the body of another def. i.e. we can add a flag to some parsers indicating whether we are at the "top level" or not, and allow def only when at the top level. Then if we saw a def at the wrong level we could say "Unexpected def. Perhaps you have a missing end?" or something like that.

@byorgey byorgey added the L-Parsing Parsing the Swarm language from a string into an AST. label Mar 7, 2023
@ussgarci
Copy link
Collaborator

I'd like to work on this issue. 😅

@ussgarci ussgarci self-assigned this May 16, 2023
@byorgey byorgey added C-Low Hanging Fruit Ideal issue for new contributors. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. labels Apr 10, 2024
@xsebek
Copy link
Member

xsebek commented Sep 28, 2024

Just to update, since #1087 the def inside def is valid:

def a =
  def b = 1 end;
  return (b + b)
end;
a

It's still a good bet that the end should be before the next def though. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Low Hanging Fruit Ideal issue for new contributors. L-Error reporting Reporting language or runtime errors to the player. L-Parsing Parsing the Swarm language from a string into an AST. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Feature A new feature to be added to the game.
Projects
None yet
Development

No branches or pull requests

4 participants