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
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
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));
}
\f
/* Token matching functions. */
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
(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;
+}
#define TOKEN_H 1
#include <stdio.h>
+#include "libpspp/assertion.h"
#include "libpspp/str.h"
#include "data/identifier.h"
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 */