-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.lua
More file actions
200 lines (161 loc) · 4.17 KB
/
parser.lua
File metadata and controls
200 lines (161 loc) · 4.17 KB
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
verbs = {};
junkWords = {"the", "a"};
capture = {
Object = {__action="capture", type=Object};
Thing = {__action="capture", type=Thing};
Inventory = {__action="capture", type="INVENTORY"};
Rest = {__action="capture", type="REST"};
};
function getVerbHandlers(verb)
if(verbs[verb] == nil) then
verbs[verb] = {};
end
return verbs[verb];
end
function addVerb(spec, func)
local verblist;
if(type(spec[1]) == "table") then
verblist = spec[1];
else
verblist = {spec[1]};
end
for i,v in ipairs(verblist) do
local handlers = getVerbHandlers(v);
handlers[#handlers + 1] = {spec = spec, func = func};
end
end
function updateParser()
parserSearchList = {};
for k in pairs(verbs) do
parserSearchList[#parserSearchList+1] = {verb = k, tokens = tokenize(k)};
end
table.sort(parserSearchList,
function(a, b)
return #a.tokens > #b.tokens;
end);
end
function parse(st)
local foundVerb = false;
st = string.lower(st);
local tokens = tokenize(st);
for _,verb in ipairs(parserSearchList) do
if(matchVerb(tokens, verb)) then
foundVerb = true;
for _,spec in ripairs(verbs[verb.verb]) do
if(parseRest(spec, tokens, #verb.tokens)) then
return "Success";
end
end
end
end
if(foundVerb) then
return "CouldNotUnderstand";
else
return "VerbNotFound";
end
end
function matchVerb(tokens, verb)
if(#tokens < #verb.tokens) then return false; end
for i=1,#verb.tokens do
if(tokens[i] ~= verb.tokens[i]) then return false; end
end
return true;
end
function parseRest(spec, tokens, tknidx)
local thiscaptokens;
local captures = {};
local captureTypes = {};
local func = spec.func;
spec = spec.spec;
--use token literals to break up the tokens for the captures.
local capinqueue = false;
for i=2,#spec do
local tknspec;
if(type(spec[i]) == "string") then
tknspec = {spec[i]};
else
tknspec = spec[i];
end
if(tknspec.__action == "capture") then
if(capinqueue) then error("Cannot have two consecutive captures.") end;
thiscaptokens = {};
captureTypes[#captureTypes+1] = tknspec;
captures[#captures+1] = thiscaptokens;
capinqueue = true;
else
local nfound = true;
while(tknidx < #tokens and nfound) do
tknidx = tknidx + 1;
for i=1,#tknspec do
if(tokens[tknidx] == tknspec[i]) then
nfound = false;
break;
end
end
if(nfound) then
if(not capinqueue) then return false;
else thiscaptokens[#thiscaptokens + 1] = tokens[tknidx]; end
end
end
if(nfound) then return false; end --ran out of tokens
capinqueue = false;
end
end
if(capinqueue) then
if(tknidx >= #tokens) then return false; end
while(tknidx < #tokens) do
tknidx = tknidx+1;
thiscaptokens[#thiscaptokens+1] = tokens[tknidx];
end
else
if(tknidx < #tokens) then
return false;
end
end
--now try to capture the results
local captureObjects = {};
for i,v in ipairs(captures) do
if(captureTypes[i].type == "REST") then
captureObjects = v;
elseif(captureTypes[i].type == "INVENTORY") then
for x in visible(current.inventory) do
if(x:match(v)) then
captureObjects[i] = x;
break;
end
end
else
for x in visible() do
if(x:isA(captureTypes[i].type) and x:match(v)) then
captureObjects[i] = x;
break;
end
end
end
if(captureObjects[i] == nil) then return false; end
end
return func(unpack(captureObjects));
end
function tokenize(st)
local tokens = {};
local tokenstart = 1;
local type = 0;
for i=1,#st do
local chartype = getCharType(st, i);
if(chartype ~= type) then
if(type ~= 0) then
tokens[#tokens+1] = string.sub(st, tokenstart, i-1);
end
tokenstart = i;
type = chartype;
end
end
tokens[#tokens+1] = string.sub(st, tokenstart);
return tokens;
end
function getCharType(st, i)
local char = string.sub(st, i, i);
if(char:match('[%a-]')) then return 1; end
if(char:match('%p')) then return 2; end
return 0;
end