#include "../include/parser.h" #include typedef struct { TslTokenizer tokenizer; } TslParser; static int tsl_parser_parse_rhs(TslParser *self); static void tsl_parser_init(TslParser *self, const char *code, size_t code_size) { tsl_tokenizer_init(&self->tokenizer, code, code_size); } static int tsl_parser_parse_map(TslParser *self) { #define parse_map_element_separator \ if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_COLON)) \ return -1; \ if(tsl_parser_parse_rhs(self) != 0) \ return -1; \ token = tsl_tokenizer_next(&self->tokenizer); \ if(token == TSL_TOKEN_COMMA) { \ continue; \ } else if(token == TSL_TOKEN_RBRACE) { \ return 0; \ } else { \ fprintf(stderr, "Error: Expected ',' or '}', got TODO\n"); \ return -1; \ } for(;;) { TslToken token = tsl_tokenizer_next(&self->tokenizer); if(token == TSL_TOKEN_NUM) { printf("rhs num: %ld\n", self->tokenizer.number_value); parse_map_element_separator } else if(token == TSL_TOKEN_BOOL) { printf("rhs bool: %s\n", self->tokenizer.bool_value ? "true" : "false"); parse_map_element_separator } else if(token == TSL_TOKEN_NULL) { printf("rhs null\n"); parse_map_element_separator } else if(token == TSL_TOKEN_STRING) { printf("rhs string: |%.*s|\n", self->tokenizer.string.size, self->tokenizer.string.data); parse_map_element_separator } else if(token == TSL_TOKEN_RBRACE) { /* '}' after trailing ',' or an empty map */ return 0; } else { fprintf(stderr, "Error: Expected '}', got TODO\n"); return -1; } token = tsl_tokenizer_next(&self->tokenizer); } } static int tsl_parser_parse_list(TslParser *self) { #define parse_list_element_separator \ token = tsl_tokenizer_next(&self->tokenizer); \ if(token == TSL_TOKEN_COMMA) { \ continue; \ } else if(token == TSL_TOKEN_RBRACKET) { \ return 0; \ } else { \ fprintf(stderr, "Error: Expected ',' or ']', got TODO\n"); \ return -1; \ } for(;;) { TslToken token = tsl_tokenizer_next(&self->tokenizer); if(token == TSL_TOKEN_NUM) { printf("rhs num: %ld\n", self->tokenizer.number_value); parse_list_element_separator } else if(token == TSL_TOKEN_BOOL) { printf("rhs bool: %s\n", self->tokenizer.bool_value ? "true" : "false"); parse_list_element_separator } else if(token == TSL_TOKEN_NULL) { printf("rhs null\n"); parse_list_element_separator } else if(token == TSL_TOKEN_STRING) { printf("rhs string: |%.*s|\n", self->tokenizer.string.size, self->tokenizer.string.data); parse_list_element_separator } else if(token == TSL_TOKEN_RBRACKET) { /* ']' after trailing ',' or an empty list */ return 0; } else { fprintf(stderr, "Error: Expected '}', got TODO\n"); return -1; } } } int tsl_parser_parse_rhs(TslParser *self) { TslToken token = tsl_tokenizer_next(&self->tokenizer); if(token == TSL_TOKEN_NUM) { printf("rhs num: %ld\n", self->tokenizer.number_value); } else if(token == TSL_TOKEN_BOOL) { printf("rhs bool: %s\n", self->tokenizer.bool_value ? "true" : "false"); } else if(token == TSL_TOKEN_NULL) { printf("rhs null\n"); } else if(token == TSL_TOKEN_STRING) { printf("rhs string: |%.*s|\n", self->tokenizer.string.size, self->tokenizer.string.data); } else if(token == TSL_TOKEN_LBRACE) { tsl_parser_parse_map(self); } else if(token == TSL_TOKEN_LBRACKET) { tsl_parser_parse_list(self); } else { fprintf(stderr, "Error: Expected number, bool or null, got TODO\n"); return -1; } return 0; } static int tsl_parser_parse(TslParser *self) { for(;;) { TslToken token = tsl_tokenizer_next(&self->tokenizer); if(token == TSL_TOKEN_IDENTIFIER) { TslStringView identifier = self->tokenizer.identifier; printf("identifier: %.*s\n", identifier.size, identifier.data); if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_EQUAL)) { return -1; } if(tsl_parser_parse_rhs(self) != 0) { return -1; } } else if(token == TSL_TOKEN_END_OF_FILE) { break; } else { fprintf(stderr, "Error: Expected identifier, got TODO\n"); return -1; } } return 0; } int tsl_parse(const char *code, size_t code_size) { TslParser parser; tsl_parser_init(&parser, code, code_size); return tsl_parser_parse(&parser); }