aboutsummaryrefslogtreecommitdiff
path: root/src/token.c
blob: 41e7e0ceed0202df0c621f33cf9ead28c37b85c7 (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
#include <stdlib.h>

#include "include/token.h"

// token constructor
token_t* token_init(int type, char* val) {
   token_t* token = calloc(1, sizeof(struct TOKEN_STRUC));
   token->type = type;
   token->value = val;

   return token;
}

char* token_get_type(int type) {
   switch (type) {
      case TOKEN_KEYWORD:
         return "TOKEN_KEYWORD";
         break;
      case TOKEN_PRIM_STR:
         return "TOKEN_PRIM_STR";
         break;
      case TOKEN_PRIM_INT:
         return "TOKEN_PRIM_INT";
         break;
      case TOKEN_COMM:
         return "TOKEN_COMM";
         break;
      case TOKEN_STMNT_END:
         return "TOKEN_EXPR_END";
         break;
      case TOKEN_LGROUP:
         return "TOKEN_LGROUP";
         break;
      case TOKEN_RGROUP:
         return "TOKEN_RGROUP";
         break;
      case TOKEN_DIRECTIVE:
         return "TOKEN_DIRECTIVE";
         break;
      case TOKEN_FN_APPLY:
         return "TOKEN_FN_APPLY";
         break;
      case TOKEN_LIST_DELIM:
         return "TOKEN_LIST_DELIM";
         break;
      case TOKEN_DEF_TAG:
         return "TOKEN_DEF_TAG";
         break;
      case TOKEN_BLOCK_START:
         return "TOKEN_BLOCK_START";
         break;
      case TOKEN_BLOCK_END:
         return "TOKEN_BLOCK_END";
         break;
      case TOKEN_NAMESPACE_DELIM:
         return "TOKEN_NAMESPACE_DELIM";
         break;
      case TOKEN_ARRAY_START:
         return "TOKEN_ARRAY_START";
         break;
      case TOKEN_ARRAY_END:
         return "TOKEN_ARRAY_END";
         break;
      case TOKEN_DEF_SET:
         return "TOKEN_DEF_SET";
         break;
      case TOKEN_UNKNOWN:
         return "TOKEN_UNKNOWN";
         break;
      case TOKEN_EOF:
         return "TOKEN_EOF";
         break;
      default:
         return "???";
   }
}

int char_could_start_keyword(char* character) {
   for (int i = 0; i < TOKEN_DEFNAME_FIRST_CHAR_ALLOWED_CHARS_LEN; ++ i) {
      if (TOKEN_DEFNAME_FIRST_CHAR_ALLOWED_CHARS[i] == *character) {
         return 1;
      }
   }

   return 0;
}

int char_could_split_keyword(char* character) {
   if (char_could_start_keyword(character)) {
      return 1;
   } else {
      for (int i = 0; i < TOKEN_DEFNAME_SPLIT_CHAR_ALLOWED_CHARS_LEN; ++ i) {
         if (TOKEN_DEFNAME_SPLIT_CHAR_ALLOWED_CHARS[i] == *character) {
            return 1;
         }
      }

      return 0;
   }
}

int char_could_start_int(char* character) {
   for (int i = 0; i < 10; ++ i) {
      if (TOKEN_CHAR_FIRST_CHAR_INT[i] == *character) {
         return 1;
      }
   }

   return 0;
}

int char_can_ignore(char* character) {
   for (int i = 0; i < TOKEN_CHAR_IGNORE_LEN; ++ i) {
      if (TOKEN_CHAR_IGNORE[i] == *character) {
         return 1;
      }
   }

   return 0;
}


int token_char_quote(char c) { return (c != '\''); }
int token_char_grave(char c) { return (c != '`'); }
int token_char_pound(char c) { return (c != '#'); }
int token_char_colon(char c) { return (c != ':'); }
int token_char_kywrd(char c) { return (char_could_split_keyword(&c)); }

void token_destroy(token_t* token) {
   free(token->value);
   free(token);
}