diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2012-05-13 16:25:34 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2012-05-13 16:25:34 +0100 |
commit | 7ce9834646fa91b4abf92f50f86e08d90220e013 (patch) | |
tree | 8c1dcd73b46951a44a04bb7db3affd00ce4f6e08 /lib | |
parent | 0745d0828f55f348c86468dd2a4894682c85f200 (diff) | |
download | lace-7ce9834646fa91b4abf92f50f86e08d90220e013.tar.gz |
Lexer, fixes and tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lace/lex.lua | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/lib/lace/lex.lua b/lib/lace/lex.lua index c768d08..07277a5 100644 --- a/lib/lace/lex.lua +++ b/lib/lace/lex.lua @@ -54,8 +54,11 @@ local function lex_one_line(line) elseif c == " " or c == "\t" then -- A space (or tab) and not quoting, so clear the accumulator if acc ~= "" then - r[#r+1] = { spos, acc } + r[#r+1] = { spos, acc, pos = spos, str = acc } spos = cpos + 1 + elseif cpos == spos then + -- Increment the start position since we've not found a word yet + spos = spos + 1 end acc = "" else @@ -64,7 +67,7 @@ local function lex_one_line(line) end end if acc ~= "" then - r[#r+1] = { spos, acc } + r[#r+1] = { spos, acc, pos = spos, str = acc } end local warnings = {} @@ -86,25 +89,28 @@ local function lex_a_ruleset(ruleset, sourcename) local ret = { source = sourcename, lines = lines } local n = 1 local warn + if ruleset:match("[^\n]$") then + ruleset = ruleset .. "\n" + end for oneline in ruleset:gmatch("([^\n]*)\n") do local linetab = { original = oneline } - if linetab:find("^[ \t]*#") or - linetab:find("^[ \t]*//") or - linetab:find("^[ \t]*%-%-") then + if oneline:match("^[ \t]*#") or + oneline:match("^[ \t]*//") or + oneline:match("^[ \t]*%-%-") then linetab.type = "comment" - elseif linetab:find("^[ \t]*$") then + elseif oneline:match("^[ \t]*$") then linetab.type = "whitespace" else linetab.type = "rule" linetab.content, warn = lex_one_line(oneline) - if #warn then + if #warn > 0 then linetab.warnings = warn end end lines[n] = linetab n = n + 1 end - return lines + return ret end local function lex_a_file(filename) |