feat(lex_parser): rename functions and update header guard prefix

- Change header guard from `__SMCC_LEX_PARSER_H__` to `__SCC_LEX_PARSER_H__`
- Prefix all lexer functions with `scc_` (e.g., `lex_parse_char` → `scc_lex_parse_char`)
- Add new helper function `scc_lex_parse_is_identifier_header`
- Update references in source and test files to use new function names
- Replace `core_stream_eof` with `scc_stream_eof` for consistency
This commit is contained in:
zzy
2025-12-13 14:06:13 +08:00
parent 94d3f46fac
commit 874a58281f
17 changed files with 98 additions and 92 deletions

View File

@@ -9,11 +9,11 @@
#include "lexer_token.h"
#include <libcore.h>
typedef struct lexer_token {
typedef struct scc_lexer_token {
scc_tok_type_t type;
scc_cvalue_t value;
scc_pos_t loc;
} lexer_tok_t;
} scc_lexer_tok_t;
/**
* @brief 词法分析器核心结构体
@@ -39,7 +39,7 @@ void scc_lexer_init(scc_lexer_t *lexer, scc_probe_stream_t *stream);
*
* 此函数会返回所有类型的token包括空白符等无效token
*/
void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token);
void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token);
/**
* @brief 获取有效token
@@ -48,6 +48,6 @@ void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token);
*
* 此函数会自动跳过空白符等无效token返回对语法分析有意义的token
*/
void scc_lexer_get_valid_token(scc_lexer_t *lexer, lexer_tok_t *token);
void scc_lexer_get_valid_token(scc_lexer_t *lexer, scc_lexer_tok_t *token);
#endif /* __SCC_LEXER_H__ */

View File

