Skip to content

Commit d0575ba

Browse files
author
Heitor Danilo
committed
doc: comment block
1 parent 521fe07 commit d0575ba

File tree

1 file changed

+124
-4
lines changed

1 file changed

+124
-4
lines changed

src/parser/def.h

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,136 @@
33

44
#include <ast/def.h>
55
#include <lexer/def.h>
6+
#include <langdef.h>
67
#include <token/def.h>
78

9+
/**
10+
* Represents a node in a linked list of parser errors.
11+
*
12+
* Each node contains a string representing an error message, and a pointer to
13+
* the next error node in the list. This structure is used to track and report
14+
* errors that occur during the parsing process.
15+
*/
16+
struct Parser_error {
17+
/** The error message. */
18+
char *error;
19+
20+
/** A pointer to the next error node in the list. */
21+
struct Parser_error *next;
22+
};
23+
24+
/**
25+
* Represents a parser.
26+
*
27+
* A parser is responsible for transforming a sequence of tokens, produced by a
28+
* lexer, into an abstract syntax tree (AST). This structure contains a lexer,
29+
* the current and next tokens being parsed, functions to consume tokens and
30+
* check token types, and functions to parse different types of statements and
31+
* the whole program.
32+
*/
833
struct Parser {
34+
/** A linked list of error messages produced during the parsing process. */
35+
struct Parser_error *errors;
36+
37+
/** The lexer used to produce tokens for the parser. */
938
struct Lexer *lexer;
1039

11-
struct Token *curr_tok;
12-
struct Token *next_tok;
40+
/** The current token being parsed. */
41+
struct Token *curr_token;
42+
43+
/** The next token to be parsed. */
44+
struct Token *next_token;
45+
46+
/**
47+
* Consumes the current token and advances the lexer.
48+
*
49+
* @param self - The Parser object.
50+
*/
51+
void (*consume_token)(struct Parser *self);
52+
53+
/**
54+
* Checks if the next token's code matches the provided code.
55+
*
56+
* @param self - The Parser object.
57+
* @param code - The code to be compared with the next token's code.
58+
*
59+
* @return 1 if the codes match, 0 otherwise.
60+
*/
61+
int (*next_token_is)(struct Parser *self, uint8 code);
1362

14-
void (*next_token)(struct Parser *self);
63+
/**
64+
* Checks if the current token's code matches the provided code.
65+
*
66+
* @param self - The Parser object.
67+
* @param code - The code to be compared with the current token's code.
68+
*
69+
* @return 1 if the codes match, 0 otherwise.
70+
*/
71+
int (*curr_token_is)(struct Parser *self, uint8 code);
72+
73+
/**
74+
* Consumes the next token if its code matches the provided code, or does nothing otherwise.
75+
*
76+
* @param self - The Parser object.
77+
* @param code - The code to be compared with the next token's code.
78+
*
79+
* @return 1 if the next token was consumed, 0 otherwise.
80+
*/
81+
int (*consume_or_ignore_if_next_token_is)(struct Parser *self, uint8 code);
82+
83+
/**
84+
* Parses a program, which is a series of statements until the end of file token.
85+
*
86+
* This function creates a new program object and continuously parses statements,
87+
* adding them to the program, until it reaches the end of file token. It consumes
88+
* a token after each statement is parsed.
89+
*
90+
* @param self The parser object.
91+
*
92+
* @return The parsed program.
93+
*/
1594
struct Program *(*parse_program)(struct Parser *self);
95+
96+
/**
97+
* Parses a statement based on the type of the current token.
98+
*
99+
* If the current token represents a variable declaration, it calls the function to
100+
* parse variable declarations. If it's a return statement, it calls the function to
101+
* parse return statements. Otherwise, it assumes the statement is an expression
102+
* statement and calls the function to parse expression statements.
103+
*
104+
* @param self The parser object.
105+
*
106+
* @return The parsed statement.
107+
*/
108+
struct Statement *(*parse_statement)(struct Parser *self);
109+
110+
/**
111+
* Parses a variable declaration statement from the current and following tokens.
112+
*
113+
* This function creates a new variable statement and fills its properties based on
114+
* the current and following tokens. Errors are pushed to the parser's error list if
115+
* the expected tokens are not found. It keeps consuming tokens until a semicolon is found.
116+
*
117+
* @param self The parser object.
118+
*
119+
* @return The parsed variable statement, or NULL if an error occurs.
120+
*/
121+
struct Statement *(*parse_var_statement)(struct Parser *self);
122+
123+
/**
124+
* Parses a return statement from the current and following tokens.
125+
*
126+
* This function creates a new return statement and fills its properties based on
127+
* the current state of the parser. It keeps consuming tokens until a semicolon is found.
128+
*
129+
* @param self The parser object.
130+
*
131+
* @return The parsed return statement.
132+
*/
133+
struct Statement *(*parse_return_statement)(struct Parser *self);
134+
135+
struct Statement *(*parse_expression_statement)(struct Parser *self);
16136
};
17137

18-
#endif /* PARSER_DEFINITION_H */
138+
#endif /* PARSER_DEFINITION_H */

0 commit comments

Comments
 (0)