hwiguna/g33k

Not escaping multiline strings correctly

myplacedk opened this issue · 8 comments

When I have a multiline string, "Save to ESP" fails. Example:

print([[Hello
World]])

I can execute it, but when I save it I get this:

> SENT: file.remove("delme.lua")
SENT: file.open("delme.lua","w")
SENT: file.writeline([[print([[Hello]])
SENT: file.writeline([[World]])]])
SENT: file.close()
file.remove("delme.lua")
> file.open("delme.lua","w")
> file.writeline([[print([[Hello]])
stdin:1: nesting of [[...]] is deprecated near '['
> file.writeline([[World]])]])
stdin:1: unexpected symbol near ']'
> file.close()

I think the problem is in https://github.com/hwiguna/g33k/blob/master/ArduinoProjects/Windows/ESP8266_Related/LuaUploader/SerialPort/Form1.cs line 211:

stringBuilder.AppendLine("file.writeline([[" + line + "]])");

You may want to check if there's anything else that should be escaped.

myplacedk, thanks for reporting that. I'm a lua n00b, is that a realistic scenario? I've always used double quotes for strings: print("Hello World") or if you want the line break, then print("Hello\r\nWorld")
If we need to escape the square brackets, what's the syntax? Thanks again!

It's a very realistic scenario. I've also had the problem with a line like this:

      h = b[1] * 256 + b[2]

Which becomes...

file.writeline([[h = b[1] * 256 + b[2]]])

...and the interpreter thinks there's an extra ] in the end.

I'm a Lua newbie myself, but I think a better solution may be to use single-quotes, and make sure that all single quotes and backslashes gets escaped with a backslash.

print('This is a test\twith tabs')

=>

file.writeline('print(\'This is a test\\twith tabs\')')

Good point!
I should be able to work on this this weekend. If you like, feel free to
fix it and submit a pull request so you'll get proper credit for finding
and fixing the bug. :-)

On Fri, Feb 6, 2015 at 7:32 AM, Niels Ulrik Andersen <
notifications@github.com> wrote:

It's a very realistic scenario. I've also had the problem with a line like
this:

  h = b[1] * 256 + b[2]

Which becomes...

file.writeline([[h = b[1] * 256 + b[2]]])

...and the interpreter thinks there's an extra ] in the end.

I'm a Lua newbie myself, but I think a better solution may be to use
single-quotes, and make sure that all single quotes and backslashes gets
escaped with a backslash.

print('This is a test\twith tabs')

=>

file.writeline('print('This is a test\twith tabs')')


Reply to this email directly or view it on GitHub
#1 (comment).

Sorry, I'm not a C# developer. If I didn't have to set up a development environment to test my fix, I'd probably have fixed it myself. :)

I don't have an esp8266 with me, but this looks okay to me.

Input:

a = [[ This is
a test]]
b = a[0]
c = 'More test'
d = 'Here\'s even more'`

1.0.2.2:

file.writeline([[a = [[ This is]])
file.writeline([[a test]]]]) -- Syntax error
file.writeline([[b = a[0]]]) -- Syntax error
file.writeline([[c = 'More test']])
file.writeline([[d = 'Here\'s even more']])

1.0.2.3:

file.writeline('a = [[ This is')
file.writeline('a test]]')
file.writeline('b = a[0]')
file.writeline('c = \'More test\'')
file.writeline('d = \'Here\\\'s even more\'')
-- No syntax errors, yay!

Niels,
I've pulled the fix into the master branch. Thank you VERY MUCH for
improving LUA Uploader!!!

On Mon, Feb 9, 2015 at 2:58 AM, Niels Ulrik Andersen <
notifications@github.com> wrote:

I don't have an esp8266 with me, but this looks okay with me.

Input:

a = [[ This isa test]]
b = a[0]
c = 'More test'
d = 'Here's even more'`

1.0.2.2:

file.writeline([[a = [[ This is]])
file.writeline([[a test]]]]) -- Syntax error
file.writeline([[b = a[0]]]) -- Syntax error
file.writeline([[c = 'More test']])
file.writeline([[d = 'Here's even more']])

1.0.2.3:

file.writeline('a = [[ This is')
file.writeline('a test]]')
file.writeline('b = a[0]')
file.writeline('c = 'More test'')
file.writeline('d = 'Here\'s even more'')-- No syntax errors, yay!


Reply to this email directly or view it on GitHub
#1 (comment).

i still have problem with '\n' in buf="textBox.value.split('\n');"
it shows
stdin:1: unexpected symbol near ''