libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
parse_common.c
Go to the documentation of this file.
1#include "ast.h"
2#include "module.h"
3#include "parser.h"
4#include "types/keywords.h"
5
6unsigned int
7parse_size_spec(module_t* module, ast_node_t* parent, token_t* token, const char* example)
8{
9 char message[128];
10 token_t* tkopen_bracket = token;
11 token_t* tkexpr = token + 1;
12
13 if (tkopen_bracket->type != TK_OPEN_BRACKET) {
14 snprintf(
15 message,
16 sizeof message - 1,
17 "Expected a size specifier after the %s name. Example: %s",
19 example
20 );
21
22 module_add_error(module, tkopen_bracket->line, tkopen_bracket->column, tkopen_bracket->lexeme.length, message);
23
24 return 2;
25 }
26
27 unsigned int ntokens = parse_expression(module, parent, tkexpr);
28
29 token_t* tkclose_bracket = token + ntokens + 1;
30
31 if (tkclose_bracket->type != TK_CLOSE_BRACKET) {
32 snprintf(message, sizeof message - 1, "Expected a close bracket here. Example: %s", example);
33
35 module,
36 tkclose_bracket->line,
37 tkclose_bracket->column,
38 tkclose_bracket->lexeme.length,
39 "Expected a close bracket here. Example: bitfield Name[4]"
40 );
41 }
42
43 return ntokens + 2;
44}
45
46unsigned int
48{
49 ast_node_t* node_statement = ast_add_children(parent, NT_BITFIELD_SPEC, token);
50 token_t* tkid = token;
51 token_t* tkopen_braces = token + 1;
52
53 if (tkid->type != TK_IDENTIFIER) {
55 module,
56 tkid->line,
57 tkid->column,
58 tkid->lexeme.length,
59 "Expected a bitfield identifier here. Example: Reg{1}"
60 );
61
62 return 1;
63 }
64
65 if (tkopen_braces->type != TK_OPEN_BRACES) {
67 module,
68 tkopen_braces->line,
69 tkopen_braces->column,
70 tkopen_braces->lexeme.length,
71 "Expected a open braces after bitfield name. Examples:\n"
72 " Reg{1}\n"
73 "\n"
74 " Opcode {\n"
75 " opcode = 0x01,\n"
76 " reg = Reg{arg1}\n"
77 " }"
78 );
79
80 return 2;
81 }
82
83 unsigned int ntokens = 1 + parse_fieldlist_spec(module, node_statement, tkopen_braces);
84
85 token_t* tkclose_braces = token + ntokens - 1;
86 if (tkclose_braces->type != TK_CLOSE_BRACES) {
88 module,
89 tkclose_braces->line,
90 tkclose_braces->column,
91 tkclose_braces->lexeme.length,
92 "Expected a close braces at end of field specification list. Examples:\n"
93 " Reg{1}\n"
94 "\n"
95 " Opcode {\n"
96 " opcode = 0x01,\n"
97 " reg = Reg{arg1},\n"
98 " }"
99 );
100 }
101
102 return ntokens;
103}
104
105unsigned int
107{
108 ast_node_t* node_statement = ast_add_children(parent, NT_FIELD_SPEC, token);
109 unsigned int ntokens = 0;
110
111 do {
112 ntokens++;
113
114 if (token[ntokens].type == TK_CLOSE_BRACES) {
115 break;
116 }
117
118 ntokens += parse_expression(module, node_statement, &token[ntokens]);
119 } while (token[ntokens].type == TK_COMMA);
120
121 return ntokens + 1;
122}
123
124unsigned int
125parse_advance(token_t* token, token_type_t* types, size_t ntypes)
126{
127 int ntokens = 0;
128
129 while (token[ntokens].type != TK_EOF) {
130 for (int i = 0; i < ntypes; i++) {
131 if (token[ntokens].type == types[i]) {
132 return ntokens;
133 }
134 }
135
136 ntokens++;
137 }
138
139 return ntokens;
140}
ast_node_t * ast_add_children(ast_node_t *parent, node_type_t type, token_t *token)
Add a new children for the given AST node.
Definition ast.c:40
const char * mya_keywords[]
Definition globals.c:11
void module_add_error(module_t *module, unsigned int line, unsigned int column, unsigned int length, const char *message)
Add error for the given module.
Definition module.c:97
unsigned int parse_advance(token_t *token, token_type_t *types, size_t ntypes)
Finds the next token TK_EOF or any of the specified types and then returns the number of tokens trave...
unsigned int parse_size_spec(module_t *module, ast_node_t *parent, token_t *token, const char *example)
Parse a size specification in the format [ EXPRESSION ] adding it as a children on parent AST node.
Definition parse_common.c:7
unsigned int parse_bitfield_spec(module_t *module, ast_node_t *parent, token_t *token)
Parse a bitfield specification in the format Bitfield { FIELD_LIST } or Bitfield { EXPRESSION }...
unsigned int parse_fieldlist_spec(module_t *module, ast_node_t *parent, token_t *token)
Parse a field list specification in the format field = EXPRESSION, ... adding it as a children on par...
unsigned int parse_expression(module_t *module, ast_node_t *parent, token_t *token)
Parse a mathematical expression adding it as a children on parent AST node.
unsigned int length
The length of the string.
Definition dstring.h:13
Struct that represents a Mya module.
Definition module.h:34
Struct for a Mya token.
Definition token.h:34
long long int value
Integer value of the token.
Definition token.h:38
token_type_t type
Token type.
Definition token.h:35
dstring_t lexeme
Lexeme of the token.
Definition token.h:41
unsigned int line
Token line inside the module.
Definition token.h:36
unsigned int column
Column of the token position on the line.
Definition token.h:37
struct ast_node ast_node_t
@ NT_FIELD_SPEC
Definition ast.h:15
@ NT_BITFIELD_SPEC
Definition ast.h:13
struct module module_t
Struct that represents a Mya module.
enum token_type token_type_t
Enumeration of token types.
struct token token_t
Struct for a Mya token.
@ TK_OPEN_BRACKET
Definition token.h:23
@ TK_IDENTIFIER
Definition token.h:19
@ TK_OPEN_BRACES
Definition token.h:22
@ TK_COMMA
Definition token.h:16
@ TK_CLOSE_BRACKET
Definition token.h:13
@ TK_EOF
Definition token.h:17
@ TK_CLOSE_BRACES
Definition token.h:12