lua-users home
lua-l archive

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

mitchell wrote:
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.

> 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 * "'"

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