forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlines.lua
More file actions
130 lines (113 loc) · 3.29 KB
/
Copy pathlines.lua
File metadata and controls
130 lines (113 loc) · 3.29 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
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
m = require 'lpeg'
m.setmaxstack(1000)
local CNT = 1
local LINE = 1
local FILE = _OPTS.input
local patt
_LINES = {
i2l = {},
}
local open = m.Cmt('/*{-{*/',
function ()
if _OPTS.join then
CNT = CNT - 1
end
return true
end )
local close = m.Cmt('/*}-}*/',
function ()
if _OPTS.join then
CNT = CNT + 1
end
return true
end )
local line = m.Cmt('\n',
function (s,i)
for i=#_LINES.i2l, i do
_LINES.i2l[i] = { FILE, LINE }
end
if CNT > 0 then
LINE = LINE + 1
end
return true
end )
local S = (m.S'\t\r ' + m.P'\\'*(1-m.P'\n')^0*'\n')
local SS = S^0
-- #line N "file" :: directive to set line/filename
local dir_lins = m.Cmt( m.P'#' *SS* m.P'line'^-1
*SS* m.C(m.R'09'^1) -- line
*SS* ( m.P'"' * m.C((1-m.P'"')^0) * m.P'"'
+ m.Cc(false) ) -- file
* (S + (m.P(1)-'\n'))^0 * '\n' -- \n
,
function (s,i, line, file)
LINE = line
FILE = file
return true
end )
patt = (line + open + close + dir_lins + 1)^0
_OPTS.source = '#line 1 "'.._OPTS.input..'"\n'.._OPTS.source
if _OPTS.cpp or _OPTS.cpp_args then
local args = _OPTS.cpp_args or ''
local orig = (_OPTS.input=='-' and 'tmp.ceu')
or _OPTS.input
local base, name = string.match(orig, '(.*/)(.*)')
if not base then
base = ''
name = orig
end
-- fin, fout, ferr
local fout = base..'_ceu_cpp_'..name
local ferr = fout..'.err'
local fin = fout..'.in'
local f = assert( io.open(fin,'w') )
f:write(_OPTS.source)
f:close()
-- execute cpp
local ret = os.execute(_OPTS.cpp_exe..' -C -dD '..args..' '..fin
..' > '..fout..' 2>'..ferr)
-- "-C": keep comments (because of nesting)
-- "-dD": repeat #define's (because of macros used as C functions)
os.remove(fin)
assert(ret == 0 or ret == true, assert(io.open(ferr)):read'*a')
os.remove(ferr)
-- remove blank lines of #define's (because of "-dD")
_OPTS.source = assert(io.open(fout)):read'*a'
_OPTS.source = string.gsub(_OPTS.source, '(#define[^\n]*)(\n)(\n)', '%1%3')
os.remove(fout)
--print(_OPTS.source)
end
patt:match(_OPTS.source..'\n')
-------------------------------------------------------------------------------
function DBG (...)
local t = {}
for i=1, select('#',...) do
t[#t+1] = tostring( select(i,...) )
end
if #t == 0 then
t = { [1]=debug.traceback() }
end
io.stderr:write(table.concat(t,'\t')..'\n')
end
function MAX (v1, v2)
return (v1 > v2) and v1 or v2
end
function WRN (cond, ln, msg)
ln = (_AST.isNode(ln) and ln.ln) or ln
if not cond then
DBG('WRN : '..ln[1]..' : line '..ln[2]..' : '..msg)
end
return cond
end
function ASR (cond, ln, msg)
ln = (_AST.isNode(ln) and ln.ln) or ln
if _RUNTESTS and (not cond) then
return assert(false, 'ERR : '..ln[1]..' : line '..ln[2]..' : '..msg)
else
if not cond then
DBG('ERR : '..ln[1]..' : line '..ln[2]..' : '..msg)
os.exit(1)
end
return cond
end
end