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

Syntax highlighting broken for more complicated string types (string with escaping) #797

Closed
goto40 opened this issue Nov 29, 2022 · 4 comments
Labels
bug Something isn't working highlighting Related to syntax highlighting
Milestone

Comments

@goto40
Copy link
Contributor

goto40 commented Nov 29, 2022

Langium version: 0.5.0
Package name: langium-cli

Steps To Reproduce

  1. create hello-world example (yo langium)
  2. change grammar to
grammar HelloWorld

entry Model:
    (persons+=Person | greetings+=Greeting)*;

Person:
    'person' name=STRING;

Greeting:
    'Hello' person=[Person:STRING] '!';

hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;
terminal INT returns number: /[0-9]+/;
terminal STRING: /"(\\"|[^"])*"|'(\\'|[^'])*'/;

hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
hidden terminal SL_COMMENT: /\/\/[^\n\r]*/;
  1. Start vscode extension and enter a model
person "Pierre with \" inside"
Hello "Pierre with \" inside"!

Link to code example: (see above)
The STRING definition is the crucial point...

The current behavior

Scoping resolution works as expected (misspell the name and you get an error as expected), but Syntax highlighting is broken (after the first string everything formatted as string).

The expected behavior

No broken highlighting.

@goto40 goto40 added the bug Something isn't working label Nov 29, 2022
@luan-xiaokun
Copy link
Contributor

It seems that this is because the textmate file (the json file in the syntaxes folder) is not correctly generated. The terminal rule STRING generates the following

    {
      "name": "string.quoted.double.langium-debug",
      "begin": "\"",
      "end": "\\\\\"[^\"]\""
    },
    {
      "name": "string.quoted.single.langium-debug",
      "begin": "'",
      "end": "\\\\'[^']'"
    }

Basically, it means that it will take anything between a starting " and an ending \"x" as a string and highlight it as a string, where x is anything that is not ". Clearly there is a missing alternative operator | in the generated textmate file.

@msujew
Copy link
Member

msujew commented Nov 30, 2022

Note that the generated syntax highlighting file is just a helper to get you started with writing/extending the textMate grammar. The heuristics used in the generator to figure out the highlighting rules are fairly simple and are not intended to be complete. We accept improvements to the code though. So if you can figure out what's wrong there, any contributions are welcome.

@msujew msujew added the highlighting Related to syntax highlighting label Nov 30, 2022
@luan-xiaokun
Copy link
Contributor

luan-xiaokun commented Nov 30, 2022

String and comment are using the same regex visitor. Since the textmate generator is doing a good job for multiline comments, we can modify the string regex to make it look like a comment regex, such as "(\\"|[^"])*?" (note the non-greedy option), this can fix the issue, though I haven't figured out how the visitor does it. (Oops, not working actually)

@msujew
Copy link
Member

msujew commented Feb 2, 2023

This has been fixed with #888.

@msujew msujew closed this as completed Feb 2, 2023
@msujew msujew added this to the v1.1.0 milestone Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working highlighting Related to syntax highlighting
Projects
None yet
Development

No branches or pull requests

3 participants