Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement parser for #include preprocessor directive #110

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,6 @@ int get_size(var_t *var, type_t *type)
return type->size;
}

/* abort when invalidate is true and the line contains character other than
* whitespace */
void skip_line(int invalidate)
{
/* FIXME: Comments will causes current validation failed. */
skip_whitespace();
do {
if (invalidate && !is_whitespace(peek_char(0)) &&
!is_newline(peek_char(0))) {
error("Expects whitespace after preprocessor directive");
}
} while (read_char(0) != '\n');
}

/* Skips lines where preprocessor match is false, this will stop once next
* token is either `T_cppd_elif`, `T_cppd_else` or `cppd_endif`.
*/
Expand Down Expand Up @@ -114,10 +100,23 @@ int read_preproc_directive()
char token[MAX_ID_LEN];

if (lex_peek(T_cppd_include, token)) {
skip_line(0); /* FIXME: remove this line after syntax parsing is
implemented */
lex_expect(T_cppd_include);
/* TODO: parse include syntax here */

/* Basic #define syntax validation */
if (lex_peek(T_string, NULL)) {
/* #define "header.h" */
lex_expect(T_string);
} else {
/* #define <stdlib.h> */
lex_expect(T_lt);

while (!lex_peek(T_gt, NULL)) {
next_token = lex_token();
}

lex_expect(T_gt);
}

return 1;
}
if (lex_accept(T_cppd_define)) {
Expand Down
Loading