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
|
/*
* LSPIPAT - LUA SPIPAT WRAPPER
* Copyright (C) 2010, Robin Haberkorn
* License: LGPL
*
* CORE: UNARY OPERATORS (ALSO USED AS PRIMITIVES)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdbool.h>
#include <string.h>
#include "lspipat.h"
static void
setcurFncHandler(unsigned pos, void *global __attribute__((unused)), void *local)
{
struct simplefncRefs *simplefnc = local;
lua_State *L = simplefnc->cb.L;
lua_rawgeti(L, LUA_REGISTRYINDEX, simplefnc->cb.function);
lua_pushinteger(L, pos);
lua_rawgeti(L, LUA_REGISTRYINDEX, simplefnc->cb.cookie);
#if 0
lua_rawgeti(L, LUA_REGISTRYINDEX, *(int *)global);
#endif
lua_call(L, 2, 0);
}
/*
* if called as an operator, there will be a nil on top of the stack
*/
LUA_SIG(l_setcur)
{
int top = lua_gettop(L);
PATTERN_WRAPPER *new;
struct simplefncRefs *simplefnc;
luaL_argcheck(L, top == 1 || top == 2, top, L_NUMBER);
luaL_argcheck(L, lua_isfunction(L, 1), 1, L_TYPE);
if (!(new = lua_newuserdata(L, sizeof(PATTERN_WRAPPER))))
L_ERROR(L_ALLOC);
memset(new, 0, sizeof(PATTERN_WRAPPER));
lua_insert(L, 1); /* move wrapper to bottom */
if (top == 1)
lua_pushnil(L); /* cookie will be LUA_REFNIL */
new->type = PATTERN_SIMPLEFNC;
simplefnc = &new->u.simplefnc;
simplefnc->cb.L = L;
simplefnc->cb.cookie = luaL_ref(L, LUA_REGISTRYINDEX);
simplefnc->cb.function = luaL_ref(L, LUA_REGISTRYINDEX);
/* wrapper at top again */
new->pattern = spipat_setcur_fnc(setcurFncHandler, simplefnc);
if (!new->pattern)
L_ERROR(L_ALLOC);
luaL_getmetatable(L, PATTERN_MT);
lua_setmetatable(L, -2);
return 1;
}
static void
predFncHandler(void *global __attribute__((unused)), void *local, struct dynamic *ret)
{
struct retfncRefs *retfnc = local;
lua_State *L = retfnc->cb.L;
lua_rawgeti(L, LUA_REGISTRYINDEX, retfnc->cb.function);
lua_rawgeti(L, LUA_REGISTRYINDEX, retfnc->cb.cookie);
#if 0
lua_rawgeti(L, LUA_REGISTRYINDEX, *(int *)global);
#endif
lua_call(L, 1, 1);
switch (lua_type(L, -1)) {
case LUA_TNUMBER:
case LUA_TSTRING: {
VString *str = &ret->val.str;
ret->type = DY_VSTR;
str->ptr = lua_tolstring(L, -1, (size_t *)&str->len);
str->release = retfncUnrefRet;
str->cookie = retfnc;
/*
* Register value so Lua doesn't free it until spipat
* doesn't need it anymore (value has to be popped now)
*/
retfnc->ret = luaL_ref(L, LUA_REGISTRYINDEX);
return;
}
case LUA_TNIL: /* default behaviour: continue matching (Succeed) */
ret->type = DY_BOOL;
ret->val.pred = true;
lua_pop(L, 1);
return;
case LUA_TBOOLEAN:
ret->type = DY_BOOL;
ret->val.pred = lua_toboolean(L, -1);
lua_pop(L, 1);
return;
case LUA_TUSERDATA: { /* FIXME: check whether it's really a Pattern */
PATTERN_WRAPPER *wrapper = lua_touserdata(L, -1);
if (!wrapper->pattern) {
lua_pop(L, 1);
L_ERROR(L_RETURN);
}
ret->type = DY_PAT;
ret->val.pat.p = wrapper->pattern;
ret->val.pat.release = retfncUnrefRet;
ret->val.pat.cookie = retfnc;
/*
* Register value so Lua doesn't free it until spipat
* doesn't need it anymore (value has to be popped now)
*/
retfnc->ret = luaL_ref(L, LUA_REGISTRYINDEX);
return;
}
default:
lua_pop(L, 1);
L_ERROR(L_RETURN);
}
/* not reached */
}
LUA_SIG(l_pred)
{
int top = lua_gettop(L);
PATTERN_WRAPPER *new;
struct retfncRefs *retfnc;
luaL_argcheck(L, top == 1 || top == 2, top, L_NUMBER);
luaL_argcheck(L, lua_isfunction(L, 1), 1, L_TYPE);
if (!(new = lua_newuserdata(L, sizeof(PATTERN_WRAPPER))))
L_ERROR(L_ALLOC);
memset(new, 0, sizeof(PATTERN_WRAPPER));
lua_insert(L, 1); /* move wrapper to bottom */
if (top == 1)
lua_pushnil(L); /* cookie will be LUA_REFNIL */
new->type = PATTERN_RETFNC;
retfnc = &new->u.retfnc;
retfnc->cb.L = L;
retfnc->cb.cookie = luaL_ref(L, LUA_REGISTRYINDEX);
retfnc->cb.function = luaL_ref(L, LUA_REGISTRYINDEX);
/* wrapper at top again */
new->pattern = spipat_dynamic_fnc(predFncHandler, retfnc);
if (!new->pattern)
L_ERROR(L_ALLOC);
luaL_getmetatable(L, PATTERN_MT);
lua_setmetatable(L, -2);
return 1;
}
|