Skip to content

Commit

Permalink
wip: try to move to advanced parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Dec 21, 2023
1 parent e23568b commit fdf934d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions lua-resty-router-tree-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ build = {
["router-tree.parser.style.advanced"] = "src/resty/parser/style/advanced.lua",
["router-tree.trie"] = "src/resty/trie.lua",
["router-tree.utils"] = "src/resty/utils.lua",
["router-tree.constatns"] = "src/resty/constatns.lua",
},
}
16 changes: 15 additions & 1 deletion spec/router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ describe("Router", function()
},
})
assert.equal("1", router:match("/a"))

it("sanity", function()
local router = Router.new({
{
paths = { "/" },
handler = "1",
},
{
paths = { "/aa/:/" },
handler = "2",
},
})
assert.equal("1", router:match("/a"))
end)
end)

describe("parameter matching", function()
Expand Down Expand Up @@ -373,4 +387,4 @@ describe("Router", function()
--assert.same({ name = "name/id/match1" }, ctx.matched)
end)
end)
end)
end)
15 changes: 15 additions & 0 deletions src/resty/constants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

local constants = {
node_types = {


},
token_types = {
literal = 1,
variable = 2,
catchall = 3,
},

}

return constants
18 changes: 17 additions & 1 deletion src/resty/parser/style/advanced.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
-- - `{name}`: variable parameter
-- - `{*name}`: catch-all parameter

local constants = require "router-tree.constatns"

local byte = string.byte
local sub = string.sub

Expand All @@ -15,6 +17,8 @@ local BYTE_ASTERISK = byte("*")
local BYTE_LEFT_BRACKET = byte("{")
local BYTE_RIGHT_BRACKET = byte("}")

local TOKEN_TYPES = constants.token_types

local Parser = {}
local mt = { __index = Parser }

Expand Down Expand Up @@ -46,7 +50,7 @@ function Parser:next()
return nil
end

local char, token
local char, token, token_type
while self.pos <= self.path_n do
char = byte(self.path, self.pos)
--print("pos: " .. self.pos .. "(" .. string.char(char) .. ")")
Expand Down Expand Up @@ -126,4 +130,16 @@ function Parser:params()
return params
end

function Parser:token_type(token)
if byte(token) == BYTE_LEFT_BRACKET and
byte(token, #token) == BYTE_RIGHT_BRACKET then
if byte(token, 2) == BYTE_ASTERISK then
return TOKEN_TYPES.catchall
end
return TOKEN_TYPES.variable
end

return TOKEN_TYPES.literal
end

return Parser
43 changes: 42 additions & 1 deletion src/resty/trie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ local EMPTY = {}
local BYTE_SLASH = byte("/")
local BYTE_COLON = byte(":")
local BYTE_ASTERISK = byte("*")

local BYTE_LEFT_BRACKET = byte("{")
local BYTE_RIGHT_BRACKET = byte("}")

local TrieNode = {}
local mt = { __index = TrieNode }
Expand Down Expand Up @@ -95,6 +96,46 @@ local function insert_child(node, path, value, fn)
node:set(value, fn)
end

local function insert_child2(node, path, value, fn)
local parser = Parser.new(path, "advanced") -- 外部 :add 应该传一个 parser 进来?
local token = parser:next()
while token do
if byte(token) == BYTE_LEFT_BRACKET then
node.type = TYPES.PARAM
node.path = ""
node.path_n = 0
elseif byte(token) == BYTE_ASTERISK then
node.type = TYPES.CATCH_ALL
node.path = ""
node.path_n = 0
else
node.type = TYPES.STATIC
node.path = token
node.path_n = #token
end

token = parser:next()
if token then
local child = TrieNode.new()
node.children = { child }
node.children_n = node.children_n + 1
local c = str_sub(token, 1, 1)
if c ~= "{" then
node.indexs[c] = 1
else
-- variable children
parser:get_type(token)
-- 如果是 variable
-- 如果是 catchall

end
node = child
end
end

node:set(value, fn)
end

local function split(node, path, prefix_n)
local child = TrieNode.new({
path = str_sub(node.path, prefix_n + 1),
Expand Down

0 comments on commit fdf934d

Please sign in to comment.