22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
..
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
...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
for k, v in pairs(list) do
if fn(k,v) then new[k] = v end
end
return new
end
local function
main(input, output, log, mode, vars)
local doc = ct.parse(input.stream, input.src, mode)
input.stream:close()
if mode['parse:show-tree'] then
log:write(dump(doc))
end
if not mode['render:format'] then
error 'what output format should i translate the input to?'
end
if mode['render:format'] == 'none' then return 0 end
if not ct.render[mode['render:format']] then
ct.exns.unimpl('output format ā%sā unsupported', mode['render:format']):throw()
................................................................................
-- this is kind of gross but the context object belongs to the parser,
-- not the renderer, so that's not a suitable place for this information
doc.stage = {
kind = 'render';
format = mode['render:format'];
mode = mode;
}
output:write(ct.render[mode['render:format']](doc, render_opts))
return 0
end
local inp,outp,log = io.stdin, io.stdout, io.stderr
local function entry_cli()
local mode, vars, input = default_mode, {}, {
stream = inp;
src = {
file = '(stdin)';
}
}
local optnparams = function(o)
local param_opts = {
out = 1;
log = 1;
define = 2; -- key value
['mode-set'] = 1;
['mode-clear'] = 1;
mode = 2;
}
return param_opts[o] or 0
end
local optmap = {
o = 'out';
l = 'log';
d = 'define';
V = 'version';
h = 'help';
y = 'mode-set', n = 'mode-clear';
m = 'mode';
}
local checkmodekey = function(key)
if not key:match '[^:]+:.+' then
ct.exns.cli('invalid mode key %s', key):throw()
end
return key
................................................................................
ct.exns.cli 'cannot define variable in restricted namespace':throw()
end
vars[key] = value
end;
mode = function(key,value) mode[checkmodekey(key)] = value end;
['mode-set'] = function(key) mode[checkmodekey(key)] = true end;
['mode-clear'] = function(key) mode[checkmodekey(key)] = false end;
}
local args = {}
local keepParsing = true
do local i = 1 while i <= #arg do local v = arg[i]
local execLongOpt = function(longopt)
if not onswitch[longopt] then
................................................................................
if args[1] and args[1] ~= '' then
local file = io.open(arg[1], "rb")
if not file then error('unable to load file ' .. args[1]) end
input.stream = file
input.src.file = args[1]
end
return main(input, outp, log, mode, vars)
end
local ok, e = pcall(entry_cli)
-- local ok, e = true, entry_cli()
if not ok then
local str = 'translation failure'
if ss.exn.is(e) then
|
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
|
>
>
>
>
|
|
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
..
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
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
for k, v in pairs(list) do
if fn(k,v) then new[k] = v end
end
return new
end
local function
main(input, output, log, mode, suggestions, vars)
local doc = ct.parse(input.stream, input.src, mode)
input.stream:close()
if mode['parse:show-tree'] then
log:write(dump(doc))
end
-- the document has now had a chance to give its say; if it hasn't specified
-- any modes of its own, we now merge in the 'weak modes' (suggestions)
for k,v in pairs(suggestions) do
if not mode[k] then mode[k] = v end
end
if not mode['render:format'] then
error 'what output format should i translate the input to?'
end
if mode['render:format'] == 'none' then return 0 end
if not ct.render[mode['render:format']] then
ct.exns.unimpl('output format ā%sā unsupported', mode['render:format']):throw()
................................................................................
-- this is kind of gross but the context object belongs to the parser,
-- not the renderer, so that's not a suitable place for this information
doc.stage = {
kind = 'render';
format = mode['render:format'];
mode = mode;
suggestions = suggestions;
}
output:write(ct.render[mode['render:format']](doc, render_opts))
return 0
end
local inp,outp,log = io.stdin, io.stdout, io.stderr
local function entry_cli()
local suggestions, vars, input = default_mode, {}, {
stream = inp;
src = {
file = '(stdin)';
}
}
local mode = {}
local optnparams = function(o)
local param_opts = {
out = 1;
log = 1;
define = 2; -- key value
['mode-set'] = 1;
['mode-clear'] = 1;
mode = 2;
['mode-set-weak'] = 1;
['mode-clear-weak'] = 1;
['mode-weak'] = 2;
}
return param_opts[o] or 0
end
local optmap = {
o = 'out';
l = 'log';
d = 'define';
V = 'version';
h = 'help';
y = 'mode-set', Y = 'mode-set-weak';
n = 'mode-clear', N = 'mode-clear-weak';
m = 'mode', M = 'mode-weak';
}
local checkmodekey = function(key)
if not key:match '[^:]+:.+' then
ct.exns.cli('invalid mode key %s', key):throw()
end
return key
................................................................................
ct.exns.cli 'cannot define variable in restricted namespace':throw()
end
vars[key] = value
end;
mode = function(key,value) mode[checkmodekey(key)] = value end;
['mode-set'] = function(key) mode[checkmodekey(key)] = true end;
['mode-clear'] = function(key) mode[checkmodekey(key)] = false end;
['mode-weak'] = function(key,value) suggestions[checkmodekey(key)] = value end;
['mode-set-weak'] = function(key) suggestions[checkmodekey(key)] = true end;
['mode-clear-weak'] = function(key) suggestions[checkmodekey(key)] = false end;
}
local args = {}
local keepParsing = true
do local i = 1 while i <= #arg do local v = arg[i]
local execLongOpt = function(longopt)
if not onswitch[longopt] then
................................................................................
if args[1] and args[1] ~= '' then
local file = io.open(arg[1], "rb")
if not file then error('unable to load file ' .. args[1]) end
input.stream = file
input.src.file = args[1]
end
return main(input, outp, log, mode, suggestions, vars)
end
local ok, e = pcall(entry_cli)
-- local ok, e = true, entry_cli()
if not ok then
local str = 'translation failure'
if ss.exn.is(e) then
|