From 2b596488ddf1e401f7bb51411775bbced4f07185 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 13 May 2012 23:17:01 +0100 Subject: Allow empty quotes to force an empty word to get into the lexer output --- lib/lace/lex.lua | 8 ++++++-- test/test-lace.lex.lua | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/lace/lex.lua b/lib/lace/lex.lua index f8e5160..ba9dabe 100644 --- a/lib/lace/lex.lua +++ b/lib/lace/lex.lua @@ -15,6 +15,7 @@ local function lex_one_line(line) local c local escaping = false local quoting = false + local force_empty = false local spos, cpos = 1, 0 while #line > 0 do c, line = line:match("^(.)(.*)$") @@ -36,9 +37,11 @@ local function lex_one_line(line) if c == "'" and quoting == false then -- Start single quotes quoting = c + force_empty = true elseif c == '"' and quoting == false then -- Start double quotes quoting = c + force_empty = true elseif c == "'" and quoting == c then -- End single quotes quoting = false @@ -53,9 +56,10 @@ local function lex_one_line(line) acc = acc .. c elseif c == " " or c == "\t" then -- A space (or tab) and not quoting, so clear the accumulator - if acc ~= "" then + if acc ~= "" or force_empty then r[#r+1] = { spos = spos, epos = cpos - 1, str = acc } spos = cpos + 1 + force_empty = false elseif cpos == spos then -- Increment the start position since we've not found a word yet spos = spos + 1 @@ -66,7 +70,7 @@ local function lex_one_line(line) end end end - if acc ~= "" then + if acc ~= "" or force_empty then r[#r+1] = { spos = spos, epos = cpos, str = acc } end diff --git a/test/test-lace.lex.lua b/test/test-lace.lex.lua index 5d9d552..41f1685 100644 --- a/test/test-lace.lex.lua +++ b/test/test-lace.lex.lua @@ -248,6 +248,18 @@ function suite.escape_inside_unclosed_unused() assert(content.lines[1].warnings[2]:find("escape"), "The warning should be about the escape") end +function suite.empty_string_words_work() + local content = assert(lex.string("allow ''", "SRC")) + assert(content.source == "SRC", "Source name not propagated") + assert(type(content.lines) == "table", "Lines is not a table") + assert(#content.lines == 1, "There should have been one line") + assert(#content.lines[1].content == 2, "The line should have 2 words") + assert(content.lines[1].content[1].spos == 1, "The first word starts at the first character") + assert(content.lines[1].content[1].str == "allow", "The word is \"allow\"") + assert(content.lines[1].content[2].str == "", "The second word is empty") + assert(content.lines[1].content[2].spos == 7, "The empty word starts at the seventh character") +end + local count_ok = 0 for _, testname in ipairs(testnames) do print("Run: " .. testname) -- cgit v1.2.1