Removed Definition Generator for rewrite

This commit is contained in:
BuildTools 2023-09-10 12:12:37 +02:00
parent a7f93cabaa
commit c53e7cbc2f
2 changed files with 0 additions and 330 deletions

View File

@ -1,91 +0,0 @@
#ifndef CARROT_DEFINITION_GENERATOR_H
#define CARROT_DEFINITION_GENERATOR_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tokenizer.h>
typedef struct carrot_definition carrot_definition_s;
typedef struct carrot_rule carrot_rule_s;
typedef struct carrot_compound carrot_compound_s;
typedef struct carrot_element carrot_element_s;
typedef struct carrot_end_node carrot_end_node_s;
struct carrot_definition
{
uint32_t num_rules;
carrot_rule_s *rules;
};
struct carrot_rule
{
uint32_t len_name;
char *name;
uint32_t num_elements;
carrot_element_s *elements;
};
struct carrot_compound
{
carrot_rule_s *up;
uint32_t num_rules;
carrot_rule_s *rules;
};
typedef enum
{
CARROT_ELEMENT_RULE_INSERTION,
CARROT_ELEMENT_PATTERN,
CARROT_ELEMENT_RAW_LITERAL
} carrot_element_e;
typedef enum
{
CARROT_PATTERN_INVALID,
CARROT_PATTERN_WORD, // Keyword: "|word|"
CARROT_PATTERN_KEYWORD, // Keyword: "|keyword|" (belonging to a list of user-defined keywords
CARROT_PATTERN_NUMBER // Keyword: "|number|"
} carrot_pattern_e;
struct carrot_element
{
// char *text;
carrot_element_e type;
union carrot_specific_literal
{
struct carrot_tag_name
{
uint32_t len_name;
char *name;
} nonterminal;
struct carrot_pattern
{
carrot_pattern_e value;
} pattern;
struct carrot_raw_literal
{
uint32_t num_bytes;
char *bytes;
} raw_literal;
} specific;
};
carrot_definition_s carrot_parse_definition (carrot_token_stream_s *stream);
#endif // CARROT_DEFINITION_GENERATOR_H

View File

