-
Notifications
You must be signed in to change notification settings - Fork 2
/
shrinkfsm.lua
257 lines (251 loc) · 6.13 KB
/
shrinkfsm.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
-- Shrink FSM
-- Created by Kiritow
local function isSpace(c)
return (c==" " or c=="\n" or c=="\r" or c=="\t")
end
local function GetTrans()
-- state_trans(input,output,peek)
local trans={}
trans["init"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(isSpace(c)) then
return "init"
elseif(c=='-') then
return "comment_ping1"
elseif(c=="'") then
output(c)
return "single_quote"
elseif(c=='"') then
output(c)
return "double_quote"
else
output(c)
return "normal"
end
end
trans["comment_ping1"]=function(input,output,peek)
local c=peek()
if(c==nil) then
return "stop"
elseif(c=='-') then
input()
return "comment_ping2"
else
output('-')
return "normal"
end
end
trans["comment_ping2"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(c=='[') then
return "comment_ping3"
else
return "comment"
end
end
trans["comment_ping3"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(c=='[') then
return "long_comment"
else
return "comment"
end
end
trans["comment"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(c=="\n") then
return "normal"
else
return "comment"
end
end
trans["long_comment"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(c==']') then
return "endcomment_ping"
else
return "long_comment"
end
end
trans["endcomment_ping"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(c==']') then
return "normal"
else
return "long_comment"
end
end
trans["single_quote"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
end
output(c)
if(c=="'") then
return "normal"
else
return "single_quote"
end
end
trans["double_quote"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
end
output(c)
if(c=='"') then
return "normal"
else
return "double_quote"
end
end
trans["normal"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(c=='"') then
output(c)
return "double_quote"
elseif(c=="'") then
output(c)
return "single_quote"
elseif(c=='-') then
return "comment_ping1"
elseif(isSpace(c)) then
output(' ')
return "normal_space"
elseif(c=='[') then
output(c)
trans.lqsz=0
return "long_quote_ping"
else
output(c)
return "normal"
end
end
trans["normal_space"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
elseif(c=='"') then
output(c)
return "double_quote"
elseif(c=="'") then
output(c)
return "single_quote"
elseif(c=='-') then
return "comment_ping1"
elseif(isSpace(c)) then
return "normal_space"
elseif(c=='[') then
output(c)
trans.lqsz=0
return "long_quote_ping"
else
output(c)
return "normal"
end
end
trans["long_quote_ping"]=function(input,output,peek)
local c=peek()
if(c==nil) then
return "stop"
end
if(c=="=") then
output(input())
trans.lqsz=trans.lqsz+1
return "long_quote_ping"
elseif(c=='[') then
output(input())
return "long_quote"
else
if(trans.lqsz==0) then
return "normal_space"
else -- Just like: [==x
return "stxerror"
end
end
end
trans["long_quote"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
end
output(c)
if(c=="]") then
trans.lqdt=0
return "endquote_ping"
else
return "long_quote"
end
end
trans["endquote_ping"]=function(input,output)
local c=input()
if(c==nil) then
return "stop"
end
output(c)
if(c=="=") then
trans.lqdt=trans.lqdt+1
return "endquote_ping"
elseif(c=="]") then
if(trans.lqdt==trans.lqsz) then
return "normal"
else
trans.lqdt=0
return "endquote_ping"
end
else
return "long_quote"
end
end
return trans
end
local function shrinkFSM(str)
local trans=GetTrans()
local out={}
local cur=0
local len=str:len()
local function input()
if(cur<len) then
cur=cur+1
return str:sub(cur,cur)
else
return nil
end
end
local function peek()
if(cur<len) then
return str:sub(cur+1,cur+1)
else
return nil
end
end
local function output(c)
table.insert(out,c)
end
local curState="init"
while trans[curState] do
curState=trans[curState](input,output,peek)
end
if(curState=="stop") then
return table.concat(out,"")
elseif(curState=="stxerror") then
error("SyntaxError: invalid long string delimiter")
else
error("FSMInvalidState: State not found: " .. curState)
end
end
return shrinkFSM