Factorise code to skip till a given token kind.
authorChristoph Mallon <christoph.mallon@gmx.de>
Tue, 22 May 2012 15:47:08 +0000 (17:47 +0200)
committerChristoph Mallon <christoph.mallon@gmx.de>
Wed, 23 May 2012 05:31:15 +0000 (07:31 +0200)
parser.c

index 0d2c9a2..5d7f6ac 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -628,33 +628,33 @@ static void type_error_incompatible(const char *msg,
               msg, type1, type2);
 }
 
-/**
- * Expect the current token is the expected token.
- * If not, generate an error and skip until the next anchor.
- */
-static void expect(token_kind_t const expected)
+static bool skip_till(token_kind_t const expected, char const *const context)
 {
        if (UNLIKELY(token.kind != expected)) {
-               parse_error_expected(NULL, expected, NULL);
+               parse_error_expected(context, expected, NULL);
                add_anchor_token(expected);
                eat_until_anchor();
                rem_anchor_token(expected);
                if (token.kind != expected)
-                       return;
+                       return false;
        }
-       eat(expected);
+       return true;
+}
+
+/**
+ * Expect the current token is the expected token.
+ * If not, generate an error and skip until the next anchor.
+ */
+static void expect(token_kind_t const expected)
+{
+       if (skip_till(expected, NULL))
+               eat(expected);
 }
 
 static symbol_t *expect_identifier(char const *const context, source_position_t *const pos)
 {
-       if (token.kind != T_IDENTIFIER) {
-               parse_error_expected(context, T_IDENTIFIER, NULL);
-               add_anchor_token(T_IDENTIFIER);
-               eat_until_anchor();
-               rem_anchor_token(T_IDENTIFIER);
-               if (token.kind != T_IDENTIFIER)
-                       return NULL;
-       }
+       if (!skip_till(T_IDENTIFIER, context))
+               return NULL;
        symbol_t *const sym = token.base.symbol;
        if (pos)
                *pos = *HERE;