summaryrefslogtreecommitdiff
path: root/42sh/src/lexer/lexer.h
blob: 8d8cdf55cc48924b8d412d9c2db03190569794fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 */