]> source.dussan.org Git - rspamd.git/commitdiff
Fix more issues in expressions parser.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 17 Mar 2015 23:29:27 +0000 (23:29 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 17 Mar 2015 23:29:49 +0000 (23:29 +0000)
src/libutil/expression.c
src/lua/lua_expression.c
test/lua/unit/expressions.lua

index 2cd1fe53f8e8faf4a025da74df355f538755adbe..3ae7ed469d732b4c93e2bfa8ca1d3d040e6ecff9 100644 (file)
@@ -184,6 +184,9 @@ rspamd_expr_str_to_op (const gchar *a, const gchar *end, const gchar **next)
                                *next = a + 1;
                        }
                }
+               else {
+                       *next = end;
+               }
                /* XXX: not especially effective */
                switch (*a) {
                case '!':
@@ -336,7 +339,7 @@ rspamd_parse_expression (const gchar *line, gsize len,
                                state = SKIP_SPACES;
                        }
                        else if (rspamd_expr_is_operation_symbol (*p)) {
-                               state = PARSE_ATOM;
+                               state = PARSE_OP;
                        }
                        else {
                                /*
@@ -480,6 +483,7 @@ rspamd_parse_expression (const gchar *line, gsize len,
                                rspamd_expr_stack_push (e, GINT_TO_POINTER (op));
                        }
 
+                       state = SKIP_SPACES;
                        break;
                case SKIP_SPACES:
                        if (g_ascii_isspace (*p)) {
index 55b0c1556e9b99666865532033feb71fd1669d7e..284fccb650a923ab90e3be2df7226192a249a73c 100644 (file)
@@ -64,6 +64,7 @@ LUA_FUNCTION_DEF (expr, to_string);
 
 static const struct luaL_reg exprlib_m[] = {
        LUA_INTERFACE_DEF (expr, to_string),
+       {"__tostring", lua_expr_to_string},
        {NULL, NULL}
 };
 
@@ -231,6 +232,7 @@ lua_expr_create (lua_State *L)
                }
 
                pe = lua_newuserdata (L, sizeof (struct lua_expression *));
+               rspamd_lua_setclass (L, "rspamd{expr}", -1);
                *pe = e;
                lua_pushnil (L);
        }
index 4c6c55f7d650263e7854058d1387d07854496e39..f191019e68d6d2d9a8661ad601c1efc817a65ffa 100644 (file)
@@ -4,11 +4,15 @@ context("Rspamd expressions", function()
   local rspamd_expression = require "rspamd_expression"
   local rspamd_mempool = require "rspamd_mempool"
   local rspamd_regexp = require "rspamd_regexp"
-  local split_re = rspamd_regexp.create('/\\s+/')
+  local split_re = rspamd_regexp.create('/\\s+|\\)|\\(/')
   
   local function parse_func(str)
     -- extract token till the first space character
-    local token = split_re:split(str)[1]
+    local token = str
+    local t = split_re:split(str)
+    if t then
+      token = t[1]
+    end
     -- Return token name
     return token
   end
@@ -21,15 +25,17 @@ context("Rspamd expressions", function()
     local pool = rspamd_mempool.create()
     
     local cases = {
-       {'A & B | !C', 'A B & C ! |'}
+       {'A & B | !C', 'A B & C ! |'},
+       {'A & (B | !C)', 'A B C ! | &'}
     }
     for _,c in ipairs(cases) do
       local expr,err = rspamd_expression.create(c[1], 
         {parse_func, process_func}, pool)
       
-      if c[2] then
-        assert_not_nil(expr, "Cannot parse " .. c[1] ": " .. err)
+      if not c[2] then
+        assert_nil(expr, "Should not be able to parse " .. c[1])
       else
+        assert_not_nil(expr, "Cannot parse " .. c[1])
         assert_equal(expr:to_string(), c[2], string.format("Evaluated expr to '%s', expected: '%s'",
             expr:to_string(), c[2]))
       end