aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
blob: aa068a960cc6ae70ba727d7ffb6f500fd5050ee6 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

#include "include/lexer.h" 
#include "include/syntax.h"
#include "include/token.h"

lexer_t* lexer_init(char* src) {
   lexer_t* lexer;

   lexer = emalloc(sizeof(struct LEXER_STRUC));

   lexer->src = src;
   lexer->state = LEXER_STATE_REG;
   lexer->tokenl = NULL;
   lexer->tokenl_last = NULL;
   lexer->tokenc = 0;

   return lexer;
}

void lexer_destroy(lexer_t* lexer) {
   free(lexer);
}

void lexer_add_token(lexer_t* lexer, token_t* token) {
   if (lexer->tokenl) {
      lexer->tokenl_last->nxt = token;
      lexer->tokenl_last = token;
   } else {
      lexer->tokenl = token;
      lexer->tokenl_last = token;
   }

   lexer->tokenc ++;
}

void lexer_add_current_char(lexer_t* lexer, int type) {
   token_t* t; /* the token to be added */

   t = token_init(type, *lexer->src);

   lexer_add_token(lexer, t);
}

void lexer_add_current_char_to_last_token(lexer_t* lexer, token_type_t type) {
   if (lexer->tokenl_last && lexer->tokenl_last->type == type) {
      token_add_char(lexer->tokenl_last, *lexer->src);
   } else {
      lexer_add_current_char(lexer, type);
   }
}

void lexer_do_reg(lexer_t* lexer) {
   switch (*lexer->src) {
      case SYNTAX_APPLY:
         lexer_add_current_char(lexer, TOKEN_TYPE_APPLY);
         break;
      case SYNTAX_TAG_DELIM:
         lexer->state = LEXER_STATE_TAG;
         break;
         /*case SYNTAX_NAMESPACE_DELIM:
         lexer_add_current_char(lexer, TOKEN_NAMESPACE_DELIM);
         break;*/
      case SYNTAX_SET:
         lexer_add_current_char(lexer, TOKEN_TYPE_SET);
         break;
      case SYNTAX_LLIST:
         lexer_add_current_char(lexer, TOKEN_TYPE_LLIST);
         break;
      case SYNTAX_RLIST:
         lexer_add_current_char(lexer, TOKEN_TYPE_RLIST);
         break;
      case SYNTAX_LGROUP:
         lexer_add_current_char(lexer, TOKEN_TYPE_LGROUP);
         break;
      case SYNTAX_RGROUP:
         lexer_add_current_char(lexer, TOKEN_TYPE_RGROUP);
         break;
      case SYNTAX_LBLOCK:
         lexer_add_current_char(lexer, TOKEN_TYPE_LBLOCK);
         break;
      case SYNTAX_RBLOCK:
         lexer_add_current_char(lexer, TOKEN_TYPE_RBLOCK);
         break;
      case SYNTAX_EXPR_END:
         lexer_add_current_char(lexer, TOKEN_TYPE_EXPR_END);
         break;
      case SYNTAX_LIST_DELIM:
         lexer_add_current_char(lexer, TOKEN_TYPE_LIST_DELIM);
         break;
      case SYNTAX_STR_DELIM:
         lexer->state = LEXER_STATE_STR;
         break;
      default:
         if (isdigit(*lexer->src)) {
            lexer_add_current_char(lexer, TOKEN_TYPE_INT);
            if (isdigit(*(lexer->src + 1))) { lexer->state = LEXER_STATE_INT; }

         } else if (strchr(SYNTAX_KWD_CHARS, *lexer->src)) {
            lexer_add_current_char(lexer, TOKEN_TYPE_KWD);
            if (strchr(SYNTAX_KWD_CHARS, *(lexer->src + 1))) { lexer->state = LEXER_STATE_KWD; }

         } else {
            lexer_add_current_char(lexer, TOKEN_TYPE_UNKNOWN);
            lexer->state = LEXER_STATE_REG;
         }
   }
}

void lexer_do_tag(lexer_t* lexer) {
   switch (*lexer->src) {
      case SYNTAX_LIST_DELIM:
         lexer_add_current_char(lexer, TOKEN_TYPE_LIST_DELIM);
         lexer->state = LEXER_STATE_REG;
         break;
      case SYNTAX_SET:
         lexer_add_current_char(lexer, TOKEN_TYPE_SET);
         lexer->state = LEXER_STATE_REG;
         break;
      case SYNTAX_APPLY:
         lexer_add_current_char(lexer, TOKEN_TYPE_APPLY);
         lexer->state = LEXER_STATE_REG;
         break;
      case SYNTAX_TAG_DELIM:
         lexer_add_token(lexer, token_init(TOKEN_TYPE_TAG, '\0'));
         break;
      default: lexer_add_current_char_to_last_token(lexer, TOKEN_TYPE_TAG);
   }
}

void lexer_do_str(lexer_t* lexer) {
   if (*lexer->src == SYNTAX_STR_DELIM) {
      lexer->state = LEXER_STATE_REG;
   } else {
      lexer_add_current_char_to_last_token(lexer, TOKEN_TYPE_STR);
   }
}

void lexer_do_int(lexer_t* lexer) {
   if (isdigit(*lexer->src)) {
      lexer_add_current_char_to_last_token(lexer, TOKEN_TYPE_INT);
      ! isdigit(*(lexer->src + 1)) && ( lexer->state = LEXER_STATE_REG );
   } else {
      LOG_ERR("int state at non-int token");
   }
}

void lexer_do_kwd(lexer_t* lexer) {
   if (strchr(SYNTAX_KWD_CHARS, *lexer->src)) {
      lexer_add_current_char_to_last_token(lexer, TOKEN_TYPE_KWD);
      ! strchr(SYNTAX_KWD_CHARS, *(lexer->src + 1)) && 
         ( lexer->state = LEXER_STATE_REG );
   } else {
      LOG_ERR("keyword state at non-keyword token");
   }
   
}

void lexer_run(lexer_t* lexer) {
   while (*lexer->src) {
      if (lexer->state == LEXER_STATE_REG) { lexer_do_reg(lexer); }
      else if (lexer->state == LEXER_STATE_TAG) { lexer_do_tag(lexer); }
      else if (lexer->state == LEXER_STATE_STR) { lexer_do_str(lexer); }
      else if (lexer->state == LEXER_STATE_INT) { lexer_do_int(lexer); }
      else if (lexer->state == LEXER_STATE_KWD) { lexer_do_kwd(lexer); }
      lexer->src ++;
   }
}