diff options
Diffstat (limited to '42sh/src/lexer/lexer.h')
| -rw-r--r-- | 42sh/src/lexer/lexer.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/42sh/src/lexer/lexer.h b/42sh/src/lexer/lexer.h new file mode 100644 index 0000000..8d8cdf5 --- /dev/null +++ b/42sh/src/lexer/lexer.h @@ -0,0 +1,48 @@ +#ifndef LEXER_H +#define LEXER_H + +#include <lexer/token.h> +#include <stddef.h> +#include <utils/libstring.h> + +// True if C could be used as a word +#define ISWORD(C) \ + C == TOKEN_WORD || C == TOKEN_THEN || C == TOKEN_ELIF || C == TOKEN_ELSE \ + || C == TOKEN_IF || C == TOKEN_WHILE || C == TOKEN_UNTIL \ + || C == TOKEN_DO || C == TOKEN_DONE || C == TOKEN_FOR || C == TOKEN_IN \ + || C == TOKEN_NEG || C == TOKEN_FI || C == TOKEN_CURLY_LEFT \ + || C == TOKEN_CURLY_RIGHT + +struct lexer +{ + struct string *input; // input data + size_t pos; // the current offset inside the input data + char processed; + struct token current_tok; // next (if processed) token +}; + +// Creates a new lexer given an input string +struct lexer *lexer_new(struct string *input); + +// Frees the given lexer, not its input +void lexer_free(struct lexer *lexer); + +// Returns a token from the input string +// If the token is a WORD, copies the word to the current_tok.value field +struct token lexer_next_token(struct lexer *lexer); + +/* +** Processes the next token if necessary +** (previous call to lexer_pop or first call) +*/ +// Returns the next token +struct token lexer_peek(struct lexer *lexer); + +/* +** Processes the next token if necessary +** (previous call to lexer_pop or first call) +*/ +// Returns the next token and moves the cursor forward +struct token lexer_pop(struct lexer *lexer); + +#endif /* ! LEXER_H */ |
