[Pas2js] Multiline string and INCLUDESTRING directive

Michael Van Canneyt michael at freepascal.org
Wed Dec 9 02:33:44 CET 2020


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.



More information about the Pas2js mailing list