Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2020-12

Multiline string and INCLUDESTRING directive

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Van Canneyt

Hello,

I have implemented support for multiline strings and the $INCLUDESTRING or INCLUDESTRINGFILE directive.

I have used the same approach as the patches for the same features in Free Pascal: These are currently only in the bugtracker, but are scheduled to be applied in the hopefully near future...

How does it work ?

Multiline strings must be enabled with a modeswitch:

{$MODESWITCH MULTILINESTRINGS}

When enabled, you can use multiline strings.

The backtick indicates a multi-line string:

Const
SMyMulti = `My
string
that
spans multiple lines`;

Will create a multiple-line string.

To include a backtick in the string itself, duplicate it: Const
SMyMulti = `My
string
that ``is quoted`` and
spans multiple lines`;

#12 etc. will work as for normal strings.

The line endings in the literal string are controlled by the MULTILINESTRINGLINEENDING directive:

{$MULTILINESTRINGLINEENDING CR|LF|CRLF|SOURCE|PLATFORM}

The value SOURCE means the line ending of the source file is taken, the other values enforce a particular line ending, where PLATFORM is the line ending of the compiler platform. CR, LF or CRLF are self-explanatory.

'Platform' is the default.

The multiline string can also have initial spaces stripped; This is controlled by the MULTILINESTRINGTRIMLEFT directive:

{$MULTILINESTRINGTRIMLEFT N|AUTO|ALL}

N is an integer: At most N spaces will be stripped. AUTO means the position of the initial backtick is used to determine how many spaces will be stripped..
ALL means all spaces will be stripped.

{$MULTILINESTRINGTRIMLEFT AUTO}
Const
  S = `SELECT
         *
       FROM
         MyTable`;

is equivalent to

{$MULTILINESTRINGTRIMLEFT 0}
Const
  S = `SELECT
   *
FROM
   MyTable`;

The $INCLUDESTRING directive will include the file as a literal string.

This means that:

Const
   A = {$INCLUDESTRING myinclude.inc};

Will define a constant A that has as a value the contents of myinclude.inc {$INCLUDESTRINGFILE myinclude.inc)
has the same effect.

The line endings in the constant A are determined also from the MULTILINESTRINGLINEENDING directive: the include file is read line by line and lines will be separated using the same mechanism as for multiline strings.

The include file is searched using the include file path, just as for $I.

These mechanisms have been implemented in the scanner/parser for pas2js in trunk. The 2.0 version will not have this.

Some details may change when FPC gets the support for these features (for compatibility) but they will probably be minor.

Enjoy,

Michael.

Reply by email

Michael Van Canneyt
to heliosroots
heliosroots wrote
Very good, when will it be available on the trunk?

Sorry, I hadn't noticed the compiler filters out allowed switches...

I enabled it:

home:~/pas2js> pas2js -Tnodejs -Jirtl.js -Jc a.pp -l Pas2JS Compiler version 2.1.1 [2020/12/09] for Linux x86_64 Copyright (c) 2019 Free Pascal team.
Info: 1157 lines in 2 files compiled, 0.0 secs
home:~/pas2js> nodejs a.js
1
2
3
4
home:~/pas2js> cat a.pp
{$modeswitch multilinestrings}

Const
a = `1
2
3
4`;

begin
   writeln(a);
end.

Michael.

Reply by email

Ben Grasset
to Michael Van Canneyt: I have implemented support for multiline strings and the $INCLUDESTRING or…

Very cool! And unexpected. That reminds me, I think I actually need to upload a fresh version of the FPC multiline strings patch to that bugtracker issue... had to fix some merge conflicts recently in my personal branch, so the currently uploaded patch probably is not properly applicable.

PM Michael Van Canneyt wrote 110 lines
Hello, I have implemented support for multiline strings and the $INCLUDESTRING or INCLUDESTRINGFILE directive. I have used the same approach as the patches for the same features in Free Pascal: These are currently only in the bugtracker, but are scheduled to be applied in the hopefully near future... How does it work ? Multiline strings must be enabled with a modeswitch: {$MODESWITCH MULTILINESTRINGS} When enabled, you can use multiline strings. The backtick indicates a multi-line string: Const SMyMulti = `My string that spans multiple lines`; Will create a multiple-line string. To include a backtick in the string itself, duplicate it: Const SMyMulti = `My string that ``is quoted`` and spans multiple lines`; #12 etc. will work as for normal strings. The line endings in the literal string are controlled by the MULTILINESTRINGLINEENDING directive: {$MULTILINESTRINGLINEENDING CR|LF|CRLF|SOURCE|PLATFORM} The value SOURCE means the line ending of the source file is taken, the other values enforce a particular line ending, where PLATFORM is the line ending of the compiler platform. CR, LF or CRLF are self-explanatory. 'Platform' is the default. The multiline string can also have initial spaces stripped; This is controlled by the MULTILINESTRINGTRIMLEFT directive: {$MULTILINESTRINGTRIMLEFT N|AUTO|ALL} N is an integer: At most N spaces will be stripped. AUTO means the position of the initial backtick is used to determine how many spaces will be stripped.. ALL means all spaces will be stripped. {$MULTILINESTRINGTRIMLEFT AUTO} Const S = `SELECT * FROM MyTable`; is equivalent to {$MULTILINESTRINGTRIMLEFT 0} Const S = `SELECT * FROM MyTable`; The $INCLUDESTRING directive will include the file as a literal string. This means that: Const A = {$INCLUDESTRING myinclude.inc}; Will define a constant A that has as a value the contents of myinclude.inc {$INCLUDESTRINGFILE myinclude.inc) has the same effect. The line endings in the constant A are determined also from the MULTILINESTRINGLINEENDING directive: the include file is read line by line and lines will be separated using the same mechanism as for multiline strings. The include file is searched using the include file path, just as for $I. These mechanisms have been implemented in the scanner/parser for pas2js in trunk. The 2.0 version will not have this. Some details may change when FPC gets the support for these features (for compatibility) but they will probably be minor. Enjoy, Michael. _______________________________________________ Pas2js maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20201212/cd1878cb/attachment.htm>

Reply by email

Michael Van Canneyt
to Ben Grasset
Ben Grasset wrote
Very cool! And unexpected. That reminds me, I think I actually need to upload a fresh version of the FPC multiline strings patch to that bugtracker issue... had to fix some merge conflicts recently in my personal branch, so the currently uploaded patch probably is not properly applicable.

I think Sven will be very happy if you do...

Michael.

Reply by email

Ben Grasset
to Michael Van Canneyt

I've just gone ahead and done so. Also updated the Github-based upload I have of the multiline strings branch, which is linked from that bugtracker issue too.

PM Michael Van Canneyt wrote 20 lines
On Sat, 12 Dec 2020, Ben Grasset wrote: > Very cool! And unexpected. That reminds me, I think I actually need to > upload a fresh version of the FPC multiline strings patch to that > bugtracker issue... had to fix some merge conflicts recently in my personal > branch, so the currently uploaded patch probably is not properly applicable. I think Sven will be very happy if you do... Michael. _______________________________________________ Pas2js maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20201212/aa6aed00/attachment.htm>

Reply by email