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

@@ -1,26 +1,31 @@
#ifndef __SMCC_LEX_PARSER_H__
#define __SMCC_LEX_PARSER_H__
#ifndef __SCC_LEX_PARSER_H__
#define __SCC_LEX_PARSER_H__
#include <libcore.h>
static inline cbool lex_parse_is_endline(int ch) {
static inline cbool scc_lex_parse_is_endline(int ch) {
return ch == '\n' || ch == '\r';
}
static inline cbool lex_parse_is_whitespace(int ch) {
static inline cbool scc_lex_parse_is_whitespace(int ch) {
return ch == ' ' || ch == '\t';
}
int lex_parse_char(scc_probe_stream_t *input, scc_pos_t *pos);
cbool lex_parse_string(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output);
cbool lex_parse_number(scc_probe_stream_t *input, scc_pos_t *pos,
usize *output);
cbool lex_parse_identifier(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output);
void lex_parse_skip_endline(scc_probe_stream_t *input, scc_pos_t *pos);
void lex_parse_skip_block_comment(scc_probe_stream_t *input, scc_pos_t *pos);
void lex_parse_skip_line(scc_probe_stream_t *input, scc_pos_t *pos);
void lex_parse_skip_whitespace(scc_probe_stream_t *input, scc_pos_t *pos);
static inline cbool scc_lex_parse_is_identifier_header(int ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
}
#endif /* __SMCC_LEX_PARSER_H__ */
int scc_lex_parse_char(scc_probe_stream_t *input, scc_pos_t *pos);
cbool scc_lex_parse_string(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output);
cbool scc_lex_parse_number(scc_probe_stream_t *input, scc_pos_t *pos,
usize *output);
cbool scc_lex_parse_identifier(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output);
void scc_lex_parse_skip_endline(scc_probe_stream_t *input, scc_pos_t *pos);
void scc_lex_parse_skip_block_comment(scc_probe_stream_t *input,
scc_pos_t *pos);
void scc_lex_parse_skip_line(scc_probe_stream_t *input, scc_pos_t *pos);
void scc_lex_parse_skip_whitespace(scc_probe_stream_t *input, scc_pos_t *pos);
#endif /* __SCC_LEX_PARSER_H__ */

View File

@@ -1,6 +1,6 @@
#include <lex_parser.h>
void lex_parse_skip_endline(scc_probe_stream_t *input, scc_pos_t *pos) {
void scc_lex_parse_skip_endline(scc_probe_stream_t *input, scc_pos_t *pos) {
Assert(input != null && pos != null);
scc_probe_stream_reset(input);
int ch = scc_probe_stream_peek(input);
@@ -57,20 +57,20 @@ static inline int got_simple_escape(int ch) {
/* clang-format on */
}
void lex_parse_skip_line(scc_probe_stream_t *input, scc_pos_t *pos) {
void scc_lex_parse_skip_line(scc_probe_stream_t *input, scc_pos_t *pos) {
scc_probe_stream_t *stream = input;
Assert(stream != null && pos != null);
scc_probe_stream_reset(stream);
while (1) {
int ch = scc_probe_stream_peek(stream);
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
return;
}
// TODO endline
if (lex_parse_is_endline(ch)) {
lex_parse_skip_endline(stream, pos);
if (scc_lex_parse_is_endline(ch)) {
scc_lex_parse_skip_endline(stream, pos);
return;
} else {
scc_probe_stream_consume(stream);
@@ -79,7 +79,8 @@ void lex_parse_skip_line(scc_probe_stream_t *input, scc_pos_t *pos) {
}
}
void lex_parse_skip_block_comment(scc_probe_stream_t *input, scc_pos_t *pos) {
void scc_lex_parse_skip_block_comment(scc_probe_stream_t *input,
scc_pos_t *pos) {
scc_probe_stream_t *stream = input;
Assert(stream != null && pos != null);
int ch;
@@ -97,13 +98,13 @@ void lex_parse_skip_block_comment(scc_probe_stream_t *input, scc_pos_t *pos) {
scc_probe_stream_reset(stream);
ch = scc_probe_stream_peek(stream);
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LOG_WARN("Unterminated block comment");
return;
}
if (lex_parse_is_endline(ch)) {
lex_parse_skip_endline(stream, pos);
if (scc_lex_parse_is_endline(ch)) {
scc_lex_parse_skip_endline(stream, pos);
continue;
}
scc_probe_stream_consume(stream);
@@ -119,14 +120,14 @@ void lex_parse_skip_block_comment(scc_probe_stream_t *input, scc_pos_t *pos) {
}
}
void lex_parse_skip_whitespace(scc_probe_stream_t *input, scc_pos_t *pos) {
void scc_lex_parse_skip_whitespace(scc_probe_stream_t *input, scc_pos_t *pos) {
scc_probe_stream_t *stream = input;
Assert(stream != null && pos != null);
scc_probe_stream_reset(stream);
while (1) {
int ch = scc_probe_stream_peek(stream);
if (!lex_parse_is_whitespace(ch)) {
if (!scc_lex_parse_is_whitespace(ch)) {
return;
}
@@ -149,7 +150,7 @@ static inline cbool _lex_parse_uint(scc_probe_stream_t *input, scc_pos_t *pos,
while (1) {
ch = scc_probe_stream_peek(input);
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
break;
} else if (ch >= 'a' && ch <= 'z') {
tmp = ch - 'a' + 10;
@@ -187,14 +188,14 @@ static inline cbool _lex_parse_uint(scc_probe_stream_t *input, scc_pos_t *pos,
* @return int
* https://cppreference.cn/w/c/language/character_constant
*/
int lex_parse_char(scc_probe_stream_t *input, scc_pos_t *pos) {
int scc_lex_parse_char(scc_probe_stream_t *input, scc_pos_t *pos) {
scc_probe_stream_t *stream = input;
Assert(stream != null && pos != null);
scc_probe_stream_reset(stream);
int ch = scc_probe_stream_peek(stream);
int ret = core_stream_eof;
int ret = scc_stream_eof;
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
goto ERR;
} else if (ch != '\'') {
@@ -207,7 +208,7 @@ int lex_parse_char(scc_probe_stream_t *input, scc_pos_t *pos) {
ch = scc_probe_stream_consume(stream);
scc_pos_next(pos);
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LOG_WARN("Unexpected EOF at middle");
goto ERR;
} else if (ch == '\\') {
@@ -245,7 +246,7 @@ int lex_parse_char(scc_probe_stream_t *input, scc_pos_t *pos) {
return ret;
ERR:
return core_stream_eof;
return scc_stream_eof;
}
/**
@@ -257,15 +258,15 @@ ERR:
* @return cbool
* https://cppreference.cn/w/c/language/string_literal
*/
cbool lex_parse_string(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output) {
cbool scc_lex_parse_string(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output) {
scc_probe_stream_t *stream = input;
Assert(stream != null && pos != null && output != null);
scc_probe_stream_reset(stream);
int ch = scc_probe_stream_peek(stream);
Assert(scc_cstring_is_empty(output));
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
goto ERR;
} else if (ch != '"') {
@@ -279,10 +280,10 @@ cbool lex_parse_string(scc_probe_stream_t *input, scc_pos_t *pos,
while (1) {
ch = scc_probe_stream_peek(stream);
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LOG_ERROR("Unexpected EOF at string literal");
goto ERR;
} else if (lex_parse_is_endline(ch)) {
} else if (scc_lex_parse_is_endline(ch)) {
LOG_ERROR("Unexpected newline at string literal");
goto ERR;
} else if (ch == '\\') {
@@ -323,15 +324,15 @@ ERR:
* @return cbool
* https://cppreference.cn/w/c/language/integer_constant
*/
cbool lex_parse_number(scc_probe_stream_t *input, scc_pos_t *pos,
usize *output) {
cbool scc_lex_parse_number(scc_probe_stream_t *input, scc_pos_t *pos,
usize *output) {
scc_probe_stream_t *stream = input;
Assert(stream != null && pos != null && output != null);
scc_probe_stream_reset(stream);
int ch = scc_probe_stream_peek(stream);
int base = 10; // 默认十进制
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
goto ERR;
}
@@ -406,15 +407,15 @@ ERR:
* @return cbool
* https://cppreference.cn/w/c/language/identifier
*/
cbool lex_parse_identifier(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output) {
cbool scc_lex_parse_identifier(scc_probe_stream_t *input, scc_pos_t *pos,
scc_cstring_t *output) {
Assert(input != null && pos != null && output != null);
Assert(scc_cstring_is_empty(output));
scc_probe_stream_t *stream = input;
scc_probe_stream_reset(stream);
int ch = scc_probe_stream_peek(stream);
if (ch == core_stream_eof) {
if (ch == scc_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
} else if (ch == '_' || (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z')) {

View File

@@ -8,7 +8,7 @@ cbool check_char(const char *str, int expect, int *output) {
scc_mem_probe_stream_t mem_stream;
scc_probe_stream_t *stream =
scc_mem_probe_stream_init(&mem_stream, str, scc_strlen(str), false);
*output = lex_parse_char(stream, &pos);
*output = scc_lex_parse_char(stream, &pos);
return *output == expect;
}
@@ -22,8 +22,8 @@ cbool check_char(const char *str, int expect, int *output) {
#define CHECK_CHAR_INVALID(str) \
do { \
int _output; \
check_char(str, core_stream_eof, &_output); \
TEST_CHECK(_output == core_stream_eof); \
check_char(str, scc_stream_eof, &_output); \
TEST_CHECK(_output == scc_stream_eof); \
} while (0)
void test_simple_char(void) {

View File

@@ -10,7 +10,7 @@ cbool check_identifier(const char *str, const char *expect,
scc_probe_stream_t *stream =
scc_mem_probe_stream_init(&mem_stream, str, scc_strlen(str), false);
cbool ret = lex_parse_identifier(stream, &pos, output);
cbool ret = scc_lex_parse_identifier(stream, &pos, output);
if (ret && expect) {
return strcmp(output->data, expect) == 0;
}

View File

@@ -9,7 +9,7 @@ cbool check(const char *str, usize expect, usize *output) {
scc_mem_probe_stream_t mem_stream;
scc_probe_stream_t *stream =
scc_mem_probe_stream_init(&mem_stream, str, scc_strlen(str), false);
return lex_parse_number(stream, &pos, output);
return scc_lex_parse_number(stream, &pos, output);
}
#define CHECK_VALID(str, expect) \

View File

@@ -9,13 +9,13 @@ void check_skip_block_comment(const char *str, const char *expect_remaining) {
scc_probe_stream_t *stream =
scc_mem_probe_stream_init(&mem_stream, str, scc_strlen(str), false);
lex_parse_skip_block_comment(stream, &pos);
scc_lex_parse_skip_block_comment(stream, &pos);
// Check remaining content
char buffer[256] = {0};
int i = 0;
int ch;
while ((ch = scc_probe_stream_consume(stream)) != core_stream_eof &&
while ((ch = scc_probe_stream_consume(stream)) != scc_stream_eof &&
i < 255) {
buffer[i++] = (char)ch;
}

View File

@@ -9,13 +9,13 @@ void check_skip_line(const char *str, const char *expect_remaining) {
scc_probe_stream_t *stream =
scc_mem_probe_stream_init(&mem_stream, str, scc_strlen(str), false);
lex_parse_skip_line(stream, &pos);
scc_lex_parse_skip_line(stream, &pos);
// Check remaining content
char buffer[256] = {0};
int i = 0;
int ch;
while ((ch = scc_probe_stream_consume(stream)) != core_stream_eof &&
while ((ch = scc_probe_stream_consume(stream)) != scc_stream_eof &&
i < 255) {
buffer[i++] = (char)ch;
}

View File

@@ -9,7 +9,7 @@ cbool check_string(const char *str, const char *expect, scc_cstring_t *output) {
scc_probe_stream_t *stream =
scc_mem_probe_stream_init(&mem_stream, str, scc_strlen(str), false);
cbool ret = lex_parse_string(stream, &pos, output);
cbool ret = scc_lex_parse_string(stream, &pos, output);
if (ret && expect) {
return strcmp(output->data, expect) == 0;
}