Recent

Author Topic: If you've ever wanted to be able to use multi-line strings in FPC...  (Read 5908 times)

Akira1364

  • Hero Member
  • *****
  • Posts: 521
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #45 on: July 12, 2019, 11:48:17 pm »
I would gladly use something like :
Code: Pascal  [Select]
  1.     const
  2.       Str2 = `SELECT o.*, C.Company
  3.               from Orders O
  4.               join Customer C
  5.                 on o.CustNo=C.ID
  6.               where
  7.                 O.saledate=DATE '2001.03.20'`;
  8. or
  9.  
  10.     const
  11.       Str2 =
  12.         `SELECT o.*, C.Company
  13.          from Orders O
  14.          join Customer C
  15.            on o.CustNo=C.ID
  16.          where
  17.            O.saledate=DATE '2001.03.20'`;
  18.  
would be interpreted by the scanner as  :
Code: Pascal  [Select]
  1.       Str2 = 'SELECT o.*, C.Company' + LineEnding +
  2.              'from Orders O' + LineEnding +
  3.              'join Customer C' + LineEnding +
  4.              '  on o.CustNo=C.ID' + LineEnding +
  5.              'where' + LineEnding +
  6.              '  O.saledate=DATE ''2001.03.20''';
  7.  

That works just as you've written it already, BTW:

Code: Pascal  [Select]
  1. program SQL;
  2.  
  3. {$modeswitch MultiLineStrings}
  4.  
  5. {$MultiLineStringTrimLeft 10}
  6.  
  7. const
  8.   Str1 = `SELECT o.*, C.Company
  9.           from Orders O
  10.           join Customer C
  11.             on o.CustNo=C.ID
  12.           where
  13.             O.saledate=DATE '2001.03.20'`;
  14.  
  15. {$MultiLineStringTrimLeft 5}
  16.  
  17. const
  18.   Str2 =
  19.     `SELECT o.*, C.Company
  20.      from Orders O
  21.      join Customer C
  22.        on o.CustNo=C.ID
  23.      where
  24.        O.saledate=DATE '2001.03.20'`;
  25.  
  26. begin
  27.   WriteLn(Str1);
  28.   WriteLn(Str2);
  29. end.

I like the idea, didn't read all the opinions btw.

Also I like the backtick usage, is like is done in JavaScript for example.

Keep going and get it bug free in time.  :)

Thanks! I intend to. Don't think we're too far out. I should have the second bug engkin reported fixed later tonight.
« Last Edit: July 12, 2019, 11:55:00 pm by Akira1364 »

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #46 on: July 12, 2019, 11:49:46 pm »
Multi lines is a great idea for the compiler to read but I want it to assemble exactly what it see's …
Where did you get this idea from? This feature does *not* affect any strings in your old source code, and will not change strings in your new source code. Moreover, it is turned off by default. To turn it on, you need to use {$modeswitch MultiLineStrings}. Even then, it will *not* change the strings that you already have.

Your misunderstanding probably comes from MultiLineStringLineEnding. As is today, it leaves the line endings the way they are. It is default value is {$MultiLineStringLineEnding RAW} which does not make any changes to line endings. The other possibility of your misunderstanding comes from another switch MultiLineStringTrimLeft, which, again, by default it does nothing. You need to use either switch to affect multi-line strings, and please notice they only affect strings enclosed in the newly introduced character *backtick* ` usually found on the same key with the tilde mark ~.

As you can see, it will not affect the usual Pascal strings enclosed in between two apostrophe characters:

'**This will not get affected**'  <--- your usual string

It only affects the new string enclosed in between two backticks:

`This might get affected`  <-- the new string


Let me zoom in --> Apostrophe '  Backtick `
« Last Edit: July 14, 2019, 03:08:55 am by engkin »

kupferstecher

  • Sr. Member
  • ****
  • Posts: 287
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #47 on: July 12, 2019, 11:50:57 pm »
Typing 's and +es each line is really no fun. And it's not only about typing, the (necessary) formatting disturbes my thinking.
BUT, in the cases a singleline is not enough for me, I often don't want a line break to be inserted.
So this new syntax wouldn't cover my cases.

