parse: generate diags to report parse errors

This commit is contained in:
2026-03-16 11:53:35 +00:00
parent 2858323eca
commit 51c8d2744c
5 changed files with 682 additions and 81 deletions

View File

@@ -17,6 +17,7 @@ MIE_API void mie_lex_destroy(struct mie_lex *lex);
MIE_API enum mie_status mie_lex_get_status(const struct mie_lex *lex);
MIE_API struct mie_line_source *mie_lex_get_line_source(const struct mie_lex *lex);
MIE_API const struct mie_file_cell *mie_lex_get_cursor(const struct mie_lex *lex);
MIE_API struct mie_token *mie_lex_peek(struct mie_lex *lex);
MIE_API void mie_lex_advance(struct mie_lex *lex);

View File

@@ -25,6 +25,30 @@ struct mie_register;
struct mie_attribute_map;
struct mie_attribute;
#define MIE_PARSE_ITEM_NONE {.i_type = MIE_PARSER_ITEM_NONE}
#define MIE_PARSE_ITEM_TOKEN(tok) \
{.i_type = MIE_PARSER_ITEM_TOK, .i_tok = (tok)}
#define MIE_PARSE_ITEM_CSTR(s) {.i_type = MIE_PARSER_ITEM_CSTR, .i_cstr = (s)}
struct mie_parser_scope {
struct mie_region *s_region;
struct mie_name_map *s_names;
};
enum mie_parser_item_type {
MIE_PARSER_ITEM_NONE = 0,
MIE_PARSER_ITEM_TOK,
MIE_PARSER_ITEM_CSTR,
};
struct mie_parser_item {
enum mie_parser_item_type i_type;
union {
unsigned int i_tok;
const char *i_cstr;
};
};
MIE_API struct mie_parser *mie_parser_create(
struct mie_ctx *ctx, struct mie_lex *lex);
MIE_API void mie_parser_destroy(struct mie_parser *ctx);
@@ -51,6 +75,8 @@ MIE_API bool mie_parser_parse_float(
struct mie_parser *ctx, double *out, struct mie_file_span *loc);
MIE_API bool mie_parser_parse_word(
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
MIE_API bool mie_parser_parse_name(
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
MIE_API bool mie_parser_parse_instname(
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
MIE_API bool mie_parser_parse_graphname(
@@ -71,17 +97,22 @@ MIE_API bool mie_parser_parse_symname(
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
MIE_API bool mie_parser_parse_string(
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
MIE_API bool mie_parser_parse_keyword(struct mie_parser *ctx, const char *kw);
MIE_API bool mie_parser_parse_keyword(
struct mie_parser *ctx, const char *kw, struct mie_file_span *loc);
MIE_API bool mie_parser_parse_symbol(
struct mie_parser *ctx, enum mie_token_symbol sym);
MIE_API bool mie_parser_parse_linefeed(struct mie_parser *ctx);
MIE_API bool mie_parser_parse_type(
struct mie_parser *ctx, const struct mie_type **out);
struct mie_parser *ctx, const char *context,
const struct mie_type **out, struct mie_file_span *out_span);
MIE_API bool mie_parser_parse_type_list(
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(const struct mie_type *, out));
struct mie_parser *ctx, const char *context,
MIE_VECTOR_REF_PARAM(const struct mie_type *, out),
struct mie_file_span *out_span);
MIE_API bool mie_parser_parse_function_type(
struct mie_parser *ctx, struct mie_type **out);
struct mie_parser *ctx, const char *context, struct mie_type **out,
struct mie_file_span *out_span);
MIE_API bool mie_parser_parse_operand(
struct mie_parser *ctx, struct mie_op_arg *out);
@@ -89,30 +120,33 @@ MIE_API bool mie_parser_parse_operand_list(
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_arg, out));
MIE_API bool mie_parser_parse_parameter(
struct mie_parser *ctx, struct mie_op_arg *out);
struct mie_parser *ctx, bool include_type, struct mie_op_arg *out,
const char *context);
MIE_API bool mie_parser_parse_parameter_list(
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_arg, out));
struct mie_parser *ctx, bool include_type,
MIE_VECTOR_REF_PARAM(struct mie_op_arg, out), const char *context);
MIE_API bool mie_parser_parse_unknown_keyword(struct mie_parser *ctx, b_string *out);
MIE_API bool mie_parser_parse_unknown_symbol(
struct mie_parser *ctx, enum mie_token_symbol sym);
MIE_API bool mie_parser_parse_register(
struct mie_parser *ctx, struct mie_name_map *names,
struct mie_register *out);
struct mie_parser *ctx, struct mie_parser_scope *scope,
const char *context, struct mie_register *out);
MIE_API bool mie_parser_parse_register_list(
struct mie_parser *ctx, struct mie_name_map *names,
MIE_VECTOR_REF_PARAM(struct mie_register, out));
struct mie_parser *ctx, struct mie_parser_scope *scope,
const char *context, MIE_VECTOR_REF_PARAM(struct mie_register, out));
MIE_API bool mie_parser_parse_region(
struct mie_parser *ctx, struct mie_op *parent, struct mie_region *region);
struct mie_parser *ctx, struct mie_op *parent,
struct mie_region *region, struct mie_block *first_block);
MIE_API bool mie_parser_parse_region_list(
struct mie_parser *ctx, struct mie_op *parent);
MIE_API bool mie_parser_parse_anonymous_block(
struct mie_parser *ctx, struct mie_name_map *names,
struct mie_parser *ctx, struct mie_parser_scope *scope,
struct mie_block *block);
MIE_API bool mie_parser_parse_block(
struct mie_parser *ctx, struct mie_name_map *names,
struct mie_parser *ctx, struct mie_parser_scope *scope,
struct mie_block *block);
MIE_API bool mie_parser_parse_successor(
@@ -122,11 +156,34 @@ MIE_API bool mie_parser_parse_successor_list(
MIE_API bool mie_parser_parse_module(struct mie_parser *ctx, struct mie_module *mod);
MIE_API bool mie_parser_parse_op(
struct mie_parser *ctx, struct mie_name_map *names, struct mie_op *dest);
struct mie_parser *ctx, struct mie_parser_scope *scope,
struct mie_op *dest);
MIE_API bool mie_parser_parse_attribute(
struct mie_parser *ctx, const struct mie_attribute **dest);
MIE_API bool mie_parser_parse_attribute_map(
struct mie_parser *ctx, struct mie_attribute_map *out);
MIE_API struct mie_diag *mie_parser_report_error_simple(
struct mie_parser *parser, const char *dialect, unsigned int diag_class,
unsigned int msg, const struct mie_file_span *loc);
MIE_API void mie_parser_report_unexpected_token_v(
struct mie_parser *parser,
const struct mie_parser_item expected_tokens[], const char *context);
MIE_API void mie_parser_report_unexpected_token_s(
struct mie_parser *parser, const char *expected_token, const char *context);
static inline void mie_parser_report_unexpected_token(
struct mie_parser *parser, unsigned int expected_token, const char *context)
{
struct mie_parser_item t[]
= {MIE_PARSE_ITEM_TOKEN(expected_token), MIE_PARSE_ITEM_NONE};
mie_parser_report_unexpected_token_v(parser, t, context);
}
MIE_API struct mie_name *mie_parser_scope_put_name(
struct mie_parser_scope *scope, struct mie_name *entry,
const char *hint, enum mie_name_map_flags flags);
MIE_API struct mie_register *mie_parser_scope_find_value(
struct mie_parser_scope *scope, const char *name);
#endif

View File

@@ -39,6 +39,7 @@ enum mie_token_type {
MIE_TOK_TYPENAME = 0x4000u,
/* word or name, prefixed with a # hash */
MIE_TOK_ATTRIBNAME = 0x8000u,
__MIE_TOK_UBOUND = 0x9000u,
};
enum mie_token_value_type {
@@ -51,7 +52,7 @@ enum mie_token_value_type {
enum mie_token_symbol {
MIE_SYM_NONE = 0,
MIE_SYM_COLON,
MIE_SYM_COLON = __MIE_TOK_UBOUND,
MIE_SYM_EQUAL,
MIE_SYM_COMMA,
MIE_SYM_HYPHEN,
@@ -64,6 +65,7 @@ enum mie_token_symbol {
MIE_SYM_TILDE,
MIE_SYM_BANG,
MIE_SYM_ATSIGN,
MIE_SYM_QUESTION,
MIE_SYM_LEFT_BRACE,
MIE_SYM_RIGHT_BRACE,
MIE_SYM_LEFT_BRACKET,

View File

@@ -40,6 +40,7 @@ static struct lex_token_def symbols[] = {
LEX_TOKEN_DEF(MIE_SYM_HASH, "#"),
LEX_TOKEN_DEF(MIE_SYM_ATSIGN, "@"),
LEX_TOKEN_DEF(MIE_SYM_BANG, "!"),
LEX_TOKEN_DEF(MIE_SYM_QUESTION, "?"),
LEX_TOKEN_DEF(MIE_SYM_TILDE, "~"),
LEX_TOKEN_DEF(MIE_SYM_LEFT_BRACE, "{"),
LEX_TOKEN_DEF(MIE_SYM_RIGHT_BRACE, "}"),
@@ -216,6 +217,11 @@ struct mie_line_source *mie_lex_get_line_source(const struct mie_lex *lex)
return lex->lex_source;
}
const struct mie_file_cell *mie_lex_get_cursor(const struct mie_lex *lex)
{
return &lex->lex_source->s_cursor;
}
static bool char_can_begin_symbol(char c)
{
for (size_t i = 0; i < nr_symbols; i++) {
@@ -526,6 +532,7 @@ static enum mie_status read_ident(struct mie_lex *lex, enum mie_token_type type)
return push_string_token(lex, type, s);
} else {
push_symbol(lex, MIE_SYM_ASTERISK);
lex->lex_token_start.c_col++;
return push_string_token(lex, MIE_TOK_WORD, s);
}
break;

File diff suppressed because it is too large Load Diff