lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I am having trouble understanding the following. (I don't think it's
documented very clearly)
You mean my code isn’t documented clearly enough? I’ll add some more comments, though my problem is always deciding which parts need them, since LPeg already has clearly defined operators and spelling everything out in human language quickly gets redundant.

Oh no, sorry; I meant the LPeg documentation. I found a reference to what you were saying in the LPeg.P function, but your code had a single number, so I wasn't sure if it was the same thing.


> P "'" * ((1 - S "'\r\n\f\\") + (P '\\' * 1)) ^ 0 * "'" What is the P '\\' * 1 for? Does it match a \ and then any single character?
Correct. A plain number matches/consumes it’s own number of characters. I should probably have matched the full escape but I didn’t because I don’t think there are any error cases to handle. This would be the correct pattern:

local escape = S ""\r\n\f\\"
local sq_string = P "'" * ((1-escape) + (P'\\' * escape))^0 * "'"

That's what I was thinking when I was looking over it, thanks for clarifying.

Much appreciated,
-Mitchell;


also just noticed that the above could be made faster (I think):

local sq_string = P "'" * ((1-escape)^0 + (P'\\' * escape))^0 * "'"

Have a nice day,

 - Peter Odding