aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
blob: 384d8ea02e6ca0f184a5b4edb6b564c37a7956fe (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "include/parser.h"
#include "include/token.h"
#include "include/util.h"

parser_t* parser_init(token_t* token) {
   parser_t* parser;

   parser = emalloc(sizeof(struct PARSER));
   parser->token = token;
   parser->tree = NULL;
   parser->ltree = NULL;

   return parser;
}

void parser_destroy(parser_t* parser) {
   if (parser) { free(parser); }
}

int parser_nxt_token(parser_t* parser) {
   /* Preserve original token list, to be cleaned up by lexer. */
   if (!parser->token) { return 0; }
   parser->token = parser->token->nxt;
   if (parser->token && parser->token->type == TOKEN_TYPE_EXPR_END) {
      return parser_nxt_token(parser);
   }
   return parser->token ? 1 : 0;
}

#if 0 /* TODO: This seems like it might be useful??
tree_t* parser_parse_init(parser_t* parser) {
   /* There is nothing to do. */
   if (!parser->token) {
      return NULL;
   } 

   tree_t* block;
   block = tree_init(TREE_TYPE_BLOCK, NULL);

   block->data.block.val = parser_parse_expr(parser);
   block->data.block.nxt = parser_parse_block(parser);

   return block;
}
#endif

tree_t* parser_parse_block(parser_t* parser) {
   /* There is nothing to do. */
   if (!parser->token || parser->token->type == TOKEN_TYPE_RBLOCK) {
      (void) parser_nxt_token(parser); /* Skip over closing bracket. */
      return NULL;
   } 

   tree_t* block;
   block = tree_init(TREE_TYPE_BLOCK, parser->ltree);

   parser->ltree = block;
   block->data.block.val = parser_parse_expr(parser);

   parser->ltree = block;
   block->data.block.nxt = parser_parse_block(parser);

   return block;
}

tree_t* parser_parse_expr(parser_t* parser) {
   tree_t* expr = NULL;

   switch (parser->token->type) {
      case TOKEN_TYPE_INT:
         expr = parser_parse_lint(parser);
         break;
      case TOKEN_TYPE_STR:
         expr = parser_parse_lstr(parser);
         break;
      case TOKEN_TYPE_LBLOCK:
         /* TODO: Move this into parser_parse_block, parser_parse_init exists
          * now. */
         parser_nxt_token(parser);  /* Skip over opening curly bracket. */
         expr = parser_parse_block(parser);
         break;
      case TOKEN_TYPE_KWD:
         expr = parser_parse_call(parser);
         break;
      case TOKEN_TYPE_TAG:
         expr = parser_parse_def(parser);
         break;
      default:
         LOG_WARF("%s: Unknown token type: %d", __func__, parser->token->type);
         parser_nxt_token(parser);
   }

   return expr;
}

tree_t* parser_parse_def(parser_t* parser) {
   tree_t* def = tree_init(TREE_TYPE_DEF, parser->ltree);

   parser->ltree = def;
   def->data.def.tag = parser_parse_tag(parser);

   if (parser->token->type == TOKEN_TYPE_APPLY) {
      parser_nxt_token(parser);  /* Skip over apply. */

      parser->ltree = def;
      def->data.def.arg = parser_parse_darg(parser);
   } else {
      def->data.def.arg = NULL;
   }

   if (parser->token->type == TOKEN_TYPE_SET) {
      parser_nxt_token(parser);  /* Skip over set. */

      parser->ltree = def;
      def->data.def.val = parser_parse_expr(parser);
   } else {
      def->data.def.val = NULL;
   }

   return def;
}

tree_t* parser_parse_tag(parser_t* parser) {
   if (
      parser->token && 
      parser->token->type != TOKEN_TYPE_TAG
   ) { return NULL; }

   tree_t* tag = tree_init(TREE_TYPE_TAG, parser->ltree);

   tag->data.tag.val = parser->token->val;
   parser->token->val = NULL;

   parser_nxt_token(parser);

   parser->ltree = tag;
   tag->data.tag.nxt = parser_parse_tag(parser);

   return tag;
}

tree_t* parser_parse_darg(parser_t* parser) {
   tree_t* darg = tree_init(TREE_TYPE_DARG, parser->ltree);

   parser->ltree = darg;
   darg->data.darg.tag = parser_parse_tag(parser);
   
   if (parser->token->type == TOKEN_TYPE_LIST_DELIM) {
      parser_nxt_token(parser);  /* Skip over list delim. */

      parser->ltree = darg;
      darg->data.darg.nxt = parser_parse_darg(parser);
   } else {
      darg->data.darg.nxt = NULL;
   }

   return darg;
}

tree_t* parser_parse_carg(parser_t* parser) {
   tree_t* carg;

   carg = tree_init(TREE_TYPE_CARG, parser->ltree); 

   parser->ltree = carg;
   carg->data.carg.val = parser_parse_expr(parser);

   if (
      parser->token
      && parser->token->type == TOKEN_TYPE_LIST_DELIM
      && parser_nxt_token(parser)
   ) {
      parser->ltree = carg;
      carg->data.carg.nxt = parser_parse_carg(parser);
   } else {
      carg->data.carg.nxt = NULL;
   }

   return carg;
}

tree_t* parser_parse_kwd(parser_t* parser) {
   tree_t* call;

   call = tree_init(TREE_TYPE_CALL, parser->ltree);

   call->data.call.target = parser->token->val;
   parser->token->val = NULL;

   /* Skip over keyword, check for arguments. */
   if (
      parser_nxt_token(parser) &&
      parser->token->type == TOKEN_TYPE_APPLY
   ) {
      /* Expect some arguments. */
      parser_nxt_token(parser);

      parser->ltree = call;
      call->data.call.arg = parser_parse_carg(parser);
   } else {
      /* There're no arguments. */
      call->data.call.arg = NULL;
   }

   return call;
}

/* I don't know if these need to exist. Guess I'll wait and see… */
int parser_match(parser_t* parser, token_type_t type) {
   return parser->token ? parser->token->type == type : 0;
}

int parser_nxt_token_match(parser_t* parser, token_type_t type) {
   parser_nxt_token(parser);
   return parser_match(parser, type);
}

tree_t* parser_parse_lint(parser_t* parser) {
   tree_t* lint;

   lint = tree_init(TREE_TYPE_LINT, parser->ltree);
   lint->data.lint.val = atoi(parser->token->val);
   parser_nxt_token(parser);

   return lint;
}

tree_t* parser_parse_lstr(parser_t* parser) {
   tree_t* lstr;

   lstr = tree_init(TREE_TYPE_LSTR, parser->ltree);

   lstr->data.lstr.len = strlen(parser->token->val);
   /* Move token value to tree, to allow for future token destruction. */
   lstr->data.lstr.val = parser->token->val;
   parser->token->val = NULL; 
   parser_nxt_token(parser);

   return lstr;
}

tree_t* parser_parse_call(parser_t* parser) {
   tree_t* call;

   call = tree_init(TREE_TYPE_CALL, parser->ltree);

   call->data.call.target = parser->token->val;
   parser->token->val = NULL;

   /*
      If there's a call with an apply, but the file ends just after, NULL.
      ¯\_(ツ)_/¯ 
   */
   if (
      parser_nxt_token(parser)
      && parser->token->type == TOKEN_TYPE_APPLY
      && parser_nxt_token(parser)
   ) {
      parser->ltree = call;
      call->data.call.arg = parser_parse_carg(parser);
   } else {
      call->data.call.arg = NULL;
   }

   return call;
}

#if 0 /* ??? */
void parser_parse(parser_t* parser) {
   while (parser->token) {
      switch (parser->token->type) {
         case TOKEN_TYPE_INT:
            parser->tree->data.block.val = parser_parse_lint(parser);
            break;
      }

      parser_nxt_token(parser);
   }

}
#endif

void parser_run(parser_t* parser) {
   parser->ltree = NULL;
   parser->tree = parser_parse_block(parser);
}