quasi-literal strawman

Mike Samuel mikesamuel at gmail.com
Mon Dec 14 16:04:23 PST 2009


2009/12/14 Ash Berlin <ash_js at firemirror.com>:
>
> On 14 Dec 2009, at 22:44, Mike Samuel wrote:
>
>> http://wiki.ecmascript.org/doku.php?id=strawman:quasis
>> is a strawman for a concrete syntax that enables string interpolation
>> as described in
>> http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation .
>>
>> So far it's just an uploaded HTML file but I plan to make it more wiki friendly.
>> If people want to edit it, let me know and I'll put out a wiki form pronto.
>
> First things first: the string interpolation strawman[1] is not string interpolation. It's LINQ (Language Integrated Query) or very similar, which dare I say it, is what E4X is (or attempts to be, spec bug not withstanding) Which isn't to say I don't think the idea has merit, it is just more than simply string interpolation. (When i say its LINQ - it feels very much like the query syntax front end for it.)

I'll look into LINQ.  I think it's more general purpose than E4X, but
I've never read the E4X spec so take that with a grain of salt.


> The pessimist in my would like to point out that you could (and people inevitably will) still say `UPDATE foo SET x=$x`. Unless i've missed something crucial this doesn't protect against that (as its not  a sql`UPDATE ...`. Also which 'sql' - there are many variants)

Please see the late binding arguments in
http://google-caja.googlecode.com/svn/changes/mikesamuel/string-interpolation-29-Jan-2008/trunk/src/js/com/google/caja/interp/index.html
for an explanation of why the sql prefix is unnecessary, and as you
describe, often damaging.


> backticks In perl, ruby and php is all the system execute command (which I suspect you know) but the "where it allows interpolation though with a more specific meaning than macro expansion" wording isn't quite correct -- it does more than just interpolattion.

Yes, you're right.  It interpolates first and then executes it as a
shell command in perl and PHP, and I believe ruby.

> I really like the idea of being able to do things like |var my regexp = re`(?i:\w+$foo\w+)`;|
>
> quasi-strawman.html says:
>>> It would be convenient for some DSL use cases to allow LineTerminators inside code, but it is unclear how this will interact with revision control systems that rewrite newlines on checkout
>
>
> If you want to use a certain form of a new line for what ever reason, it would probably be best to use \n. Line continuations are rather annoying to use in practice - if you want the literal to be split over multiple lines, and to have new lines in the source you end up ending every line with \n\ which is just ugly. Since we are already talking about new syntax, I would vote for allowing multi-line literals.

So to keep score:  (for multi-line 1, against: 0, unspecified, ~6B :)


> If you haven't yet read http://www.python.org/dev/peps/pep-3101/ (Advanced String Formatting) I suggest you do - its well worth a read and feels like a possible very javascripty solution.

I have not read it.  Thanks for the link.  It has a good summary of
alternate syntaxes and establishes a point partway between positional
and inline syntax.  It does include a bunch of format specifiers that
I think incompatible with DSL schemes.

> One thing I'm not seeing here is a way to interpolate one quasi literal inside another one. I'm guessing the intent is html`<h1>$x</h1>` would html escape the x variable (this wasn't actually explicitly stated anywhere that I read). But how do you say 'x is already html, don't escape it again?' I guess this comes down to how do you detect and interact with quasi-literals? Do they have a new typeof? What methods do the instances have? How exactly to you write new quasi tags/types?

Not quite.  The same secure string interpolation scheme paper covers this issue.



> I can understand not having printf style formatting codes, but how would you suggest you format a number to 2 decimal places?

`Avg. Rainfall: ${n.ToFixed(2)}cm`

> In summary:
> * Quasi-Literal or string interpolation strawman? whats what?

A general desugaring that supports a variety of content generation
schemes including the quasi-proposal and the secure string
interpolation proposals linked under references, and hopefully allow
experimentation with DSLs.

> * How do you interact with literals (in more detail)

I'm not sure I understand the question.

> * What quasi types would be built in?

I think HTML/CSS/JS should be built into a new version of DOM.  And a
SQL version might be built into a new version of HTML5 persistence.
I'm not sure that any need to be built into Ecmascript.

> * I would *really* like multiline literals. (akin to pythons """ operator or heredocs in perl and ruby)

That seems orthogonal to this proposal, though this strawman does try
to make sure that such changes won't change the meaning of programs
that use quasis but not python triple quoted strings or heredoc.


> * The metadata example - there is no other mention to metadata

The motivation there was to show that metadata is not something that
has to be explicitly handled in the proposal.
Different quasi functions can handle metadata however they like.


> * I'm not sure its worth allowing such complex expressions inside the ${} (brackets example)

DSLs become much more powerful when they can nest.

> * No printf/formatting at all might be missed

printf formatting can be built on top of this.

Imagine

sprintf`foo=%(foo)s, bar=%(bar)s % ${{ foo: 1, bar: 2 }}`

with

function sprintfQuasi(format_string) {
  format_string = format_string.replace(/\s+%\s+$/, '');
  return function formatter(substitution) {
    var i = 0;
    return format_string.replace(/%%|%\((\w+)\)([sd])|%([sd])/g,
function (_, name, fmt1, fmt2) {
      var value, fmt;
      if (fmt1) {
       fmt = fmt1, value = substitution[name];
      } else if (fmt2) {
       fmt = fmt2, value = substitution[i++];
      } else return '%';
      return fmt === 's' ? '' + value : +value;
    };
  };
}

for a quick and dirty approach.



> Thanks for fleshing this out in more detail - I've been meaning to write an email about the lack of detail in the string interpolation strawman for a couple of weeks.

> -ash
>
> [1]: http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list