How does Lua parse newlines?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How does Lua parse newlines?

Soni "They/Them" L.
How does Lua parse newlines? For example:

print(#load('return "\\\r\n"')()) --> 1
print(#load('return "\\\n\r"')()) --> 1
print(#load('return "\\\r"')()) --> 1
print(#load('return "\\\n"')()) --> 1

print(load('return "\\z\r\n\\x"', "@[needed so results display
correctly]")) --> nil, error line at 2
print(load('return "\\z\n\r\\x"', "@[needed so results display
correctly]")) --> nil, error line at 2
print(load('return "\\z\n\\x"', "@[needed so results display
correctly]")) --> nil, error line at 2
print(load('return "\\z\r\\x"', "@[needed so results display
correctly]")) --> nil, error line at 2

So it seems that any of "\n", "\r", "\r\n" and "\n\r" are valid
newlines. But where in the code is it?

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: How does Lua parse newlines?

Soni "They/Them" L.


On 2017-10-14 12:34 PM, Soni L. wrote:

> How does Lua parse newlines? For example:
>
> print(#load('return "\\\r\n"')()) --> 1
> print(#load('return "\\\n\r"')()) --> 1
> print(#load('return "\\\r"')()) --> 1
> print(#load('return "\\\n"')()) --> 1
>
> print(load('return "\\z\r\n\\x"', "@[needed so results display
> correctly]")) --> nil, error line at 2
> print(load('return "\\z\n\r\\x"', "@[needed so results display
> correctly]")) --> nil, error line at 2
> print(load('return "\\z\n\\x"', "@[needed so results display
> correctly]")) --> nil, error line at 2
> print(load('return "\\z\r\\x"', "@[needed so results display
> correctly]")) --> nil, error line at 2
>
> So it seems that any of "\n", "\r", "\r\n" and "\n\r" are valid
> newlines. But where in the code is it?
>

Quick update:

 > print(load('return "\\z \n\r \n\r \r\n \n \n\\x"', "@test"))
nil    test:6: hexadecimal digit expected near '"\x"

WHY LINE 6. I NEED TO KNOW.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: How does Lua parse newlines?

Francisco Olarte
On Sat, Oct 14, 2017 at 5:43 PM, Soni L. <[hidden email]> wrote:
...
>> print(load('return "\\z \n\r \n\r \r\n \n \n\\x"', "@test"))
> nil    test:6: hexadecimal digit expected near '"\x"
>
> WHY LINE 6. I NEED TO KNOW.

Because you forgot to escape the newlines and it is in the sixth line?
i.e., preceeding your line with:

> function load(...) return ... end

gives me: (start of line dots added for easier counting )

.> print(load('return "\\z \n\r \n\r \r\n \n \n\\x"', "@test"))
.return "\z
.
.
.
.
.\x"    @test
.>

Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: How does Lua parse newlines?

Soni "They/Them" L.


On 2017-10-14 02:00 PM, Francisco Olarte wrote:
> On Sat, Oct 14, 2017 at 5:43 PM, Soni L. <[hidden email]> wrote:
> ...
>>> print(load('return "\\z \n\r \n\r \r\n \n \n\\x"', "@test"))
>> nil    test:6: hexadecimal digit expected near '"\x"
>>
>> WHY LINE 6. I NEED TO KNOW.
> Because you forgot to escape the newlines and it is in the sixth line?
> i.e., preceeding your line with:

Oh. Yeah, that's right. 5 newlines make 6 lines.

Why can't I figure out how to parse it myself tho...[1]

>> function load(...) return ... end
> gives me: (start of line dots added for easier counting )
>
> .> print(load('return "\\z \n\r \n\r \r\n \n \n\\x"', "@test"))
> .return "\z
> .
> .
> .
> .
> .\x"    @test
> .>
>
> Francisco Olarte.
>

[1]:
https://stackoverflow.com/questions/46746752/parsing-lua-strings-more-specifically-newlines

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: How does Lua parse newlines?

Hugo Musso Gualandi
In reply to this post by Soni "They/Them" L.
> So it seems that any of "\n", "\r", "\r\n" and "\n\r" are valid
> newlines. But where in the code is it?

I think you are looking for "inclinenumber", in llex.c:



    /*
    ** increment line number and skips newline sequence (any of
    ** \n, \r, \n\r, or \r\n)
    */
    static void inclinenumber (LexState *ls) {