my_lua_string = [=[ This is a string delimited with long brackets; it contains other valid long brackets like [[this]] (or this: ]==]), but they are ignored because Lua looks for a long bracket that matches the opening delimiter. ]=]

Lua supports something I haven't seen in any other language: so-called 'long brackets', sometimes also referred to as 'long strings' or 'double brackets'. There is an infinite variety of valid long bracket pairs: [[my multiline string]], [=[...]=], [==[...]==], and so on. You can use them for multiline comments like so: --[[...]], --[=[...]=], etc. This means that any string literal can be created, or any code can be commented, without escaping the contents: you just choose a long bracket with a number of equals signs such that the ending delimiter doesn't occur inside the string.

Are there any other languages with infinitely extensible string delimiters? I know about Perl's q-style, but that is AFAIK limited to single characters: q#...#, qx...x, etc. I'm especially interested in solutions which, like Lua's, go beyond single-character delimiters.

(For the curious: this question bubbled up when I tried to ssh multiple commands with ssh 'multiple; commands', where one of those commands was a call to sudo sh -c '...'. I think shell is the only environment where I frequently manually create string literals with multiple levels of quoting.)


For those who want to know more about Lua's long brackets:

A valid Lua comment, and a valid Lua string:

--[[ This comments out an assignment to my_lua_string
my_lua_string = [==[one [=[inner]=] two]==] 
]]

-- This is a string delimited with long brackets
[=[one ]] two]=]
--> 'one ]] two'
  • Java allows this via java.util.Scanner. Too broad. – user207421 Feb 2 at 9:22
  • @EJP Reading the java.util.Scanner docs it seems that is for parsing strings, not a syntactic construct to quote string literals. 'Too broad' does not seem to apply: " if your question could be answered by an entire book, or has many valid answers (but no way to determine which - if any - are correct), then it is probably too broad for our format." This question is about a clearly rare syntactic construct; it does not need a book, nor has it got many valid answers. – Esteis Feb 2 at 9:29
up vote 1 down vote accepted

PostgreSQL allows this: https://www.postgresql.org/docs/8.0/static/sql-syntax.html#SQL-SYNTAX-DOLLAR-QUOTING

It is most-often used in defining stored procedures, like this (see the $$'s):

CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $myAddOne$ BEGIN RETURN i + 1; END; $myAddOne$ LANGUAGE plpgsql;

However, I'm not aware of any others.

The UNIX shell (and its derivatives), Perl, PHP and Ruby (at least) support HEREDOCs where anything can be used as the delimiting TAG.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.