-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
44 lines (38 loc) · 1.4 KB
/
Copy pathinit.lua
File metadata and controls
44 lines (38 loc) · 1.4 KB
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
-- MoonScript parser. The native module is generated from
-- moonscript_parser/grammar.lua by pgen (https://github.com/leafo/pgen).
--
-- local parse = require "moonscript_parser"
-- local tree, err = parse.string(moon_code)
--
-- Returns the AST shapes consumed by the moonscript compiler. On failure,
-- err is a line/column message built from the parser's furthest-failure
-- position, or from the offending node for assignment errors.
local parser = require "moonscript_parser.native"
local errors = require "moonscript_parser.errors"
local parse = {}
function parse.string(str)
-- Grammar transforms run during parse(); format_assign raises
-- error({node, msg}) for invalid assignment targets
local ok, result, label, err_pos = pcall(parser.parse, str)
if not ok then
if type(result) == "table" then
local node, msg = result[1], result[2]
local pos = type(node) == "table" and node[-1]
if pos then
return nil, errors.format(str, pos, msg)
end
return nil, "failed to parse: " .. tostring(msg)
end
-- errors thrown by the parser itself (e.g. the recursion depth limit)
-- are returned like any other parse failure, not raised
return nil, tostring(result)
end
if not result then
if err_pos then
return nil, errors.format(str, err_pos, label or "failed to parse")
end
return nil, "failed to parse"
end
return result
end
return parse