-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwed_lua.c
347 lines (273 loc) · 9.33 KB
/
wed_lua.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright (C) 2016 Richard Burke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "wed_lua.h"
#include "session.h"
#include "config.h"
#include "util.h"
#include "build_config.h"
/* Files and directories under WEDRUNTIME related to lua */
#define WED_LUA_DIR "lua"
#define WED_LUA_INIT_FILE "wed_init.lua"
#define WED_LUA_LEXERS_DIR "lexers"
/* Lua variables and functions created and initalised by WED_LUA_INIT_FILE */
#define WED_LUA_GLOBAL_VAR "wed"
#define WED_LUA_LOAD_LEXER_FUNC "load_lexer"
#define WED_LUA_TOKENIZE_FUNC "tokenize"
static int ls_add_to_package_path(LuaState *, const char *path);
/* Structure mapping a Scintillua token to a wed token */
typedef struct {
const char *sl_token; /* Scintillua token name */
SyntaxToken wed_token; /* wed syntax token */
} ScintilluaTokenMap;
static ScintilluaTokenMap ls_token_map[] = {
{ "bracebad" , ST_ERROR },
{ "bracelight" , ST_NORMAL },
{ "calltip" , ST_NORMAL },
{ "class" , ST_STATEMENT },
{ "comment" , ST_COMMENT },
{ "constant" , ST_CONSTANT },
{ "controlchar" , ST_NORMAL },
{ "default" , ST_NORMAL },
{ "embedded" , ST_NORMAL },
{ "error" , ST_ERROR },
{ "function" , ST_IDENTIFIER },
{ "identifier" , ST_NORMAL },
{ "indentguide" , ST_NORMAL },
{ "keyword" , ST_STATEMENT },
{ "label" , ST_STATEMENT },
{ "lexerpath" , ST_NORMAL },
{ "linenumber" , ST_NORMAL },
{ "nothing" , ST_NORMAL },
{ "number" , ST_CONSTANT },
{ "operator" , ST_NORMAL },
{ "preprocessor", ST_SPECIAL },
{ "regex" , ST_CONSTANT },
{ "string" , ST_CONSTANT },
{ "type" , ST_TYPE },
{ "variable" , ST_NORMAL }
};
LuaState *ls_new(Session *sess)
{
LuaState *ls = malloc(sizeof(LuaState));
RETURN_IF_NULL(ls);
ls->state = luaL_newstate();
if (ls->state == NULL) {
ls_free(ls);
return NULL;
}
luaL_openlibs(ls->state);
ls->sess = sess;
ls->token_map = new_hashmap();
if (ls->token_map == NULL) {
ls_free(ls);
return NULL;
}
const size_t token_num = ARRAY_SIZE(ls_token_map, ScintilluaTokenMap);
char token_style_name[100];
for (size_t k = 0; k < token_num; k++) {
if (!hashmap_set(ls->token_map, ls_token_map[k].sl_token,
&ls_token_map[k].wed_token)) {
ls_free(ls);
return NULL;
}
/* Custom tokens are mapped to token styles which wed can map to
* standard Scintillua token names. Below we add the token style names
* to the map */
snprintf(token_style_name, sizeof(token_style_name), "$(style.%s)",
ls_token_map[k].sl_token);
if (!hashmap_set(ls->token_map, token_style_name,
&ls_token_map[k].wed_token)) {
ls_free(ls);
return NULL;
}
}
return ls;
}
void ls_free(LuaState *ls)
{
if (ls == NULL) {
return;
}
if (ls->state != NULL) {
lua_close(ls->state);
}
free_hashmap(ls->token_map);
free(ls);
}
Status ls_init(LuaState *ls)
{
Session *sess = ls->sess;
const char *wrt = cf_string(sess->config, CV_WEDRUNTIME);
Status status = OUT_OF_MEMORY("Unable to allocate memory for file path");
#if WED_STATIC_BUILD
/* Load the lpeg library compiled into wed so that when wed_init.lua runs
* require('lpeg') a shared lpeg library is not used instead */
int luaopen_lpeg(lua_State *);
lua_getglobal(ls->state, "package");
lua_getfield(ls->state, -1, "preload");
lua_pushcfunction(ls->state, luaopen_lpeg);
lua_setfield(ls->state, -2, "lpeg");
lua_pop(ls->state, 2);
#endif
char *wed_lua_dir_path = NULL;
char *wed_lua_lexers_path = NULL;
char *wed_lua_init_script = NULL;
wed_lua_dir_path = concat(wrt, "/" WED_LUA_DIR);
if (wed_lua_dir_path == NULL) {
goto cleanup;
}
wed_lua_lexers_path = concat_all(4, wed_lua_dir_path, "/",
WED_LUA_LEXERS_DIR, "/?.lua");
if (wed_lua_lexers_path == NULL) {
goto cleanup;
}
if (!ls_add_to_package_path(ls, wed_lua_lexers_path)) {
goto cleanup;
}
wed_lua_init_script = concat_all(3, wed_lua_dir_path, "/",
WED_LUA_INIT_FILE);
if (wed_lua_init_script == NULL) {
goto cleanup;
}
if (luaL_dofile(ls->state, wed_lua_init_script)) {
status = st_get_error(ERR_LUA_ERROR, "Error occured when running wed "
"lua init script %s",
lua_tostring(ls->state, -1));
lua_pop(ls->state, 1);
goto cleanup;
}
status = STATUS_SUCCESS;
cleanup:
free(wed_lua_dir_path);
free(wed_lua_lexers_path);
free(wed_lua_init_script);
return status;
}
static int ls_add_to_package_path(LuaState *ls, const char *path)
{
lua_getglobal(ls->state, "package");
lua_getfield(ls->state, -1, "path");
const char *package_path = lua_tostring(ls->state, -1);
char *package_path_new = concat_all(3, package_path, ";", path);
if (package_path_new == NULL) {
lua_pop(ls->state, 2);
return 0;
}
lua_pop(ls->state, 1);
lua_pushstring(ls->state, package_path_new);
lua_setfield(ls->state, -2, "path");
lua_pop(ls->state, 1);
free(package_path_new);
return 1;
}
Status ls_load_syntax_def(LuaState *ls, const char *syntax_type)
{
lua_getglobal(ls->state, WED_LUA_GLOBAL_VAR);
if (!lua_istable(ls->state, -1)) {
lua_pop(ls->state, 1);
return st_get_error(ERR_LUA_ERROR, "Unable to load variable %s",
WED_LUA_GLOBAL_VAR);
}
lua_getfield(ls->state, -1, WED_LUA_LOAD_LEXER_FUNC);
if (!lua_isfunction(ls->state, -1)) {
lua_pop(ls->state, 2);
return st_get_error(ERR_LUA_ERROR, "Unable to load function %s.%s",
WED_LUA_GLOBAL_VAR, WED_LUA_LOAD_LEXER_FUNC);
}
lua_insert(ls->state, -2);
lua_pushstring(ls->state, syntax_type);
Status status = STATUS_SUCCESS;
if (lua_pcall(ls->state, 2, 0, 0) != 0) {
status = st_get_error(ERR_LUA_ERROR, "Loading lexer %s failed: %s",
syntax_type, lua_tostring(ls->state, -1));
lua_pop(ls->state, 1);
}
return status;
}
SyntaxMatches *ls_generate_matches(LuaState *ls, const char *syntax_type,
const char *str, size_t str_len)
{
lua_getglobal(ls->state, WED_LUA_GLOBAL_VAR);
if (!lua_istable(ls->state, -1)) {
lua_pop(ls->state, 1);
return NULL;
}
lua_getfield(ls->state, -1, WED_LUA_TOKENIZE_FUNC);
if (!lua_isfunction(ls->state, -1)) {
lua_pop(ls->state, 2);
return NULL;
}
lua_insert(ls->state, -2);
lua_pushstring(ls->state, syntax_type);
lua_pushlstring(ls->state, str, str_len);
if (lua_pcall(ls->state, 3, 1, 0) != 0) {
lua_pop(ls->state, 1);
return NULL;
}
if (!lua_istable(ls->state, -1)) {
lua_pop(ls->state, 1);
return NULL;
}
SyntaxMatches *syn_matches = sy_new_matches(0);
if (syn_matches == NULL) {
lua_pop(ls->state, 1);
return NULL;
}
size_t token_array_size =
#if LUA_VERSION_NUM > 501
lua_rawlen(ls->state, -1);
#else
lua_objlen(ls->state, -1);
#endif
const char *sl_token;
lua_Number end_pos;
SyntaxToken *wed_token;
SyntaxMatch syn_match;
size_t token_length;
size_t offset = 0;
for (size_t k = 1; k + 1 <= token_array_size; k += 2) {
lua_rawgeti(ls->state, -1, k);
if (!lua_isstring(ls->state, -1)) {
lua_pop(ls->state, 1);
continue;
}
sl_token = lua_tostring(ls->state, -1);
lua_rawgeti(ls->state, -2, k + 1);
if (!lua_isnumber(ls->state, -1)) {
lua_pop(ls->state, 2);
continue;
}
end_pos = lua_tonumber(ls->state, -1);
lua_pop(ls->state, 2);
token_length = (end_pos - 1) - offset;
if (sl_token != NULL && strstr(sl_token, "whitespace") == NULL) {
wed_token = hashmap_get(ls->token_map, sl_token);
syn_match.token = wed_token == NULL ? ST_NORMAL : *wed_token;
syn_match.offset = offset;
syn_match.length = token_length;
sy_add_match(syn_matches, &syn_match);
}
offset += token_length;
}
lua_pop(ls->state, 1);
return syn_matches;
}