[master] 3c02e5586 vcc: New function to print a series of tokens

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue Jun 1 13:50:07 UTC 2021


commit 3c02e5586113d0a85cfed36f9f58d1c52ea82bb7
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Tue May 25 12:34:53 2021 +0200

    vcc: New function to print a series of tokens
    
    The same pattern occurred 3 times while reviewing potential usage for the
    new vcc_PeekToken*() functions.

diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index a48d1a902..40c46ca5d 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -415,6 +415,8 @@ void vcc_Warn(struct vcc *);
 
 void vcc__Expect(struct vcc *tl, unsigned tok, unsigned line);
 int vcc_IdIs(const struct token *t, const char *p);
+void vcc_PrintTokens(struct vcc *tl, const struct token *tb,
+    const struct token *te);
 void vcc_ExpectVid(struct vcc *tl, const char *what);
 void vcc_Lexer(struct vcc *tl, struct source *sp);
 void vcc_NextToken(struct vcc *tl);
diff --git a/lib/libvcc/vcc_symb.c b/lib/libvcc/vcc_symb.c
index 8ace651a8..0d98ed084 100644
--- a/lib/libvcc/vcc_symb.c
+++ b/lib/libvcc/vcc_symb.c
@@ -318,8 +318,7 @@ VCC_SymbolGet(struct vcc *tl, vcc_ns_t ns, vcc_kind_t kind,
 	tl->t = VTAILQ_NEXT(tn, list);
 	if (sym == NULL) {
 		VSB_printf(tl->sb, "%s: '", e->name);
-		for (tn1 = t0; tn1 != tl->t; tn1 = VTAILQ_NEXT(tn1, list))
-			VSB_printf(tl->sb, "%.*s", PF(tn1));
+		vcc_PrintTokens(tl, t0, tl->t);
 		VSB_cat(tl->sb, "'");
 		sym = vcc_sym_in_tab(tl, st, kind, VCL_LOW, VCL_HIGH);
 		if (sym != NULL && sym->kind != SYM_OBJECT &&
@@ -338,8 +337,7 @@ VCC_SymbolGet(struct vcc *tl, vcc_ns_t ns, vcc_kind_t kind,
 	}
 	if (kind != SYM_NONE && kind != sym->kind) {
 		VSB_cat(tl->sb, "Symbol '");
-		for (tn1 = t0; tn1 != tl->t; tn1 = VTAILQ_NEXT(tn1, list))
-			VSB_printf(tl->sb, "%.*s", PF(tn1));
+		vcc_PrintTokens(tl, t0, tl->t);
 		VSB_printf(tl->sb, "' has wrong type (%s), expected %s:",
 		    sym->kind->name, kind->name);
 		VSB_cat(tl->sb, "\nAt: ");
diff --git a/lib/libvcc/vcc_token.c b/lib/libvcc/vcc_token.c
index f8d14a86e..b965c298d 100644
--- a/lib/libvcc/vcc_token.c
+++ b/lib/libvcc/vcc_token.c
@@ -329,29 +329,45 @@ vcc_IdIs(const struct token *t, const char *p)
  * Check that we have a Varnish identifier
  */
 
+void
+vcc_PrintTokens(struct vcc *tl, const struct token *tb, const struct token *te)
+{
+
+	CHECK_OBJ_NOTNULL(tl, VCC_MAGIC);
+	AN(tb);
+	AN(te);
+	while (tb != te) {
+		VSB_printf(tl->sb, "%.*s", PF(tb));
+		tb = vcc_PeekTokenFrom(tl, tb);
+		AN(tb);
+	}
+}
+
 void
 vcc_ExpectVid(struct vcc *tl, const char *what)
 {
 	const char *bad = NULL;
-	struct token *t2, *t3;
+	struct token *t2;
 
 	ExpectErr(tl, ID);
 	ERRCHK(tl);
 
-	t2 = VTAILQ_NEXT(tl->t, list);
+	t2 = vcc_PeekToken(tl);
+	ERRCHK(tl);
 	while (t2->tok == '.') {
 		bad = ".";
-		t2 = VTAILQ_NEXT(t2, list);
+		t2 = vcc_PeekTokenFrom(tl, t2);
+		ERRCHK(tl);
 		if (t2->tok != ID)
 			break;
-		t2 = VTAILQ_NEXT(t2, list);
+		t2 = vcc_PeekTokenFrom(tl, t2);
+		ERRCHK(tl);
 	}
 	if (bad == NULL)
 		bad = VCT_invalid_name(tl->t->b, tl->t->e);
 	if (bad != NULL) {
 		VSB_printf(tl->sb, "Name of %s, '", what);
-		for (t3 = tl->t; t3 != t2; t3 = VTAILQ_NEXT(t3, list))
-			VSB_printf(tl->sb, "%.*s", PF(t3));
+		vcc_PrintTokens(tl, tl->t, t2);
 		VSB_printf(tl->sb,
 		    "', contains illegal character '%c'\n", *bad);
 		vcc_ErrWhere2(tl, tl->t, t2);


More information about the varnish-commit mailing list