c3lang / c3c

Compiler for the C3 language
https://c3-lang.org
GNU Lesser General Public License v3.0
2.94k stars 182 forks source link

Improve error messages when keywords are misspelled #1420

Open MinekPo1 opened 2 months ago

MinekPo1 commented 2 months ago

Currently when a keyword like interface are misspelled, the error is difficult to connect to a typo:

Error: A type name was expected, but this looks a variable or function name (as it doesn't start with an uppercase letter).

This error message can be replaced by contextually checking if the token is close (see edit distance) to a keyword.

in the case of keywords used for top level statement, since IDENT tokens cannot be used in global variable declarations, (as builtin types like int are keywords and custom types are IDENT_TYPE) when encountering an IDENT token in parse_top_level_statement, before calling parse_global_declaration, we can first go through the keywords we could expect and calculating the edit distance to the value and showing a message which explicitly suggests a typo.

lerno commented 2 months ago

Yes, this is good and there is already something related to find function names.