lua-users home
lua-l archive

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


On Thu, Oct 12, 2017 at 10:16 PM, Martin wrote:
On 10/12/2017 07:56 PM, Egor Skriptunoff wrote:
> function end_of_string_literal (text, start_pos, quote)
>    return text:gsub("\\?.", {[quote]="\0"}):match("%z()", start_pos)
> end
>
> print(end_of_string_literal ([[a="\"A",b="\\\"B\"",c="C"]], 4,  '"'))  --> 8
> print(end_of_string_literal ([[a="\"A",b="\\\"B\"",c="C"]], 12, '"'))  --> 20
> print(end_of_string_literal ([[a="\"A",b="\\\"B\"",c="C"]], 24, '"'))  --> 26

What task this code solves?


The task is to simplify Sony's code:

local m, pos
repeat
  m, pos = string.match(word_eol[2], "(\\*)"..cw:sub(1,1).."()", pos or 2)
until m == nil or #m % 2 == 0

(cw:sub(1,1) being one of " or ')

What this code does?
It is probably a part of some parser (or should I say scanner?)
A text (in variable word_eol[2]) starts with quote-delimited string literal (the quote is cw:sub(1,1))
This code finds the position where the string literal terminates.
String literal syntax implied here is allowing backslash escaping.