lex: New functions lex_lookahead() and lexer_match_id().
authorBen Pfaff <blp@nicira.com>
Thu, 23 Apr 2015 23:12:11 +0000 (16:12 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 29 Apr 2015 00:03:47 +0000 (17:03 -0700)
These functions will have their first users in upcoming commits.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Russell Bryant <rbryant@redhat.com>
Acked-by: Justin Pettit <jpettit@nicira.com>
ovn/lib/lex.c
ovn/lib/lex.h

index 73f0ca3..fe11b72 100644 (file)
@@ -704,6 +704,21 @@ lexer_get(struct lexer *lexer)
     return lexer->token.type;
 }
 
+/* Returns the type of the next token that will be fetched by lexer_get(),
+ * without advancing 'lexer->token' to that token. */
+enum lex_type
+lexer_lookahead(const struct lexer *lexer)
+{
+    struct lex_token next;
+    enum lex_type type;
+    const char *start;
+
+    lex_token_parse(&next, lexer->input, &start);
+    type = next.type;
+    lex_token_destroy(&next);
+    return type;
+}
+
 /* If 'lexer''s current token has the given 'type', advances 'lexer' to the
  * next token and returns true.  Otherwise returns false. */
 bool
@@ -716,3 +731,16 @@ lexer_match(struct lexer *lexer, enum lex_type type)
         return false;
     }
 }
+
+/* If 'lexer''s current token is the identifier given in 'id', advances 'lexer'
+ * to the next token and returns true.  Otherwise returns false.  */
+bool
+lexer_match_id(struct lexer *lexer, const char *id)
+{
+    if (lexer->token.type == LEX_T_ID && !strcmp(lexer->token.s, id)) {
+        lexer_get(lexer);
+        return true;
+    } else {
+        return false;
+    }
+}
index 29e922c..df4db2d 100644 (file)
@@ -105,6 +105,8 @@ void lexer_init(struct lexer *, const char *input);
 void lexer_destroy(struct lexer *);
 
 enum lex_type lexer_get(struct lexer *);
+enum lex_type lexer_lookahead(const struct lexer *);
 bool lexer_match(struct lexer *, enum lex_type);
+bool lexer_match_id(struct lexer *, const char *id);
 
 #endif /* ovn/lex.h */