summary refs log tree commit diff stats
path: root/luatokens.lua
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2019-04-08 13:57:28 -0300
committerSoniEx2 <endermoneymod@gmail.com>2019-04-08 13:58:06 -0300
commitf56e1bd7e7f9a8d0a55146edba4e7c2ee071487a (patch)
tree7a41eb92ffadeffa0d779573399bb488be4b6ede /luatokens.lua
parentfd48534de3427de16c3077f7d112d0bcfb030b73 (diff)
Add copyright notices, everything almost works
Numbers and long comments aren't working
Diffstat (limited to 'luatokens.lua')
-rw-r--r--luatokens.lua238
1 files changed, 237 insertions, 1 deletions
diff --git a/luatokens.lua b/luatokens.lua
index 7bf9f68..406fba5 100644
--- a/luatokens.lua
+++ b/luatokens.lua
@@ -1,4 +1,74 @@
--- Lua defs
+--[[
+    luatokens.lua - pure-Lua Lua tokenizer
+    Copyright (C) 2019  Soni L.
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+--]]
+
+--[[
+    This software is based on Lua 5.1 and Lua 5.3
+
+    Lua 5.1 license:
+
+/******************************************************************************
+* Copyright (C) 1994-2012 Lua.org, PUC-Rio.  All rights reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining
+* a copy of this software and associated documentation files (the
+* "Software"), to deal in the Software without restriction, including
+* without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to
+* permit persons to whom the Software is furnished to do so, subject to
+* the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+******************************************************************************/
+
+    Lua 5.3 license:
+
+/******************************************************************************
+* Copyright (C) 1994-2018 Lua.org, PUC-Rio.
+*
+* Permission is hereby granted, free of charge, to any person obtaining
+* a copy of this software and associated documentation files (the
+* "Software"), to deal in the Software without restriction, including
+* without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to
+* permit persons to whom the Software is furnished to do so, subject to
+* the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+******************************************************************************/
+--]]
 
 -- we need some stuff from here
 local parser = require "parser"
@@ -54,6 +124,11 @@ local keywords = {
     ["while"] = TK_WHILE,
 }
 
