From 001ab2916d6a0c1133da4d9ea7f39ba1f5999c26 Mon Sep 17 00:00:00 2001 From: Kyle Lin Date: Tue, 20 Feb 2024 11:09:10 +0800 Subject: [PATCH] Implement parser for path section of '#include' This syntax parser can parse 2 forms of #include, including '#include "header.h"' (string path form) and '#include ' (system defined path form), the former one is guaranteed to be always parsed, however, the later one is not guaranteed will be always successfully parsed due to lack of platform support. --- src/parser.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/parser.c b/src/parser.c index 2825a39b..a2e5843b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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`. */ @@ -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 */ + 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)) {