diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-10-10 22:15:21 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-10-10 22:15:21 +0800 |
commit | 046b6785bc62ad8f8b0b95f18cf9cf39fa3bd346 (patch) | |
tree | 0f1c491db66b3f112e3af99e6ac777c01d2c985f /src/matcher | |
parent | 1ea77b03f7e976843b3816ff6a55e2997705124d (diff) | |
download | lua-language-server-046b6785bc62ad8f8b0b95f18cf9cf39fa3bd346.zip |
匹配第一个词
Diffstat (limited to 'src/matcher')
-rw-r--r-- | src/matcher/definition.lua | 58 | ||||
-rw-r--r-- | src/matcher/init.lua | 5 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/matcher/definition.lua b/src/matcher/definition.lua new file mode 100644 index 00000000..fe92ae50 --- /dev/null +++ b/src/matcher/definition.lua @@ -0,0 +1,58 @@ +local parser = require 'parser' + +local pos +local defs = {} +local scopes +local result + +local function getResult(name, p) + result = {name, p} + for k in pairs(defs) do + defs[k] = nil + end +end + +local function scopeInit() + scopes = {{}} +end + +local function scopeSet(name, p) + local scope = scopes[#scopes] + scope[name] = p +end + +local function scopeGet(name) + local scope = scopes[#scopes] + return scope[name] +end + +local function checkDifinition(name, p) + if pos < p or pos > p + #name then + return + end + getResult(name, scopeGet(name)) +end + +function defs.Name(p, name) + checkDifinition(name, p) + return name +end + +function defs.LocalVar(p, name) + scopeSet(name, p) +end + +return function (buf, pos_) + pos = pos_ + scopeInit() + parser.grammar(buf, 'Lua', defs) + + if not result then + return nil + end + local name, start = result[1], result[2] + if not start then + return nil + end + return start, start + #name - 1 +end diff --git a/src/matcher/init.lua b/src/matcher/init.lua new file mode 100644 index 00000000..8e2a01e2 --- /dev/null +++ b/src/matcher/init.lua @@ -0,0 +1,5 @@ +local api = { + definition = require 'matcher.definition', +} + +return api |