@ -1,239 +0,0 @@
#include <defgen.h>
#define TOKEN_AT(index) (stream->tokens[(index)])
#define TOKEN_TEXT_AT(index) (&stream->source[stream->tokens[(index)].char_index])
/*
bool carrot_token_text_equals(carrot_token_stream_s *stream, int index, char *text)
{
}
*/
// int32_t carrot_parse_compound
bool carrot_tag_name_starts_here(carrot_token_stream_s *stream, uint32_t index)
{
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_OPENING_ARROW) return false;
++index; // Skip the opening arrow in front of the rule-name
if(TOKEN_AT(index).type != CARROT_TOKEN_WORD) return false;
++index; // Skip the name of the rule
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_CLOSING_ARROW) return false;
++index; // Skip the opening arrow in front of the rule-name
return true;
}
bool carrot_pattern_starts_here(carrot_token_stream_s *stream, uint32_t index)
{
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_VERTICAL_BAR)
{
return false;
}
++index;
if(TOKEN_AT(index).type != CARROT_TOKEN_WORD)
{
return false;
}
++index;
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_VERTICAL_BAR)
{
return false;
}
return true;
}
int32_t carrot_parse_tag_name(carrot_token_stream_s *stream, uint32_t index, carrot_element_s *out_element)
{
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_OPENING_ARROW)
{
return -1;
}
++index; // Skip the opening arrow
uint32_t tag_name_index = index;
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_CLOSING_ARROW)
{
return -1;
}
++index; // Skip the closing arrow
out_element->specific.nonterminal.len_name = TOKEN_AT(tag_name_index).length;
out_element->specific.nonterminal.name = malloc(out_element->specific.nonterminal.len_name+1);
out_element->specific.nonterminal.name[out_element->specific.nonterminal.len_name] = 0;
return 3;
}
carrot_pattern_e carrot_resolve_keyword_to_enum_value(char *keyword)
{
if(strcpy(keyword, "word")) return CARROT_PATTERN_WORD;
if(strcpy(keyword, "keyword")) return CARROT_PATTERN_KEYWORD;
if(strcpy(keyword, "number")) return CARROT_PATTERN_NUMBER;
return CARROT_PATTERN_INVALID;
}
int32_t carrot_parse_pattern(carrot_token_stream_s *stream, uint32_t index, carrot_element_s *out_element)
{
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_VERTICAL_BAR)
{
return -1;
}
++index; // Skip the opening vertical bar
uint32_t word_index = index;
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_VERTICAL_BAR)
{
return -1;
}
++index; // Skip the opening vertical bar
uint32_t len_word = TOKEN_AT(word_index).length;
char word[len_word+1];
memcpy(word, TOKEN_TEXT_AT(word_index), len_word);
word[len_word] = 0x00;
out_element->type = CARROT_ELEMENT_PATTERN;
out_element->specific.pattern.value = carrot_resolve_keyword_to_enum_value(word);
return 3;
}
bool carrot_element_starts_here(carrot_token_stream_s *stream, uint32_t index)
{
if(carrot_tag_name_starts_here(stream, index))
{
return true;
}
if(carrot_pattern_starts_here(stream, index))
{
return true;
}
return -1;
}
int32_t carrot_parse_element(carrot_token_stream_s *stream, uint32_t index, carrot_element_s *out_element)
{
uint32_t first_index = index;
if(carrot_tag_name_starts_here(stream, index))
{
index += carrot_parse_tag_name(stream, index, out_element);
return index - first_index;
}
if(carrot_pattern_starts_here(stream, index))
{
index += carrot_parse_pattern(stream, index, out_element);
return index - first_index;
}
return -1;
}
int32_t carrot_parse_rule(carrot_token_stream_s *stream, uint32_t index, carrot_rule_s *out_rule)
{
uint32_t first_index = index;
carrot_rule_s rule;
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_OPENING_ARROW) return -1;
++index; // Skip the opening arrow in front of the rule-name
rule.len_name = TOKEN_AT(index).length;
rule.name = malloc(rule.len_name + 1);
memcpy(rule.name, &stream->source[TOKEN_AT(index).char_index], rule.len_name);
rule.name[rule.len_name] = 0x00;
++index; // Skip the name of the rule
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_CLOSING_ARROW) return -2;
++index; // Skip the opening arrow in front of the rule-name
if(TOKEN_AT(index).type != CARROT_TOKEN_SPECIAL_EQUALS_SIGN) return -3;
++index; // Skip the equals sign between the rule's identifier and the rule
uint32_t max_elements_in_rule = 64;
int element_index = 0;
bool running = true;
while(running)
{
if(!carrot_element_starts_here(stream, index))
{
if(TOKEN_AT(index).type == CARROT_TOKEN_SPECIAL_SEMICOLON)
{
printf("found semicolon'n");
break;
}
printf("non-element at token: %d'\n", index);
return -1024;
}
if(out_rule->num_elements >= max_elements_in_rule)
{
max_elements_in_rule += 64;
out_rule->elements = realloc(
out_rule->elements,
sizeof(carrot_element_s) * max_elements_in_rule
);
}
carrot_element_s element;
int32_t len_element = carrot_parse_element(stream, index, &element);
if(len_element < 1)
{
printf("Failed parsing element #%d.\n", element_index);
return (-element_index)-1;
}
index += len_element;
out_rule->elements[out_rule->num_elements] = element;
++element_index;
++out_rule->num_elements;
}
(*out_rule) = rule;
return index - first_index;
}
carrot_definition_s carrot_parse_definition(carrot_token_stream_s *stream)
{
uint32_t rules_capacity = 64;
carrot_definition_s definition;
definition.num_rules = 0;
definition.rules = calloc(sizeof(carrot_definition_s), rules_capacity);
uint32_t rule_index = 0;
uint32_t token_index = 0;
while(token_index < stream->num_tokens)
{
puts("Getting a rule of a definition.");
if(definition.num_rules >= rules_capacity)
{
rules_capacity *= 2;
definition.rules = realloc(definition.rules, sizeof(carrot_rule_s) * rules_capacity);
}
uint32_t len_rule_in_tokens = carrot_parse_rule(stream, token_index, &definition.rules[rule_index]);
printf("Rule is %d tokens long.\n", len_rule_in_tokens);
if(len_rule_in_tokens < 1) return definition;
token_index += len_rule_in_tokens;
++rule_index;
}
return definition;
}