sonar-delphi icon indicating copy to clipboard operation
sonar-delphi copied to clipboard

Parsing error on caret-escaped special characters

Open zaneduffield opened this issue 1 year ago • 3 comments

Prerequisites

  • [X] This bug is in SonarDelphi, not SonarQube or my Delphi code.
  • [X] This bug has not already been reported.

SonarDelphi version

1.0.0

SonarQube version

No response

Issue description

An undocumented Delphi feature (carried forward from TurboPascal) are escaped 'control characters'.

For example

const
  CtrlC = ^C;

While it may be intended for use with visible characters, it's valid to escape any ~single-byte~ ascii character with this technique.

const
  Space = ^ ;
  Comment = ^{;
  Plus = ^+;

The relevant section of the grammar in SonarDelphi currently has a few problems

escapedCharacter             : TkCharacterEscapeCode
                             | '^' (TkIdentifier | TkIntNumber | TkAnyChar) -> ^({changeTokenType(TkEscapedCharacter)})
  1. It's not valid to accept any identifier or integer after the ^; only one (~single-byte~ ascii) character may be escaped
  2. Whitespace and the comment-starting { are handled by the 'hidden' channel and never make it to this section

For more info about the caret-escaped characters, see:

  1. https://stackoverflow.com/a/4916503/21058101
  2. http://www.delphibasics.co.uk/RTL.php?Name=Char

Steps to reproduce

Run SonarDelphi on the following program

program Test;

const A = ^ '';

begin
end.

observe the error

no viable alternative at input 'A'

Minimal Delphi code exhibiting the issue

No response

zaneduffield avatar Nov 27 '23 22:11 zaneduffield