From 7c3ed885759bae94d80064c24135083e597873e1 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 13 Jun 2021 10:33:09 -0700 Subject: [PATCH] lexer: Factor some token inspectors out into new token functions. --- src/language/lexer/lexer.c | 22 ++++++---------------- src/language/lexer/token.c | 16 ++++++++++++++++ src/language/lexer/token.h | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index 7f2d0290a6..cde5f58ac4 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -491,15 +491,14 @@ lex_integer (const struct lexer *lexer) bool lex_next_is_number (const struct lexer *lexer, int n) { - enum token_type next_token = lex_next_token (lexer, n); - return next_token == T_POS_NUM || next_token == T_NEG_NUM; + return token_is_number (lex_next (lexer, n)); } /* Returns true if the token N ahead of the current token is a string. */ bool lex_next_is_string (const struct lexer *lexer, int n) { - return lex_next_token (lexer, n) == T_STRING; + return token_is_string (lex_next (lexer, n)); } /* Returns the value of the token N ahead of the current token, which must be a @@ -507,21 +506,14 @@ lex_next_is_string (const struct lexer *lexer, int n) double lex_next_number (const struct lexer *lexer, int n) { - assert (lex_next_is_number (lexer, n)); - return lex_next_tokval (lexer, n); + return token_number (lex_next (lexer, n)); } /* Returns true if the token N ahead of the current token is an integer. */ bool lex_next_is_integer (const struct lexer *lexer, int n) { - double value; - - if (!lex_next_is_number (lexer, n)) - return false; - - value = lex_next_tokval (lexer, n); - return value > LONG_MIN && value <= LONG_MAX && floor (value) == value; + return token_is_integer (lex_next (lexer, n)); } /* Returns the value of the token N ahead of the current token, which must be @@ -529,8 +521,7 @@ lex_next_is_integer (const struct lexer *lexer, int n) long lex_next_integer (const struct lexer *lexer, int n) { - assert (lex_next_is_integer (lexer, n)); - return lex_next_tokval (lexer, n); + return token_integer (lex_next (lexer, n)); } /* Token matching functions. */ @@ -910,8 +901,7 @@ lex_next_token (const struct lexer *lexer, int n) double lex_next_tokval (const struct lexer *lexer, int n) { - const struct token *token = lex_next (lexer, n); - return token->number; + return token_number (lex_next (lexer, n)); } /* Returns the null-terminated string in the token N after the current one, in diff --git a/src/language/lexer/token.c b/src/language/lexer/token.c index 718f3d07f3..ec64bbfb4a 100644 --- a/src/language/lexer/token.c +++ b/src/language/lexer/token.c @@ -172,3 +172,19 @@ token_print (const struct token *token, FILE *stream) (int) token->string.length, token->string.string); putc ('\n', stream); } + +bool +token_is_integer (const struct token *t) +{ + return (token_is_number (t) + && t->number > LONG_MIN + && t->number <= LONG_MAX + && floor (t->number) == t->number); +} + +long +token_integer (const struct token *t) +{ + assert (token_is_integer (t)); + return t->number; +} diff --git a/src/language/lexer/token.h b/src/language/lexer/token.h index cab1a8cf9c..67bc7ddc3b 100644 --- a/src/language/lexer/token.h +++ b/src/language/lexer/token.h @@ -18,6 +18,7 @@ #define TOKEN_H 1 #include +#include "libpspp/assertion.h" #include "libpspp/str.h" #include "data/identifier.h" @@ -42,4 +43,29 @@ char *token_to_string (const struct token *); void token_print (const struct token *, FILE *); +static inline bool token_is_number (const struct token *); +static inline double token_number (const struct token *); +bool token_is_integer (const struct token *); +long token_integer (const struct token *); +static inline bool token_is_string (const struct token *); + +static inline bool +token_is_number (const struct token *t) +{ + return t->type == T_POS_NUM || t->type == T_NEG_NUM; +} + +static inline double +token_number (const struct token *t) +{ + assert (token_is_number (t)); + return t->number; +} + +static inline bool +token_is_string (const struct token *t) +{ + return t->type == T_STRING; +} + #endif /* token.h */ -- 2.30.2