[1/2] string: add helper string_expression()
diff mbox series

Message ID 20190126160948.54590-2-luc.vanoostenryck@gmail.com
State New
Headers show
Series
  • new helper: string_expression()
Related show

Commit Message

Luc Van Oostenryck Jan. 26, 2019, 4:09 p.m. UTC
This will help to detect earlier syntax errors concerning
string constants.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expression.c | 11 +++++++++++
 expression.h |  1 +
 2 files changed, 12 insertions(+)

Patch
diff mbox series

diff --git a/expression.c b/expression.c
index f955fb159..ffb6cb9dc 100644
--- a/expression.c
+++ b/expression.c
@@ -83,6 +83,17 @@  struct token *parens_expression(struct token *token, struct expression **expr, c
 	return expect(token, ')', where);
 }
 
+struct token *string_expression(struct token *token, struct expression **expr, const char *where)
+{
+	struct token *next = primary_expression(token, expr);
+
+	if (!*expr || (*expr)->type != EXPR_STRING) {
+		sparse_error(token->pos, "string literal expected for %s", where);
+		*expr = NULL;
+	}
+	return next;
+}
+
 /*
  * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
  * conversion
diff --git a/expression.h b/expression.h
index ed6f4d65d..13f35e64b 100644
--- a/expression.h
+++ b/expression.h
@@ -270,6 +270,7 @@  struct token *parse_expression(struct token *token, struct expression **tree);
 struct token *conditional_expression(struct token *token, struct expression **tree);
 struct token *primary_expression(struct token *token, struct expression **tree);
 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
+struct token *string_expression(struct token *token, struct expression **expr, const char *where);
 struct token *assignment_expression(struct token *token, struct expression **tree);
 
 extern int expand_symbol(struct symbol *);