diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2015-11-09 22:21:42 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2015-11-09 22:21:42 +0000 |
commit | 85693cd4ab8d1c3b26901c88a736541b484891eb (patch) | |
tree | 65b820dee953b3d84b8ac9049b690deb2178beed | |
parent | 3a3c54b1912f5d86bb8e8e57839dbda010b1661b (diff) | |
download | lace-85693cd4ab8d1c3b26901c88a736541b484891eb.tar.gz |
Add support for lexing {}d strings
-rw-r--r-- | lib/lace/lex.lua | 26 | ||||
-rw-r--r-- | test/test-lace.lex.lua | 19 |
2 files changed, 39 insertions, 6 deletions
diff --git a/lib/lace/lex.lua b/lib/lace/lex.lua index 80ec3cf..2e99702 100644 --- a/lib/lace/lex.lua +++ b/lib/lace/lex.lua @@ -15,6 +15,10 @@ local M = {} +local lexer_line_cache = {} + +local lex_one_line + local function _lex_one_line(line, terminator) local r = {} local acc = "" @@ -51,6 +55,17 @@ local function _lex_one_line(line, terminator) -- Start double quotes quoting = c force_empty = true + elseif c == '{' and quoting == false then + if acc == "" then + -- Something worth lexing + local ltab, rest, warns = lex_one_line(line, "}") + -- For now, assume the accumulator is good enough + cpos = cpos + #line - #rest + r[#r+1] = { spos = spos, epos = cpos, sub = ltab } + spos = cpos + 1 + line = rest + acc = "" + end elseif c == "'" and quoting == c then -- End single quotes quoting = false @@ -94,13 +109,12 @@ local function _lex_one_line(line, terminator) return r, line, warnings end -local lexer_line_cache = {} - -local function lex_one_line(line) - if not lexer_line_cache[line] then - lexer_line_cache[line] = { _lex_one_line(line) } +function lex_one_line(line, terminator) + local tag = line .. "\n" .. tostring(terminator) + if not lexer_line_cache[tag] then + lexer_line_cache[tag] = { _lex_one_line(line, terminator) } end - return lexer_line_cache[line][1], lexer_line_cache[line][2], lexer_line_cache[line][3] + return lexer_line_cache[tag][1], lexer_line_cache[tag][2], lexer_line_cache[tag][3] end local cached_full_lexes = {} diff --git a/test/test-lace.lex.lua b/test/test-lace.lex.lua index d234566..36025b7 100644 --- a/test/test-lace.lex.lua +++ b/test/test-lace.lex.lua @@ -268,6 +268,25 @@ function suite.empty_string_words_work() assert(content.lines[1].content[2].spos == 7, "The empty word starts at the seventh character") end +function suite.braced_sections_sublex() + local content = assert(lex.string("foo {bar baz} meta", "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 == 3, "The line should have 3 words") + assert(content.lines[1].content[1].spos == 1, "The first word starts at the first character") + assert(content.lines[1].content[1].str == "foo", "The word is \"foo\"") + assert(content.lines[1].content[3].spos == 15, "The third word starts at the first character") + assert(content.lines[1].content[3].str == "meta", "The word is \"foo\"") + -- Now the sublex... + assert(content.lines[1].content[2].sub, "The second word is a sublex") + assert(#content.lines[1].content[2].sub == 2, "The sublex should have 2 words") + assert(content.lines[1].content[2].sub[1].str == "bar", "The sublex first word should be bar") + assert(content.lines[1].content[2].sub[2].str == "baz", "The sublex first word should be bar") + assert(content.lines[1].content[2].spos == 5, "The sublex starts at location 5") + assert(content.lines[1].content[2].epos == 13, "The sublex ends at location 13") +end + local count_ok = 0 for _, testname in ipairs(testnames) do -- print("Run: " .. testname) |