summaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-11-19 16:16:27 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-11-19 16:16:27 +0800
commited1bf904bc15c97344dc59745c32d3f741eb1449 (patch)
tree5996f587858d62241f2bd483c7bcc0affca6e4d8 /src/parser
parent14f6d57ba711eb2d64470270ee7191181bfa25dc (diff)
downloadlua-language-server-ed1bf904bc15c97344dc59745c32d3f741eb1449.zip
转到定义
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/calcline.lua24
-rw-r--r--src/parser/init.lua5
2 files changed, 26 insertions, 3 deletions
diff --git a/src/parser/calcline.lua b/src/parser/calcline.lua
index ef0bfa3c..0c692a85 100644
--- a/src/parser/calcline.lua
+++ b/src/parser/calcline.lua
@@ -7,7 +7,6 @@ local NL = (m.P'\r\n' + m.S'\r\n') * m.Cp() / function (pos)
fl = pos
end
local ROWCOL = (NL + m.P(1))^0
-
local function rowcol(str, n)
row = 1
fl = 1
@@ -16,6 +15,28 @@ local function rowcol(str, n)
return row, col
end
+local function position(str, _row, _col)
+ local cur = 1
+ local row = 1
+ while true do
+ if row == _row then
+ return cur + _col - 1
+ elseif row > _row then
+ return cur - 1
+ end
+ local pos = str:find('[\r\n]', cur)
+ if not pos then
+ return #str
+ end
+ row = row + 1
+ if str:sub(pos, pos+1) == '\r\n' then
+ cur = pos + 2
+ else
+ cur = pos + 1
+ end
+ end
+end
+
local NL = m.P'\r\n' + m.S'\r\n'
local function line(str, row)
@@ -37,4 +58,5 @@ end
return {
rowcol = rowcol,
line = line,
+ position = position,
}
diff --git a/src/parser/init.lua b/src/parser/init.lua
index dd60dbc1..3216fa39 100644
--- a/src/parser/init.lua
+++ b/src/parser/init.lua
@@ -1,6 +1,7 @@
local api = {
- grammar = require 'parser.grammar',
- split = require 'parser.split',
+ grammar = require 'parser.grammar',
+ split = require 'parser.split',
+ calcline = require 'parser.calcline',
}
return api