Use ip_parse() and ipv6_parse() and variants in more places.
[cascardo/ovs.git] / ovn / lib / lex.c
index 46e86c2..481f11e 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdarg.h>
 #include "dynamic-string.h"
 #include "json.h"
+#include "packets.h"
 #include "util.h"
 \f
 /* Returns a string that represents 'format'. */
@@ -117,7 +118,7 @@ lex_token_format_value(const union mf_subvalue *value,
         break;
 
     case LEX_F_IPV6:
-        print_ipv6_addr(s, &value->ipv6);
+        ipv6_format_addr(&value->ipv6, s);
         break;
 
     case LEX_F_ETHERNET:
@@ -238,6 +239,12 @@ lex_token_format(const struct lex_token *token, struct ds *s)
     case LEX_T_EQUALS:
         ds_put_cstr(s, "=");
         break;
+    case LEX_T_EXCHANGE:
+        ds_put_cstr(s, "<->");
+        break;
+    case LEX_T_DECREMENT:
+        ds_put_cstr(s, "--");
+        break;
     default:
         OVS_NOT_REACHED();
     }
@@ -332,13 +339,9 @@ lex_parse_integer__(const char *p, struct lex_token *token)
         memcpy(copy, p, len);
         copy[len] = '\0';
 
-        struct in_addr ipv4;
-        struct in6_addr ipv6;
-        if (inet_pton(AF_INET, copy, &ipv4) == 1) {
-            token->value.ipv4 = ipv4.s_addr;
+        if (ip_parse(copy, &token->value.ipv4)) {
             token->format = LEX_F_IPV4;
-        } else if (inet_pton(AF_INET6, copy, &ipv6) == 1) {
-            token->value.ipv6 = ipv6;
+        } else if (ipv6_parse(copy, &token->value.ipv6)) {
             token->format = LEX_F_IPV6;
         } else {
             lex_error(token, "Invalid numeric constant.");
@@ -599,6 +602,9 @@ next:
         if (*p == '=') {
             token->type = LEX_T_LE;
             p++;
+        } else if (*p == '-' && p[1] == '>') {
+            token->type = LEX_T_EXCHANGE;
+            p += 2;
         } else {
             token->type = LEX_T_LT;
         }
@@ -634,6 +640,16 @@ next:
         token->type = LEX_T_SEMICOLON;
         break;
 
+    case '-':
+        p++;
+        if (*p == '-') {
+            token->type = LEX_T_DECREMENT;
+            p++;
+        } else {
+            lex_error(token, "`-' is only valid as part of `--'.");
+        }
+        break;
+
     case '0': case '1': case '2': case '3': case '4':
     case '5': case '6': case '7': case '8': case '9':
     case ':':
@@ -744,3 +760,24 @@ lexer_match_id(struct lexer *lexer, const char *id)
         return false;
     }
 }
+
+bool
+lexer_is_int(const struct lexer *lexer)
+{
+    return (lexer->token.type == LEX_T_INTEGER
+            && lexer->token.format == LEX_F_DECIMAL
+            && ntohll(lexer->token.value.integer) <= INT_MAX);
+}
+
+bool
+lexer_get_int(struct lexer *lexer, int *value)
+{
+    if (lexer_is_int(lexer)) {
+        *value = ntohll(lexer->token.value.integer);
+        lexer_get(lexer);
+        return true;
+    } else {
+        *value = 0;
+        return false;
+    }
+}