@@ -84,7 +84,7 @@ void scc_lexer_init(scc_lexer_t *lexer, scc_probe_stream_t *stream) {
#define set_err_token(token) ((token)->type = SCC_TOK_UNKNOWN)
static void parse_line(scc_lexer_t *lexer, lexer_tok_t *token) {
static void parse_line(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
token->loc = lexer->pos;
scc_probe_stream_t *stream = lexer->stream;
scc_probe_stream_reset(stream);
@@ -93,7 +93,7 @@ static void parse_line(scc_lexer_t *lexer, lexer_tok_t *token) {
usize n;
scc_cstring_t str = scc_cstring_new();
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LEX_WARN("Unexpected EOF at begin");
goto ERR;
} else if (ch != '#') {
@@ -113,13 +113,13 @@ static void parse_line(scc_lexer_t *lexer, lexer_tok_t *token) {
}
}
if (lex_parse_number(lexer->stream, &lexer->pos, &n) == false) {
if (scc_lex_parse_number(lexer->stream, &lexer->pos, &n) == false) {
LEX_ERROR("Invalid line number");
goto SKIP_LINE;
}
if (scc_probe_stream_consume(stream) != ' ') {
lex_parse_skip_line(lexer->stream, &lexer->pos);
scc_lex_parse_skip_line(lexer->stream, &lexer->pos);
token->loc.line = token->value.n;
}
@@ -127,26 +127,26 @@ static void parse_line(scc_lexer_t *lexer, lexer_tok_t *token) {
LEX_ERROR("Invalid `#` line");
goto SKIP_LINE;
}
if (lex_parse_string(lexer->stream, &lexer->pos, &str) == false) {
if (scc_lex_parse_string(lexer->stream, &lexer->pos, &str) == false) {
LEX_ERROR("Invalid filename");
goto SKIP_LINE;
}
lex_parse_skip_line(lexer->stream, &lexer->pos);
scc_lex_parse_skip_line(lexer->stream, &lexer->pos);
token->loc.line = n;
// FIXME memory leak
token->loc.name = scc_cstring_from_cstr(scc_cstring_as_cstr(&str));
scc_cstring_free(&str);
return;
SKIP_LINE:
lex_parse_skip_line(lexer->stream, &lexer->pos);
scc_lex_parse_skip_line(lexer->stream, &lexer->pos);
ERR:
set_err_token(token);
scc_cstring_free(&str);
}
// /zh/c/language/operator_arithmetic.html
void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token) {
void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
token->loc = lexer->pos;
token->type = SCC_TOK_UNKNOWN;
scc_probe_stream_t *stream = lexer->stream;
@@ -212,11 +212,11 @@ void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token) {
type = SCC_TOK_ASSIGN_DIV;
goto double_char;
case '/':
lex_parse_skip_line(lexer->stream, &lexer->pos);
scc_lex_parse_skip_line(lexer->stream, &lexer->pos);
token->type = SCC_TOK_LINE_COMMENT;
goto END;
case '*':
lex_parse_skip_block_comment(lexer->stream, &lexer->pos);
scc_lex_parse_skip_block_comment(lexer->stream, &lexer->pos);
token->type = SCC_TOK_BLOCK_COMMENT;
goto END;
default:
@@ -369,7 +369,7 @@ void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token) {
break;
case '\r':
case '\n':
lex_parse_skip_endline(lexer->stream, &lexer->pos);
scc_lex_parse_skip_endline(lexer->stream, &lexer->pos);
token->type = SCC_TOK_BLANK;
goto END;
case '#':
@@ -377,15 +377,15 @@ void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token) {
token->type = SCC_TOK_BLANK;
goto END;
case '\0':
case core_stream_eof:
case scc_stream_eof:
// EOF
type = SCC_TOK_EOF;
break;
case '\'': {
token->loc = lexer->pos;
token->type = SCC_TOK_CHAR_LITERAL;
int ch = lex_parse_char(lexer->stream, &lexer->pos);
if (ch == core_stream_eof) {
int ch = scc_lex_parse_char(lexer->stream, &lexer->pos);
if (ch == scc_stream_eof) {
LEX_ERROR("Unexpected character literal");
token->type = SCC_TOK_UNKNOWN;
} else {
@@ -397,7 +397,7 @@ void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token) {
token->loc = lexer->pos;
token->type = SCC_TOK_STRING_LITERAL;
scc_cstring_t output = scc_cstring_new();
if (lex_parse_string(lexer->stream, &lexer->pos, &output) == true) {
if (scc_lex_parse_string(lexer->stream, &lexer->pos, &output) == true) {
token->value.cstr.data = scc_cstring_as_cstr(&output);
token->value.cstr.len = scc_cstring_len(&output);
} else {
@@ -414,7 +414,7 @@ void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token) {
token->loc = lexer->pos;
token->type = SCC_TOK_INT_LITERAL;
usize output;
if (lex_parse_number(lexer->stream, &lexer->pos, &output) == true) {
if (scc_lex_parse_number(lexer->stream, &lexer->pos, &output) == true) {
token->value.n = output;
} else {
LEX_ERROR("Unexpected number literal");
@@ -432,7 +432,7 @@ void scc_lexer_get_token(scc_lexer_t *lexer, lexer_tok_t *token) {
case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_':
/* clang-format on */
scc_cstring_t str = scc_cstring_new();
cbool ret = lex_parse_identifier(lexer->stream, &lexer->pos, &str);
cbool ret = scc_lex_parse_identifier(lexer->stream, &lexer->pos, &str);
Assert(ret == true);
int res = keyword_cmp(scc_cstring_as_cstr(&str), scc_cstring_len(&str));
@@ -467,7 +467,7 @@ END:
}
// scc_lexer_get_token maybe got invalid (with parser)
void scc_lexer_get_valid_token(scc_lexer_t *lexer, lexer_tok_t *token) {
void scc_lexer_get_valid_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
scc_tok_subtype_t type;
do {
scc_lexer_get_token(lexer, token);

View File

@@ -7,7 +7,7 @@
static inline void test_lexer_string(const char *input,
scc_tok_type_t expected_type) {
scc_lexer_t lexer;
lexer_tok_t token;
scc_lexer_tok_t token;
scc_mem_probe_stream_t stream;
scc_lexer_init(&lexer, scc_mem_probe_stream_init(&stream, input,

View File

@@ -70,11 +70,11 @@ int main(int argc, char *argv[]) {
scc_cstring_clear(&stream->name);
scc_cstring_append_cstr(&stream->name, file_name, strlen(file_name));
scc_lexer_init(&lexer, stream);
lexer_tok_t tok;
scc_lexer_tok_t tok;
while (1) {
scc_lexer_get_valid_token(&lexer, &tok);
if (tok.type == SCC_TOK_EOF) {
if (tok.type == SCC_TOK_EOF) {
break;
}
LOG_DEBUG("token `%s` at %s:%u:%u", scc_get_tok_name(tok.type),