Index: trunk/extensions/AbuseFilter/parser_native/parser.h |
— | — | @@ -9,21 +9,25 @@ |
10 | 10 | |
11 | 11 | #include "aftypes.h" |
12 | 12 | |
| 13 | +namespace afp { |
| 14 | + |
13 | 15 | struct parser_grammar; |
14 | 16 | |
15 | 17 | struct expressor : boost::noncopyable { |
16 | | - typedef boost::function<AFPData (std::vector<AFPData>)> func_t; |
| 18 | + typedef boost::function<datum (std::vector<datum>)> func_t; |
17 | 19 | |
18 | 20 | expressor(); |
19 | 21 | ~expressor(); |
20 | 22 | |
21 | | - AFPData evaluate(std::string const &expr) const; |
| 23 | + datum evaluate(std::string const &expr) const; |
22 | 24 | |
23 | | - void add_variable(std::string const &name, AFPData value); |
| 25 | + void add_variable(std::string const &name, datum value); |
24 | 26 | void add_function(std::string const &name, func_t value); |
25 | 27 | |
26 | 28 | private: |
27 | 29 | parser_grammar *grammar_; |
28 | 30 | }; |
29 | 31 | |
| 32 | +} // namespace afp |
| 33 | + |
30 | 34 | #endif /* !EXPRESSOR_H */ |
Index: trunk/extensions/AbuseFilter/parser_native/filter_evaluator.cpp |
— | — | @@ -2,6 +2,8 @@ |
3 | 3 | #include "parser.h" |
4 | 4 | #include "affunctions.h" |
5 | 5 | |
| 6 | +namespace afp { |
| 7 | + |
6 | 8 | filter_evaluator::filter_evaluator() |
7 | 9 | { |
8 | 10 | e.add_function("length", af_length); |
— | — | @@ -26,8 +28,9 @@ |
27 | 29 | } |
28 | 30 | |
29 | 31 | void |
30 | | -filter_evaluator::add_variable(std::string const &key, AFPData value) |
| 32 | +filter_evaluator::add_variable(std::string const &key, datum value) |
31 | 33 | { |
32 | 34 | e.add_variable(key, value); |
33 | 35 | } |
34 | 36 | |
| 37 | +} // namespace afp |
Index: trunk/extensions/AbuseFilter/parser_native/aftypes.cpp |
— | — | @@ -1,30 +1,27 @@ |
2 | | -#include "aftypes.h" |
3 | 2 | #include <sstream> |
4 | 3 | #include <ios> |
5 | 4 | #include <iostream> |
6 | 5 | #include <cassert> |
7 | 6 | #include <algorithm> |
8 | 7 | #include <cmath> |
| 8 | + |
9 | 9 | #include <boost/lexical_cast.hpp> |
10 | 10 | |
11 | | -AFPToken::AFPToken(unsigned int new_type, string new_value, unsigned int new_pos) { |
12 | | - type = new_type; |
13 | | - value = new_value; |
14 | | - pos = new_pos; |
15 | | -} |
| 11 | +#include "aftypes.h" |
16 | 12 | |
| 13 | +namespace afp { |
17 | 14 | |
18 | | -AFPData::AFPData(std::string const &var) { |
| 15 | +datum::datum(std::string const &var) { |
19 | 16 | _init_from_string(var); |
20 | 17 | } |
21 | 18 | |
22 | | -AFPData::AFPData(char const *var) |
| 19 | +datum::datum(char const *var) |
23 | 20 | { |
24 | 21 | _init_from_string(var); |
25 | 22 | } |
26 | 23 | |
27 | 24 | void |
28 | | -AFPData::_init_from_string(std::string const &var) |
| 25 | +datum::_init_from_string(std::string const &var) |
29 | 26 | { |
30 | 27 | // Try integer |
31 | 28 | try { |
— | — | @@ -39,35 +36,35 @@ |
40 | 37 | } |
41 | 38 | } |
42 | 39 | |
43 | | -AFPData::AFPData() { |
| 40 | +datum::datum() { |
44 | 41 | } |
45 | 42 | |
46 | | -AFPData::AFPData(AFPData const &other) |
| 43 | +datum::datum(datum const &other) |
47 | 44 | : value_(other.value_) |
48 | 45 | { |
49 | 46 | } |
50 | 47 | |
51 | | -AFPData::AFPData(long int var) |
| 48 | +datum::datum(long int var) |
52 | 49 | : value_(var) |
53 | 50 | { |
54 | 51 | } |
55 | 52 | |
56 | | -AFPData::AFPData(double var) |
| 53 | +datum::datum(double var) |
57 | 54 | : value_(var) |
58 | 55 | { |
59 | 56 | } |
60 | 57 | |
61 | | -AFPData::AFPData(float var) |
| 58 | +datum::datum(float var) |
62 | 59 | : value_(var) |
63 | 60 | { |
64 | 61 | } |
65 | 62 | |
66 | | -AFPData::AFPData(bool var) |
| 63 | +datum::datum(bool var) |
67 | 64 | : value_((long int) var) |
68 | 65 | { |
69 | 66 | } |
70 | 67 | |
71 | | -AFPData & AFPData::operator= (AFPData const &other) { |
| 68 | +datum & datum::operator= (datum const &other) { |
72 | 69 | // Protect against self-assignment |
73 | 70 | if (this == &other) { |
74 | 71 | return *this; |
— | — | @@ -77,10 +74,6 @@ |
78 | 75 | return *this; |
79 | 76 | } |
80 | 77 | |
81 | | -bool isInVector(std::string const &needle, std::vector<std::string> const &haystack) { |
82 | | - return std::find(haystack.begin(), haystack.end(), needle) != haystack.end(); |
83 | | -} |
84 | | - |
85 | 78 | /* |
86 | 79 | * Convert a string to an integer value. |
87 | 80 | */ |
— | — | @@ -108,7 +101,7 @@ |
109 | 102 | }; |
110 | 103 | |
111 | 104 | /* |
112 | | - * Conversions from AFPData to other types. |
| 105 | + * Conversions from datum to other types. |
113 | 106 | */ |
114 | 107 | struct to_string_visitor : boost::static_visitor<std::string> { |
115 | 108 | std::string operator() (std::string const &v) const { |
— | — | @@ -156,17 +149,17 @@ |
157 | 150 | }; |
158 | 151 | |
159 | 152 | std::string |
160 | | -AFPData::toString() const { |
| 153 | +datum::toString() const { |
161 | 154 | return boost::apply_visitor(to_string_visitor(), value_); |
162 | 155 | } |
163 | 156 | |
164 | 157 | long int |
165 | | -AFPData::toInt() const { |
| 158 | +datum::toInt() const { |
166 | 159 | return boost::apply_visitor(to_int_visitor(), value_); |
167 | 160 | } |
168 | 161 | |
169 | 162 | double |
170 | | -AFPData::toFloat() const { |
| 163 | +datum::toFloat() const { |
171 | 164 | return boost::apply_visitor(to_double_visitor(), value_); |
172 | 165 | } |
173 | 166 | |
— | — | @@ -213,13 +206,13 @@ |
214 | 207 | * after doing appropriate int->double promotion. |
215 | 208 | */ |
216 | 209 | template<template<typename V> class Operator> |
217 | | -struct arith_visitor : boost::static_visitor<AFPData> { |
| 210 | +struct arith_visitor : boost::static_visitor<datum> { |
218 | 211 | /* |
219 | 212 | * Anything involving a double returns a double. |
220 | 213 | * Otherwise, int is returned. |
221 | 214 | */ |
222 | 215 | template<typename T, typename U> |
223 | | - AFPData operator() (T const &a, U const &b) const { |
| 216 | + datum operator() (T const &a, U const &b) const { |
224 | 217 | typedef typename from_string_converter<T>::type a_type; |
225 | 218 | typedef typename from_string_converter<U>::type b_type; |
226 | 219 | |
— | — | @@ -272,7 +265,7 @@ |
273 | 266 | * For comparisons that only work on integers - strings will be converted. |
274 | 267 | */ |
275 | 268 | template<template<typename V> class Operator> |
276 | | -struct arith_compare_visitor : boost::static_visitor<AFPData> { |
| 269 | +struct arith_compare_visitor : boost::static_visitor<datum> { |
277 | 270 | template<typename T, typename U> |
278 | 271 | bool operator() (T const &a, U const &b) const { |
279 | 272 | typedef typename from_string_converter<T>::type a_type; |
— | — | @@ -285,83 +278,83 @@ |
286 | 279 | } |
287 | 280 | }; |
288 | 281 | |
289 | | -AFPData & |
290 | | -AFPData::operator+=(AFPData const &other) |
| 282 | +datum & |
| 283 | +datum::operator+=(datum const &other) |
291 | 284 | { |
292 | | - AFPData result = boost::apply_visitor(arith_visitor<std::plus>(), value_, other.value_); |
| 285 | + datum result = boost::apply_visitor(arith_visitor<std::plus>(), value_, other.value_); |
293 | 286 | *this = result; |
294 | 287 | return *this; |
295 | 288 | } |
296 | 289 | |
297 | | -AFPData & |
298 | | -AFPData::operator-=(AFPData const &other) |
| 290 | +datum & |
| 291 | +datum::operator-=(datum const &other) |
299 | 292 | { |
300 | | - AFPData result = boost::apply_visitor(arith_visitor<std::minus>(), value_, other.value_); |
| 293 | + datum result = boost::apply_visitor(arith_visitor<std::minus>(), value_, other.value_); |
301 | 294 | *this = result; |
302 | 295 | return *this; |
303 | 296 | } |
304 | 297 | |
305 | | -AFPData & |
306 | | -AFPData::operator*=(AFPData const &other) |
| 298 | +datum & |
| 299 | +datum::operator*=(datum const &other) |
307 | 300 | { |
308 | | - AFPData result = boost::apply_visitor(arith_visitor<std::multiplies>(), value_, other.value_); |
| 301 | + datum result = boost::apply_visitor(arith_visitor<std::multiplies>(), value_, other.value_); |
309 | 302 | *this = result; |
310 | 303 | return *this; |
311 | 304 | } |
312 | 305 | |
313 | | -AFPData& |
314 | | -AFPData::operator/=(AFPData const &other) |
| 306 | +datum& |
| 307 | +datum::operator/=(datum const &other) |
315 | 308 | { |
316 | | - AFPData result = boost::apply_visitor(arith_visitor<std::divides>(), value_, other.value_); |
| 309 | + datum result = boost::apply_visitor(arith_visitor<std::divides>(), value_, other.value_); |
317 | 310 | *this = result; |
318 | 311 | return *this; |
319 | 312 | } |
320 | 313 | |
321 | | -AFPData& |
322 | | -AFPData::operator%=(AFPData const &other) |
| 314 | +datum& |
| 315 | +datum::operator%=(datum const &other) |
323 | 316 | { |
324 | | - AFPData result = boost::apply_visitor(arith_visitor<afpmodulus>(), value_, other.value_); |
| 317 | + datum result = boost::apply_visitor(arith_visitor<afpmodulus>(), value_, other.value_); |
325 | 318 | *this = result; |
326 | 319 | return *this; |
327 | 320 | } |
328 | 321 | |
329 | | -AFPData |
330 | | -operator+(AFPData const &a, AFPData const &b) { |
331 | | - return AFPData(a) += b; |
| 322 | +datum |
| 323 | +operator+(datum const &a, datum const &b) { |
| 324 | + return datum(a) += b; |
332 | 325 | } |
333 | 326 | |
334 | | -AFPData |
335 | | -operator-(AFPData const &a, AFPData const &b) { |
336 | | - return AFPData(a) -= b; |
| 327 | +datum |
| 328 | +operator-(datum const &a, datum const &b) { |
| 329 | + return datum(a) -= b; |
337 | 330 | } |
338 | 331 | |
339 | | -AFPData |
340 | | -operator*(AFPData const &a, AFPData const &b) { |
341 | | - return AFPData(a) *= b; |
| 332 | +datum |
| 333 | +operator*(datum const &a, datum const &b) { |
| 334 | + return datum(a) *= b; |
342 | 335 | } |
343 | 336 | |
344 | | -AFPData |
345 | | -operator/(AFPData const &a, AFPData const &b) { |
346 | | - return AFPData(a) /= b; |
| 337 | +datum |
| 338 | +operator/(datum const &a, datum const &b) { |
| 339 | + return datum(a) /= b; |
347 | 340 | } |
348 | 341 | |
349 | | -AFPData |
350 | | -operator%(AFPData const &a, AFPData const &b) { |
351 | | - return AFPData(a) %= b; |
| 342 | +datum |
| 343 | +operator%(datum const &a, datum const &b) { |
| 344 | + return datum(a) %= b; |
352 | 345 | } |
353 | 346 | |
354 | 347 | bool |
355 | | -operator==(AFPData const &a, AFPData const &b) { |
| 348 | +operator==(datum const &a, datum const &b) { |
356 | 349 | return a.compare(b); |
357 | 350 | } |
358 | 351 | |
359 | 352 | bool |
360 | | -AFPData::compare(AFPData const &other) const { |
| 353 | +datum::compare(datum const &other) const { |
361 | 354 | return boost::apply_visitor(compare_visitor<std::equal_to>(), value_, other.value_); |
362 | 355 | } |
363 | 356 | |
364 | 357 | bool |
365 | | -AFPData::compare_with_type(AFPData const &other) const { |
| 358 | +datum::compare_with_type(datum const &other) const { |
366 | 359 | if (value_.which() != other.value_.which()) |
367 | 360 | return false; |
368 | 361 | |
— | — | @@ -369,36 +362,38 @@ |
370 | 363 | } |
371 | 364 | |
372 | 365 | bool |
373 | | -AFPData::less_than(AFPData const &other) const { |
| 366 | +datum::less_than(datum const &other) const { |
374 | 367 | return boost::apply_visitor(arith_compare_visitor<std::less>(), value_, other.value_); |
375 | 368 | } |
376 | 369 | |
377 | 370 | bool |
378 | | -operator< (AFPData const &a, AFPData const &b) { |
| 371 | +operator< (datum const &a, datum const &b) { |
379 | 372 | return a.less_than(b); |
380 | 373 | } |
381 | 374 | |
382 | 375 | bool |
383 | | -operator<= (AFPData const &a, AFPData const &b) { |
| 376 | +operator<= (datum const &a, datum const &b) { |
384 | 377 | return a.less_than(b) || a == b; |
385 | 378 | } |
386 | 379 | |
387 | 380 | bool |
388 | | -operator> (AFPData const &a, AFPData const &b) { |
| 381 | +operator> (datum const &a, datum const &b) { |
389 | 382 | return !(a <= b); |
390 | 383 | } |
391 | 384 | |
392 | 385 | bool |
393 | | -operator>= (AFPData const &a, AFPData const &b) { |
| 386 | +operator>= (datum const &a, datum const &b) { |
394 | 387 | return !(a < b); |
395 | 388 | } |
396 | 389 | |
397 | 390 | bool |
398 | | -operator!= (AFPData const &a, AFPData const &b) { |
| 391 | +operator!= (datum const &a, datum const &b) { |
399 | 392 | return !(a == b); |
400 | 393 | } |
401 | 394 | |
402 | 395 | bool |
403 | | -AFPData::operator! () const { |
| 396 | +datum::operator! () const { |
404 | 397 | return !(int) *this; |
405 | 398 | } |
| 399 | + |
| 400 | +} // namespace afp |
Index: trunk/extensions/AbuseFilter/parser_native/filter_evaluator.h |
— | — | @@ -7,15 +7,19 @@ |
8 | 8 | #include "aftypes.h" |
9 | 9 | #include "parser.h" |
10 | 10 | |
| 11 | +namespace afp { |
| 12 | + |
11 | 13 | struct filter_evaluator { |
12 | 14 | filter_evaluator(); |
13 | 15 | |
14 | 16 | bool evaluate(std::string const &filter) const; |
15 | 17 | |
16 | | - void add_variable(std::string const &key, AFPData value); |
| 18 | + void add_variable(std::string const &key, datum value); |
17 | 19 | |
18 | 20 | private: |
19 | 21 | expressor e; |
20 | 22 | }; |
21 | 23 | |
| 24 | +} // namespace afp |
| 25 | + |
22 | 26 | #endif /* !FILTER_EVALUATOR_H */ |
Index: trunk/extensions/AbuseFilter/parser_native/check.cpp |
— | — | @@ -2,15 +2,15 @@ |
3 | 3 | #include "affunctions.h" |
4 | 4 | |
5 | 5 | int main( int argc, char** argv ) { |
6 | | - filter_evaluator f; |
| 6 | + afp::filter_evaluator f; |
7 | 7 | |
8 | 8 | bool result = false; |
9 | 9 | |
10 | 10 | for(int i=0;i<=100;i++) { |
11 | 11 | try { |
12 | | - f.add_variable( "foo", AFPData(string("love")) ); |
| 12 | + f.add_variable("foo", afp::datum("love")); |
13 | 13 | result = f.evaluate( "specialratio('foo;') == 0.25" ); |
14 | | - } catch (AFPException* excep) { |
| 14 | + } catch (afp::exception* excep) { |
15 | 15 | printf( "Exception: %s\n", excep->what() ); |
16 | 16 | } |
17 | 17 | } |
Index: trunk/extensions/AbuseFilter/parser_native/affunctions.cpp |
— | — | @@ -6,18 +6,18 @@ |
7 | 7 | #include <iostream> |
8 | 8 | #include <ctype.h> |
9 | 9 | |
10 | | -#include <unicode/utf8.h> |
11 | | -#include <unicode/ustring.h> |
| 10 | +#include "utf8.h" |
| 11 | +#include "equiv.h" |
12 | 12 | |
13 | | -#define EQUIVSET_LOC "equivset.txt" |
| 13 | +namespace afp { |
14 | 14 | |
15 | | -AFPData |
16 | | -af_count(std::vector<AFPData> const &args) { |
| 15 | +datum |
| 16 | +af_count(std::vector<datum> const &args) { |
17 | 17 | if (!args.size()) { |
18 | | - throw AFPException( "Not enough arguments to count" ); |
| 18 | + throw exception( "Not enough arguments to count" ); |
19 | 19 | } |
20 | 20 | |
21 | | - string needle, haystack; |
| 21 | + std::string needle, haystack; |
22 | 22 | |
23 | 23 | if (args.size() < 2) { |
24 | 24 | needle = ","; |
— | — | @@ -40,52 +40,48 @@ |
41 | 41 | count--; |
42 | 42 | } |
43 | 43 | |
44 | | - return AFPData((long int)count); |
| 44 | + return datum((long int)count); |
45 | 45 | } |
46 | 46 | |
47 | | -AFPData |
48 | | -af_norm(vector<AFPData> const &args) { |
| 47 | +datum |
| 48 | +af_norm(std::vector<datum> const &args) { |
49 | 49 | if (!args.size()) { |
50 | | - throw AFPException( "Not enough arguments to norm" ); |
| 50 | + throw exception( "Not enough arguments to norm" ); |
51 | 51 | } |
52 | 52 | |
53 | | - string orig = args[0].toString(); |
| 53 | + std::string orig = args[0].toString(); |
54 | 54 | |
55 | | - string::const_iterator p, charStart, end; |
56 | | - int chr = 0,lastchr = 0; |
57 | | - map<int,int> const &equivSet = getEquivSet(); |
58 | | - string result; |
| 55 | + std::string::const_iterator p, charStart, end; |
| 56 | + int chr = 0, lastchr = 0; |
| 57 | + equiv_set const &equivs = equiv_set::instance(); |
| 58 | + std::string result; |
59 | 59 | |
60 | 60 | p = orig.begin(); |
61 | 61 | end = orig.end(); |
62 | 62 | |
63 | | - while (chr = next_utf8_char( p, charStart, end )) { |
64 | | - std::map<int, int>::const_iterator it; |
65 | | - if ((it = equivSet.find(chr)) != equivSet.end()) { |
66 | | - chr = it->second; |
67 | | - } |
| 63 | + while (chr = utf8::next_utf8_char( p, charStart, end )) { |
| 64 | + chr = equivs.get(chr); |
68 | 65 | |
69 | | - if (chr != lastchr && isalnum(chr)) { |
70 | | - result.append(codepointToUtf8(chr)); |
71 | | - } |
| 66 | + if (chr != lastchr && isalnum(chr)) |
| 67 | + result.append(utf8::codepoint_to_utf8(chr)); |
72 | 68 | |
73 | 69 | lastchr = chr; |
74 | 70 | } |
75 | 71 | |
76 | | - return AFPData(result); |
| 72 | + return datum(result); |
77 | 73 | } |
78 | 74 | |
79 | | -string |
| 75 | +std::string |
80 | 76 | rmdoubles(std::string const &orig) { |
81 | | - string::const_iterator p, charStart, end; |
| 77 | + std::string::const_iterator p, charStart, end; |
82 | 78 | int chr,lastchr = 0; |
83 | | - string result; |
| 79 | + std::string result; |
84 | 80 | |
85 | 81 | p = orig.begin(); |
86 | 82 | end = orig.end(); |
87 | | - while (chr = next_utf8_char( p, charStart, end )) { |
| 83 | + while (chr = utf8::next_utf8_char( p, charStart, end )) { |
88 | 84 | if (chr != lastchr) { |
89 | | - result.append(codepointToUtf8(chr)); |
| 85 | + result.append(utf8::codepoint_to_utf8(chr)); |
90 | 86 | } |
91 | 87 | |
92 | 88 | lastchr = chr; |
— | — | @@ -94,277 +90,108 @@ |
95 | 91 | return result; |
96 | 92 | } |
97 | 93 | |
98 | | -AFPData |
99 | | -af_specialratio(std::vector<AFPData> const &args) { |
| 94 | +datum |
| 95 | +af_specialratio(std::vector<datum> const &args) { |
100 | 96 | if (!args.size()) { |
101 | | - throw AFPException( "Not enough arguments to specialratio" ); |
| 97 | + throw exception( "Not enough arguments to specialratio" ); |
102 | 98 | } |
103 | 99 | |
104 | | - string orig = args[0].toString(); |
105 | | - string::const_iterator p, charStart, end; |
106 | | - int chr,lastchr = 0; |
| 100 | + std::string orig = args[0].toString(); |
| 101 | + std::string::const_iterator p, charStart, end; |
| 102 | + int chr; |
107 | 103 | int specialcount = 0; |
108 | 104 | |
109 | 105 | p = orig.begin(); |
110 | 106 | end = orig.end(); |
111 | | - while (chr = next_utf8_char( p, charStart, end )) { |
| 107 | + while (chr = utf8::next_utf8_char( p, charStart, end )) { |
112 | 108 | if (!isalnum(chr)) { |
113 | 109 | specialcount++; |
114 | 110 | } |
115 | 111 | } |
116 | 112 | |
117 | | - double ratio = (float)(specialcount) / (float)(utf8_strlen(orig)); |
| 113 | + double ratio = (float)(specialcount) / (float)(utf8::utf8_strlen(orig)); |
118 | 114 | |
119 | | - return AFPData(ratio); |
| 115 | + return datum(ratio); |
120 | 116 | } |
121 | 117 | |
122 | | -AFPData |
123 | | -af_rmspecials(std::vector<AFPData> const &args) { |
| 118 | +datum |
| 119 | +af_rmspecials(std::vector<datum> const &args) { |
124 | 120 | if (!args.size()) { |
125 | | - throw AFPException( "Not enough arguments to rmspecials" ); |
| 121 | + throw exception( "Not enough arguments to rmspecials" ); |
126 | 122 | } |
127 | 123 | |
128 | | - return AFPData(rmspecials(args[0].toString())); |
| 124 | + return datum(rmspecials(args[0].toString())); |
129 | 125 | } |
130 | 126 | |
131 | 127 | std::string |
132 | 128 | rmspecials(std::string const &orig) { |
133 | | - string::const_iterator p, charStart, end; |
| 129 | + std::string::const_iterator p, charStart, end; |
134 | 130 | int chr = 0; |
135 | | - string result; |
| 131 | + std::string result; |
136 | 132 | |
137 | 133 | p = orig.begin(); |
138 | 134 | end = orig.end(); |
139 | | - while (chr = next_utf8_char( p, charStart, end )) { |
| 135 | + while (chr = utf8::next_utf8_char( p, charStart, end )) { |
140 | 136 | if (isalnum(chr)) { |
141 | | - result.append(codepointToUtf8(chr)); |
| 137 | + result.append(utf8::codepoint_to_utf8(chr)); |
142 | 138 | } |
143 | 139 | } |
144 | 140 | |
145 | 141 | return result; |
146 | 142 | } |
147 | 143 | |
148 | | -AFPData |
149 | | -af_ccnorm(std::vector<AFPData> const &args) { |
| 144 | +datum |
| 145 | +af_ccnorm(std::vector<datum> const &args) { |
150 | 146 | if (!args.size()) { |
151 | | - throw AFPException( "Not enough arguments to ccnorm" ); |
| 147 | + throw exception( "Not enough arguments to ccnorm" ); |
152 | 148 | } |
153 | 149 | |
154 | | - return AFPData( confusable_character_normalise( args[0].toString() ) ); |
| 150 | + return datum( confusable_character_normalise( args[0].toString() ) ); |
155 | 151 | } |
156 | 152 | |
157 | | -AFPData |
158 | | -af_rmdoubles(std::vector<AFPData> const &args) { |
| 153 | +datum |
| 154 | +af_rmdoubles(std::vector<datum> const &args) { |
159 | 155 | if (!args.size()) { |
160 | | - throw AFPException( "Not enough arguments to rmdoubles" ); |
| 156 | + throw exception( "Not enough arguments to rmdoubles" ); |
161 | 157 | } |
162 | 158 | |
163 | | - return AFPData(rmdoubles(args[0].toString())); |
| 159 | + return datum(rmdoubles(args[0].toString())); |
164 | 160 | } |
165 | 161 | |
166 | | -AFPData |
167 | | -af_length(std::vector<AFPData> const &args) { |
| 162 | +datum |
| 163 | +af_length(std::vector<datum> const &args) { |
168 | 164 | if (!args.size()) { |
169 | | - throw AFPException( "Not enough arguments to lcase" ); |
| 165 | + throw exception( "Not enough arguments to lcase" ); |
170 | 166 | } |
171 | 167 | |
172 | | - return AFPData( (long int)utf8_strlen(args[0].toString()) ); |
| 168 | + return datum( (long int)utf8::utf8_strlen(args[0].toString()) ); |
173 | 169 | } |
174 | 170 | |
175 | | -AFPData |
176 | | -af_lcase(std::vector<AFPData> const &args) { |
| 171 | +datum |
| 172 | +af_lcase(std::vector<datum> const &args) { |
177 | 173 | if (!args.size()) { |
178 | | - throw AFPException( "Not enough arguments to lcase" ); |
| 174 | + throw exception( "Not enough arguments to lcase" ); |
179 | 175 | } |
180 | 176 | |
181 | | - return AFPData(utf8_tolower(args[0].toString())); |
| 177 | + return datum(utf8::utf8_tolower(args[0].toString())); |
182 | 178 | } |
183 | 179 | |
184 | 180 | std::string |
185 | 181 | confusable_character_normalise(std::string const &orig) { |
186 | | - string::const_iterator p, charStart, end; |
| 182 | + std::string::const_iterator p, charStart, end; |
187 | 183 | int chr; |
188 | | - map<int,int> const &equivSet = getEquivSet(); |
189 | | - string result; |
| 184 | + equiv_set const &equivs = equiv_set::instance(); |
| 185 | + std::string result; |
190 | 186 | |
191 | 187 | p = orig.begin(); |
192 | 188 | end = orig.end(); |
193 | 189 | |
194 | | - while (chr = next_utf8_char( p, charStart, end )) { |
195 | | - map<int, int>::const_iterator it; |
196 | | - if ((it = equivSet.find(chr)) != equivSet.end()) { |
197 | | - chr = it->second; |
198 | | - } |
199 | | - |
200 | | - result.append(codepointToUtf8(chr)); |
| 190 | + while (chr = utf8::next_utf8_char( p, charStart, end )) { |
| 191 | + chr = equivs.get(chr); |
| 192 | + result.append(utf8::codepoint_to_utf8(chr)); |
201 | 193 | } |
202 | 194 | |
203 | 195 | return result; |
204 | 196 | } |
205 | 197 | |
206 | | -map<int,int> const & |
207 | | -getEquivSet() { |
208 | | - static map<int,int> equivSet; |
209 | | - // Map of codepoint:codepoint |
210 | | - |
211 | | - if (equivSet.empty()) { |
212 | | - ifstream eqsFile( EQUIVSET_LOC ); |
213 | | - |
214 | | - if (!eqsFile) { |
215 | | - throw AFPException( "Unable to open equivalence sets!" ); |
216 | | - } |
217 | | - |
218 | | - string line; |
219 | | - |
220 | | - while (getline(eqsFile,line)) { |
221 | | - size_t pos = line.find_first_of( ":", 0 ); |
222 | | - |
223 | | - if (pos != line.npos) { |
224 | | - // We have a codepoint:codepoint thing. |
225 | | - int actual = 0; |
226 | | - int canonical = 0; |
227 | | - |
228 | | - istringstream actual_buffer(line.substr(0,pos)); |
229 | | - istringstream canon_buffer( line.substr(pos+1)); |
230 | | - actual_buffer >> actual; |
231 | | - canon_buffer >> canonical; |
232 | | - |
233 | | - if (actual != 0 && canonical != 0) { |
234 | | - equivSet[actual] = canonical; |
235 | | - } |
236 | | - } |
237 | | - } |
238 | | - |
239 | | - eqsFile.close(); |
240 | | - } |
241 | | - |
242 | | - return equivSet; |
243 | | -} |
244 | | - |
245 | | -// Weak UTF-8 decoder |
246 | | -// Will return garbage on invalid input (overshort sequences, overlong sequences, etc.) |
247 | | -// Stolen from wikidiff2 extension by Tim Starling (no point in reinventing the wheel) |
248 | | -int |
249 | | -next_utf8_char(std::string::const_iterator & p, std::string::const_iterator & charStart, |
250 | | - std::string::const_iterator end) |
251 | | -{ |
252 | | - int c=0; |
253 | | - unsigned char byte; |
254 | | - int bytes = 0; |
255 | | - charStart = p; |
256 | | - if (p == end) { |
257 | | - return 0; |
258 | | - } |
259 | | - do { |
260 | | - byte = (unsigned char)*p; |
261 | | - if (byte < 0x80) { |
262 | | - c = byte; |
263 | | - bytes = 0; |
264 | | - } else if (byte >= 0xc0) { |
265 | | - // Start of UTF-8 character |
266 | | - // If this is unexpected, due to an overshort sequence, we ignore the invalid |
267 | | - // sequence and resynchronise here |
268 | | - if (byte < 0xe0) { |
269 | | - bytes = 1; |
270 | | - c = byte & 0x1f; |
271 | | - } else if (byte < 0xf0) { |
272 | | - bytes = 2; |
273 | | - c = byte & 0x0f; |
274 | | - } else { |
275 | | - bytes = 3; |
276 | | - c = byte & 7; |
277 | | - } |
278 | | - } else if (bytes) { |
279 | | - c <<= 6; |
280 | | - c |= byte & 0x3f; |
281 | | - --bytes; |
282 | | - } else { |
283 | | - // Unexpected continuation, ignore |
284 | | - } |
285 | | - ++p; |
286 | | - } while (bytes && p != end); |
287 | | - return c; |
288 | | -} |
289 | | - |
290 | | -std::size_t |
291 | | -utf8_strlen(std::string const &s) |
292 | | -{ |
293 | | -std::size_t ret = 0; |
294 | | - for (std::string::const_iterator it = s.begin(), end = s.end(); |
295 | | - it < end; ++it) |
296 | | - { |
297 | | - int skip = 1; |
298 | | - |
299 | | - skip = U8_LENGTH(*it); |
300 | | - if (it + skip >= end) |
301 | | - return ret; /* end of string */ |
302 | | - |
303 | | - it += skip; |
304 | | - } |
305 | | - |
306 | | - return ret; |
307 | | -} |
308 | | - |
309 | | -/* |
310 | | - * This could almost certainly be done in a nicer way. |
311 | | - */ |
312 | | -std::string |
313 | | -utf8_tolower(std::string const &s) |
314 | | -{ |
315 | | - std::vector<UChar> ustring; |
316 | | - UErrorCode error = U_ZERO_ERROR; |
317 | | - |
318 | | - for (int i = 0; i < s.size(); ) { |
319 | | - UChar32 c; |
320 | | - U8_NEXT(s.data(), i, s.size(), c); |
321 | | - ustring.push_back(c); |
322 | | - } |
323 | | - |
324 | | - std::vector<UChar> dest; |
325 | | - u_strToLower(&dest[0], dest.size(), &ustring[0], ustring.size(), |
326 | | - NULL, &error); |
327 | | - |
328 | | - if (U_FAILURE(error)) |
329 | | - return s; |
330 | | - |
331 | | - std::vector<unsigned char> u8string; |
332 | | - int i, j; |
333 | | - for (i = 0, j = 0; i < dest.size(); j++) { |
334 | | - U8_APPEND_UNSAFE(&u8string[0], i, dest[j]); |
335 | | - } |
336 | | - return std::string(u8string.begin(), u8string.begin() + i); |
337 | | -} |
338 | | - |
339 | | -// Ported from MediaWiki core function in PHP. |
340 | | -string |
341 | | -codepointToUtf8(int codepoint) { |
342 | | - string ret; |
343 | | - |
344 | | - if(codepoint < 0x80) { |
345 | | - ret.append(1, codepoint); |
346 | | - return ret; |
347 | | - } |
348 | | - |
349 | | - if(codepoint < 0x800) { |
350 | | - ret.append(1, codepoint >> 6 & 0x3f | 0xc0); |
351 | | - ret.append(1, codepoint & 0x3f | 0x80); |
352 | | - return ret; |
353 | | - } |
354 | | - |
355 | | - if(codepoint < 0x10000) { |
356 | | - ret.append(1, codepoint >> 12 & 0x0f | 0xe0); |
357 | | - ret.append(1, codepoint >> 6 & 0x3f | 0x80); |
358 | | - ret.append(1, codepoint & 0x3f | 0x80); |
359 | | - return ret; |
360 | | - } |
361 | | - |
362 | | - if(codepoint < 0x110000) { |
363 | | - ret.append(1, codepoint >> 18 & 0x07 | 0xf0); |
364 | | - ret.append(1, codepoint >> 12 & 0x3f | 0x80); |
365 | | - ret.append(1, codepoint >> 6 & 0x3f | 0x80); |
366 | | - ret.append(1, codepoint & 0x3f | 0x80); |
367 | | - return ret; |
368 | | - } |
369 | | - |
370 | | - throw AFPException("Asked for code outside of range ($codepoint)\n"); |
371 | | -} |
| 198 | +} // namespace afp |
Index: trunk/extensions/AbuseFilter/parser_native/aftypes.h |
— | — | @@ -8,71 +8,46 @@ |
9 | 9 | #include <boost/variant.hpp> |
10 | 10 | #include <boost/lexical_cast.hpp> |
11 | 11 | |
12 | | -using namespace std; |
| 12 | +namespace afp { |
13 | 13 | |
14 | | -#define T_NONE 0 |
15 | | -#define T_ID 1 |
16 | | -#define T_KEYWORD 2 |
17 | | -#define T_STRING 3 |
18 | | -#define T_NUMBER 4 |
19 | | -#define T_OP 5 |
20 | | -#define T_BRACE 6 |
21 | | -#define T_COMMA 7 |
22 | | - |
23 | | -#define D_NULL 0 |
24 | | -#define D_INTEGER 1 |
25 | | -#define D_FLOAT 2 |
26 | | -#define D_STRING 3 |
27 | | - |
28 | | -#define DATATYPE_MAX 3 |
29 | | - |
30 | | -class AFPToken { |
| 14 | +class datum { |
31 | 15 | public: |
32 | | - AFPToken() {} |
33 | | - AFPToken(unsigned int type, string value, unsigned int pos); |
34 | | - unsigned int type; |
35 | | - string value; |
36 | | - unsigned int pos; |
37 | | -}; |
| 16 | + datum(); |
38 | 17 | |
39 | | -class AFPData { |
40 | | -public: |
41 | | - AFPData(); |
42 | | - |
43 | 18 | /* |
44 | 19 | * Generic ctor tries to convert to an int. |
45 | 20 | */ |
46 | 21 | template<typename T> |
47 | | - AFPData(T const &v) |
| 22 | + datum(T const &v) |
48 | 23 | : value_(boost::lexical_cast<long int>(v)) |
49 | 24 | { |
50 | 25 | } |
51 | 26 | |
52 | 27 | // Specific type constructors |
53 | | - AFPData( std::string const &var ); |
54 | | - AFPData( char const *var ); |
55 | | - AFPData( long int var ); |
56 | | - AFPData( float var ); |
57 | | - AFPData( double var ); |
58 | | - AFPData( bool var ); |
| 28 | + datum( std::string const &var ); |
| 29 | + datum( char const *var ); |
| 30 | + datum( long int var ); |
| 31 | + datum( float var ); |
| 32 | + datum( double var ); |
| 33 | + datum( bool var ); |
59 | 34 | |
60 | | - AFPData( const AFPData & oldData ); |
| 35 | + datum( const datum & oldData ); |
61 | 36 | |
62 | 37 | // Assignment operator |
63 | | - AFPData &operator= (const AFPData & other); |
| 38 | + datum &operator= (const datum & other); |
64 | 39 | |
65 | | - AFPData &operator+=(AFPData const &other); |
66 | | - AFPData &operator-=(AFPData const &other); |
67 | | - AFPData &operator*=(AFPData const &other); |
68 | | - AFPData &operator/=(AFPData const &other); |
69 | | - AFPData &operator%=(AFPData const &other); |
| 40 | + datum &operator+=(datum const &other); |
| 41 | + datum &operator-=(datum const &other); |
| 42 | + datum &operator*=(datum const &other); |
| 43 | + datum &operator/=(datum const &other); |
| 44 | + datum &operator%=(datum const &other); |
70 | 45 | bool operator!() const; |
71 | 46 | |
72 | | - bool compare(AFPData const &other) const; |
73 | | - bool compare_with_type(AFPData const &other) const; |
74 | | - bool less_than(AFPData const &other) const; |
| 47 | + bool compare(datum const &other) const; |
| 48 | + bool compare_with_type(datum const &other) const; |
| 49 | + bool less_than(datum const &other) const; |
75 | 50 | |
76 | | - string toString() const; |
| 51 | + std::string toString() const; |
77 | 52 | long int toInt() const; |
78 | 53 | double toFloat() const; |
79 | 54 | bool toBool() const { |
— | — | @@ -108,38 +83,40 @@ |
109 | 84 | valuetype value_; |
110 | 85 | }; |
111 | 86 | |
112 | | -class AFPException :exception { |
113 | | - public: |
114 | | - const char* what() {return this->s;} |
115 | | - AFPException( const char* str ) {s = str;} |
116 | | - AFPException( string str, string var ) { char* s1 = new char[1024]; sprintf( s1, str.c_str(), var.c_str() ); s = s1; } |
117 | | - AFPException( string str, int var ) { char* s1 = new char[1024]; sprintf( s1, str.c_str(), var ); s = s1; } |
118 | | - AFPException( string str, string svar, int ivar ) { char* s1 = new char[1024]; sprintf( s1, str.c_str(), ivar, svar.c_str() ); s = s1; } |
119 | | - |
120 | | - private: |
121 | | - const char* s; |
| 87 | +class exception : std::exception { |
| 88 | +public: |
| 89 | + exception(std::string const &what) |
| 90 | + : what_(what) {} |
| 91 | + ~exception() throw() {} |
| 92 | + |
| 93 | + char const *what() const throw() { |
| 94 | + return what_.c_str(); |
| 95 | + } |
| 96 | + |
| 97 | +private: |
| 98 | + std::string what_; |
122 | 99 | }; |
123 | 100 | |
124 | | -AFPData operator+(AFPData const &a, AFPData const &b); |
125 | | -AFPData operator-(AFPData const &a, AFPData const &b); |
126 | | -AFPData operator*(AFPData const &a, AFPData const &b); |
127 | | -AFPData operator/(AFPData const &a, AFPData const &b); |
128 | | -AFPData operator%(AFPData const &a, AFPData const &b); |
| 101 | +datum operator+(datum const &a, datum const &b); |
| 102 | +datum operator-(datum const &a, datum const &b); |
| 103 | +datum operator*(datum const &a, datum const &b); |
| 104 | +datum operator/(datum const &a, datum const &b); |
| 105 | +datum operator%(datum const &a, datum const &b); |
129 | 106 | |
130 | | -bool operator==(AFPData const &a, AFPData const &b); |
131 | | -bool operator!=(AFPData const &a, AFPData const &b); |
132 | | -bool operator<(AFPData const &a, AFPData const &b); |
133 | | -bool operator>(AFPData const &a, AFPData const &b); |
134 | | -bool operator<=(AFPData const &a, AFPData const &b); |
135 | | -bool operator>=(AFPData const &a, AFPData const &b); |
| 107 | +bool operator==(datum const &a, datum const &b); |
| 108 | +bool operator!=(datum const &a, datum const &b); |
| 109 | +bool operator<(datum const &a, datum const &b); |
| 110 | +bool operator>(datum const &a, datum const &b); |
| 111 | +bool operator<=(datum const &a, datum const &b); |
| 112 | +bool operator>=(datum const &a, datum const &b); |
136 | 113 | |
137 | 114 | template<typename char_type, typename traits> |
138 | 115 | std::basic_ostream<char_type, traits> & |
139 | | -operator<<(std::basic_ostream<char_type, traits> &s, AFPData const &d) { |
| 116 | +operator<<(std::basic_ostream<char_type, traits> &s, datum const &d) { |
140 | 117 | d.print_to(s); |
141 | 118 | return s; |
142 | 119 | } |
143 | 120 | |
144 | | -bool isInVector(std::string const &needle, std::vector<std::string> const &haystack); |
| 121 | +} // namespace afp |
145 | 122 | |
146 | 123 | #endif /* !AFTYPES_H */ |
Index: trunk/extensions/AbuseFilter/parser_native/syntax_check.cpp |
— | — | @@ -5,21 +5,22 @@ |
6 | 6 | |
7 | 7 | #include "filter_evaluator.h" |
8 | 8 | |
9 | | -int main( int argc, char** argv ) { |
10 | | - stringbuf ss( ios::in | ios::out ); |
| 9 | +int main(int argc, char** argv) |
| 10 | +{ |
| 11 | + std::stringbuf ss( std::ios::in | std::ios::out ); |
11 | 12 | |
12 | 13 | // Fill the stringstream |
13 | | - cin.get(ss,'\x04'); |
| 14 | + std::cin.get(ss,'\x04'); |
14 | 15 | |
15 | | - string filter = ss.str(); |
| 16 | + std::string filter = ss.str(); |
16 | 17 | |
17 | 18 | try { |
18 | | - filter_evaluator f; |
| 19 | + afp::filter_evaluator f; |
19 | 20 | f.evaluate(filter); |
20 | | - } catch (AFPException excep) { |
21 | | - cout << "PARSERR: " << excep.what() << endl; |
22 | | - exit(0); |
| 21 | + } catch (afp::exception &excep) { |
| 22 | + std::cout << "PARSERR: " << excep.what() << std::endl; |
| 23 | + std::exit(0); |
23 | 24 | } |
24 | 25 | |
25 | | - cout << "SUCCESS" << endl; |
| 26 | + std::cout << "SUCCESS" << std::endl; |
26 | 27 | } |
Index: trunk/extensions/AbuseFilter/parser_native/affunctions.h |
— | — | @@ -1,28 +1,26 @@ |
2 | 2 | #ifndef AFFUNCTIONS_H |
3 | 3 | #define AFFUNCTIONS_H |
4 | 4 | |
5 | | -#include "aftypes.h" |
6 | 5 | #include <map> |
7 | 6 | #include <vector> |
8 | 7 | |
9 | | -AFPData af_length(std::vector<AFPData> const &args); |
10 | | -AFPData af_lcase(std::vector<AFPData> const &args); |
11 | | -AFPData af_ccnorm(std::vector<AFPData> const &args); |
12 | | -AFPData af_rmdoubles(std::vector<AFPData> const &args); |
13 | | -AFPData af_specialratio(std::vector<AFPData> const &args); |
14 | | -AFPData af_rmspecials(std::vector<AFPData> const &args); |
15 | | -AFPData af_norm(std::vector<AFPData> const &args); |
16 | | -AFPData af_count(std::vector<AFPData> const &args); |
| 8 | +#include "aftypes.h" |
17 | 9 | |
18 | | -map<int,int> const &getEquivSet(); |
19 | | -int next_utf8_char(std::string::const_iterator & p, std::string::const_iterator & charStart, std::string::const_iterator end); |
20 | | -string codepointToUtf8( int codepoint ); |
21 | | -string confusable_character_normalise(std::string const &orig); |
22 | | -vector<AFPData> makeFuncArgList( AFPData arg ); |
23 | | -AFPData callFunction(string const &name, AFPData arg); |
24 | | -string rmdoubles(string const &orig); |
25 | | -string rmspecials(string const &orig); |
26 | | -std::size_t utf8_strlen(std::string const &s); |
27 | | -std::string utf8_tolower(std::string const &s); |
| 10 | +namespace afp { |
28 | 11 | |
| 12 | +datum af_length (std::vector<datum> const &args); |
| 13 | +datum af_lcase (std::vector<datum> const &args); |
| 14 | +datum af_ccnorm (std::vector<datum> const &args); |
| 15 | +datum af_rmdoubles (std::vector<datum> const &args); |
| 16 | +datum af_specialratio (std::vector<datum> const &args); |
| 17 | +datum af_rmspecials (std::vector<datum> const &args); |
| 18 | +datum af_norm (std::vector<datum> const &args); |
| 19 | +datum af_count (std::vector<datum> const &args); |
| 20 | + |
| 21 | +std::string confusable_character_normalise(std::string const &orig); |
| 22 | +std::string rmdoubles(std::string const &orig); |
| 23 | +std::string rmspecials(std::string const &orig); |
| 24 | + |
| 25 | +} // namespace afp |
| 26 | + |
29 | 27 | #endif /* !AFFUNCTIONS_H */ |
Index: trunk/extensions/AbuseFilter/parser_native/main.cpp |
— | — | @@ -15,7 +15,7 @@ |
16 | 16 | |
17 | 17 | int main( int argc, char** argv ) { |
18 | 18 | while (true) { |
19 | | - request r; |
| 19 | + afp::request r; |
20 | 20 | bool result = false; |
21 | 21 | |
22 | 22 | try { |
— | — | @@ -35,11 +35,11 @@ |
36 | 36 | } |
37 | 37 | |
38 | 38 | result = r.evaluate(); |
39 | | - } catch (AFPException &excep) { |
40 | | - cerr << "EXCEPTION: " << excep.what() << endl; |
| 39 | + } catch (afp::exception &excep) { |
| 40 | + std::cerr << "EXCEPTION: " << excep.what() << std::endl; |
41 | 41 | } |
42 | 42 | |
43 | | - cout << ( result ? "MATCH\n" : "NOMATCH\n" ); |
| 43 | + std::cout << ( result ? "MATCH\n" : "NOMATCH\n" ); |
44 | 44 | } |
45 | 45 | } |
46 | 46 | |
Index: trunk/extensions/AbuseFilter/parser_native/eval.cpp |
— | — | @@ -1,25 +1,24 @@ |
2 | 2 | #include <cstdlib> |
3 | 3 | #include <iostream> |
4 | 4 | #include <string> |
5 | | -#include <sstream> |
6 | | -#include <map> |
7 | 5 | |
8 | 6 | #include "filter_evaluator.h" |
9 | 7 | #include "request.h" |
10 | 8 | |
11 | | -int main( int argc, char** argv ) { |
12 | | - request r; |
13 | | - string result; |
| 9 | +int main(int argc, char** argv) |
| 10 | +{ |
| 11 | + afp::request r; |
| 12 | + std::string result; |
14 | 13 | |
15 | 14 | try { |
16 | 15 | if (!r.load(std::cin)) |
17 | 16 | return 1; |
18 | 17 | |
19 | 18 | result = r.evaluate(); |
20 | | - } catch (AFPException excep) { |
21 | | - cout << "EXCEPTION: " << excep.what() << endl; |
22 | | - cerr << "EXCEPTION: " << excep.what() << endl; |
| 19 | + } catch (afp::exception &excep) { |
| 20 | + std::cout << "EXCEPTION: " << excep.what() << std::endl; |
| 21 | + std::cerr << "EXCEPTION: " << excep.what() << std::endl; |
23 | 22 | } |
24 | 23 | |
25 | | - cout << result << "\0"; |
| 24 | + std::cout << result << "\0"; |
26 | 25 | } |
Index: trunk/extensions/AbuseFilter/parser_native/request.cpp |
— | — | @@ -1,11 +1,13 @@ |
2 | 2 | #include "request.h" |
3 | 3 | |
| 4 | +namespace afp { |
| 5 | + |
4 | 6 | // Protocol: |
5 | 7 | // code NULL <key> NULL <value> NULL ... <value> NULL NULL |
6 | 8 | |
7 | 9 | bool |
8 | 10 | request::load(std::istream &inp) { |
9 | | - inp.unsetf(ios_base::skipws); |
| 11 | + inp.unsetf(std::ios_base::skipws); |
10 | 12 | |
11 | 13 | std::istream_iterator<char> it(inp), p, end; |
12 | 14 | |
— | — | @@ -55,7 +57,7 @@ |
56 | 58 | |
57 | 59 | it++; |
58 | 60 | |
59 | | - f.add_variable(key, AFPData(value)); |
| 61 | + f.add_variable(key, datum(value)); |
60 | 62 | } |
61 | 63 | |
62 | 64 | return true; |
— | — | @@ -67,3 +69,4 @@ |
68 | 70 | return f.evaluate(filter); |
69 | 71 | } |
70 | 72 | |
| 73 | +} // namespace afp |
Index: trunk/extensions/AbuseFilter/parser_native/makefile |
— | — | @@ -13,6 +13,8 @@ |
14 | 14 | af_expr-parser.o \ |
15 | 15 | af_expr-filter_evaluator.o \ |
16 | 16 | af_expr-eval.o \ |
| 17 | + af_expr-utf8.o \ |
| 18 | + af_expr-equiv.o \ |
17 | 19 | af_expr-request.o |
18 | 20 | |
19 | 21 | af_parser_objs = \ |
— | — | @@ -21,6 +23,8 @@ |
22 | 24 | af_parser-main.o \ |
23 | 25 | af_parser-parser.o \ |
24 | 26 | af_parser-request.o \ |
| 27 | + af_parser-utf8.o \ |
| 28 | + af_parser-equiv.o \ |
25 | 29 | af_parser-filter_evaluator.o |
26 | 30 | |
27 | 31 | check_objs = \ |
— | — | @@ -28,6 +32,8 @@ |
29 | 33 | check-aftypes.o \ |
30 | 34 | check-check.o \ |
31 | 35 | check-parser.o \ |
| 36 | + check-utf8.o \ |
| 37 | + check-equiv.o \ |
32 | 38 | check-filter_evaluator.o |
33 | 39 | |
34 | 40 | syntax_check_objs = \ |
— | — | @@ -35,6 +41,8 @@ |
36 | 42 | syntax_check-aftypes.o \ |
37 | 43 | syntax_check-filter_evaluator.o \ |
38 | 44 | syntax_check-parser.o \ |
| 45 | + syntax_check-utf8.o \ |
| 46 | + syntax_check-equiv.o \ |
39 | 47 | syntax_check-syntax_check.o |
40 | 48 | |
41 | 49 | expr_objs = \ |
Index: trunk/extensions/AbuseFilter/parser_native/parser.cpp |
— | — | @@ -8,6 +8,7 @@ |
9 | 9 | #include <boost/spirit/phoenix/operators.hpp> |
10 | 10 | #include <boost/function.hpp> |
11 | 11 | #include <boost/noncopyable.hpp> |
| 12 | +#include <boost/format.hpp> |
12 | 13 | |
13 | 14 | #include "aftypes.h" |
14 | 15 | #include "parser.h" |
— | — | @@ -17,30 +18,32 @@ |
18 | 19 | |
19 | 20 | namespace px = phoenix; |
20 | 21 | |
| 22 | +namespace afp { |
| 23 | + |
21 | 24 | struct parse_error : std::runtime_error { |
22 | 25 | parse_error(char const *what) : std::runtime_error(what) {} |
23 | 26 | }; |
24 | 27 | |
25 | | -struct parser_closure : boost::spirit::closure<parser_closure, AFPData> |
| 28 | +struct parser_closure : boost::spirit::closure<parser_closure, datum> |
26 | 29 | { |
27 | 30 | member1 val; |
28 | 31 | }; |
29 | 32 | |
30 | 33 | namespace { |
31 | 34 | |
32 | | -AFPData f_in(AFPData const &a, AFPData const &b) |
| 35 | +datum f_in(datum const &a, datum const &b) |
33 | 36 | { |
34 | 37 | std::string sa = a, sb = b; |
35 | | - return AFPData(std::search(sb.begin(), sb.end(), sa.begin(), sa.end()) != sb.end()); |
| 38 | + return datum(std::search(sb.begin(), sb.end(), sa.begin(), sa.end()) != sb.end()); |
36 | 39 | } |
37 | 40 | |
38 | 41 | } |
39 | 42 | |
40 | 43 | struct function_closure : boost::spirit::closure< |
41 | 44 | function_closure, |
42 | | - AFPData, |
43 | | - boost::function<AFPData (std::vector<AFPData>)>, |
44 | | - std::vector<AFPData> > |
| 45 | + datum, |
| 46 | + boost::function<datum (std::vector<datum>)>, |
| 47 | + std::vector<datum> > |
45 | 48 | { |
46 | 49 | member1 val; |
47 | 50 | member2 func; |
— | — | @@ -49,14 +52,14 @@ |
50 | 53 | |
51 | 54 | struct parser_grammar : public grammar<parser_grammar, parser_closure::context_t> |
52 | 55 | { |
53 | | - symbols<AFPData> variables; |
54 | | - symbols<boost::function<AFPData (std::vector<AFPData>)> > functions; |
| 56 | + symbols<datum> variables; |
| 57 | + symbols<boost::function<datum (std::vector<datum>)> > functions; |
55 | 58 | |
56 | | - void add_variable(std::string const &name, AFPData const &value) { |
| 59 | + void add_variable(std::string const &name, datum const &value) { |
57 | 60 | variables.add(name.c_str(), value); |
58 | 61 | } |
59 | 62 | |
60 | | - void add_function(std::string const &name, boost::function<AFPData (std::vector<AFPData>)> func) { |
| 63 | + void add_function(std::string const &name, boost::function<datum (std::vector<datum>)> func) { |
61 | 64 | functions.add(name.c_str(), func); |
62 | 65 | } |
63 | 66 | |
— | — | @@ -84,11 +87,11 @@ |
85 | 88 | struct call_function_impl { |
86 | 89 | template<typename F, typename A> |
87 | 90 | struct result { |
88 | | - typedef AFPData type; |
| 91 | + typedef datum type; |
89 | 92 | }; |
90 | 93 | |
91 | 94 | template<typename F, typename A> |
92 | | - AFPData operator() (F const &func, A const &args) const { |
| 95 | + datum operator() (F const &func, A const &args) const { |
93 | 96 | return func(args); |
94 | 97 | } |
95 | 98 | }; |
— | — | @@ -173,9 +176,9 @@ |
174 | 177 | "==" >> eq_expr[eq2_expr.val = eq2_expr.val == arg1] |
175 | 178 | | "!=" >> eq_expr[eq2_expr.val = eq2_expr.val != arg1] |
176 | 179 | | "===" >> eq_expr[eq2_expr.val = |
177 | | - bind(&AFPData::compare_with_type)(eq2_expr.val, arg1)] |
| 180 | + bind(&datum::compare_with_type)(eq2_expr.val, arg1)] |
178 | 181 | | "!==" >> eq_expr[eq2_expr.val = |
179 | | - !bind(&AFPData::compare_with_type)(eq2_expr.val, arg1)] |
| 182 | + !bind(&datum::compare_with_type)(eq2_expr.val, arg1)] |
180 | 183 | ) |
181 | 184 | ; |
182 | 185 | |
— | — | @@ -198,7 +201,7 @@ |
199 | 202 | } |
200 | 203 | |
201 | 204 | rule_t value, variable, basic, bool_expr, |
202 | | - eq_expr, eq2_expr, mult_expr, plus_expr, in_expr, not_expr, expr; |
| 205 | + eq_expr, eq2_expr, mult_expr, plus_expr, in_expr, expr; |
203 | 206 | rule<ScannerT, function_closure::context_t> function; |
204 | 207 | }; |
205 | 208 | }; |
— | — | @@ -213,12 +216,13 @@ |
214 | 217 | delete grammar_; |
215 | 218 | } |
216 | 219 | |
217 | | -AFPData |
| 220 | +datum |
218 | 221 | expressor::evaluate(std::string const &filter) const |
219 | 222 | { |
220 | | - AFPData ret; |
| 223 | + datum ret; |
221 | 224 | parse_info<std::string::const_iterator> info = |
222 | | - parse(filter.begin(), filter.end(), (*grammar_)[var(ret) = arg1], space_p); |
| 225 | + parse(filter.begin(), filter.end(), (*grammar_)[var(ret) = arg1], |
| 226 | + comment_p("/*", "*/") | chset<>("\n\t ")); |
223 | 227 | if (info.full) { |
224 | 228 | return ret; |
225 | 229 | } else { |
— | — | @@ -228,7 +232,7 @@ |
229 | 233 | } |
230 | 234 | |
231 | 235 | void |
232 | | -expressor::add_variable(std::string const &name, AFPData value) |
| 236 | +expressor::add_variable(std::string const &name, datum value) |
233 | 237 | { |
234 | 238 | grammar_->add_variable(name, value); |
235 | 239 | } |
— | — | @@ -239,13 +243,17 @@ |
240 | 244 | grammar_->add_function(name, value); |
241 | 245 | } |
242 | 246 | |
| 247 | +} // namespace afp |
| 248 | + |
243 | 249 | #ifdef TEST_PARSER |
244 | | -AFPData f_add(std::vector<AFPData> const &args) |
| 250 | +afp::datum |
| 251 | +f_add(std::vector<afp::datum> const &args) |
245 | 252 | { |
246 | 253 | return args[0] + args[1]; |
247 | 254 | } |
248 | 255 | |
249 | | -AFPData f_norm(std::vector<AFPData> const &args) |
| 256 | +afp::datum |
| 257 | +f_norm(std::vector<afp::datum> const &args) |
250 | 258 | { |
251 | 259 | return args[0]; |
252 | 260 | } |
— | — | @@ -253,7 +261,13 @@ |
254 | 262 | int |
255 | 263 | main(int argc, char **argv) |
256 | 264 | { |
257 | | - expressor e; |
| 265 | + if (argc != 2) { |
| 266 | + std::cerr << boost::format("usage: %s <expr>\n") |
| 267 | + % argv[0]; |
| 268 | + return 1; |
| 269 | + } |
| 270 | + |
| 271 | + afp::expressor e; |
258 | 272 | e.add_variable("ONE", 1); |
259 | 273 | e.add_variable("TWO", 2); |
260 | 274 | e.add_variable("THREE", 3); |
Index: trunk/extensions/AbuseFilter/parser_native/request.h |
— | — | @@ -6,6 +6,8 @@ |
7 | 7 | |
8 | 8 | #include "filter_evaluator.h" |
9 | 9 | |
| 10 | +namespace afp { |
| 11 | + |
10 | 12 | struct request { |
11 | 13 | bool load(std::istream &); |
12 | 14 | bool evaluate(void); |
— | — | @@ -15,4 +17,6 @@ |
16 | 18 | std::string filter; |
17 | 19 | }; |
18 | 20 | |
| 21 | +} // namespace afp |
| 22 | + |
19 | 23 | #endif /* !REQUEST_H */ |