aboutsummaryrefslogtreecommitdiff
path: root/core/errors.nom
blob: 8c6261a392be628b98b91694e5813f1ab7e82fac (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#
    This file contains basic error reporting code

use "core/metaprogramming.nom"

compile [traceback] to: Lua value "debug.traceback()"
compile [traceback %] to: Lua value "debug.traceback('', \(% as lua expr))"
compile [barf] to: Lua "error(nil, 0);"
compile [barf %msg] to: Lua "error(\(%msg as lua expr), 0);"
compile [assume %condition] to
    lua> "local \%assumption = 'Assumption failed: '..tostring(nomsu:tree_to_nomsu(\%condition));"
    return
        Lua ".."
            if not \(%condition as lua expr) then
                error(\(quote "\%assumption"), 0);
            end

compile [assume %condition or barf %message] to
    Lua ".."
        if not \(%condition as lua expr) then
            error(\(%message as lua expr), 0);
        end

# Try/except
immediately
    compile [..]
        try %action and if it succeeds %success or if it barfs %msg %fallback
        try %action and if it barfs %msg %fallback or if it succeeds %success
    ..to
        Lua ".."
            do
                local fell_through = false
                local err, erred = nil, false
                local ok, ret = xpcall(function()
                    \(%action as lua statements)
                    fell_through = true
                end, function(\(%msg as lua expr))
                    local ok, ret = pcall(function()
                        \(%fallback as lua statements)
                    end)
                    if not ok then err, erred = ret, true end
                end)
                if ok then
                    \(%success as lua statements)
                    if not fell_through then
                        return ret
                    end
                elseif erred then
                    error(err, 0)
                end
            end
immediately
    parse [..]
        try %action and if it succeeds %success or if it barfs %fallback
        try %action and if it barfs %fallback or if it succeeds %success
    ..as: try %action and if it succeeds %success or if it barfs (=lua "") %fallback
immediately
    parse [try %action] as
        try %action and if it succeeds: do nothing
        ..or if it barfs: do nothing
    parse [try %action and if it barfs %fallback] as
        try %action and if it succeeds: do nothing
        ..or if it barfs %fallback
    parse [try %action and if it barfs %msg %fallback] as
        try %action and if it succeeds: do nothing
        ..or if it barfs %msg %fallback
    parse [try %action and if it succeeds %success] as
        try %action and if it succeeds %success or if it barfs: do nothing