Unfinished long string near <eof> - lua

function writeFloat([=[==[===[====["game.exe"+XXXXXXXX]+XXX====]+XXX===]+XXX==]+XXX=]+XXX, trackbar_getPosition(TRAINERFORM_CETrackBar1))
end
gives me the error
[string "--code..."]:4: unfinished long string near

Lua has "long strings", which are induced by the syntax of [=*[, where "=*" means "zero or more = characters". So [[ begins a long string, as does [==[ or [=[, as in your case.
A long string is so named because it accepts every character between the inducing syntax and the terminating syntax. This allows you to do useful things like add verbatim XML, C++, or even Lua code within your Lua script as a literal string.
The terminating syntax is ]=*], where "=*" means the exact same number of = characters that was used to induce the long string. So if you start with [=[, the long string will only end with ]=]. ]] and ]====] or any other terminus will not end the long string; they'll be taken verbatim into the string.
So this:
local lit = [=[Long String]==]=]
Results in lit taking the value Long String]==.
In your code, you never see a ]=] sequence. You have ====] and similar things, but they don't even start with a ] character.
It is illegal to start a long string that never ends in a Lua script. Hence the compile error.

Related

How to escape brackets in a multi-line string in Lua

This is an exercise question from the book the lua programming language the 3th edition.
Exercise2.4: How can you embed the following piece of XML as a string in Lua?
Show at least two different ways.
Here is my answer:
s = "<![CDATA\n Hello world\n]]>"
print(s)
s2 = [[
<![CDATA
Hello world
\]\]>
]]
print(s2)
and the output:
<![CDATA
Hello world
]]>
<![CDATA
Hello world
\]\]>
Way 1 is right. The output of way 2 is not as expected. Without the backslash char, lua will show an error:
lua: execrcise-4.1.lua:7: unexpected symbol near ']'
So I have a question, how to escape brackets in a multi-line string in Lua ?
My lua interpreter version is 5.4.2.
Actually the whole point of this exercise is that you find out how to solve this problem.
Ideally by reading the Lua manual.
There you'll learn that opening and closing brackets for long strings have levels.
Literal strings can also be defined using a long format enclosed by
long brackets. We define an opening long bracket of level n as an
opening square bracket followed by n equal signs followed by another
opening square bracket. So, an opening long bracket of level 0 is
written as [[, an opening long bracket of level 1 is written as [=[,
and so on. A closing long bracket is defined similarly; for instance,
a closing long bracket of level 4 is written as ]====]. A long literal
starts with an opening long bracket of any level and ends at the first
closing long bracket of the same level. It can contain any text except
a closing bracket of the same level.
s2 = [[
<![CDATA
Hello world
]]>
]]
violates that bold rule as you close the long string prematurely leaving you with two extra brackets that cause a syntax error.
So what do you need to do if the string may not contain a closing bracket of level 0 ]] ? We increase the level of our long string.
s2 = [=[
<![CDATA
Hello world
]]>
]=]
You cannot escape a square bracket with a backslash in a Lua string btw.
The only reason why you didn't get an error for the invalid escape sequence \] is that long strings ignore escape sequences.

How can I use single and normal quotation marks in the same string?

I am working on a project, in which you type your input sentence, and I need to be able to use " and ' in the sentence, such as Input = "I said, "Hi what's up?" print(Input) in which I get an error. If anyone knows how to fix this that would be great.
See https://www.lua.org/pil/2.4.html. Lua has very interesting feature to declare string with square brackets:
input = [[I said, "Hi what's up?"]]
input = "I said, \"Hi what's up?\""
input = 'I said, "Hi what\'s up?"'
I will tell some things in addition to what #Darius told above
When you tried to add a quatation mark inside a string, the lua interpreter get confused and break your string after the next quation mark without reaching the end of the line. That's the reason for the error.
Try to understand it by the following code
str = "Hello I"m somebody" -- here the interpreter will think str equals to "Hello I" at first, and then it will find some random characters after which may make it confused (as m somebody is neither a variable nor a keyword)"
-- you can also see the way it got confused by looking at the highlighted code
--What you can do to avoid this is escaping the quotes
str = "Hello I\"m somebody" -- here the interpreter will treat \" as a raw character (") and parse the rest.
You can also use the escape character () with others such as \', \", \[, \n (newline character), \t (tab) and so on.

