Compare commits

..

8 Commits

Author SHA1 Message Date
2474e0c773 Slightly change the valid test input file
All checks were successful
Validate the build / validate-build (push) Successful in 27s
2025-04-02 12:33:36 +02:00
6b840ad888 add functionality to main to parse and print the ast 2025-04-02 12:33:36 +02:00
b1391b91bd Partial parser implementation 2025-04-02 12:33:36 +02:00
c9b29e10e8 Fix incorrect size comparison in lexer_consume_n
The buffer length len and the requested number of tokens n are mixed up
in an invalid comparison. This causes all valid requests for n < len
tokens to be denied and all invalid requests for n > len tokens to be
accepted. This may cause a buffer overflow if the caller requests more
characters than they provide space for.
2025-04-02 12:33:30 +02:00
0bc6aaa6af Add a parser combinator to parse a delimited list 2025-04-02 12:24:26 +02:00
68c0f7e525 Add basic parser combinators 2025-04-02 12:24:26 +02:00
5cbb7c85da Add "primitive" parsers for all the non-trivia tokens in the lexer grammar 2025-04-02 12:24:11 +02:00
e2fa229c1d Add basic parser utilities 2025-04-02 12:04:36 +02:00
10 changed files with 34 additions and 37 deletions

View File

@ -1,6 +1,6 @@
#include "error.h" #include "error.h"
#include "lexer.h" #include "lexer.h"
#include "parser/parser.h" #include "parser.h"
#include "tokenlist.h" #include "tokenlist.h"
#include <limits.h> #include <limits.h>

View File

@ -1,10 +1,10 @@
#include "parser.h" #include "parser.h"
#include "../ast.h" #include "ast.h"
#include "../lexer.h" #include "lexer.h"
#include "../tokenlist.h" #include "parser_combinators.h"
#include "combinators.h" #include "parser_primitives.h"
#include "primitives.h" #include "parser_util.h"
#include "util.h" #include "tokenlist.h"
parse_result_t parse_number(tokenlist_entry_t *current) { parse_result_t parse_number(tokenlist_entry_t *current) {
parser_t parsers[] = {parse_octal, parse_decimal, parse_hexadecimal, parser_t parsers[] = {parse_octal, parse_decimal, parse_hexadecimal,

11
src/parser.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef INCLUDE_SRC_PARSER_H_
#define INCLUDE_SRC_PARSER_H_
#include "ast.h"
#include "error.h"
#include "parser_util.h"
#include "tokenlist.h"
parse_result_t parse(tokenlist_entry_t *current);
#endif // INCLUDE_SRC_PARSER_H_

View File

@ -1,9 +0,0 @@
#ifndef INCLUDE_PARSER_PARSER_H_
#define INCLUDE_PARSER_PARSER_H_
#include "../tokenlist.h"
#include "util.h"
parse_result_t parse(tokenlist_entry_t *current);
#endif // INCLUDE_PARSER_PARSER_H_

View File

@ -1,4 +1,4 @@
#include "combinators.h" #include "parser_combinators.h"
// Parse a list of the given parser delimited by the given token id. Does not // Parse a list of the given parser delimited by the given token id. Does not
// store the delimiters in the parent node // store the delimiters in the parent node

View File

@ -1,7 +1,4 @@
#ifndef INCLUDE_PARSER_COMBINATORS_H_ #include "parser_util.h"
#define INCLUDE_PARSER_COMBINATORS_H_
#include "util.h"
typedef parse_result_t (*parser_t)(tokenlist_entry_t *); typedef parse_result_t (*parser_t)(tokenlist_entry_t *);
@ -21,5 +18,3 @@ parse_result_t parse_list(tokenlist_entry_t *current, node_id_t id,
// wraps the parsed nodes in a new parent node. // wraps the parsed nodes in a new parent node.
parse_result_t parse_consecutive(tokenlist_entry_t *current, node_id_t id, parse_result_t parse_consecutive(tokenlist_entry_t *current, node_id_t id,
parser_t parsers[]); parser_t parsers[]);
#endif // INCLUDE_PARSER_COMBINATORS_H_

View File

@ -1,5 +1,5 @@
#include "primitives.h" #include "parser_primitives.h"
#include "../ast.h" #include "ast.h"
#include <string.h> #include <string.h>
parse_result_t parse_identifier(tokenlist_entry_t *current) { parse_result_t parse_identifier(tokenlist_entry_t *current) {

View File

@ -1,7 +1,7 @@
#ifndef INCLUDE_PARSER_PRIMITIVES_H_ #ifndef INCLUDE_SRC_PARSER_PRIMITIVES_H_
#define INCLUDE_PARSER_PRIMITIVES_H_ #define INCLUDE_SRC_PARSER_PRIMITIVES_H_
#include "util.h" #include "parser_util.h"
parse_result_t parse_identifier(tokenlist_entry_t *current); parse_result_t parse_identifier(tokenlist_entry_t *current);
parse_result_t parse_decimal(tokenlist_entry_t *current); parse_result_t parse_decimal(tokenlist_entry_t *current);
@ -26,4 +26,4 @@ parse_result_t parse_dot(tokenlist_entry_t *current);
parse_result_t parse_register(tokenlist_entry_t *current); parse_result_t parse_register(tokenlist_entry_t *current);
parse_result_t parse_section(tokenlist_entry_t *current); parse_result_t parse_section(tokenlist_entry_t *current);
#endif // INCLUDE_PARSER_PRIMITIVES_H_ #endif // INCLUDE_SRC_PARSER_PRIMITIVES_H_

View File

@ -1,5 +1,5 @@
#include "util.h" #include "parser_util.h"
#include "../tokenlist.h" #include "tokenlist.h"
error_t *err_parse_no_match = error_t *err_parse_no_match =
&(error_t){.message = "parsing failed to find the correct token sequence"}; &(error_t){.message = "parsing failed to find the correct token sequence"};

View File

@ -1,9 +1,9 @@
#ifndef INCLUDE_PARSER_UTIL_H_ #ifndef INCLUDE_SRC_PARSER_UTIL_H_
#define INCLUDE_PARSER_UTIL_H_ #define INCLUDE_SRC_PARSER_UTIL_H_
#include "../ast.h" #include "ast.h"
#include "../error.h" #include "error.h"
#include "../tokenlist.h" #include "tokenlist.h"
typedef struct parse_result { typedef struct parse_result {
error_t *err; error_t *err;
@ -24,4 +24,4 @@ tokenlist_entry_t *skip_insignificant(tokenlist_entry_t *);
extern error_t *err_parse_no_match; extern error_t *err_parse_no_match;
#endif // INCLUDE_PARSER_UTIL_H_ #endif // INCLUDE_SRC_PARSER_UTIL_H_