aboutsummaryrefslogtreecommitdiff
path: root/src/include/lexer.h
blob: d83e35f7748db16c80df13cd3a42189d3423abb7 (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef LEXER_H
#define LEXER_H

#include "util.h"
#include "syntax.h"
#include "token.h"

#define LEXER_VALID (lexer->c != '\0' && lexer->i < strlen(lexer->content))

/* the lexer struct */
typedef struct LEXER_STRUC {
   /* source being read */
   char* src;

   /* what the lexer is looking at right now */
   enum LEXER_STATE {
      /* normal 1-character token */
      LEXER_STATE_REG,
      /* definition tag */
      LEXER_STATE_TAG,
      /* string */
      LEXER_STATE_STR,
      /* escaped character in string */
      LEXER_STATE_STR_ESC,
      /* integer */
      LEXER_STATE_INT,
      /* keyword */
      LEXER_STATE_KWD,
   } state;

   /* the linked list of tokens generated */
   token_t* tokenl;
   /* pointer to the last token in tokenl */
   token_t* tokenl_last;
   /* number of tokens in tokenl */
   int tokenc;
} lexer_t;

/* create lexer from source */
lexer_t* lexer_init (char* src);

/* destroy lexer **but not src or tokenl** */
void lexer_destroy (lexer_t* lexer);

/* add token to tokenl */
void lexer_add_token(lexer_t* lexer, token_t* token);
/* add the current character as a token to tokenl -- utility function for
   lexer_do_reg() */
void lexer_add_current_char(lexer_t* lexer, int type);

/* add first character of lexer's src to the value of the last token in tokenl, if it exists. otherwise, create new token and add it */
void lexer_add_current_char_to_last_token(lexer_t* lexer, int type);

/* handle regular state */
void lexer_do_reg(lexer_t* lexer);
/* handle definition tag state*/
void lexer_do_tag(lexer_t* lexer);
/* TODO: handle character state */
void lexer_do_chr(lexer_t* lexer);
/* handle string state */
void lexer_do_str(lexer_t* lexer);
/* handle integer */
void lexer_do_int(lexer_t* lexer);
/* handle keywords */
void lexer_do_kwd(lexer_t* lexer);

/* run lexer */
void lexer_run(lexer_t* lexer);

#endif