|
|
1 | # |
|
|
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | # |
|
|
4 | # Copyright (©) 2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | # |
|
|
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
|
|
7 | # the terms of the Affero GNU General Public License as published by the |
|
|
8 | # Free Software Foundation, either version 3 of the License, or (at your |
|
|
9 | # option) any later version. |
|
|
10 | # |
|
|
11 | # This program is distributed in the hope that it will be useful, |
|
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
14 | # GNU General Public License for more details. |
|
|
15 | # |
|
|
16 | # You should have received a copy of the Affero GNU General Public License |
|
|
17 | # and the GNU General Public License along with this program. If not, see |
|
|
18 | # <http://www.gnu.org/licenses/>. |
|
|
19 | # |
|
|
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
|
|
21 | # |
|
|
22 | |
1 | =head1 NAME |
23 | =head1 NAME |
2 | |
24 | |
3 | cf::match - object matching language |
25 | cf::match - object matching language |
4 | |
26 | |
5 | =head1 DESCRIPTION |
27 | =head1 DESCRIPTION |
… | |
… | |
25 | |
47 | |
26 | Find all potions with spell objects inside them in someones inventory: |
48 | Find all potions with spell objects inside them in someones inventory: |
27 | |
49 | |
28 | type=SPELL in type=POTION in inv |
50 | type=SPELL in type=POTION in inv |
29 | |
51 | |
30 | Find all potions inside someones inventory, or inside applied containers: |
52 | Find all scrolls inside someones inventory, or inside applied scroll |
|
|
53 | containers: |
31 | |
54 | |
32 | type=POTION also in type=CONTAINER and applied in inv |
55 | type=SCROLL also in applied type=CONTAINER race="scroll" in inv |
|
|
56 | |
|
|
57 | Find all unpaid items, anywhere, even deeply nested inside other items, in |
|
|
58 | the originator: |
|
|
59 | |
|
|
60 | unpaid also deep in inv of originator |
33 | |
61 | |
34 | =head1 MATCH EXPRESSIONS |
62 | =head1 MATCH EXPRESSIONS |
35 | |
63 | |
36 | =head2 STRUCTURE |
64 | =head2 STRUCTURE |
37 | |
65 | |
38 | The two main structures are the C<match>, which selects objects matching |
66 | The two main structures are the C<match>, which selects objects matching |
39 | various criteria, and the C<condition, which determines if an object |
67 | various criteria, and the C<condition, which determines if an object |
40 | matches some desired properties: |
68 | matches some desired properties: |
41 | |
69 | |
42 | condition |
70 | condition |
43 | condition in set |
71 | condition in set-modifier |
|
|
72 | condition of root-object |
44 | |
73 | |
45 | A C<cond> receives a set of "context objects" that it is applied to. This |
74 | A C<condition> receives a set of "context objects" that it is applied |
46 | is initially just one object - for altars, it is the object dropped on it, |
75 | to. This is initially just one object - by default, for altars, it is the |
47 | for pedestals, the object on top of it and so on. |
76 | object dropped on it, for pedestals, the object on top of it and so on. |
48 | |
77 | |
49 | This set of context objects can be modified in various ways, for example |
78 | This set of context objects can be modified in various ways, for example |
50 | by replacing it with the inventories of all objects, or all objects on the |
79 | by replacing it with the inventories of all objects, or all items on the |
51 | same mapspace, and so on, by using the C<in> operator: |
80 | same mapspace, and so on, by using the C<in> operator: |
52 | |
81 | |
53 | condition in inv |
82 | condition in inv |
|
|
83 | condition in map |
|
|
84 | |
|
|
85 | Also, besides the default root object where all this begins, you can start |
|
|
86 | elsewhere, for example in the I<originator> (usually the player): |
|
|
87 | |
54 | condition in inv in originator |
88 | condition in inv of originator |
55 | |
89 | |
56 | Once the set of context objects has been established, each object is |
90 | Once the final set of context objects has been established, each object |
57 | matched against the C<cond> expression. Sometimes the server is only |
91 | is matched against the C<condition>. |
58 | interested in knowing whether I<anything> matches, and sometimes the |
92 | |
59 | server is interested in I<all> objects that match. |
93 | It is possible to chain modifiers from right-to-left, so this example |
|
|
94 | would start with the originator, take it's inventory, find all inventory |
|
|
95 | items which are potions, looks into their inventory, and then finds all |
|
|
96 | spells. |
|
|
97 | |
|
|
98 | type=SPELL in type=POTION in inv of originator |
|
|
99 | |
|
|
100 | Sometimes the server is only interested in knowing whether I<anything> |
|
|
101 | matches, and sometimes the server is interested in I<all> objects that |
|
|
102 | match. |
60 | |
103 | |
61 | =head2 OPERATORS |
104 | =head2 OPERATORS |
62 | |
105 | |
63 | =over 4 |
106 | =over 4 |
64 | |
107 | |
65 | =item and, or, not, () |
108 | =item and, or, not, () |
66 | |
109 | |
67 | Conditions can be combined with C<and> or C<or> to build larger |
110 | Conditions can be combined with C<and> or C<or> to build larger |
68 | expressions. C<not> negates the expression, and parentheses can be used to |
111 | expressions. C<not> negates the condition, and parentheses can be used to |
69 | group conditions. |
112 | override operator precedence and execute submatches. |
|
|
113 | |
|
|
114 | Not that C<not> only negates a condition and not the whole match |
|
|
115 | expressions, thus |
|
|
116 | |
|
|
117 | not applied in inv |
|
|
118 | |
|
|
119 | is true if there is I<any> non-applied object in the inventory. To negate |
|
|
120 | a whole match, you have to use a sub-match: To check whether there is |
|
|
121 | I<no> applied object in someones inventory, write this: |
|
|
122 | |
|
|
123 | not (applied in inv) |
70 | |
124 | |
71 | Example: match applied weapons. |
125 | Example: match applied weapons. |
72 | |
126 | |
73 | type=WEAPON and applied |
127 | applied type=WEAPON |
74 | |
128 | |
75 | Example: match horns or rods. |
129 | Example: match horns or rods. |
76 | |
130 | |
77 | type=HORN or type=ROD |
131 | type=HORN or type=ROD |
|
|
132 | |
|
|
133 | Example: see if the originator is a player. |
|
|
134 | |
|
|
135 | type=PLAYER of originator |
78 | |
136 | |
79 | =item in ... |
137 | =item in ... |
80 | |
138 | |
81 | The in operator takes the context set and modifies it in various ways. As |
139 | The in operator takes the context set and modifies it in various ways. As |
82 | a less technical description, think of the C<in> as being a I<look into> |
140 | a less technical description, think of the C<in> as being a I<look into> |
… | |
… | |
84 | the match, the C<in> operator lets you look at other sets of objects, most |
142 | the match, the C<in> operator lets you look at other sets of objects, most |
85 | often the inventory. |
143 | often the inventory. |
86 | |
144 | |
87 | =over 4 |
145 | =over 4 |
88 | |
146 | |
89 | =item in object |
|
|
90 | |
|
|
91 | Replaces all objects by the default object - this is the object passed to |
|
|
92 | the match to match against by default. All matches have an explicit C<in |
|
|
93 | ctx> appended. |
|
|
94 | |
|
|
95 | This must be the last C<in> expression in a match. |
|
|
96 | |
|
|
97 | =item in source |
|
|
98 | |
|
|
99 | Replaces all objects by the source object - this object is sometimes |
|
|
100 | passed to matches and represents the object is the source of the action, |
|
|
101 | such as a rod or a potion when it is applied. Often, the I<source> is the |
|
|
102 | same as the I<originator>. |
|
|
103 | |
|
|
104 | This must be the last C<in> expression in a match. |
|
|
105 | |
|
|
106 | =item in originator |
|
|
107 | |
|
|
108 | Replaces all objects by the originator object - one step farther removed |
|
|
109 | than the I<source>, the I<originator> is sometimes passed to matches and |
|
|
110 | represents the original initiator of an action, most commonly a player or |
|
|
111 | monster. |
|
|
112 | |
|
|
113 | =item in self |
|
|
114 | |
|
|
115 | Replaces all objects by the object initiating/asking for the match - this |
|
|
116 | is basically always the object thatt he match expression is attached to. |
|
|
117 | |
|
|
118 | This must be the last C<in> expression in a match. |
|
|
119 | |
|
|
120 | =item in inv |
147 | =item in inv |
121 | |
148 | |
122 | Replaces all objects by their inventory. |
149 | Replaces all objects by their inventory. |
123 | |
150 | |
124 | Example: find all spell objects inside the object to be matched. |
151 | Example: find all spell objects inside the object to be matched. |
… | |
… | |
127 | |
154 | |
128 | =item in env |
155 | =item in env |
129 | |
156 | |
130 | Replaces all objects by their containing object, if they have one. |
157 | Replaces all objects by their containing object, if they have one. |
131 | |
158 | |
|
|
159 | =item in arch |
|
|
160 | |
|
|
161 | Replaces all objects by their archetypes. |
|
|
162 | |
132 | =item in map |
163 | =item in map |
133 | |
164 | |
134 | Replaces all objects by the objects that are on the same mapspace as them. |
165 | Replaces all objects by the objects that are on the same mapspace as them. |
135 | |
166 | |
|
|
167 | =item in head |
|
|
168 | |
|
|
169 | Replaces all objects by their head objects. |
|
|
170 | |
136 | =item in <cond> |
171 | =item in <condition> |
137 | |
172 | |
138 | Finds all context objects matching the condition, and then puts their |
173 | Finds all context objects matching the condition, and then puts their |
139 | inventories into the context set. |
174 | inventories into the context set. |
140 | |
175 | |
141 | Note that C<in inv> is simply a special case of an C<< in <cond> >> that |
176 | Note that C<in inv> is simply a special case of an C<< in <condition> >> that |
142 | matches any object. |
177 | matches any object. |
143 | |
178 | |
144 | Example: find all spells inside potions inside the inventory of the context |
179 | Example: find all spells inside potions inside the inventory of the context |
145 | object(s). |
180 | object(s). |
146 | |
181 | |
… | |
… | |
153 | |
188 | |
154 | Example: check if the context object I<is> a spell, or I<contains> a spell. |
189 | Example: check if the context object I<is> a spell, or I<contains> a spell. |
155 | |
190 | |
156 | type=SPELL also in inv |
191 | type=SPELL also in inv |
157 | |
192 | |
158 | =item repeatedly in ... |
193 | =item also deep in ... |
159 | |
194 | |
160 | Repeats the operation as many times as possible. This can be used to |
195 | Repeats the operation as many times as possible. This can be used to |
161 | recursively look into objects. |
196 | recursively look into objects. |
162 | |
197 | |
163 | =item also repeatedly in ... |
198 | So for example, C<also deep in inv> means to take the inventory of all |
|
|
199 | objects, taking their inventories, and so on, and adding all these objects |
|
|
200 | to the context set. |
164 | |
201 | |
165 | C<also> and C<repeatedly> can be combined. |
202 | Similarly, C<also deep in env> means to take the environment object, their |
|
|
203 | environemnt object and so on. |
166 | |
204 | |
167 | Example: check if there are any unpaid items in an inventory, |
205 | Example: check if there are any unpaid items in an inventory, |
168 | or in the inventories of the inventory objects, and so on. |
206 | or in the inventories of the inventory objects, and so on. |
169 | |
207 | |
170 | unpaid also repeatedly in inv |
208 | unpaid also deep in inv |
171 | |
209 | |
172 | Example: check if a object is inside a player. |
210 | Example: check if a object is inside a player. |
173 | |
211 | |
174 | type=PLAYER also repeatedly in env |
212 | type=PLAYER also deep in env |
175 | |
213 | |
176 | =back |
214 | =back |
|
|
215 | |
|
|
216 | =item of ... |
|
|
217 | |
|
|
218 | By default, all matches are applied to the "obviously appropriate" object, |
|
|
219 | such as the item dropped on a button or moving over a detector. This can |
|
|
220 | be changed to a number of other objects - not all of them are available |
|
|
221 | for each match (when not available, the match will simply fail). |
|
|
222 | |
|
|
223 | An C<of> term ends a match, nothing is allowed to follow. |
|
|
224 | |
|
|
225 | =over 4 |
|
|
226 | |
|
|
227 | =item of object |
|
|
228 | |
|
|
229 | Starts with the default object - this is the object passed to the match to |
|
|
230 | match against by default. Matches have an explicit C<of object> appended, |
|
|
231 | but submatches start at the current object, and in this case C<of object> |
|
|
232 | can be used to start at the original object once more. |
|
|
233 | |
|
|
234 | =item of self |
|
|
235 | |
|
|
236 | Starts with the object initiating/asking for the match - this is basically |
|
|
237 | always the object that the match expression is attached to. |
|
|
238 | |
|
|
239 | =item of source |
|
|
240 | |
|
|
241 | Starts with the I<source> object - this object is sometimes passed to |
|
|
242 | matches and represents the object that is the source of the action, such |
|
|
243 | as a rod or a potion when it is applied. Often, the I<source> is the same |
|
|
244 | as the I<originator>. |
|
|
245 | |
|
|
246 | =item of originator |
|
|
247 | |
|
|
248 | Starts with the I<originator> - one step farther removed than the |
|
|
249 | I<source>, the I<originator> is sometimes passed to matches and represents |
|
|
250 | the original initiator of an action, most commonly a player or monster. |
|
|
251 | |
|
|
252 | This object is often identical to the I<source> (e.g. when a player casts |
|
|
253 | a spell, the player is both source and originator). |
177 | |
254 | |
178 | =back |
255 | =back |
179 | |
256 | |
180 | =head2 EXPRESSIONS |
257 | =head2 EXPRESSIONS |
181 | |
258 | |
… | |
… | |
250 | =item any |
327 | =item any |
251 | |
328 | |
252 | This simply evaluates to true, and simply makes matching I<any> object a |
329 | This simply evaluates to true, and simply makes matching I<any> object a |
253 | bit easier to read. |
330 | bit easier to read. |
254 | |
331 | |
|
|
332 | =item none |
|
|
333 | |
|
|
334 | This simply evaluates to false, and simply makes matching I<never> a bit |
|
|
335 | easier to read. |
|
|
336 | |
255 | =item has(condition) |
337 | =item has(condition) |
256 | |
338 | |
257 | True iff the object has a matching inventory object. |
339 | True iff the object has a matching inventory object. |
258 | |
340 | |
259 | =item count(match) |
341 | =item count(match) |
260 | |
342 | |
261 | Number of matching objects - the context object for the C<match> is the |
343 | Number of matching objects - the context object for the C<match> is the |
262 | currently tested object - you can override this with an C<in object> for |
344 | currently tested object - you can override this with an C<in object> for |
263 | example. |
345 | example. |
264 | |
346 | |
265 | =item match(match) |
|
|
266 | |
|
|
267 | An independent match - semantics like C<count>, except it only matters |
|
|
268 | whether the match finds any object (which is faster). |
|
|
269 | |
|
|
270 | =item dump |
347 | =item dump() |
271 | |
348 | |
272 | Dumps the object to the server log when executed, and evaluates to true. |
349 | Dumps the object to the server log when executed, and evaluates to true. |
273 | |
350 | |
274 | Note that logical operations are short-circuiting, so this only dumps |
351 | Note that logical operations are short-circuiting, so this only dumps |
275 | potions: |
352 | potions: |
276 | |
353 | |
277 | type=POTION and dump |
354 | type=POTION and dump() |
278 | |
355 | |
279 | =back |
356 | =back |
280 | |
357 | |
281 | =head2 GRAMMAR |
358 | =head2 GRAMMAR |
282 | |
359 | |
… | |
… | |
284 | module. It is meant to be easily readable by humans, not to implement it |
361 | module. It is meant to be easily readable by humans, not to implement it |
285 | exactly as-is. |
362 | exactly as-is. |
286 | |
363 | |
287 | # object matching and selecting |
364 | # object matching and selecting |
288 | |
365 | |
289 | match = set |
366 | match = chain |
|
|
367 | | chain 'of' root |
|
|
368 | root = 'object' | 'self' | 'source' | 'originator' |
|
|
369 | chain = condition |
290 | | match also rep 'in' set |
370 | | chain also deep 'in' modifier |
291 | also = nothing | 'also' |
371 | also = nothing | 'also' |
292 | rep = nothing | 'rep' | 'repeatedly' |
372 | deep = nothing | 'deep' |
|
|
373 | modifier ='inv' | 'env' | 'arch' | 'map' | 'head' |
293 | |
374 | |
294 | set = 'inv' | 'env' | 'map' |
375 | nothing = |
295 | | 'object' | 'source' | 'originator' | 'self' |
|
|
296 | |
|
|
297 | empty = |
|
|
298 | |
376 | |
299 | # boolean matching condition |
377 | # boolean matching condition |
300 | |
378 | |
301 | condition = factor |
379 | condition = factor |
302 | | factor 'and'? cond |
380 | | factor 'and'? condition |
303 | | factor 'or' cond |
381 | | factor 'or' condition |
304 | |
382 | |
305 | factor = 'not' factor |
383 | factor = 'not' factor |
306 | | '(' cond ')' |
384 | | '(' match ')' |
307 | | expr |
385 | | expr |
308 | | expr operator constant |
386 | | expr operator constant |
309 | |
387 | |
310 | operator = '=' | '==' | '!=' | '<' | '<=' | '>' | '>=' |
388 | operator = '=' | '==' | '!=' | '<' | '<=' | '>' | '>=' |
311 | |
389 | |
312 | expr = flag |
390 | expr = flag |
313 | | sattr |
391 | | sattr |
314 | | aattr '[' <constant> ']' |
392 | | aattr '[' <constant> ']' |
|
|
393 | | 'stat.' statattr |
315 | | special |
394 | | special |
316 | | func '(' args ')' |
395 | | func '(' args ')' |
317 | | '{' perl code block '}' |
396 | | '{' perl code block '}' |
318 | |
397 | |
319 | func = <any function name> |
398 | func = <any function name> |
320 | sattr = <any scalar object attribute> |
399 | sattr = <any scalar object attribute> |
321 | aattr = <any array object attribute> |
400 | aattr = <any array object attribute> |
322 | flag = <any object flag> |
401 | flag = <any object flag> |
|
|
402 | statattr = <any stat attribute: exp, food, str, dex, hp, maxhp...> |
323 | special = <any ()-less "function"> |
403 | special = <any ()-less "function"> |
324 | |
404 | |
325 | constant = <number> | '"' <string> '"' | <uppercase cf::XXX name> |
405 | constant = <number> | '"' <string> '"' | <uppercase cf::XXX name> |
326 | args = <depends on function> |
406 | args = <depends on function> |
327 | |
407 | |
328 | TODO: repeatedly, env, contains, possbly matches |
408 | TODO: contains, matches, query_name, selling_price, buying_price? |
329 | |
409 | |
330 | =cut |
410 | =cut |
331 | |
411 | |
332 | =head2 PERL FUNCTIONS |
412 | =head2 PERL FUNCTIONS |
333 | |
413 | |
… | |
… | |
338 | package cf::match; |
418 | package cf::match; |
339 | |
419 | |
340 | use common::sense; |
420 | use common::sense; |
341 | |
421 | |
342 | use List::Util (); |
422 | use List::Util (); |
343 | |
|
|
344 | # parser state |
|
|
345 | # $_ # string to be parsed |
|
|
346 | our $all; # find all, or just the first matching object |
|
|
347 | |
423 | |
348 | { |
424 | { |
349 | package cf::match::exec; |
425 | package cf::match::exec; |
350 | |
426 | |
351 | use List::Util qw(first); |
427 | use List::Util qw(first); |
352 | |
|
|
353 | sub env_chain { |
|
|
354 | my @res; |
|
|
355 | push @res, $_ |
|
|
356 | while $_ = $_->env; |
|
|
357 | @res |
|
|
358 | } |
|
|
359 | |
428 | |
360 | package cf::match::parser; |
429 | package cf::match::parser; |
361 | |
430 | |
362 | use common::sense; |
431 | use common::sense; |
363 | |
432 | |
364 | sub ws { |
433 | sub ws { |
365 | /\G\s+/gc; |
434 | /\G\s+/gc; |
366 | } |
435 | } |
367 | |
436 | |
|
|
437 | sub condition (); |
|
|
438 | sub match ($$); |
|
|
439 | |
368 | our %func = ( |
440 | our %func = ( |
369 | has => sub { |
441 | has => sub { |
370 | 'first { ' . &condition . ' } $_->inv' |
442 | 'first { ' . condition . ' } $_->inv' |
371 | }, |
443 | }, |
372 | count => sub { |
444 | count => sub { |
373 | local $all = 1; |
|
|
374 | '(scalar ' . &match ('$_') . ')' |
445 | '(scalar ' . (match 1, '$_') . ')' |
375 | }, |
|
|
376 | match => sub { |
|
|
377 | local $all = 0; |
|
|
378 | '(scalar ' . &match ('$_') . ')' |
|
|
379 | }, |
|
|
380 | ); |
|
|
381 | |
|
|
382 | our %special = ( |
|
|
383 | any => sub { |
|
|
384 | 1 |
|
|
385 | }, |
446 | }, |
386 | dump => sub { |
447 | dump => sub { |
387 | 'do { |
448 | 'do { |
388 | warn "cf::match::match dump:\n" |
449 | warn "cf::match::match dump:\n" |
389 | . "self: " . eval { $self->name } . "\n" |
450 | . "self: " . eval { $self->name } . "\n" |
… | |
… | |
391 | 1 |
452 | 1 |
392 | }'; |
453 | }'; |
393 | }, |
454 | }, |
394 | ); |
455 | ); |
395 | |
456 | |
|
|
457 | our %special = ( |
|
|
458 | any => sub { |
|
|
459 | 1 |
|
|
460 | }, |
|
|
461 | none => sub { |
|
|
462 | 0 |
|
|
463 | }, |
|
|
464 | ); |
|
|
465 | |
396 | sub constant { |
466 | sub constant { |
397 | ws; |
467 | ws; |
398 | |
468 | |
399 | return $1 if /\G([\-\+0-9\.]+)/gc; |
469 | return $1 if /\G([\-\+0-9\.]+)/gc; |
400 | return "cf::$1" if /\G([A-Z0-9_]+)/gc; |
470 | return "cf::$1" if /\G([A-Z0-9_]+)/gc; |
… | |
… | |
405 | die "number, string or uppercase constant name expected\n"; |
475 | die "number, string or uppercase constant name expected\n"; |
406 | } |
476 | } |
407 | |
477 | |
408 | our $flag = $cf::REFLECT{object}{flags}; |
478 | our $flag = $cf::REFLECT{object}{flags}; |
409 | our $sattr = $cf::REFLECT{object}{scalars}; |
479 | our $sattr = $cf::REFLECT{object}{scalars}; |
|
|
480 | # quick hack to support archname, untested |
|
|
481 | $sattr->{archname} = "W"; |
410 | our $aattr = $cf::REFLECT{object}{arrays}; |
482 | our $aattr = $cf::REFLECT{object}{arrays}; |
|
|
483 | our $lattr = $cf::REFLECT{living}{scalars}; |
411 | |
484 | |
412 | sub expr { |
485 | sub expr { |
413 | # ws done by factor |
486 | # ws done by factor |
414 | my $res; |
487 | my $res; |
415 | |
488 | |
… | |
… | |
418 | |
491 | |
419 | my $expr = $1; |
492 | my $expr = $1; |
420 | |
493 | |
421 | $res .= $expr =~ /\{([^;]+)\}/ ? $1 : "do $expr"; |
494 | $res .= $expr =~ /\{([^;]+)\}/ ? $1 : "do $expr"; |
422 | |
495 | |
|
|
496 | } elsif (/\Gstats\.([A-Za-z0-9_]+)/gc) { |
|
|
497 | |
|
|
498 | if (exists $lattr->{$1}) { |
|
|
499 | $res .= "\$_->stats->$1"; |
|
|
500 | } elsif (exists $lattr->{"\u$1"}) { |
|
|
501 | $res .= "\$_->stats->\u$1"; |
|
|
502 | } else { |
|
|
503 | die "living statistic name expected (str, pow, hp, sp...)\n"; |
|
|
504 | } |
|
|
505 | |
423 | } elsif (/\G([A-Za-z0-9_]+)/gc) { |
506 | } elsif (/\G([A-Za-z0-9_]+)/gc) { |
424 | |
507 | |
425 | if (my $func = $func{$1}) { |
508 | if (my $func = $func{$1}) { |
426 | /\G\s*\(/gc |
509 | /\G\s*\(/gc |
427 | or die "'(' expected after function name\n"; |
510 | or die "'(' expected after function name\n"; |
… | |
… | |
455 | } else { |
538 | } else { |
456 | $res .= constant; |
539 | $res .= constant; |
457 | } |
540 | } |
458 | |
541 | |
459 | } else { |
542 | } else { |
|
|
543 | Carp::cluck;#d# |
460 | die "expr expected\n"; |
544 | die "expr expected\n"; |
461 | } |
545 | } |
462 | |
546 | |
463 | $res |
547 | $res |
464 | } |
548 | } |
… | |
… | |
481 | $res .= "!"; |
565 | $res .= "!"; |
482 | } |
566 | } |
483 | |
567 | |
484 | if (/\G\(/gc) { |
568 | if (/\G\(/gc) { |
485 | # () |
569 | # () |
486 | $res .= &condition; |
570 | |
487 | ws; |
571 | $res .= '(' . (match 0, '$_') . ')'; |
|
|
572 | |
488 | /\G\)/gc or die "')' expected\n"; |
573 | /\G\s*\)/gc or die "closing ')' expected\n"; |
489 | |
574 | |
490 | } else { |
575 | } else { |
491 | my $expr = expr; |
576 | my $expr = expr; |
492 | |
577 | |
493 | $res .= $expr; |
578 | $res .= $expr; |
… | |
… | |
504 | } |
589 | } |
505 | |
590 | |
506 | "($res)" |
591 | "($res)" |
507 | } |
592 | } |
508 | |
593 | |
509 | sub condition { |
594 | sub condition () { |
510 | my $res = factor; |
595 | my $res = factor; |
511 | |
596 | |
512 | while () { |
597 | while () { |
513 | ws; |
598 | ws; |
|
|
599 | |
|
|
600 | # first check some stop-symbols, so we don't have to backtrack |
514 | if (/\G(?=also\b|in\b|\)|$)/gc) { |
601 | if (/\G(?=also\b|deep\b|in\b|of\b|\)|\z)/gc) { |
515 | # early stop => faster and requires no backtracking |
602 | pos = pos; # argh. the misop hits again. again. again. again. you die. |
516 | last; |
603 | last; |
|
|
604 | |
517 | } elsif (/\Gor\b/gc) { |
605 | } elsif (/\Gor\b/gc) { |
518 | $res .= " || "; |
606 | $res .= " || "; |
|
|
607 | |
519 | } else { |
608 | } else { |
520 | /\Gand\b/gc; |
609 | /\Gand\b/gc; |
521 | $res .= " && "; |
610 | $res .= " && "; |
522 | } |
611 | } |
523 | $res .= factor; |
612 | $res .= factor; |
524 | } |
613 | } |
525 | |
614 | |
526 | $res |
615 | $res |
527 | } |
616 | } |
528 | |
617 | |
529 | sub match { |
618 | sub match ($$) { |
530 | my $default = shift; |
619 | my ($wantarray, $defctx) = @_; |
531 | |
620 | |
532 | my $res; |
621 | my $res = condition; |
533 | |
622 | |
534 | my $also; # undef means first iteration |
623 | # if nothing follows, we have a simple condition, so |
|
|
624 | # optimise a comon case. |
|
|
625 | if ($defctx eq '$_' and /\G\s*(?=\)|$)/gc) { |
|
|
626 | return $wantarray |
|
|
627 | ? "$res ? \$_ : ()" |
|
|
628 | : $res; |
|
|
629 | } |
|
|
630 | |
|
|
631 | $res = ($wantarray ? " grep { " : " first { ") . $res . "}"; |
|
|
632 | |
535 | while () { |
633 | while () { |
536 | if (/\G\s*(inv|env|map|object|subject|originator)\b/gc) { |
634 | ws; |
|
|
635 | |
|
|
636 | my $also = /\Galso\s+/gc + 0; |
|
|
637 | my $deep = /\Gdeep\s+/gc + 0; |
|
|
638 | |
|
|
639 | if (/\Gin\s+/gc) { |
|
|
640 | my $expand; |
|
|
641 | |
|
|
642 | if (/\G(inv|env|map|arch|head)\b/gc) { |
537 | if ($1 eq "inv") { |
643 | if ($1 eq "inv") { |
538 | $res .= " map+(${also}\$_->inv),"; |
644 | $expand = "map \$_->inv,"; |
539 | } elsif ($1 eq "env") { |
645 | } elsif ($1 eq "env") { |
540 | $res .= " map+(${also}env_chain), "; # TODO |
646 | $expand = "map \$_->env // (),"; |
|
|
647 | } elsif ($1 eq "head") { |
|
|
648 | $expand = "map \$_->head,"; |
|
|
649 | $deep = 0; # infinite loop otherwise |
|
|
650 | } elsif ($1 eq "arch") { |
|
|
651 | $expand = "map \$_->arch,"; |
|
|
652 | $deep = 0; # infinite loop otherwise |
541 | } elsif ($1 eq "map") { |
653 | } elsif ($1 eq "map") { |
542 | $res .= " map+(${also}\$_->map->at (\$_->x, \$_->y)),"; |
654 | $expand = "map \$_->map->at (\$_->x, \$_->y),"; |
543 | } elsif ($1 eq "self") { |
655 | $deep = 0; # infinite loop otherwise |
544 | return "$res \$self"; |
656 | } |
545 | } elsif ($1 eq "object") { |
657 | } else { |
546 | return "$res \$object"; |
658 | $expand = "map \$_->inv, grep { " . condition . " }"; |
547 | } elsif ($1 eq "source") { |
|
|
548 | return "$res \$source"; |
|
|
549 | } elsif ($1 eq "originator") { |
|
|
550 | return "$res \$originator"; |
|
|
551 | } |
659 | } |
552 | last unless /\G\s*in\b/gc; |
660 | |
|
|
661 | if ($also || $deep) { |
|
|
662 | $res .= " do {\n" |
|
|
663 | . " my \@res;\n"; |
|
|
664 | $res .= " while (\@_) {\n" if $deep; |
|
|
665 | $res .= " push \@res, \@_;\n" if $also; |
|
|
666 | $res .= " \@_ = $expand \@_;\n"; |
|
|
667 | $res .= " }\n" if $deep; |
|
|
668 | $res .= " (\@res, \@_)\n" |
|
|
669 | . "}"; |
|
|
670 | } else { |
|
|
671 | $res .= " $expand"; |
|
|
672 | } |
553 | } else { |
673 | } else { |
554 | $res .= " map+($also\$_->inv)," if defined $also; |
|
|
555 | $res .= $all ? " grep { " : " first {"; |
|
|
556 | $res .= condition; |
|
|
557 | $res .= "}"; |
|
|
558 | |
674 | |
559 | $also = /\G\s*also\b/gc ? '$_, ' : ''; |
675 | if (/\Gof\s+(self|object|source|originator)\b/gc) { |
560 | last unless /\G\s*in\b/gc; |
676 | $also || $deep |
|
|
677 | and die "neither 'also' nor 'deep' can be used with 'of'\n"; |
|
|
678 | |
|
|
679 | if ($1 eq "self") { |
|
|
680 | return "$res \$self // ()"; |
|
|
681 | } elsif ($1 eq "object") { |
|
|
682 | return "$res \$object"; |
|
|
683 | } elsif ($1 eq "source") { |
|
|
684 | return "$res \$source // ()"; |
|
|
685 | } elsif ($1 eq "originator") { |
|
|
686 | return "$res \$originator // \$source // ()"; |
|
|
687 | } |
|
|
688 | } else { |
|
|
689 | return "$res $defctx"; |
|
|
690 | } |
561 | } |
691 | } |
562 | } |
692 | } |
563 | |
|
|
564 | "$res $default" |
|
|
565 | } |
693 | } |
566 | |
|
|
567 | } |
694 | } |
568 | |
695 | |
569 | sub parse($;$) { |
696 | sub parse($$) { # wantarray, matchexpr |
570 | local $_ = shift; |
697 | my $res; |
571 | local $all = shift; |
|
|
572 | |
698 | |
|
|
699 | local $_ = $_[1]; |
|
|
700 | |
|
|
701 | eval { |
573 | my $res = cf::match::parser::match "\$object"; |
702 | $res = cf::match::parser::match $_[0], "\$object"; |
|
|
703 | |
|
|
704 | /\G$/gc |
|
|
705 | or die "unexpected trailing characters after match\n"; |
|
|
706 | }; |
574 | |
707 | |
575 | if ($@) { |
708 | if ($@) { |
576 | my $ctx = 20; |
709 | my $ctx = 20; |
577 | my $str = substr $_, (List::Util::max 0, (pos) - $ctx), $ctx * 2; |
710 | my $str = substr $_, (List::Util::max 0, (pos) - $ctx), $ctx * 2; |
578 | substr $str, (List::Util::min $ctx, pos), 0, "<-- HERE -->"; |
711 | substr $str, (List::Util::min $ctx, pos), 0, "<-- HERE -->"; |
… | |
… | |
582 | } |
715 | } |
583 | |
716 | |
584 | $res |
717 | $res |
585 | } |
718 | } |
586 | |
719 | |
587 | if (0) { |
720 | if (0) {#d# |
588 | die parse 'match(any in object)', 1; |
721 | die parse 1, 'stats.pow'; |
589 | exit 0; |
722 | exit 0; |
|
|
723 | } |
|
|
724 | |
|
|
725 | our %CACHE; |
|
|
726 | |
|
|
727 | sub compile($$) { |
|
|
728 | my ($wantarray, $match) = @_; |
|
|
729 | my $expr = parse $wantarray, $match; |
|
|
730 | # warn "MATCH DEBUG $match,$wantarray => $expr\n";#d# |
|
|
731 | $expr = eval " |
|
|
732 | package cf::match::exec; |
|
|
733 | sub { |
|
|
734 | my (\$object, \$self, \$source, \$originator) = \@_; |
|
|
735 | $expr |
|
|
736 | } |
|
|
737 | "; |
|
|
738 | die if $@; |
|
|
739 | |
|
|
740 | $expr |
590 | } |
741 | } |
591 | |
742 | |
592 | =item cf::match::match $match, $object[, $self[, $source[, $originator]]] |
743 | =item cf::match::match $match, $object[, $self[, $source[, $originator]]] |
593 | |
744 | |
594 | Compiles (and caches) the C<$match> expression and matches it against |
745 | Compiles (and caches) the C<$match> expression and matches it against |
… | |
… | |
600 | In list context it finds and returns all matching objects, in scalar |
751 | In list context it finds and returns all matching objects, in scalar |
601 | context only a true or false value. |
752 | context only a true or false value. |
602 | |
753 | |
603 | =cut |
754 | =cut |
604 | |
755 | |
605 | our %CACHE; |
|
|
606 | |
|
|
607 | sub compile($$) { |
|
|
608 | my ($match, $all) = @_; |
|
|
609 | my $expr = parse $match, $all; |
|
|
610 | warn "$match,$all => $expr\n";#d# |
|
|
611 | $expr = eval " |
|
|
612 | package cf::match::exec; |
|
|
613 | sub { |
|
|
614 | my (\$object, \$self, \$source, \$originator) = \@_; |
|
|
615 | \$originator ||= \$source; |
|
|
616 | $expr |
|
|
617 | } |
|
|
618 | "; |
|
|
619 | die if $@; |
|
|
620 | |
|
|
621 | $expr |
|
|
622 | } |
|
|
623 | |
|
|
624 | sub match($$;$$$) { |
756 | sub match($$;$$$) { |
625 | my $match = shift; |
757 | my $match = shift; |
626 | my $all = wantarray+0; |
758 | my $wantarray = wantarray+0; |
627 | |
759 | |
628 | &{ |
760 | &{ |
629 | $CACHE{"$all$match"} ||= compile $match, $all |
761 | $CACHE{"$wantarray$match"} ||= compile $wantarray, $match |
630 | } |
762 | } |
631 | } |
763 | } |
632 | |
764 | |
|
|
765 | our $CACHE_CLEARER = AE::timer 3600, 3600, sub { |
|
|
766 | %CACHE = (); |
|
|
767 | }; |
|
|
768 | |
633 | #d# $::schmorp=cf::player::find "schmorp"& |
769 | #d# $::schmorp=cf::player::find "schmorp"& |
634 | #d# cf::match::match '', $::schmorp->ob |
770 | #d# cf::match::match '', $::schmorp->ob |
635 | |
771 | |
636 | |
772 | |
637 | =back |
773 | =back |