From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- 42sh/src/lexer/lexer.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 42sh/src/lexer/lexer.h (limited to '42sh/src/lexer/lexer.h') 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 +#include +#include + +// 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 */ -- cgit v1.2.3