summaryrefslogtreecommitdiff
path: root/Tools/LuaMacro/luam
blob: bd0226931f91d2f8cca32f29c1928bc734e1f096 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env lua
--[[--
Front end for LuaMacro, a Lua macro preprocessor.

The default action is to preprocess and run a Lua file. To just dump
the preprocessed output, use the `-d` flag. Like `lua`, the `-l` flag can
be used to load a library first, but you need to explicitly say `-i` to
get an interactive prompt.

The package loader is modified so that `require 'mod'` will preprocess `mod` if it is found as `mod.m.lua`.

Dumping is the only action available when preprocessing C code with `-C`.

@script luam
]]

-- adjust the path so that this script can see the macro package
local path = arg[0]:gsub('[^/\\]+$','')
package.path = package.path .. ';' .. path .. '?.lua;'..path .. 'macro/?.lua'
local macro = require 'macro'
require 'macro.builtin'

--- Using luam.
-- @usage follows
local usage = [[
LuaMacro 2.5.0, a Lua macro preprocessor and runner
    -l  require a library
    -e  statement to be executed
    -V  set a variable (-VX or -VY=1)
    -c  error context to be shown (default 2)
    -d  dump preprocessed output to stdout
    -o  write to this file
    -C  C lexer
    -N  No #line directives when generating C
    -i  interactive prompt
    -v  verbose error trace
    <input>    Lua source file
]]

-- parsing the args, the hard way:
local takes_value = {l = '', e = '', c = 2, o = '',V = ';'}

local args = {}
local idx,i = 1,1
while i <= #arg do
    local a = arg[i]
    local flag = a:match '^%-(.+)'
    local val
    if flag then
        if #flag > 1 then -- allow for -lmod, like Lua
            val = flag:sub(2)
            flag = flag:sub(1,1)
        end
        -- grab the next argument if we need a value
        if takes_value[flag] and not val then
            i = i + 1
            val = arg[i]
        end
        -- convert the argument, if required
        local def = takes_value[flag]
        if type(def) == 'number' then
            val = tonumber(val)
        elseif def == ';' and args[flag] then
            val = args[flag]..';'..val
        end
        args[flag] = val or true
    else
        args[idx] = a
        idx = idx + 1
    end
    i = i + 1
end

if not args[1] and not args.i then
    print(usage)
    os.exit()
elseif args[1] then
    args.input_name = args[1]
    args.input,err = io.open(args[1],'r')
    if err then return print(err) end
    table.remove(args,1)
end
-- set defaults, if flags not specified
for k,v in pairs(takes_value) do
    if not args[k] then
        args[k] = v
    end
end

---------- compiling and running the output ------
-- the tricky bit here is presenting the errors so that they refer to the
-- original line numbers. In addition, we also present a few lines of context
-- in the output.

local function lookup_line (lno,li)
    for i = 1,#li-1 do
        --print(li[i].il,li[i].ol,lno,'match')
        if lno < li[i+1].ol then
            return li[i].il + (lno - li[i].ol) - 1
        end
    end
    return li[#li].il + (lno - li[#li].ol) - 1
end

-- iterating over all lines in a string can be awkward;
-- gmatch doesn't handle the empty-line cases properly.
local function split_nl (t)
    local k1 = 1
    local k2 = t:find ('[\r\n]',k1)
    return function()
        if not k2 then return nil end
        local res = t:sub(k1,k2-1)
        k1 = k2+1
        k2 = t:find('[\r\n]',k1)
        return res
    end
end

local function fix_error_trace (err,li)
    local strname,lno = err:match '%[string "(%S+)"%]:(%d+)'
    local ino
    if strname then
        lno = tonumber(lno)
        if li then
            ino = lookup_line(lno,li)
            err = err:gsub('%[string "%S+"%]:'..(lno or '?')..':',strname..':'..(ino or '?'))
        end
    end
    return err,lno,ino
end

local function runstring (code,name,li,...)
    local res,err = loadstring(code,name)
    local lno,ok
    if not res then
        err,lno,ino = fix_error_trace(err,li)
        if ino then
            print 'preprocessed context of error:'
            local l1,l2 = lno-args.c,lno+args.c
            local l = 1
            for line in split_nl(code) do
                if l >= l1 and l <= l2 then
                    if l == lno then io.write('*') else io.write(' ') end
                    print(l,line)
                end
                l = l + 1
            end
        end
        io.stderr:write(err,'\n')
        os.exit(1)
    end
    ok,err = xpcall(function(...) return res(...) end, debug.traceback)
    if not ok then
        err = err:gsub("%[C%]: in function 'xpcall'.+",'')
        if li then
            repeat
                err,lno = fix_error_trace(err,li)
            until not lno
        end
        io.stderr:write(err,'\n')
    end
    return ok
end

local function subst (ins,name)
    local C
    if args.C then
        C = args.N and true or 'line'
    end
    return macro.substitute_tostring(ins,name,C,args.v)
end

local function subst_runstring (ins,name,...)
    local buf,li = subst(ins,name)
    if not buf then
        io.stderr:write(li,'\n')
        os.exit(1)
    end
    if args.d or args.C or args.o ~= '' then
        if args.o == '' then
            print(buf)
        else
            local f = io.open(args.o,'w')
            f:write(buf)
            f:close()
        end
    else
        return runstring(buf,name,li,...)
    end
end

-- Lua 5.1/5.2 compatibility
local pack = table.pack
if not pack then
    function pack(...)
        return {n=select('#',...),...}
    end
end
if not unpack then unpack = table.unpack end

local function eval(code)
    local status,val,f,err,rcnt
    code,rcnt = code:gsub('^%s*=','return ')
    f,err = loadstring(code,'TMP')
    if f then
        res = pack(pcall(f))
        if not res[1] then err = res[2]
        else
            return res
        end
    end
    if err then
        err = tostring(err):gsub('^%[string "TMP"%]:1:','')
        return {nil,err}
    end
end

local function interactive_loop ()
    os.execute(arg[-1]..' -v') -- for the Lua copyright
    print 'Lua Macro 2.5.0 Copyright (C) 2007-2011 Steve Donovan'

    local function readline()
        io.write(_PROMPT or '> ')
        return io.read()
    end

    require 'macro.all'
    _G.macro = macro
    macro.define 'quit os.exit()'
    macro._interactive = true

    local line = readline()
    while line do
        local s,err = subst(line..'\n')
        if not s then
            err = err:gsub('.-:%d+:','')
            print('macro error: '..err)
        elseif not s:match '^%s*$' then
            if args.d then print(s) end
            local res = eval(s)
            if not res[1] then
                print('expanded: '..s)
                print('error: '..res[2])
            elseif res[2] ~= nil then
                print(unpack(res,2))
           end
        end
        line = readline()
    end
end

macro.set_package_loader()

if args.l ~= '' then require(args.l) end

if args.V ~= ';' then
    for varset in args.V:gmatch '([^;]+)' do
        local sym,val = varset:match '([^=]+)=(.+)'
        if not sym then
            sym = varset
            val = true
        end
        _G[sym] = val
    end
end

require 'macro.ifelse'

if args.e ~= '' then
    subst_runstring(args.e,"<temp>")
else
    if args.input then
        arg = args
        arg[0] = args.input_name
        arg[-1] = 'luam'
        subst_runstring(args.input,args.input_name,unpack(args))
    elseif args.i then
        interactive_loop()
    end
end