integrated-application-development / integrated-application-development/sonar-delphi

Parsing error on caret-escaped special characters

Open
#111 3 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Java
Stars
159
Forks
31
Avg merge
5d 4h
Merged PRs (30d)
4

Description

### 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
```delphi
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.
```delphi
const
Space = ^ ;
Comment = ^{;
Plus = ^+;
```

The [relevant section](https://github.com/integrated-application-development/sonar-delphi/blob/eccc14d5243e48b3b55828e99f809fb5e6e8ee1e/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g#L650C9-L650C9) 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

```delphi
program Test;

const A = ^ '';

begin
end.
```

observe the error
> no viable alternative at input 'A'

### Minimal Delphi code exhibiting the issue

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.