Precisely Placed Multiline Comment can cause lua compiler to error

Posted by WillFa on Fri 15 Aug 2008 05:29 AM — 5 posts, 14,463 views.

USA #0
If you have multiline comments that span the 100th(*n) line of your script or module, the compiler's loadchunk fails

i.e. on line 99 start a comment:


--[[ 
And start documenting a function in your file

blah
blah

and finish it up on line 103 
--]]


the script or module won't compile.
Australia Forum Administrator #1
I can't reproduce that. See this:


s = ""
for i = 1, 98 do
  s = s .. "\n"
end -- for

s = s .. [=[
--[[ 
And start documenting a function in your file

blah
blah

and finish it up on line 103 
--]]

print "hello, world"
]=]

assert (loadstring (s)) ()

AppendToNotepad ("test", (string.gsub (s, "\n", "\r\n")))


This puts the comment at line 99 (you can check in the notepad window), and it compiles and displays "hello, world".

USA #2
Weird... Specifically, this compiles:
(and it's no longer spanning the 100th line, so I was wrong on my guess why. Sorry.)

		self.Bars[#self.Bars][self.Bars[#self.Bars][k]](self.Bars[#self.Bars], v)

		--		The above obfuscated, yet cool code basically evaluates to Bar:Caption(passedParameter)
		--		foo = self.Bars[#self.bars]   					is the nth table in the Bars table
		--			k = 1								on the first iteration
		--			foo[k] = "Caption"							because of the metatable to Bar
		--			foo[foo[k]]			is foo["Caption"] 		which is a function in the Bar metatable
		--		foo[foo[k]](foo, v)		is foo.Caption(foo,v)	is long form for foo:Caption(v)
		--


This doesn't:

		self.Bars[#self.Bars][self.Bars[#self.Bars][k]](self.Bars[#self.Bars], v)

		--[[		The above obfuscated, yet cool code basically evaluates to Bar:Caption(passedParameter)
				foo = self.Bars[#self.bars]   					is the nth table in the Bars table
					k = 1										on the first iteration
					foo[k] = "Caption"							because of the metatable to Bar
					foo[foo[k]]			is foo["Caption"] 		which is a function in the Bar metatable
				foo[foo[k]](foo, v)		is foo.Caption(foo,v)	is long form for foo:Caption(v)
		--]]



The error returned is:

World: 3K -- Spellbound
Immediate execution
error loading module 'InfoBox' from file 'C:\Program Files\MUSHclient\lua\InfoBox.lua':
        C:\Program Files\MUSHclient\lua\InfoBox.lua:288: '=' expected near 'foo'
stack traceback:
        [C]: ?
        [C]: in function 'require'
        [string "Script file"]:15: in main chunk

Line 288 is the "foo[foo[k]] ..." line.

Oddly, this doesn't compile either.
 
--[[ ...
--...
--...
--]]
Amended on Fri 15 Aug 2008 08:48 PM by WillFa
Australia Forum Administrator #3
Check out Programming in Lua:

http://www.lua.org/pil/1.3.html

In particular:


Lua also offers block comments, which start with --[[ and run until the corresponding ]].


So, you started a comment with --[[ and then ended it with:


foo[foo[k]]			is foo["Caption"] 


In other words, it tried to understand "is foo["Caption"] ".

I usually use --]] to end comments, as explained on the same page:


A common trick, when we want to comment out a piece of code, 
is to write the following:

    --[[
    print(10)         -- no action (comment)
    --]]

Now, if we add a single hyphen to the first line, the code is in again:

    ---[[
    print(10)         --> 10
    --]]



Thus you have probably got used to expecting --]] as a comment terminator.

Your solution is to use the Lua "extended string" syntax, like this:


--[=[		

The above obfuscated, yet cool code ...

 blah blah  foo[foo[k]]  blah

		--]=]


Now the sequence ]=] terminates the comment. If you might have ]=] in the text you can use --[==[ ... -- ]==] and so on.

Quote:

Oddly, this doesn't compile either.

--[[ ...
--...
--...
--]]



That compiled for me. You probably did the same thing to test it.

Amended on Fri 15 Aug 2008 09:23 PM by Nick Gammon
USA #4
Quote:
Thus you have probably got used to expecting --]] as a comment terminator.


Yup. That's exactly it. Thanks Nick. Sorry for claiming your code's buggy when it's my own ignorance. :)


Regarding the "Oddly" statement, I had started each line of the comment block with "--", but that still didn't work on line 288 for the same reasons.