develooper Front page | perl.perl5.porters | Postings from December 2021

Idea - string literals with syntax

Thread Next
From:
Branislav Zahradník
Date:
December 14, 2021 12:16
Subject:
Idea - string literals with syntax
Message ID:
CAB=rbOn69m7MWPBXK2oq+Uq9ujm1OfF1ptuSMnGG48Tc8yrHSw@mail.gmail.com
Motivation:
It's not uncommon to need a string (from perl point of view) with syntax,
eg:
- SQL statement / snippet
- Javascript snippet
- HTML/XML document / snippet
- piece of perl code as well

Behaviour:
At the beginning new syntax will behave like perl data (defaults to string,
similar to heredoc)
Later it's possible to add magic and allow it to be represented as an
object with mandatory stringification overload.

Benefit:
Development tools can recognize given syntax regardless of implementation
and offer
proper actions, eg:
- syntax highlighting
- symbol navigation
- critic / tidy

Similar work in other languages:
- inline php: <?php ?>
- jetbrains java annotation @Language

Obsoleted (partially or fully) CPAN modules
- Template::Toolkit
- parser libraries (Marpa, Parse::RecDescent, Parse::YAPP, ...)

Idea:
Introduce equivalent (or direct implementation) of SGML processing
implementation
Syntax token <? is not used yet.

Open questions:
- when treating inline data as PIcontent, there should be support for
escaping ?>
- should <? ?> by recognized as pair? (eg: <?perl my $foo = <?perl ... ?>
?>)
- when interpolating there should be support for filters (eg: like
Template::Toolkit)
- how to properly support out-of-pi content? (eg: builtin XML)? reusing
heredoc syntax somehow?
- builtin support - how to recognize between "syntax error tolerated" /
"syntax error not tolerated" ?

Examples:
- perl core tests or pluggable syntax modules can use <?perl?> to test
syntax
eval <?perl
   my sub foo () { }
?>

- (built in) SQL statements, similar behaviour to heredoc
$dbh->do (<?sql << '' ?>)
SELECT source, sum (payment) over (partition by source order by date) FROM
transactions

- (built-in) XML documents
my $doc = <?xml version="1.0" encoding="utf-8" <<~ 'EOXML' ?>
<root>
...
</root>
EOXML

Best regards,
Brano

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About