+local reverse_keywords = {}
+for k,v in pairs(keywords) do
+    reverse_keywords[v] = k
+end
+
 local defs = selfify({})
 
 defs.base = {
@@ -315,6 +390,7 @@ do local tlongstring = {}
 
         do local tllmaybe_end = selfify({defs = defs}, "maybe_end")
             tllongstring_proper.maybe_end = tllmaybe_end
+            tllmaybe_end.longstring_proper = tllongstring_proper
             tllmaybe_end["="] = function(state, token)
                 state.longstring_close = state.longstring_close + 1
                 return "maybe_end"
@@ -335,6 +411,8 @@ do local tlongstring = {}
                     return "maybe_end"
                 end
             end
+            tllmaybe_end[""] = "longstring_proper"
+            tllmaybe_end[1] = collect_fallback
             tllmaybe_end[-1] = function(state, token, rule)
                 if not rule then
                     collect_fallback(state, "]")
@@ -390,6 +468,157 @@ defs.maybe_longstring = setmetatable({
 --defs["\r"] = setmetatable({["\n"] = setmetatable({}, {__index=defs})}, {__index=defs})
 mknewline(defs, 1)
 
+-- thankfully comments are easy
+defs["-"] = "maybe_comment"
+do local tmaybe_comment = setmetatable({["-"] = "comment"}, {__index=defs})
+    defs.maybe_comment = tmaybe_comment
+    tmaybe_comment[-1] = function(state, token, rule)
+        if rule ~= "comment" then
+            state[#state+1] = "-"
+        end
+    end
+    do local tmcomment = {comment_proper = selfify({})}
+        tmaybe_comment.comment = tmcomment
+        tmcomment[""] = "comment_proper"
+        tmcomment["["] = "maybe_longcomment"
+        mknewline(tmcomment, 1, defs)
+        mknewline(tmcomment.comment_proper, 1, defs)
+        tmcomment.comment_proper[""] = "self"
+        do local tlongcomment = {}
+            tmcomment.longcomment = tlongcomment
+            do local tllongcomment_proper = selfify({[""] = "self", ["]"] = function(state, token) state.longcomment_close = 0 return "maybe_end" end})
+                do local tllmaybe_end = selfify({comment = tcomment}, "maybe_end")
+                    tllongcomment_proper.maybe_end = tllmaybe_end
+                    tllmaybe_end = tllongcomment_proper
+                    tllmaybe_end["="] = function(state, token)
+                        state.longcomment_close = state.longcomment_close + 1
+                        return "maybe_end"
+                    end
+                    tllmaybe_end["]"] = function(state, token)
+                        if state.longcomment_close == state.longcomment_count then
+                            state.longcomment_close = nil
+                            state.longcomment_count = nil
+                            return "defs"
+                        else
+                            state.longcomment_close = 0
+                            return "maybe_end"
+                        end
+                    end
+                    tllmaybe_end[""] = "longcomment_proper"
+                    tllmaybe_end[-1] = function(state, token, rule)
+                        if not rule then
+                            state.longcomment_close = nil
+                        end
+                    end
+                end
+
+                tlongcomment.longcomment_proper = tllongcomment_proper
+                mknewline(tlongcomment, 1, tllongcomment_proper)
+                setmetatable(tlongcomment, {__index=tllongcomment_proper})
+            end
+        end
+
+        tmcomment.maybe_longcomment = setmetatable({
+            comment = tmcomment,
+            ['['] = "longcomment_open",
+            ['='] = "longcomment_open",
+            longcomment_count = setmetatable(selfify({
+                ["="] = function(state, token)
+                    state.longcomment_count = state.longcomment_count + 1
+                    return "longcomment_count"
+                end,
+                ["["] = "longcomment",
+                longcomment = tmcomment.longcomment,
+            }, "longcomment_count"), {__index=tmcomment}),
+            longcomment_open = function(state, token)
+                if token == "=" then
+                    state.longcomment_count = state.longcomment_count or 0 + 1
+                    return "longcomment_count"
+                elseif token == "[" then
+                    state.longcomment_count = 0
+                    return "longstring"
+                end
+            end,
+        }, {__index=tmcomment})
+    end
+end
+
+local STATE = parser.STATE
+
+defs.multitokens = setmetatable({
+    [-1] = function(state, token, rule)
+        if not state[STATE].multitoken[token] then
+            state[#state+1] = state[STATE].first
+        end
+    end,
+    second = function(state, token)
+        state[#state+1] = state[STATE].multitoken[token]
+        return "self" -- actually goes into defs
+    end
+}, {
+    __index=defs,
+    __call=function(t, first, ...)
+        local function helper(t, second, result, ...)
+            if not second then return end
+            t[second] = "second"
+            t.multitoken[second] = result
+            return helper(t, ...)
+        end
+        defs[first] = setmetatable({
+            first = first,
+            multitoken = {}
+        }, {__index=t})
+        return helper(defs[first], ...)
+    end
+})
+
+defs.multitokens("=", "=", TK_EQ)
+defs.multitokens("/", "/", TK_IDIV)
+defs.multitokens("<", "<", TK_SHL, "=", TK_LE)
+defs.multitokens(">", ">", TK_SHR, "=", TK_GE)
+defs.multitokens("~", "=", TK_NE)
+defs.multitokens(":", ":", TK_DBCOLON)
+
+defs["."] = setmetatable({
+    [-1] = function(state, token, rule)
+        if token ~= "." then
+            if rule ~= "digit" then
+                state[#state+1] = "."
+            else
+                error("NYI") -- TODO digit handling
+            end
+        end
+    end,
+    ["."] = setmetatable({
+        [-1] = function(state, token, rule)
+            if token ~= "." then
+                state[#state+1] = TK_CONCAT
+            end
+        end,
+        ["."] = function(state, token)
+            state[#state+1] = TK_DOTS
+            return "self" -- actually goes into defs
+        end
+    }, {__index=defs})
+}, {__index=defs})
+
+function defs.digit(state, token)
+    -- TODO
+end
+
+defs.in_digit = {
+    -- TODO
+}
+
+function defs.simpletoken(state, token)
+    state[#state+1] = token
+    return "self"
+end
+
+for token in string.gmatch("+*%^#&|(){}];,", ".") do
+    defs[token] = "simpletoken"
+end
+
 defs.whitespace = "self"
 defs.hexdigit = "alpha"
 defs["_"] = "alpha"
@@ -440,4 +669,11 @@ return {
         TK_DBCOLON = TK_DBCOLON, TK_EOS = TK_EOS,
         TK_FLT = TK_FLT, TK_INT = TK_INT, TK_NAME = TK_NAME, TK_STRING = TK_STRING
     },
+    reverse_keywords = reverse_keywords,
+    reverse_tokens = {
+        [TK_IDIV] = "//", [TK_CONCAT] = "..", [TK_DOTS] = "...", [TK_EQ] = "==", [TK_GE] = ">=", [TK_LE] = "<=", [TK_NE] = "~=",
+        [TK_SHL] = "<<", [TK_SHR] = ">>",
+        [TK_DBCOLON] = "::", [TK_EOS] = "<eof>",
+        [TK_FLT] = "<float>", [TK_INT] = "<integer>", [TK_NAME] = "<identifier>", [TK_STRING] = "<string>"
+    },
 }