-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepdir.lua
221 lines (174 loc) · 4.7 KB
/
prepdir.lua
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
-- https://github.com/Lautenschlager-id/prepdir
local prepdir
do
local format = string.format
local gsub = string.gsub
local match = string.match
local sub = string.sub
local rep = string.rep
local conditionalSymbols = {
['&'] = "and",
['|'] = "or",
['!'] = "not ",
["!="] = "~="
}
local symbolReplace = "[&|!]=?"
local prefix = "()\t*@#"
local endOfLine = "\r?\n()"
local expression = "([%w_ %+%-%*/%[%]%(%){}&|!%.<>~='\"]+)"
local tokenCallbacks = { }
local tokenMatchers = {
IF = prefix .. "IF " .. expression .. endOfLine,
ELIF = prefix .. "ELIF " .. expression .. endOfLine,
ELSE = prefix .. "ELSE" .. endOfLine,
ENDIF = prefix .. "ENDIF" .. endOfLine,
__NEXT = prefix .. "(%S+)"
}
local READING_CHUNK, chunks = false
tokenCallbacks.IF = function(src, endPos)
if READING_CHUNK then return end
local iniPos, condition, endPos = match(src, tokenMatchers.IF, endPos)
if not iniPos then return end
READING_CHUNK = true
chunks[#chunks + 1] = {
__len = 1,
c = {
[1] = {
iniPos = iniPos,
endPos = endPos,
condition = condition
}
}
}
return endPos
end
tokenCallbacks.ELIF = function(src, endPos)
if not READING_CHUNK then return end
local iniPos, condition, endPos = match(src, tokenMatchers.ELIF, endPos)
assert(iniPos)
local chunk = chunks[#chunks]
chunk.__len = chunk.__len + 1
chunk.c[chunk.__len] = {
iniPos = iniPos,
endPos = endPos,
condition = condition
}
return endPos
end
tokenCallbacks.ELSE = function(src, endPos)
if not READING_CHUNK then return end
local iniPos, endPos = match(src, tokenMatchers.ELSE, endPos)
assert(iniPos)
local chunk = chunks[#chunks]
chunk.__len = chunk.__len + 1
chunk.c[chunk.__len] = {
iniPos = iniPos,
endPos = endPos,
condition = "true"
}
return endPos
end
tokenCallbacks.ENDIF = function(src, endPos)
if not READING_CHUNK then return end
local iniPos, endPos = match(src, tokenMatchers.ENDIF, endPos)
if not iniPos then return end
READING_CHUNK = false
local chunk = chunks[#chunks]
chunk.c.endif = {
iniPos = iniPos,
endPos = endPos
}
return endPos
end
tokenCallbacks.__NEXT = function(src, endPos)
local iniPos, token = match(src, tokenMatchers.__NEXT, endPos)
if not iniPos then return end
return token
end
local parser = function(src)
READING_CHUNK, chunks = false, { }
local endPos, ifPos
repeat
-- Opens the conditional
endPos = tokenCallbacks.IF(src, endPos)
if not endPos then
break
end
ifPos = endPos
-- Gets all sub conditionals
local nextToken
repeat
nextToken = tokenCallbacks.__NEXT(src, endPos)
if nextToken == "ELIF" then
endPos = tokenCallbacks.ELIF(src, endPos)
else
break
end
until false
-- Check of else
if nextToken == "ELSE" then
endPos = tokenCallbacks.ELSE(src, endPos)
nextToken = nil
end
-- Finish conditional
nextToken = nextToken or tokenCallbacks.__NEXT(src, endPos)
if nextToken ~= "ENDIF" then
error(string.format("[PREPDIR] @[%d:] Mandatory token ENDIF to close \z
token IF in @[%d]", endPos, ifPos))
end
endPos = tokenCallbacks.ENDIF(src, endPos)
until false
local cc
for c = 1, #chunks do
c = chunks[c]
cc = c.c
for sc = 1, c.__len do
sc = cc[sc]
sc.condition = gsub(sc.condition, symbolReplace, conditionalSymbols)
end
end
end
local evaluateCondition = function(condition, settings)
return load("return " .. condition, '', 't', settings)()
end
local sliceString = function(str, boundaries)
assert(#boundaries % 2 == 0)
for b = #boundaries, 1, -2 do
local _, totalLines = gsub(sub(str, boundaries[b - 1], boundaries[b] - 1), '\n', '')
str = sub(str, 1, boundaries[b - 1] - 1) .. rep('\n', totalLines) .. sub(str, boundaries[b])
end
return str
end
local matcher = function(src, settings)
local boundaries, totalBoundaries = { }, 0
local cc, scObj
for c = 1, #chunks do
c = chunks[c]
cc = c.c
totalBoundaries = totalBoundaries + 1
boundaries[totalBoundaries] = cc[1].iniPos
for sc = 1, c.__len do
scObj = cc[sc]
if evaluateCondition(scObj.condition, settings) then
totalBoundaries = totalBoundaries + 1
boundaries[totalBoundaries] = scObj.endPos
totalBoundaries = totalBoundaries + 1
boundaries[totalBoundaries] = (cc[sc + 1] or cc.endif).iniPos
break
end
end
totalBoundaries = totalBoundaries + 1
boundaries[totalBoundaries] = cc.endif.endPos
end
return sliceString(src, boundaries)
end
prepdir = function(src, settings)
-- Retrieve
src = src .. "\n" -- For pattern matching
-- Get chunks
parser(src)
src = matcher(src, settings)
return sub(src, 1, -2)
end
end
return prepdir