summaryrefslogtreecommitdiff
path: root/script/parser/parse.lua
blob: e4935e7a5a7a6a7bf21809911a994b9173bf6fd4 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
local ast = require 'parser.ast'

return function (self, lua, mode, version, options)
    local errs  = {}
    local diags = {}
    local comms = {}
    local state = {
        version = version,
        lua = lua,
        root = {},
        errs = errs,
        diags = diags,
        comms = comms,
        options = options or {},
        pushError = function (err)
            if err.finish < err.start then
                err.finish = err.start
            end
            local last = errs[#errs]
            if last then
                if last.start <= err.start and last.finish >= err.finish then
                    return
                end
            end
            err.level = err.level or 'error'
            errs[#errs+1] = err
            return err
        end,
        pushDiag = function (code, info)
            if not diags[code] then
                diags[code] = {}
            end
            diags[code][#diags[code]+1] = info
        end,
        pushComment = function (comment)
            comms[#comms+1] = comment
        end
    }
    ast.init(state)
    local suc, res, err = xpcall(self.grammar, debug.traceback, self, lua, mode)
    ast.close()
    if not suc then
        return nil, res
    end
    if not res then
        state.pushError(err)
    end
    state.ast = res
    return state
end