Skip to content

Commit

Permalink
Support line numbers for pl.lexer with string arg
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Feb 2, 2016
1 parent 2c02bda commit 1923361
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lua/pl/lexer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function lexer.scan(s,matches,filter,options)
matches = plain_matches
end
local function lex()
local line_nr = 0
local line_nr = file and 0 or 1
local next_line = file and file:read()
local sz = file and 0 or #s
local idx = 1
Expand Down Expand Up @@ -193,6 +193,11 @@ function lexer.scan(s,matches,filter,options)
lexer.finished = idx > sz
res = fun(tok, options, findres)
end
if not file and tok:find("\n") then
-- Update line number.
local _, newlines = tok:gsub("\n", {})
line_nr = line_nr + newlines
end
if res then
local tp = type(res)
-- insert a token list
Expand Down Expand Up @@ -258,9 +263,10 @@ function lexer.getline (tok)
end

--- get current line number.
-- Only available if the input source is a file-like object.
-- @param tok a token stream
-- @return the line number and current column
-- @return the line number.
-- if the input source is a file-like object,
-- also return the column.
function lexer.lineno (tok)
return tok(0)
end
Expand Down
10 changes: 10 additions & 0 deletions tests/test-lexer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ test_scan([['' "" " \\" '\'' "'"]], nil, nil, {
{'char', "\\'"},
{'string', "'"}
}, 'cpp')

local iter = lexer.lua([[
foo
bar
]])

iter()
asserteq(lexer.lineno(iter), 1)
iter()
asserteq(lexer.lineno(iter), 2)

0 comments on commit 1923361

Please sign in to comment.