Use new validator function for parse_token calls on all primitives
Also adds new validated primitives for NODE_SECTION and NODE_REGISTER
This commit is contained in:
		| @@ -1,61 +1,84 @@ | |||||||
| #include "parser_primitives.h" | #include "parser_primitives.h" | ||||||
|  | #include "ast.h" | ||||||
|  | #include <string.h> | ||||||
|  |  | ||||||
| parse_result_t parse_identifier(tokenlist_entry_t *current) { | parse_result_t parse_identifier(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_IDENTIFIER, NODE_IDENTIFIER); |     return parse_token(current, TOKEN_IDENTIFIER, NODE_IDENTIFIER, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_decimal(tokenlist_entry_t *current) { | parse_result_t parse_decimal(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_DECIMAL, NODE_DECIMAL); |     return parse_token(current, TOKEN_DECIMAL, NODE_DECIMAL, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_hexadecimal(tokenlist_entry_t *current) { | parse_result_t parse_hexadecimal(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_HEXADECIMAL, NODE_HEXADECIMAL); |     return parse_token(current, TOKEN_HEXADECIMAL, NODE_HEXADECIMAL, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_binary(tokenlist_entry_t *current) { | parse_result_t parse_binary(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_BINARY, NODE_BINARY); |     return parse_token(current, TOKEN_BINARY, NODE_BINARY, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_octal(tokenlist_entry_t *current) { | parse_result_t parse_octal(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_OCTAL, NODE_OCTAL); |     return parse_token(current, TOKEN_OCTAL, NODE_OCTAL, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_string(tokenlist_entry_t *current) { | parse_result_t parse_string(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_STRING, NODE_STRING); |     return parse_token(current, TOKEN_STRING, NODE_STRING, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_char(tokenlist_entry_t *current) { | parse_result_t parse_char(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_CHAR, NODE_CHAR); |     return parse_token(current, TOKEN_CHAR, NODE_CHAR, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_colon(tokenlist_entry_t *current) { | parse_result_t parse_colon(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_COLON, NODE_COLON); |     return parse_token(current, TOKEN_COLON, NODE_COLON, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_comma(tokenlist_entry_t *current) { | parse_result_t parse_comma(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_COMMA, NODE_COMMA); |     return parse_token(current, TOKEN_COMMA, NODE_COMMA, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_lbracket(tokenlist_entry_t *current) { | parse_result_t parse_lbracket(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_LBRACKET, NODE_LBRACKET); |     return parse_token(current, TOKEN_LBRACKET, NODE_LBRACKET, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_rbracket(tokenlist_entry_t *current) { | parse_result_t parse_rbracket(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_RBRACKET, NODE_RBRACKET); |     return parse_token(current, TOKEN_RBRACKET, NODE_RBRACKET, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_plus(tokenlist_entry_t *current) { | parse_result_t parse_plus(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_PLUS, NODE_PLUS); |     return parse_token(current, TOKEN_PLUS, NODE_PLUS, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_minus(tokenlist_entry_t *current) { | parse_result_t parse_minus(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_MINUS, NODE_MINUS); |     return parse_token(current, TOKEN_MINUS, NODE_MINUS, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_asterisk(tokenlist_entry_t *current) { | parse_result_t parse_asterisk(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_ASTERISK, NODE_ASTERISK); |     return parse_token(current, TOKEN_ASTERISK, NODE_ASTERISK, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| parse_result_t parse_dot(tokenlist_entry_t *current) { | parse_result_t parse_dot(tokenlist_entry_t *current) { | ||||||
|     return parse_token(current, TOKEN_DOT, NODE_DOT); |     return parse_token(current, TOKEN_DOT, NODE_DOT, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | const char *registers[] = {"rax", "rcx", "rdx", "rbx", "rsp",  "rbp", | ||||||
|  |                            "rsi", "rdi", "r8",  "r9",  "r10",  "r11", | ||||||
|  |                            "r12", "r13", "r14", "r15", nullptr}; | ||||||
|  | bool is_register_token(lexer_token_t *token) { | ||||||
|  |     for (size_t i = 0; registers[i] != nullptr; ++i) | ||||||
|  |         if (strcmp(token->value, registers[i]) == 0) | ||||||
|  |             return true; | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | parse_result_t parse_register(tokenlist_entry_t *current) { | ||||||
|  |     return parse_token(current, TOKEN_IDENTIFIER, NODE_REGISTER, | ||||||
|  |                        is_register_token); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool is_section_token(lexer_token_t *token) { | ||||||
|  |     return strcmp(token->value, "section") == 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | parse_result_t parse_section(tokenlist_entry_t *current) {} | ||||||
|   | |||||||
| @@ -19,4 +19,11 @@ parse_result_t parse_minus(tokenlist_entry_t *current); | |||||||
| parse_result_t parse_asterisk(tokenlist_entry_t *current); | parse_result_t parse_asterisk(tokenlist_entry_t *current); | ||||||
| parse_result_t parse_dot(tokenlist_entry_t *current); | parse_result_t parse_dot(tokenlist_entry_t *current); | ||||||
|  |  | ||||||
|  | /* These are "primitives" with a different name and some extra validation on top | ||||||
|  |  * for example, register is just an identifier but it only matches a limited set | ||||||
|  |  * of values | ||||||
|  |  */ | ||||||
|  | parse_result_t parse_register(tokenlist_entry_t *current); | ||||||
|  | parse_result_t parse_section(tokenlist_entry_t *current); | ||||||
|  |  | ||||||
| #endif // INCLUDE_SRC_PARSER_PRIMITIVES_H_ | #endif // INCLUDE_SRC_PARSER_PRIMITIVES_H_ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user