-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabetai.lua
242 lines (209 loc) · 7.64 KB
/
tabetai.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
local tabetai = {
context = {
keywords = {
["else"] = "condition_case",
["elseif"] = "condition",
["end"] = "",
["for"] = "loop",
["function"] = "function",
["if"] = "condition"
}
},
pattern = {
["a_to_z"] = "[a-zA-Z]",
["arrow"] = "%=%>",
["between_parentheses"] = "%((.-)%)",
["define_directive"] = "#define \"*([%w%s]+)\"* (.-)[\r\n]",
["empty_braces"] = "%{%}",
["equal_sign"] = "^=$",
["format_function_args"] = "function(%s)",
["format_word"] = "%s",
["function"] = "function%s*%((.-)%)",
["include_directive"] = "#include \"(.-)\".-[\r\n]",
["join_by_space"] = "%s %s",
["left_brace"] = "%{",
["left_parenthesis"] = "%(",
["match_word"] = "(%S+)",
["right_brace"] = "%}",
["right_parenthesis"] = "%)",
}
}
local function init_state()
_G["state"] = {
at = 0,
to = 0,
ahead = "",
chunk = nil,
context = "",
current = "",
hold = false,
look_for = "",
pending = nil,
skip = false,
source = "",
suspend = false,
level = 0,
level_context = {}
}
end
local function preprocessor()
local directives = {
["include"] = function(source)
local source_file = io.open(source, "r")
if source_file then
return source_file:read("*a") .. "\n"
else
return "\n"
end
end,
["define"] = function(find, replace)
state.source = state.source:gsub("%f[%a]"..find.."%f[%A]", replace)
end
}
state.source = state.source:gsub(tabetai.pattern['include_directive'], directives["include"])
local next_def = state.source:gmatch(tabetai.pattern['define_directive'])
::definitions::
local find, replace = next_def()
if not find or not replace then goto exit_definitions end
directives["define"](find, replace)
goto definitions
::exit_definitions::
state.source = state.source:gsub(tabetai.pattern['define_directive'], "")
end
local function arrow_function()
local pos = state.to - ((state.pending or ""):len() + (state.pending and state.current or ""):len())
return (tabetai.pattern["format_function_args"]):format(state.source:match(tabetai.pattern["between_parentheses"], pos))
end
local function keywords()
if tabetai.context.keywords[state.current] then
state.hold = false
state.context = tabetai.context.keywords[state.current]
end
end
local function operators()
if state.current:match(tabetai.pattern["between_parentheses"]) and state.context == "" then
state.context = "function"
end
if state.current:match(tabetai.pattern["arrow"]) then
state.context = "function"
state.chunk = arrow_function()
state.hold = false
end
if state.current:match(tabetai.pattern["equal_sign"]) and state.ahead:match(tabetai.pattern["left_brace"]) then
state.context = "declaration"
state.chunk = state.current
end
if state.current:match(tabetai.pattern["empty_braces"]) then
state.chunk = state.current
state.skip = true
end
if not state.current:match(tabetai.pattern["function"]) and state.current:gsub(tabetai.pattern["a_to_z"], ""):sub(1, 1) == "(" and state.current:sub(-1, -1) == ")" then
state.pending = state.current
return true
end
if state.current:match(tabetai.pattern["left_parenthesis"]) and state.skip == false then
state.hold = true
end
if state.current:match(tabetai.pattern["left_brace"]) and state.hold == false and state.skip == false then
if state.context ~= "" then
if state.context == "declaration" then
state.level_context[state.level + 1] = "skip_block"
else
state.level_context[state.level + 1] = "block"
local prefix = state.source:sub(state.at - 2, state.at - 2)
local suffix = state.source:sub(state.at + 1, state.at + 1)
local opening =
(state.context == "function" or state.context == "condition_case") and ""
or (state.context == "loop" and "do" or "then")
state.chunk =
state.current:gsub(
"{",
tabetai.pattern["format_word"]:rep(3):format(
(prefix ~= " " and prefix ~= "\n") and " " or "",
opening,
(suffix ~= " " and suffix ~= "\n") and " " or ""
)
)
end
state.context = ""
end
state.level = state.level + 1
end
if state.current:match(tabetai.pattern["right_brace"]) and state.hold == false and state.skip == false then
if state.context ~= ""
or state.level_context[state.level] == "block"
and state.level_context[state.level] ~= "skip_block"
then
if state.hold == false then
state.context = ""
local prefix = state.source:sub(state.at - 2, state.at - 2)
local suffix = state.source:sub(state.at + 1, state.at + 1)
if state.current:match('%S}') then prefix = '' end
state.chunk =
state.current:gsub(
"}",
tabetai.pattern["format_word"]:rep(3):format(
(prefix ~= " " and prefix ~= "\n") and " " or "",
"end",
(suffix ~= " " and suffix ~= "\n") and " " or ""
)
)
end
end
state.level_context[state.level] = ""
state.level = state.level - 1
end
if state.current:match(tabetai.pattern["right_parenthesis"]) then
state.hold = false
end
end
local function next()
local ret = state.suspend and " " or state.chunk
state.skip = false
state.suspend = false
return ret
end
local function close()
if state.pending then
state.source = state.source .. state.pending
end
end
tabetai.arrow_function = arrow_function
tabetai.close = close
tabetai.keywords = keywords
tabetai.next = next
tabetai.operators = operators
tabetai.preprocessor = preprocessor
tabetai.strip_comments = strip_comments
return function(code)
init_state()
state.source = code
tabetai.preprocessor()
state.source =
state.source:gsub(
(tabetai.pattern["format_word"]):rep(3):format("()", tabetai.pattern["match_word"], "()"),
function(at, current, to)
state.at, state.to = at + 1, to - 1
state.ahead = state.source:match(tabetai.pattern["match_word"], state.to + 1)
state.chunk = nil
state.current = current
tabetai.keywords()
if tabetai.operators() then return "" end
if not state.chunk then
state.chunk = state.current
end
if state.current == state.pending then
state.suspend = true
end
if not state.current:match(tabetai.pattern["arrow"]) and state.pending ~= nil then
state.chunk = (tabetai.pattern["join_by_space"]):format(state.pending, state.chunk)
state.pending = nil
elseif state.pending ~= nil then
state.pending = nil
end
return tabetai.next()
end
)
tabetai.close()
return state.source
end