Existing:
Code: Pascal  [Select]
  1. const
  2.   HelpText = 'This is some example text like it may look like in a help text. There is no'
  3.            + ' content here, you don''t need to go on reading. Just a string that doesn''t'
  4.            + ' fit into one line.'+#13+#10
  5.            + 'Now this is a new line.';

Akira's syntax
Code: Pascal  [Select]
  1. const
  2.   HelpText = `I''m not sure if it is even possible to have such a long line. This is some example text like it may look like in a help text. There is no content here, you don''t need to go on reading. Just a string that doesn''t fit into one line.
  3.               Now this is a new line.`;

Something I'd prefer (## for new line)
Code: Pascal  [Select]
  1. const
  2.   HelpText = 'About the trims I''m not sure. I could live with it to bring it to the front
  3.              of the line, as such strings are normally as constants in the interface section
  4.              and not distributed throughout the code.##
  5.              Now this is a new line';

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #48 on: July 12, 2019, 11:59:02 pm »
@kupferstecher,

Akira's syntax
Code: Pascal  [Select]
  1. const
  2.   HelpText = `I''m not sure if it is even possible to have such a long line. This is some example text like it may look like in a help text. There is no content here, you don''t need to go on reading. Just a string that doesn''t fit into one line.
  3.               Now this is a new line.`;
You don't need to escape the apostrophe if used in between two backticks. But you do need to escape the backtick if you need it in this text:
Code: Pascal  [Select]
  1.   HelpText = `I'm not escaping this apostrophe ' but I have to escape this back tick```

Akira1364

  • Hero Member
  • *****
  • Posts: 521
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #49 on: July 13, 2019, 12:01:56 am »
BUT, in the cases a singleline is not enough for me, I often don't want a line break to be inserted.
So this new syntax wouldn't cover my cases.

Several people have mentioned wanting a "no newlines at all" option for {$MultiLineStringLineEnding} (which would mean whatever is written would be interpreted as one long horizontal string), which would not be difficult to add, and so I may very well do so. The option would most likely be called NONE, I think.

lucamar

  • Hero Member
  • *****
  • Posts: 1943
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #50 on: July 13, 2019, 12:36:35 am »
Several people have mentioned wanting a "no newlines at all" option for {$MultiLineStringLineEnding} (which would mean whatever is written would be interpreted as one long horizontal string), which would not be difficult to add, and so I may very well do so. The option would most likely be called NONE, I think.

Doesn't that quite defeates the whole purpose of this feature? An it can already be done very easily:
Code: Pascal  [Select]
  1. MyString = 'This, of course, will appear all' +
  2.            ' in the same line (provided your' +
  3.            ' terminal is wide enaough)';

Do people really find that sintax so unuseable that they need another?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 1051
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #51 on: July 13, 2019, 01:06:58 am »
This is pure curiosity...

why the backtick (which is slightly awkward to reach on the keyboard) and not simply a double quote ?  (which is really easy to reach)

The only reason I can see for not choosing the double quote is that it could potentially be a source of confusion for programmers migrating from another language.



using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

GAN

  • Full Member
  • ***
  • Posts: 220
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #52 on: July 13, 2019, 01:07:23 am »

Doesn't that quite defeates the whole purpose of this feature? An it can already be done very easily:
Code: Pascal  [Select]
  1. MyString = 'This, of course, will appear all' +
  2.            ' in the same line (provided your' +
  3.            ' terminal is wide enaough)';

Do people really find that sintax so unuseable that they need another?

No.
Lazarus 1.8.4 FPC 3.0.4 Linux Mint Mate 17.2 x86_64 GTK-2
Zeos 7.1.3 - Sqlite 3.8.2

Foro Lazarus en español http://forum.lazarus.freepascal.org/index.php/board,73.0.html

lainz

  • Hero Member
  • *****
  • Posts: 3173
    • Lainz
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #53 on: July 13, 2019, 01:13:16 am »
This is pure curiosity...

