2023-09-10 10:41:25 +00:00
|
|
|
|
|
|
|
#ifndef CARROT_DEFINITION_H
|
|
|
|
#define CARROT_DEFINITION_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
#include <tokenizer.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
CARROT_PATTERN_ELEMENT_VARIABLE_TERMINAL,
|
|
|
|
CARROT_PATTERN_ELEMENT_LITERAL_TERMINAL,
|
|
|
|
CARROT_PATTERN_ELEMENT_RULE
|
|
|
|
} carrot_pattern_element_e;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
CARROT_VARIABLE_TERMINAL_ALPHABETICAL_WORD,
|
|
|
|
CARROT_VARIABLE_TERMINAL_ALPHANUMERIC_WORD, //
|
|
|
|
CARROT_VARIABLE_TERMINAL_KEYWORD, // Subtype of CARROT_VARIABLE_TERMINAL_ALPHANUMERIC_WORD; from a list of user-specified keywords.
|
|
|
|
CARROT_VARIABLE_TERMINAL_OPERATOR,
|
|
|
|
CARROT_VARIABLE_TERMINAL_NUMBER,
|
|
|
|
CARROT_VARIABLE_TERMINAL_FLOAT, // Subtype of CARROT_VARIABLE_TERMINAL_NUMBER; all numbers, including those with digits after the number.
|
|
|
|
CARROT_VARIABLE_TERMINAL_INTEGER // Subtype of CARROT_VARIABLE_TERMINAL_NUMBER; only means full numbers without digits after the number.
|
|
|
|
} carrot__variable_terminal_e;
|
|
|
|
|
|
|
|
|
2023-09-10 10:41:25 +00:00
|
|
|
// carrot_definition_unit_s: A whole file (with possible included sub-files) of rules.
|
|
|
|
// A definition unit describes a single format or language.
|
|
|
|
typedef struct carrot_definition_unit carrot_definition_unit_s;
|
|
|
|
|
|
|
|
// carrot_rule_definition_s: The definition of a rule with all of the patterns it contains.
|
|
|
|
// A rule definition starts with the name of the rule inside of a pair of arrow brackets ( < > ),
|
|
|
|
// followed by an equals sign and some patterns, until a semicolon ends the rule definition.
|
|
|
|
typedef struct carrot_rule_definition carrot_rule_definition_s;
|
|
|
|
|
|
|
|
// carrot_pattern_s: A pattern consisting of a sequence of pattern elements. These elements
|
|
|
|
// can be terminal or the names of whole other rules themselves.
|
|
|
|
typedef struct carrot_pattern_sequence carrot_pattern_sequence_s;
|
|
|
|
|
|
|
|
// carrot_pattern_element_s: An element of a pattern; this can be a terminal symbol or the name
|
|
|
|
// of another rule in arrow brackets ( < > ). It denotes a piece of data, one or multiple tokens,
|
|
|
|
// which have to exist for the pattern to begin (which are static, defined literals)
|
|
|
|
// or can be extracted (like variable terminals).
|
|
|
|
typedef struct carrot_pattern_element carrot_pattern_element_s;
|
|
|
|
|
|
|
|
// carrot_terminal_s: An ending symbol of the parsing tree. This can be the name of a variable terminal
|
|
|
|
// or a static, defined literal.
|
|
|
|
//
|
|
|
|
// A variable terminal could be "[word]", "[keyword]" or one of some others (as they are defined in
|
|
|
|
// docs/variable_terminals.md)
|
|
|
|
//
|
|
|
|
// A static, defined literal is a literal piece of data which must exist at that position in a rule
|
|
|
|
// in exactly the way as it was stated in the definition.
|
|
|
|
typedef struct carrot_terminal carrot_terminal_s;
|
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
struct carrot_definition_unit
|
|
|
|
{
|
|
|
|
uint32_t num_rules;
|
|
|
|
carrot_rule_definition_s *rules;
|
2023-09-10 10:41:25 +00:00
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
carrot_token_stream_s *token_stream;
|
2023-09-10 10:41:25 +00:00
|
|
|
};
|
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
struct carrot_rule_definition
|
|
|
|
{
|
|
|
|
carrot_definition_unit_s *syntax_definition;
|
2023-09-10 10:41:25 +00:00
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
uint32_t num_applying_patterns;
|
|
|
|
carrot_pattern_sequence_s *applying_patterns;
|
2023-09-10 10:41:25 +00:00
|
|
|
};
|
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
struct carrot_pattern_sequence
|
|
|
|
{
|
|
|
|
// rule: The rule that this pattern sequence belongs to.
|
|
|
|
carrot_rule_definition_s *rule;
|
2023-09-10 10:41:25 +00:00
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
uint32_t num_items;
|
|
|
|
carrot_pattern_element_s *items;
|
2023-09-10 10:41:25 +00:00
|
|
|
};
|
|
|
|
|
2023-09-10 16:14:14 +00:00
|
|
|
struct carrot_pattern_element
|
|
|
|
{
|
|
|
|
carrot_pattern_element_e type;
|
|
|
|
carrot__variable_terminal_e variable_terminal;
|
|
|
|
char *referenced_rule_name;
|
|
|
|
char *literal_terminal;
|
2023-09-10 10:41:25 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|