Convert string to multiline/raw string in LUA

Is there a way to convert a quoted string to a multiline string?
Something like "This string \66 here" to [[This string \66 here]] since I would like to ignore the interpretation of escaped characters.
Lua 5.3 Reference Manual 3.1: Lexical Conventions
Literal strings can also be defined using a long format enclosed by
long brackets. We define an opening long bracket of level n as an
opening square bracket followed by n equal signs followed by another
opening square bracket. So, an opening long bracket of level 0 is
written as [[, an opening long bracket of level 1 is written as [=[,
and so on. A closing long bracket is defined similarly; for instance,
a closing long bracket of level 4 is written as ]====]. A long literal
starts with an opening long bracket of any level and ends at the first
closing long bracket of the same level. It can contain any text except
a closing bracket of the same level. Literals in this bracketed form
can run for several lines, do not interpret any escape sequences, and
ignore long brackets of any other level. Any kind of end-of-line
sequence (carriage return, newline, carriage return followed by
newline, or newline followed by carriage return) is converted to a
simple newline.
For convenience, when the opening long bracket is immediately followed
by a newline, the newline is not included in the string.
That's all you need to know about long strings.
It does not make much sense to convert a string that has been defined using quotes "some string" to a string like [[some string]] as both quotes and square brackets are not actually part of that string and the string itself is the same.
The only difference would be a leading newline which is ignored in square brackets or escape sequences which are not interpreted.
Quotes and square brackets are only part of the string if you have nested strings. In this case conversion also doesn't make much sense because you cannot nest strings with quotes like strings with brackets.
Maybe your whole approach is a bit off?
Do you look for something like this?
local db = "google"
local tbl = "accounts"
local where = "field = 'VALUE' AND TRUE"
local order = "id DESC"
local query = string.format([[
SELECT *
FROM `%s`.`%s`
WHERE %s
ORDER BY %s
]], db, tbl, where, order)

lua: why the character "$" cause "unexpected symbol near $"

I am newbie in Lua. I write a script in which there is a line
ISH_activation.getState() = CONST_ACTIVATION_STATE$ON
Then, I need to write another C++ script to read this line from Lua and parse it. Now the problem is that, when I try to debug the lua script itself, at this line it gives me the error "unexpected symbol near '$'".
I don't know why this would happen. Is format like "CONST_ACTIVATION_STATE$ON" not allowed in Lua? And is "$" a special character in Lua that we cannot use it?(But I didn't find anything mentioning this)....
Well, did you find anything saying that you can use $? :)
Yes, you cannot use dollar signs in identifiers. From the manual:
Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit. Identifiers are used to name variables, table fields, and labels.
If you really want to keep the dollar sign you could use strings instead
"CONST_ACTIVATION_STATE$ON"

Why do I get an error due to two backslashes \\?

I am writing a test and verifying some data. It's failing due to the two \\ I get in the expected data string.
My test code is:
actual_string.should eq 'Today is Tuesday.\n It is third day of the week.'
When I execute this code, I get an error saying the actual data does not match the expected data.
The actual data is:
'Today is Tuesday.\n It is third day of the week.'
The expected data is:
'Today is Tuesday.\\n It is third day of the week.'
Not sure from where is that extra slash '\' is coming from in the expected data. How can I resolve this?
use "Text" - double quotes....
Unlike other languages (e.g. Python, JavaScript etc.), Ruby uses different escape sequences in single-quoted and double-quoted strings.
Single-quoted strings only support \' and \\. Everything else is treated literally. So, '\n' is two characters \ and n, not a single new line character.
To use the new line character, enclose your string into double quotes:
actual_string.should eq "Today is Tuesday.\n It is third day of the week."
This will fix your test.

Resources