why the backtick (which is slightly awkward to reach on the keyboard) and not simply a double quote ?  (which is really easy to reach)

The only reason I can see for not choosing the double quote is that it could potentially be a source of confusion for programmers migrating from another language.

````````````

I just press a single key both on windows and mac, english and spanish keyboard as well.

but to double quote i need to use shift """"""""""""""""  :D

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #54 on: July 13, 2019, 01:21:11 am »
This is pure curiosity...

why the backtick (which is slightly awkward to reach on the keyboard) and not simply a double quote ?  (which is really easy to reach)

Michael proposed it.

Probably escaping double quote would be more frequent if it were to be used instead of backtick:
Code: Pascal  [Select]
  1.   Sample = """It is unfair"" he said.";
« Last Edit: July 13, 2019, 01:30:46 am by engkin »

lucamar

  • Hero Member
  • *****
  • Posts: 1943
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #55 on: July 13, 2019, 01:22:43 am »
I just press a single key both on windows and mac, english and spanish keyboard as well.

Huh?  Which "Spanish" keyboards? On all of mine throughout the ages the "backtick" (acute? tilde) has always been a dead-key so it has to be pressed and then you have to press the space-bar (system-dependent, this) to obtain the tilde by itself.

Presumably so we poor Spaniards can write French with some easy, I guess :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

lainz

  • Hero Member
  • *****
  • Posts: 3173
    • Lainz
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #56 on: July 13, 2019, 01:40:24 am »
Chinese assembled Spanish keyboard  :)

Really I press the backtick always I want to add a tilde, I'm used to American Spanish keyboard not international Spanish keyboard (I currently have this one).

440bx

  • Hero Member
  • *****
  • Posts: 1051
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #57 on: July 13, 2019, 01:43:57 am »
Michael proposed it.
<snip>
Probably escaping double quote would be more frequent if it were to be used instead of backtick:
Code: Pascal  [Select]
  1.   Sample = """It is unfair"" he said.";
Thanks.  The rational for it, know that I know it, seems quite reasonable.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Akira1364

  • Hero Member
  • *****
  • Posts: 521
Re: If you've ever wanted to be able to use multi-line strings in FPC...
« Reply #58 on: July 13, 2019, 04:34:37 am »
Ok! I've now fixed the second bug engkin reported. The changes have been pushed to my github fork-branch of the compiler, and once again I've uploaded a new pair of patch files on my bugtracker issue for the feature (including engkin's example of the second bug, as a test file to ensure the problem stays fixed, with the name "tmultilinestring24.pp".)

Again, if you're someone who doesn't mind building FPC from source, please feel to free to apply the patch, test the feature, and report any other bugs you might find! (Which of course is hopefully few, or even better none, haha.)

Extra bonus: attributes were merged into trunk FPC today. Of course, once I noticed this, I right away wanted to see how they work with multi-line strings. Turns out: just fine! And they look right at home together in (my patched) Lazarus, if you ask me (see the attached image.)

Doesn't that quite defeates the whole purpose of this feature?

Not necessarily. I could see certain things being much easier to format without being separated with single-quotes and plus signs, while not specifically requiring newlines in the output.

Also, again, it's trivial enough to add that I see no reason not to.
« Last Edit: July 13, 2019, 03:02:50 pm by Akira1364 »

Kays

  • Full Member
  • ***
  • Posts: 170
  • Whasup!?
    • KaiBurghardt.de
Michael proposed it.
<snip>
Probably escaping double quote would be more frequent if it were to be used instead of backtick:
Code: Pascal  [Select]
  1.   Sample = """It is unfair"" he said.";
Thanks.  The rational for it, know that I know it, seems quite reasonable.
Also, " is already known in the (Pascal) programming universe as a string delimiter of strings containing escaped characters. (E. g. the GNU Pascal compiler allowed that.)

I still don’t understand, why we just don’t introduce a new compiler switch, that’ll eliminate the
Code: Text  [Select]
  1. Fatal: String exceeds line
error. No need for new syntax and achieved our goal with minimal efforts.
Yours Sincerely
Kai Burghardt