eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
663 stars 61 forks source link

Got "unterminated string literal" in generated grammar.ts error when grammar contains "${" #1513

Closed codehz closed 4 weeks ago

codehz commented 1 month ago

Langium version: 3.0.0 Package name: langium-cli

Steps To Reproduce

step:

  1. init project, paste those code
  2. npm run langium:generate
  3. check the generated grammar.ts

Link to code example:

grammar HelloWorld

entry Model:
   persons+=Person*;

Person:
    'person' name=TemplateLiteral;

hidden terminal WS: /\s+/;

hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
hidden terminal SL_COMMENT: /\/\/[^\n\r]*/;

TemplateLiteral:
    content+=TEMPLATE_LITERAL_FULL |
    // Or template literal parts with expressions in between
    (
        content+=TEMPLATE_LITERAL_START 
        content+="Expr"
        (
            content+=TEMPLATE_LITERAL_MIDDLE
            content+="Expr"
        )*
        content+=TEMPLATE_LITERAL_END
    );

terminal TEMPLATE_LITERAL_FULL:
    '`' IN_TEMPLATE_LITERAL* '`';
terminal TEMPLATE_LITERAL_START:
    '`' IN_TEMPLATE_LITERAL* '${';
terminal TEMPLATE_LITERAL_MIDDLE:
    '}' IN_TEMPLATE_LITERAL* '${';
terminal TEMPLATE_LITERAL_END:
    '}' IN_TEMPLATE_LITERAL* '`';
terminal fragment IN_TEMPLATE_LITERAL:
    /[^$`]|\\\$|\\`/;

The current behavior

image

The expected behavior

no error in output

msujew commented 1 month ago

@codehz Thanks for the report. As a quick workaround you can adjust the mode in your langium-config.json file:

{
  ...
  "mode": "production"
}

Note that this mode doesn't do a lot except for minifying the generated grammar, thereby circumventing the issue.