Recent

Author Topic: Spaces are allowed here ?  (Read 2893 times)

Paolo

  • Hero Member
  • *****
  • Posts: 551
Spaces are allowed here ?
« on: January 19, 2025, 12:04:39 am »
Hello,

this code compiles and works. Please note the space character around dots and after "add", is this behaviour correct ?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ListBox1 . Items . Add ('x');
  4. end;
  5.  

Fibonacci

  • Hero Member
  • *****
  • Posts: 650
  • Internal Error Hunter
Re: Spaces are allowed here ?
« Reply #1 on: January 19, 2025, 12:06:20 am »
Yes, any white characters

TRon

  • Hero Member
  • *****
  • Posts: 3979
Re: Spaces are allowed here ?
« Reply #2 on: January 19, 2025, 12:09:10 am »
Yes, any white characters
Please elaborate on the source for that answer if possible.

From a technical point of view it is wrong.
I do not have to remember anything anymore thanks to total-recall.

Fibonacci

  • Hero Member
  • *****
  • Posts: 650
  • Internal Error Hunter
Re: Spaces are allowed here ?
« Reply #3 on: January 19, 2025, 12:14:23 am »
From a technical point of view it is wrong.

Why? Sometimes I use it like this:

Code: Pascal  [Select][+][-]
  1. something
  2. .add(0)
  3. .add(1)
  4. .add(2)
  5. .execute;

If new lines are allowed, then why spaces or any other white character should not.

TRon

  • Hero Member
  • *****
  • Posts: 3979
Re: Spaces are allowed here ?
« Reply #4 on: January 19, 2025, 12:19:10 am »
If new lines are allowed, then why spaces or any other white character should not.
Because a (white) space is not valid for an identifier. Ofc as can been seen the compiler simply ignores them but that still does not make it right. That is why I asked for the source. If it isn't documented it is undefined behaviour and as such should not be used (hence me asking)  :)
I do not have to remember anything anymore thanks to total-recall.

Fibonacci

  • Hero Member
  • *****
  • Posts: 650
  • Internal Error Hunter
Re: Spaces are allowed here ?
« Reply #5 on: January 19, 2025, 12:20:20 am »
That is why I asked for the source.

Experience 8)

TRon

  • Hero Member
  • *****
  • Posts: 3979
Re: Spaces are allowed here ?
« Reply #6 on: January 19, 2025, 12:26:03 am »
I do not have to remember anything anymore thanks to total-recall.

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Spaces are allowed here ?
« Reply #7 on: January 19, 2025, 12:49:59 am »
That behavior isn't usually explicitly documented but it is a consequence of the tokenizing and production definitions.  IOW, extremely likely that all Pascal compilers (that support selection using a ".") support that construction.

It's not just whitespace but any "null token", for instance:
Code: Pascal  [Select][+][-]
  1. type
  2.   ARecord = record
  3.     Field1    : integer;
  4.   end;
  5.  
  6. var
  7.   v : ARecord;
  8.  
  9. begin                        
  10.   v { a comment before the field selector } . { another comment } Field1 := { comment } 1 { comment }; { comment }
  11. end.
  12.  
You can have block comments where whitespace is allowed.  The same thing can be done with other constructions, e.g., "array1 { comment } [ { comment } 1 { comment } ] := ...".  IOW, any token that is a delimiter is also a separator therefore a { comment } can usually (probably always} precede or follow it.

It's documented in the way Pascal is tokenized and by the structure common to all its productions.  This is part of what makes Pascal a free form language (unlike, for instance, Python).  Same thing can be done in C/C++ and other free form languages, again for the same reason.
« Last Edit: January 19, 2025, 12:52:59 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

TRon

  • Hero Member
  • *****
  • Posts: 3979
Re: Spaces are allowed here ?
« Reply #8 on: January 19, 2025, 12:55:16 am »
Thank you for the elaboration 440bx.

I have located my line of thought leading up to my response. I thought it was forbidden but I mixed that up with the Delphi style-guide in which it is not recommended to add white-space(s) around dots.
I do not have to remember anything anymore thanks to total-recall.

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Spaces are allowed here ?
« Reply #9 on: January 19, 2025, 12:59:39 am »
Thank you for the elaboration 440bx.
You're welcome TRon, my pleasure.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

n7800

  • Full Member
  • ***
  • Posts: 235
Re: Spaces are allowed here ?
« Reply #10 on: January 19, 2025, 07:33:51 am »
Just as you put spaces at the beginning of a line for indentation, they can also be between identifiers/operators, since they are not part of it:

Code: Pascal  [Select][+][-]
  1.  
  2. // different number of spaces between operators
  3. a:=b+c-d;
  4. a := b + c - d;
  5.  
  6. // connecting short operators on one line
  7. if true then
  8.   exit;
  9. if true then exit;
  10.  
  11. // splitting a long operator into different lines
  12. if Function1(Variable1, Variable2, Variable3,
  13.   Variable4, Variable5, Variable6) = Variable7
  14. then
  15.   exit;
  16.  

n7800

  • Full Member
  • ***
  • Posts: 235
Re: Spaces are allowed here ?
« Reply #11 on: January 19, 2025, 07:37:23 am »
In practice, this is often used for "horizontal alignment" of similar lines:

Code: Pascal  [Select][+][-]
  1. // without alignment
  2. test.one:='value1';
  3. test.two:='value2';
  4. test.three:='value3';
  5. test.four:='value4';
  6.  
  7. // with alignment
  8. test.one   := 'value1';
  9. test.two   := 'value2';
  10. test.three := 'value3';
  11. test.four  := 'value4';
  12.  

This usually makes it easier to read and edit, if you don't overdo it.

n7800

  • Full Member
  • ***
  • Posts: 235
Re: Spaces are allowed here ?
« Reply #12 on: January 19, 2025, 07:38:02 am »
In general, if you are so surprised by the possibility of adding spaces, then the possibility of their absence will stun you:

https://gitlab.com/freepascal.org/fpc/source/-/issues/40967

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Spaces are allowed here ?
« Reply #13 on: January 19, 2025, 08:33:52 am »
In general, if you are so surprised by the possibility of adding spaces, then the possibility of their absence will stun you:

https://gitlab.com/freepascal.org/fpc/source/-/issues/40967
Yes, another fun one (among a large number of them) is:
Code: Pascal  [Select][+][-]
  1.   for i:=1downto-2do;
  2.  
Delphi accepts the above and FPC accepts the above and also
Code: Pascal  [Select][+][-]
  1.   for&i:=1downto-2do;

It would be fairly easy to modify the scanner to require whitespace after some tokens but, it could be a bit annoying.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8221
Re: Spaces are allowed here ?
« Reply #14 on: January 19, 2025, 10:10:09 am »
It would be fairly easy to modify the scanner to require whitespace after some tokens but, it could be a bit annoying.

Things get messy, particularly with operators such as mod and div positioned adjacent to a variable (i.e. no interposed whitespace, parenthesis etc.) and with & indicating an octal number.

Code: Pascal  [Select][+][-]
  1. if i&mod&7 = 0 then
  2. ...
  3.  

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018