summary refs log tree commit diff stats
path: root/printtokens.lua
blob: 8f2d0f5356c8de5ff222e65f1d41f251af84aa86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--[[
    This file is part of luatokens.lua - pure-Lua Lua tokenizer
    Copyright (C) 2019  Soni L.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
--]]

local parser = require "parser"

do
    local filename = arg[1]
    local luatokens = require "luatokens"
    local file = io.open(filename, "r")
    local tokens = luatokens.defs
    local state, err, etoken, estate = parser.parse(tokens, function() return file:read(8192) end)
    if state then
        for i,v in ipairs(state) do
            v = luatokens.reverse_keywords[v] or luatokens.reverse_tokens[v] or v
            print(i, v) -- TODO formatting
        end
    else
        print("Parse error")
    end
end