-
Notifications
You must be signed in to change notification settings - Fork 10
/
Rule.lua
242 lines (191 loc) · 6.33 KB
/
Rule.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
--[[----------------------------------------------------------------------------
LiteMount/Rule.lua
An action rule.
Copyright 2011 Mike Battersby
----------------------------------------------------------------------------]]--
local _, LM = ...
local L = LM.Localize
LM.Rule = { }
local function ReadWord(line)
local token, rest
-- Skip whitespace
token, rest = line:match('^(%s+)(.*)$')
if token then return nil, rest end
-- Skip from # to end of line
token = line:match('^#')
if token then return nil, nil end
-- Match ""
token, rest = line:match('^"(.-)"(.*)$')
if token then return token, rest end
-- Match ''
token, rest = line:match("^'(.-)'(.*)$")
if token then return token, rest end
-- Match conditions (includes empty condition [])
token, rest = line:match('^(%[.-%])(.*)$')
if token then return token, rest end
-- Match argument operator tokens - + = , / ~
token, rest = line:match('^([-+=,/~])(.*)$')
if token then return token, rest end
-- Match argument word tokens
token, rest = line:match('^([^,/]+)(.*)$')
if token then return token, rest end
end
function LM.Rule:ParseLine(line)
local r = CreateFromMixins(LM.Rule)
r.errors = {}
r.line = line
local argTokens, condWords, rest = { }, { }
-- Note this is intentionally unanchored to skip leading whitespace
r.action, rest = line:match('(%S+)%s*(.*)')
-- Commands and empty are skipped
if not r.action or r.action == '' or r.action:sub(1,1) == '#' then
return
end
-- once we see an argument we are done with conditions
local inArgs = false
while rest ~= nil do
local word
word, rest = ReadWord(rest)
if word then
word = LM.Vars:StrSubConsts(word)
if not inArgs and word:match('^%[.*%]$') then
tinsert(condWords, word:sub(2, -2))
else
tinsert(argTokens, word)
inArgs = true
end
end
end
local conditions = { }
for _, word in ipairs(condWords) do
local clause = { }
for c in word:gmatch('[^,]+') do
if c:sub(1,2) == 'no' then
local l = LM.RuleBoolean:Leaf(c:sub(3))
table.insert(clause, LM.RuleBoolean:Not(l))
else
table.insert(clause, LM.RuleBoolean:Leaf(c))
end
end
table.insert(conditions, LM.RuleBoolean:And(unpack(clause)))
end
r.conditions = LM.RuleBoolean:Or(unpack(conditions))
-- Delay actually parsing the args until later when the actions can parse
-- them as they need. Can probably parse them using LM.Actions:GetArgType,
-- but I've done enough changing things for now so continue to let the
-- action handlers call the parsing.
r.args = LM.RuleArguments:Get(argTokens)
r:CheckErrors()
return r
end
function LM.Rule:CheckErrors()
-- XXX At some point should probably OO into LM.RuleAction XXX
local fcHandler = LM.Actions:GetFlowControlHandler(self.action)
local handler = LM.Actions:GetHandler(self.action)
if not ( fcHandler or handler ) then
table.insert(self.errors, format(L.LM_ERR_BAD_ACTION, self.action))
end
local ok, err = self.conditions:Validate()
if not ok then
table.insert(self.errors, err)
end
ok, err = self.args:Validate(self.action)
if not ok then
table.insert(self.errors, err)
end
return true
end
function LM.Rule:Dispatch(context)
if next(self.errors) then
return
end
local isTrue = self.conditions:Eval(context)
local handler = LM.Actions:GetFlowControlHandler(self.action)
if handler then
LM.Debug(" Dispatching flow control action " .. (self.line or self:ToString()))
handler(self.args, context, isTrue)
return
end
if not isTrue or LM.Actions:IsFlowSkipped(context) then
return
end
handler = LM.Actions:GetHandler(self.action)
if not handler then
-- Shouldn't reach this due to Validate at compile time
return
end
LM.Debug(" Dispatching rule " .. (self.line or self:ToString()))
return handler(self.args:ReplaceVars(), context)
end
function LM.Rule:ToString()
local out = { self.action }
local cText = self.conditions:ToString()
if cText and cText ~= "" then table.insert(out, cText) end
local aText = self.args:ToString()
if aText ~= "" then table.insert(out, aText) end
return table.concat(out, ' ')
end
function LM.Rule:ToDisplay()
local conditionText = self.conditions:ToDisplay()
local actionText, argText = LM.Actions:ToDisplay(self.action, self.args)
if argText then
return conditionText, actionText .. "\n" .. argText
else
return conditionText, actionText
end
end
-- Simple rules can be used in the user rules UI. They must have:
-- * action one of Mount/SmartMount/LimitSet/LimitInclude/LimitExclude
-- * maximum 3 ANDed conditions, no ORed conditions
-- * exactly one action argument
local SimpleActions = {
"Mount",
"SmartMount",
"LimitInclude",
"LimitExclude",
}
function LM.Rule:IsSimpleRule()
if not tContains(SimpleActions, self.action) then
return false
end
if not self.args:IsSimpleArguments() then
return false
end
if not self.conditions:IsSimpleCondition() then
return false
end
return true
end
-- This is a converter from the original storage format, used only
-- by LM.Options:VersionUpgrade8
--
-- Example:
--
-- {
-- ["action"] = "Mount",
-- ["conditions"] = {
-- "tracking:Find Herbs",
-- { "class:DRUID", ["op"] = "NOT" },
-- ["op"] = "AND",
-- },
-- ["args"] = { "HERB" },
-- }
--
function LM.Rule:MigrateFromTable(tableRule)
local textParts = { tableRule.action }
if tableRule.conditions then
local cTexts = { }
for _,c in ipairs(tableRule.conditions) do
if type(c) == 'table' then -- only 'NOT'
table.insert(cTexts, 'no'..c[1])
else
table.insert(cTexts, c)
end
end
table.insert(textParts, '[' .. table.concat(cTexts, ',') .. ']')
end
if tableRule.args then
table.insert(textParts, table.concat(tableRule.args, ','))
end
return table.concat(textParts, ' ')
end