JetBrains / JetBrains/Grammar-Kit

Allow putting keywords in quotation marks for error messages

Open
#262 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
771
Forks
137
Avg merge
11h 14m
Merged PRs (30d)
1

Description

Let's consider the following grammar.

```
file ::= root
root ::= ID [ 'or' ID ] ';'
```

And the following input.

```
id bad
```

If I follow [the tutorial](https://plugins.jetbrains.com/docs/intellij/grammar-and-parser.html), I get something like the following error message.

```
MyTokenType.; or MyTokenType.or expected, got 'bad'
```

I think this is strange to read. In contrast to the tutorial, most plugins I looked at don't override `toString()`. As a result, we get the following error message.

```
';' or or expected, got 'bad'
```

This would look much better if the keyword `or` had been quoted. `GeneratedParserUtilBase.ErrorState.appendExpected(…)` does not quote `or` because it starts like a valid Java identifier (which seems a little arbitrary to me).

I'm currently not aware of any possibility to put `or` in quotation marks? Do I miss something? If not, I would suggest extending the API to allow to override the text used for a token in error messages. As a positive side effect, this would also allow to decouple the error message from the token name in the PSI structure (where the prefix from the tutorial is actually helpful). What do you think about that?

EDIT: I already thought about prefixing keywords with `"\u2060"` (Word Joiner) in `toString()` as a workaround. However, I am not sure if this is appropriate or might cause other problems.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with GeneratedParserUtilBase.ErrorState.appendExpected(…), then review the grammar and error-message examples in this issue. Determine how token text is selected and what API shape could let grammar authors customize it while preserving readable quoted keywords. Done means the expected error output can distinguish the keyword `or` without relying on a PSI token-name prefix.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers, devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.