summaryrefslogtreecommitdiff
path: root/src/parser/calcline.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser/calcline.lua')
-rw-r--r--src/parser/calcline.lua40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/parser/calcline.lua b/src/parser/calcline.lua
new file mode 100644
index 00000000..ef0bfa3c
--- /dev/null
+++ b/src/parser/calcline.lua
@@ -0,0 +1,40 @@
+local m = require 'lpeglabel'
+
+local row
+local fl
+local NL = (m.P'\r\n' + m.S'\r\n') * m.Cp() / function (pos)
+ row = row + 1
+ fl = pos
+end
+local ROWCOL = (NL + m.P(1))^0
+
+local function rowcol(str, n)
+ row = 1
+ fl = 1
+ ROWCOL:match(str:sub(1, n))
+ local col = n - fl + 1
+ return row, col
+end
+
+local NL = m.P'\r\n' + m.S'\r\n'
+
+local function line(str, row)
+ local count = 0
+ local res
+ local LINE = m.Cmt((1 - NL)^0, function (_, _, c)
+ count = count + 1
+ if count == row then
+ res = c
+ return false
+ end
+ return true
+ end)
+ local MATCH = (LINE * NL)^0 * LINE
+ MATCH:match(str)
+ return res
+end
+
+return {
+ rowcol = rowcol,
+ line = line,
+}