Skip to content

Improve error message for trailing dollar before closing string delimiter #1606

@mhasel

Description

@mhasel

When a string literal ends with a bare $ immediately before the closing delimiter (e.g., 'hello$'), the compiler currently emits an E007 at the lexer level. This means this invalid escape is currently unreachable at the parser stage and therefore it is not possible to emit a meaningful error message to the user.

Current behaviour

PROGRAM foo
VAR
    x := 'hello$';   (* should be reported as an invalid escape at the parser level, trailing $ is an incomplete escape *)
END_VAR
END_PROGRAM

The lexer regex $. (dollar + any character) treats $ as an atomic unit: the quote-escape. This causes the lexer to interpret the closing ' as the second character of an escape sequence, leaving the string unterminated. As a result the compiler emits a cascading unexpected token error. It is not trivial to see what is actually causing this error.

error[E007]: Unexpected token: expected Literal but found "hello$";
          2 │+        END_PROGRAM
          3 │+    
          4 │+  ┌─ <internal>:4:18
          5 │+  │  
          6 │+4 │               s := "hello$";
          7 │+  │ ╭──────────────────^
          8 │+5 │ │         END_PROGRAM
          9 │+6 │ │     
         10 │+  │ ╰────^ Unexpected token: expected Literal but found "hello$";
         11 │+        END_PROGRAM
         12 │+    
         13 │+
         14 │+error[E007]: Unexpected token: expected KeywordSemicolon but found ''
         15 │+  ┌─ <internal>:6:5
         16 │+  │
         17 │+6 │     
         18 │+  │     ^ Unexpected token: expected KeywordSemicolon but found ''
         19 │+
         20 │+error[E007]: Unexpected token: expected KeywordEndProgram but found ''
         21 │+  ┌─ <internal>:6:5
         22 │+  │
         23 │+6 │     
         24 │+  │     ^ Unexpected token: expected KeywordEndProgram but found ''

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlow-priorityrefactorinternal change, cleanup, code-style-improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions