Lua multiline comments past ]]'s
40,830
As you can see in Strings tutorial there is a special [===[
syntax for nesting square braces. You can use it in block comments too. Just note, that number of =
signs must be the same in open and close sequence.
For example 5 equals will work.
--[=====[
for k,v in pairs(t) do
local d = fullToShort[k]
local col = xColours[v[1]] -- It stops here!
cecho(string.format(("<%s>%s ", col, d))
end
--]=====]
Author by
Jonathan Picazo
Updated on July 09, 2022Comments
-
Jonathan Picazo almost 2 years
I'm trying to find out a way to use a multiline comment on a batch of code, but it keeps mistaking some syntax in it as a ]] and thinking I want it to end there, which I don't!
--[[ for k,v in pairs(t) do local d = fullToShort[k] local col = xColours[v[1]] -- It stops here! cecho(string.format(("<%s>%s ", col, d)) end --]]
I thought I read somewhere it was possible to do use a different sort of combination to avoid those errors, like --[=[ or whatnot... Could someone help?
-
James over 8 yearsThat almost feels like a kludge. I wonder why they did it that way?
-
Andrey Tyukin over 5 years@James That's not a kludge - that's the only right way to do it. Every other fixed "magic comment closing character sequence" (henceforth MCCCS) fundamentally cannot cope with strings that contain the MCCCS itself. That's why you end up with atrocities like
]]]]><![CDATA[>
every single time you want to write]]>
inCDATA
. The only way to avoid it is to provide paired delimiters that can vary in length (or content, as in bash HEREDOCs). A much better question would have been: why don't all the other languages do it like Lua? -
hsandt about 5 yearsCool, it also works when inserting a multine string between
[[
and]]
inside a block comment. In this case, you can also put the=
in the embedded block to distinguish it from the containing block boundaries, e.g. using[=[
and]=]
-
bfontaine over 4 years@AndreyTyukin OCaml solves the issue in an elegant way: comments must contain valid code tokens:
(* "*)" *)
works as well as(* … (* nested comment *) ... *)
. It allows you to comment any piece of syntaxically-valid code with no issue. -
Sany over 3 yearsThe answer reflects the question title. I am fine with that. What would be good to do is to rewrite original question title.
-
Rick over 3 yearsI don't see any
]]
's in the comment.