-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
217 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
local Parser = {} | ||
|
||
local parsers = {} | ||
do | ||
local styles = { "default" } | ||
for _, style in ipairs(styles) do | ||
local module = require("router-tree.parser.style." .. style) | ||
parsers[style] = module | ||
end | ||
end | ||
|
||
|
||
-- return a parser instance | ||
function Parser.parse(path, style) | ||
local parser = parsers[style] | ||
if not parser then | ||
return nil, "invalid style: " .. style | ||
end | ||
|
||
local instance = parser.new(path) | ||
return instance | ||
end | ||
|
||
|
||
return Parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- TODO | ||
|
||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
local byte = string.byte | ||
local sub = string.sub | ||
|
||
local BYTE_SLASH = byte("/") | ||
local BYTE_COLON = byte(":") | ||
local BYTE_ASTERISK = byte("*") | ||
|
||
local Parser = {} | ||
local mt = { __index = Parser } | ||
|
||
local STATES = { | ||
none = 0, | ||
finish = 1, | ||
colon = 2, | ||
asterisk = 3, | ||
static = 4, | ||
} | ||
|
||
function Parser.new(path) | ||
local self = { | ||
path = path, | ||
anchor = 1, | ||
pos = 1, | ||
path_n = #path, | ||
state = STATES.none, | ||
} | ||
return setmetatable(self, mt) | ||
end | ||
|
||
function Parser:next() | ||
if self.state == STATES.finish then | ||
return nil | ||
end | ||
|
||
local char, token | ||
while self.pos <= self.path_n do | ||
char = byte(self.path, self.pos) | ||
--print("pos: " .. self.pos .. "(" .. string.char(char) .. ")") | ||
if self.state == STATES.none or self.state == STATES.static then | ||
if char == BYTE_COLON then | ||
if self.state == STATES.static then | ||
local anchor = self.anchor | ||
self.anchor = self.pos | ||
token = sub(self.path, anchor, self.pos - 1) | ||
end | ||
self.state = STATES.colon | ||
elseif char == BYTE_ASTERISK then | ||
if self.state == STATES.static then | ||
local anchor = self.anchor | ||
self.anchor = self.pos | ||
token = sub(self.path, anchor, self.pos - 1) | ||
end | ||
self.state = STATES.asterisk | ||
else | ||
self.state = STATES.static | ||
end | ||
elseif self.state == STATES.colon then | ||
if char == BYTE_SLASH then | ||
self.state = STATES.none | ||
local anchor = self.anchor | ||
self.anchor = self.pos | ||
return sub(self.path, anchor, self.pos - 1) | ||
end | ||
elseif self.state == STATES.asterisk then | ||
-- do nothing | ||
end | ||
self.pos = self.pos + 1 | ||
|
||
if token then | ||
return token | ||
end | ||
end | ||
|
||
self.state = STATES.finish | ||
return sub(self.path, self.anchor, self.pos) | ||
end | ||
|
||
return Parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.