Skip to content

FPC compiles programs with "potential" syntax violation

Compiler version: trunk (4ef30edb)

Summary

Despite the lack of a space between the number and the keyword, the following program compiles and works correctly:

var
  i:integer;
begin
  if 2=2then // no space: '2then'
    for i:=1to 2do // no space: '1to' and '2do'
      writeln(i);
end.

It is possible to escape keywords in identifiers with an ampersand. It turns out that it can also be placed immediately after the keyword, without a space:

var
  i:integer;
  n:integer=2;
begin
  if 2=2then
    for&i:=1to&n-0do // no space: 'for&i' and 'to&n'
      writeln(i);
end.

And if you make the number hexadecimal by adding $, then these lines will not have a single space at all:

  if$2=2then // no space: 'if$2'
    for&i:=1to&n-0do
      writeln(i);

The compilation will fail only if you put it before do, since the compiler will think that d is part of a hexadecimal number:

  if$2=2then
    for&i:=1to&n-$0do // error: '$0d' and 'o'
      writeln(i);

Just for completeness, I'll also give an example with brackets, although this is unlikely to surprise anyone:

var
  i:integer;
  l:array[0..0]of&integer=(2); // no space after/before brackets
begin
  if$2=2then
    for&i:=1to&l[0]do // no space after/before brackets
      writeln(i);
end.

It's strange that the syntax highlighting in Lazarus and on GitLab correctly "separates" all tokens.


It's actually hard to say what bad can happen because of this, and whether at least some cases can be called a bug. I'm not insisting on anything, and I don't mind if this is closed as not a bug/wont fix. But at first glance, it seems like a syntax violation, without any benefit.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information