SystemParadox / triplet

triple-quoted string transformer
1 stars 0 forks source link

String ending with double-quotes #2

Closed tsouza closed 7 years ago

tsouza commented 7 years ago

Apparently it fails to parse when string ends with double-quotes. Trying to parse a file with:

"""test""""

Fails with:

Error: test:1:13: error: Unterminated string
    at throwError (/usr/local/lib/node_modules/triplet/index.js:36:21)
    at scanStringLiteral (/usr/local/lib/node_modules/triplet/index.js:264:13)
    at parseBody (/usr/local/lib/node_modules/triplet/index.js:577:27)
    at parse (/usr/local/lib/node_modules/triplet/index.js:660:12)
    at transform (/usr/local/lib/node_modules/triplet/index.js:712:15)
    at DestroyableTransform._flush (/usr/local/lib/node_modules/triplet/index.js:768:26)
    at DestroyableTransform.<anonymous> (/usr/local/lib/node_modules/triplet/node_modules/readable-stream/lib/_stream_transform.js:138:49)
    at Object.onceWrapper (events.js:314:30)
    at emitNone (events.js:105:13)
    at DestroyableTransform.emit (events.js:207:7)
SystemParadox commented 7 years ago

That's expected behaviour.

What you actually have is the triple-quoted string """test""", followed by a lone double-quote character.

It's equivalent to this in normal JS:

"test""

Which would give a very similar error.

Thanks.

tsouza commented 7 years ago

I am sorry, but I need to disagree with you. I don't think this is expected behavior because:

  1. It can successfully transform: """test " test""" into "test \" test".
  2. It can successfully transform: """"test""" into "\"test test"

So why a double-quote in the end should be considered different?

Clearly there is bug. Either the above should be or it should be able to parse a double-quote in the end.

I would expect it to transform: """test"""" into "test\"" or fail with the above input.

SystemParadox commented 7 years ago

It's different because triple quotes are matched greedily. This behaviour is also consistent with Python's triple-quoted strings, which will give a similar syntax error.

If you need a double-quote character at the end you should probably use triple-single-quotes. I would purposely do this anyway because it's pretty hard to see the difference between three double quotes and four!

Also, I have never found a suitable use for single-line triple-quoted strings. I always put them on multiple lines for readability- in which case you can safely end with a double-quote character like this:

var x = """
    foo "bar"
""";

It should be noted that the parser is specifically designed to handle whitespace properly, so you can safely use a more-readable syntax as above without it making a mess of the output.