Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Jan 28, 2024
1 parent 7dd5ae3 commit 85dc6bd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
3 changes: 2 additions & 1 deletion spec/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe("parser", function()
["/aa/{var1}/cc/{var2}"] = { "/aa/", "{var1}", "/cc/", "{var2}" },
["/user/profile.{format}"] = { "/user/profile.", "{format}" },
["/user/{filename}.{format}"] = { "/user/", "{filename}", ".", "{format}" },
["/aa/{name:[0-9]+}/{*suffix}"] = { "/aa/", "{name:[0-9]+}", "/", "{*suffix}" }
["/aa/{name:[0-9]+}/{*suffix}"] = { "/aa/", "{name:[0-9]+}", "/", "{*suffix}" },
["/user/{id:\\d+}/profile-{year:\\d{4}}.{format:(html|pdf)}"] = { "/user/", "{id:\\d+}", "/profile-", "{year:\\d{{4}}}", ".", "{format:(html|pdf)}" },
}

for path, expected_tokens in pairs(tests) do
Expand Down
14 changes: 14 additions & 0 deletions spec/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ describe("utils", function()
assert.equal(0, utils.lcp("", "/abcd"))
assert.equal(0, utils.lcp("a", "c"))
end)
it("is_digit()", function()
assert.is_true(utils.is_digit(string.byte("0")))
assert.is_true(utils.is_digit(string.byte("1")))
assert.is_true(utils.is_digit(string.byte("2")))
assert.is_true(utils.is_digit(string.byte("3")))
assert.is_true(utils.is_digit(string.byte("4")))
assert.is_true(utils.is_digit(string.byte("5")))
assert.is_true(utils.is_digit(string.byte("6")))
assert.is_true(utils.is_digit(string.byte("7")))
assert.is_true(utils.is_digit(string.byte("8")))
assert.is_true(utils.is_digit(string.byte("9")))
assert.is_false(utils.is_digit(string.byte("/")))
assert.is_false(utils.is_digit(string.byte(":")))
end)
end)
2 changes: 1 addition & 1 deletion src/parser/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local parsers = {
function Parser.new(style)
local parser = parsers[style]
if not parser then
return nil, "invalid style: " .. style
return nil, "unknown parser style: " .. style
end

return parser.new()
Expand Down
28 changes: 25 additions & 3 deletions src/parser/style/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function _M:reset()
self.anchor = 1
self.pos = 1
self.state = nil
self.bracket_depth = 0
end


Expand All @@ -58,7 +59,8 @@ function _M:next()
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) .. ")")
--local char_str = string.char(char)
--print("pos: " .. self.pos .. "(" .. char_str .. ")")
if self.state == nil or self.state == STATES.static then
if char == BYTE_LEFT_BRACKET then
if self.state == STATES.static then
Expand All @@ -67,12 +69,18 @@ function _M:next()
self.anchor = self.pos
end
self.state = STATES.variable_start
self.bracket_depth = 1
else
self.state = STATES.static
end
elseif self.state == STATES.variable_start then
if char == BYTE_RIGHT_BRACKET then
self.state = STATES.variable_end
if char == BYTE_LEFT_BRACKET then
self.bracket_depth = self.bracket_depth + 1
elseif char == BYTE_RIGHT_BRACKET then
self.bracket_depth = self.bracket_depth - 1
if self.bracket_depth == 0 then
self.state = STATES.variable_end
end
end
elseif self.state == STATES.variable_end then
self.state = STATES.static
Expand All @@ -93,6 +101,7 @@ function _M:next()
return token, self.token_type(token)
end


function _M:parse()
self:reset()

Expand All @@ -108,6 +117,7 @@ function _M:parse()
return tokens
end


function _M.token_type(token)
if byte(token) == BYTE_LEFT_BRACKET and
byte(token, #token) == BYTE_RIGHT_BRACKET then
Expand All @@ -120,6 +130,17 @@ function _M.token_type(token)
return TOKEN_TYPES.literal
end


function _M.token_pattern(token)
for i = 1, #token do
if byte(token, i) == BYTE_COLON then
return sub(token, i + 1, -2)
end
end
return nil
end


function _M.is_dynamic(path)
local patn_n = #path
for i = 1, patn_n do
Expand All @@ -131,6 +152,7 @@ function _M.is_dynamic(path)
return false
end


function _M:params()
local param_names_n = 0
local param_names = {}
Expand Down
6 changes: 6 additions & 0 deletions src/utils.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---

local str_byte = string.byte
local math_min = math.min
local type = type
Expand Down Expand Up @@ -123,6 +125,9 @@ local function lcp(str1, str2)
return n
end

local function is_digit(char)
return char >= 48 and char <= 57
end

local function readonly(t)
return setmetatable(t, {
Expand All @@ -139,4 +144,5 @@ return {
new_table = new_table,
is_luajit = is_luajit,
readonly = readonly,
is_digit = is_digit,
}

0 comments on commit 85dc6bd

Please sign in to comment.