[ovs-dev] [PATCH v2 03/21] expr: Fine-tune parser error message for common typo.

Ben Pfaff blp at ovn.org
Mon Aug 8 16:14:14 UTC 2016


It's easy to type "=" in place of "==" in an expression but the expression
parser's error message was far from clear.  For multibit numeric fields,
it said:
    Explicit `!= 0' is required for inequality test of multibit field
    against 0.
For string fields, the parser treated such an expression as "<name> != 0"
and thus it said:
    String field <name> is not compatible with numeric constant.

This improves the error message in each case to:
    Syntax error at `=' expecting relational operator.
which I hope to be clear.

Signed-off-by: Ben Pfaff <blp at ovn.org>
---
 ovn/lib/expr.c | 7 +++++--
 tests/ovn.at   | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/ovn/lib/expr.c b/ovn/lib/expr.c
index 1649c05..cb61b5d 100644
--- a/ovn/lib/expr.c
+++ b/ovn/lib/expr.c
@@ -931,9 +931,12 @@ expr_parse_primary(struct expr_context *ctx, bool *atomic)
         }
 
         if (!expr_relop_from_token(ctx->lexer->token.type, &r)) {
-            if (f.n_bits > 1 && !ctx->not) {
+            if (!f.n_bits || ctx->lexer->token.type == LEX_T_EQUALS) {
+                expr_syntax_error(ctx, "expecting relational operator.");
+                return NULL;
+            } else if (f.n_bits > 1 && !ctx->not) {
                 expr_error(ctx, "Explicit `!= 0' is required for inequality "
-                           "test of multibit field against 0.");
+                            "test of multibit field against 0.");
                 return NULL;
             }
 
diff --git a/tests/ovn.at b/tests/ovn.at
index 54fa8c5..3543cf5 100644
--- a/tests/ovn.at
+++ b/tests/ovn.at
@@ -240,6 +240,7 @@ inport == "eth0"
 
 ip4.src == "eth0" => Integer field ip4.src is not compatible with string constant.
 inport == 1 => String field inport is not compatible with integer constant.
+ip4.src = 1.2.3.4 => Syntax error at `=' expecting relational operator.
 
 ip4.src > {1, 2, 3} => Only == and != operators may be used with value sets.
 eth.type > 0x800 => Only == and != operators may be used with nominal field eth.type.
@@ -249,6 +250,7 @@ inport != "eth0" => Nominal field inport may only be tested for equality (taking
 !(inport == "eth0") => Nominal field inport may only be tested for equality (taking enclosing `!' operators into account).
 eth.type != 0x800 => Nominal field eth.type may only be tested for equality (taking enclosing `!' operators into account).
 !(eth.type == 0x800) => Nominal field eth.type may only be tested for equality (taking enclosing `!' operators into account).
+inport = "eth0" => Syntax error at `=' expecting relational operator.
 
 123 == 123 => Syntax error at `123' expecting field name.
 
-- 
2.1.3




More information about the dev mailing list