Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid escape sequences #152

Closed
Resike opened this issue Mar 9, 2018 · 3 comments
Closed

Invalid escape sequences #152

Resike opened this issue Mar 9, 2018 · 3 comments

Comments

@Resike
Copy link

Resike commented Mar 9, 2018

Invalid escape sequences should be warnings instead of errors, since the file still can be compiled regardless.

@mpeterv
Copy link
Owner

mpeterv commented Apr 11, 2018

I think it should still be an error, but it's possible to have luacheck errors and warnings in the same file already (invalid inline options are also reported as errors). Luacheck should just check the file anyway if there are invalid escape sequences. Same in general if it can recover from a syntax error, but that's more difficult.

One problem with this is that dropping an invalid escape sequence from a string literal changes its value, which can result in extra warnings in some cases:

local t = {
   ["\x00"] = "one embedded zero",
   ["\x00\x00"] = "two embedded zeros", -- Warning: unused field, always overwritten on the next line
   ["\x00\x00\xoo"] = "three embedded zeros"
}

But the advantages of having all the checks run even with invalid escape sequences outweigh this.

@Resike Resike closed this as completed Jan 30, 2019
@rubo77
Copy link

rubo77 commented Jun 16, 2019

Couldnt regular expressions be treated as correct at least?

we get

 (E011) invalid escape sequence '\$'

at

 if string.match(value, "^\$%d\$.*") then

@hugomg
Copy link

hugomg commented Jun 16, 2019

@rubo77, your code is actually wrong though. The Lua interpreter produces a syntax error saying invalid escape sequence near '"^\$' when it attempts to evaluate that piece of code.

Syntactly speaking, all Lua string literals are treated the same, no mater whether they are intended to be used as Lua patterns, regex patterns for an external library, or just a regular old string.

If you want to use Lua pattern matching functions like string.match, the escape character is %. So to match a digit surrounded by dollar signs you should use "^%$%d%$.*"

If you are using an external regex library with PCRE syntax, where the escape character is the backslash, then you need to escape the backslash inside the Lua string literal, just like you would have to do in any other language that doesn't have special syntax for regular expression literals. That is, you would need to use "^\\$\\d\\$.*"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants