2023-11-26 18:49:54 +00:00
|
|
|
|
|
|
|
#ifndef PARCEL_AST_H
|
|
|
|
#define PARCEL_AST_H
|
|
|
|
|
2023-11-29 18:17:00 +00:00
|
|
|
#include <parcel/utility/utility.h>
|
2023-11-28 21:21:23 +00:00
|
|
|
#include <logger.h>
|
2023-11-28 15:01:46 +00:00
|
|
|
#include <tokenizer.h>
|
2023-11-26 18:49:54 +00:00
|
|
|
|
2023-11-28 23:26:56 +00:00
|
|
|
// pac_ast_set_e: An enumeration of all sets known in Parcel's AST.
|
|
|
|
//
|
|
|
|
// Sets are descriptions which describe a rough format of token, like
|
|
|
|
// with variable names; the format is known, but the actual name isn't.
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PAC_AST_SET_RUNE,
|
|
|
|
PAC_AST_SET_WORD,
|
|
|
|
PAC_AST_SET_INTEGER,
|
|
|
|
PAC_AST_SET_FLOAT
|
|
|
|
|
|
|
|
} pac_ast_set_e;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PAC_AST_ITEM_INVALID = 0x00,
|
|
|
|
PAC_AST_ITEM_REFERENCE,
|
|
|
|
PAC_AST_ITEM_LITERAL,
|
2023-11-29 21:13:15 +00:00
|
|
|
PAC_AST_ITEM_RANGE,
|
|
|
|
PAC_AST_ITEM_COLLECTION, // A collection of any of the other items which may not be interrupted by spaces.
|
2023-11-28 23:26:56 +00:00
|
|
|
PAC_AST_ITEM_SET
|
|
|
|
|
|
|
|
} pac_ast_item_e;
|
|
|
|
|
|
|
|
// pac_ast_recovery_level_e: How much higher in the call stack the program
|
|
|
|
// flow has to go to be able to recover.
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PAC_AST_STATUS_SUCCESS,
|
|
|
|
PAC_AST_STATUS_ERROR_HANDLED,
|
|
|
|
PAC_AST_STATUS_STOP_ESCALATING_IN_ITEM_PARSER,
|
|
|
|
PAC_AST_STATUS_STOP_ESCALATING_IN_VARIANT_PARSER,
|
|
|
|
PAC_AST_STATUS_STOP_ESCALATING_IN_RULE_PARSER,
|
2023-11-29 13:00:43 +00:00
|
|
|
PAC_AST_STATUS_CONTINUE_AFTER_FINDING_NEXT_VARIANT,
|
2023-11-28 23:26:56 +00:00
|
|
|
PAC_AST_STATUS_CONTINUE_AFTER_FINDING_NEXT_RULE,
|
|
|
|
PAC_AST_STATUS_NOT_RECOVERABLE,
|
|
|
|
PAC_AST_STATUS_UNEXPECTED_FILE_END
|
|
|
|
|
|
|
|
} pac_ast_status_e;
|
|
|
|
|
2023-11-26 18:49:54 +00:00
|
|
|
typedef struct pac_ast pac_ast_s;
|
|
|
|
typedef struct pac_ast_rule pac_ast_rule_s;
|
|
|
|
typedef struct pac_ast_variant pac_ast_variant_s;
|
2023-11-29 13:00:43 +00:00
|
|
|
typedef struct pac_ast_string_literal pac_ast_string_literal_s;
|
2023-11-26 18:49:54 +00:00
|
|
|
typedef struct pac_ast_reference pac_ast_reference_s;
|
2023-11-29 21:13:15 +00:00
|
|
|
typedef struct pac_ast_collection pac_ast_collection_s;
|
2023-11-26 18:49:54 +00:00
|
|
|
typedef struct pac_ast_item pac_ast_item_s;
|
|
|
|
|
|
|
|
struct pac_ast
|
|
|
|
{
|
|
|
|
usz_t num_rules;
|
|
|
|
pac_ast_rule_s *rules;
|
2023-11-29 13:00:43 +00:00
|
|
|
pac_arena_s string_arena;
|
2023-11-26 18:49:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pac_ast_rule
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
usz_t num_variants;
|
|
|
|
pac_ast_variant_s *variants;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pac_ast_variant
|
|
|
|
{
|
|
|
|
usz_t num_items;
|
|
|
|
pac_ast_item_s *items;
|
|
|
|
};
|
|
|
|
|
|
|
|
// pac_ast_reference: Also called non-terminal, a reference is an item
|
|
|
|
// which represents all the contents of another rule.
|
|
|
|
struct pac_ast_reference
|
|
|
|
{
|
|
|
|
usz_t len_name;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
2023-11-29 21:13:15 +00:00
|
|
|
struct pac_ast_collection
|
|
|
|
{
|
|
|
|
usz_t num_items;
|
|
|
|
pac_ast_item_s *items;
|
|
|
|
};
|
|
|
|
|
2023-11-29 13:00:43 +00:00
|
|
|
struct pac_ast_string_literal
|
2023-11-26 18:49:54 +00:00
|
|
|
{
|
|
|
|
usz_t length;
|
|
|
|
char *string;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pac_ast_item
|
|
|
|
{
|
2023-11-28 15:01:46 +00:00
|
|
|
pac_ast_item_e type;
|
2023-11-28 17:57:36 +00:00
|
|
|
union pac_ast_item_data
|
2023-11-26 18:49:54 +00:00
|
|
|
{
|
2023-11-29 13:00:43 +00:00
|
|
|
pac_ast_string_literal_s string_literal;
|
2023-11-28 15:21:50 +00:00
|
|
|
pac_ast_set_e set;
|
2023-11-29 21:13:15 +00:00
|
|
|
pac_ast_collection_s collection;
|
2023-11-28 15:01:46 +00:00
|
|
|
pac_ast_reference_s reference;
|
2023-11-26 18:49:54 +00:00
|
|
|
} data;
|
|
|
|
};
|
|
|
|
|
2023-11-28 23:26:56 +00:00
|
|
|
typedef struct pac_ast_builder
|
2023-11-28 21:21:23 +00:00
|
|
|
{
|
2023-11-28 23:26:56 +00:00
|
|
|
usz_t cursor;
|
2023-11-28 21:21:23 +00:00
|
|
|
pac_tlist_s *token_list;
|
|
|
|
bool_t failed;
|
|
|
|
|
2023-11-29 13:00:43 +00:00
|
|
|
pac_logger_s *logger;
|
|
|
|
pac_arena_s string_arena;
|
2023-11-28 23:26:56 +00:00
|
|
|
|
|
|
|
} pac_ast_builder_s;
|
2023-11-28 21:21:23 +00:00
|
|
|
|
2023-11-29 13:00:43 +00:00
|
|
|
pac_ast_s pac_build_ast (pac_tlist_s tokens, pac_logger_s *logger);
|
|
|
|
void pac_delete_ast (pac_ast_s ast);
|
2023-11-28 21:21:23 +00:00
|
|
|
|
2023-11-28 23:26:56 +00:00
|
|
|
pac_ast_status_e pac_ast_handle_invalid_reference_name_token (pac_ast_builder_s *builder);
|
|
|
|
pac_ast_status_e pac_ast_handle_missing_reference_close_tag (pac_ast_builder_s *builder);
|
2023-11-29 13:00:43 +00:00
|
|
|
pac_ast_status_e pac_ast_handle_missing_rule_header_closing_sign (pac_ast_builder_s *builder);
|
2023-11-28 23:26:56 +00:00
|
|
|
pac_ast_status_e pac_ast_handle_reference_with_equals_sign (pac_ast_builder_s *builder);
|
2023-11-29 17:27:46 +00:00
|
|
|
pac_ast_status_e pac_ast_handle_missing_item_separator (pac_ast_builder_s *builder, char *rule_name, usz_t variant_index, usz_t item_index);
|
2023-11-28 23:26:56 +00:00
|
|
|
pac_ast_status_e pac_ast_handle_missing_equals_sign_after_rule_header (pac_ast_builder_s *builder);
|
2023-11-29 13:00:43 +00:00
|
|
|
pac_ast_status_e pac_ast_handle_unknown_item_type (pac_ast_builder_s *builder, char *rule_name, usz_t variant_index, usz_t item_index);
|
|
|
|
|
|
|
|
char * pac_ast_stringify_status (pac_ast_status_e status);
|
2023-11-28 15:01:46 +00:00
|
|
|
|
2023-11-26 18:49:54 +00:00
|
|
|
#endif // PARCEL_AST_H
|