libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
parse_common.c File Reference
#include "ast.h"
#include "module.h"
#include "parser.h"
#include "types/keywords.h"
Include dependency graph for parse_common.c:

Go to the source code of this file.

Functions

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.
 
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 } adding it as a children on parent AST node.
 
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 parent AST node.
 
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 traveled.
 

Function Documentation

◆ parse_advance()

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 traveled.

This function could be used to skip wrong tokens when a error as found on parsing a statement. So it's avoid a lot of useless error messages.

Parameters
tokenPointer for the first token where to start searching.
typesArray of token types to search.
ntypesSize of types array.

Definition at line 125 of file parse_common.c.

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}
Struct for a Mya token.
Definition token.h:34
@ TK_EOF
Definition token.h:17

◆ parse_bitfield_spec()

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 } adding it as a children on parent AST node.

Parameters
moduleThe module where the AST is.
parentThe AST node parent for the bitfield specification.
tokenThe token where to start parsing the bitfield specification.
Returns
The number of tokens used on the bitfield specification.

Definition at line 47 of file parse_common.c.

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}
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
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_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 length
The length of the string.
Definition dstring.h:13
Struct that represents a Mya module.
Definition module.h:34
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_BITFIELD_SPEC
Definition ast.h:13
struct token token_t
Struct for a Mya token.
@ TK_IDENTIFIER
Definition token.h:19
@ TK_OPEN_BRACES
Definition token.h:22
@ TK_CLOSE_BRACES
Definition token.h:12

◆ parse_fieldlist_spec()

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 parent AST node.

Parameters
moduleThe module where the AST is.
parentThe AST node parent for the field list specification.
tokenThe token where to start parsing the field list specification. It should be the open braces ({).
Returns
The number of tokens used on the field list specification.

Definition at line 106 of file parse_common.c.

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}
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.
@ NT_FIELD_SPEC
Definition ast.h:15
@ TK_COMMA
Definition token.h:16

◆ parse_size_spec()

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.

Parameters
moduleThe module where the AST is.
parentThe AST node parent for the size specification.
tokenThe token where to start parsing the size specification.
exampleA code example printed on error messages when the size spec is syntactically wrong.
Returns
The number of tokens used on the size specification.

Definition at line 7 of file parse_common.c.

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}
const char * mya_keywords[]
Definition globals.c:11
long long int value
Integer value of the token.
Definition token.h:38
@ TK_OPEN_BRACKET
Definition token.h:23
@ TK_CLOSE_BRACKET
Definition token.h:13