feat(lex_parser): 初始化词法解析器模块

新增词法解析器库 `smcc_lex_parser`,包含基础的词法规则解析功能:
- 支持字符、字符串、数字、标识符的解析
- 支持跳过注释、空白符、行尾等辅助函数
- 提供对应的单元测试用例,覆盖各类合法与非法输入情况

该模块依赖 `libcore`,并被 `smcc_lex` 模块引用以支持更上层的词法分析逻辑。
This commit is contained in:
zzy
2025-11-23 22:53:46 +08:00
parent 67af0c6bf2
commit 871d031ceb
18 changed files with 996 additions and 392 deletions

View File

@@ -0,0 +1,5 @@
[package]
name = "smcc_lex_parser"
version = "0.1.0"
dependencies = [{ name = "libcore", path = "../../runtime/libcore" }]

View File

@@ -0,0 +1,17 @@
#ifndef __SMCC_LEX_PARSER_H__
#define __SMCC_LEX_PARSER_H__
#include <libcore.h>
int lex_parse_char(core_stream_t *input, core_pos_t *pos);
cbool lex_parse_string(core_stream_t *input, core_pos_t *pos,
cstring_t *output);
cbool lex_parse_number(core_stream_t *input, core_pos_t *pos, usize *output);
cbool lex_parse_identifier(core_stream_t *input, core_pos_t *pos,
cstring_t *output);
void lex_parse_skip_endline(core_stream_t *input, core_pos_t *pos);
void lex_parse_skip_block_comment(core_stream_t *input, core_pos_t *pos);
void lex_parse_skip_line(core_stream_t *input, core_pos_t *pos);
void lex_parse_skip_whitespace(core_stream_t *input, core_pos_t *pos);
#endif /* __SMCC_LEX_PARSER_H__ */

View File

@@ -0,0 +1,406 @@
#include <lex_parser.h>
static inline cbool is_next_line(int ch) { return ch == '\n' || ch == '\r'; }
void lex_parse_skip_endline(core_stream_t *input, core_pos_t *pos) {
core_stream_reset_char(input);
int ch = core_stream_peek_char(input);
if (ch == '\r') {
core_stream_next_char(input);
ch = core_stream_peek_char(input);
if (ch == '\n') {
core_stream_next_char(input);
}
core_pos_next_line(pos);
} else if (ch == '\n') {
core_stream_next_char(input);
core_pos_next_line(pos);
} else {
LOG_WARN("not a newline character");
}
}
/**
* @brief
*
* @param ch
* @return int
* https://cppreference.cn/w/c/language/escape
* `\'` 单引号 在 ASCII 编码中为字节 0x27
* `\"` 双引号 在 ASCII 编码中为字节 0x22
* `\?` 问号 在 ASCII 编码中为字节 0x3f
* `\\` 反斜杠 在 ASCII 编码中为字节 0x5c
* `\a` 响铃 在 ASCII 编码中为字节 0x07
* `\b` 退格 在 ASCII 编码中为字节 0x08
* `\f` 换页 - 新页 在 ASCII 编码中为字节 0x0c
* `\n` 换行 - 新行 在 ASCII 编码中为字节 0x0a
* `\r` 回车 在 ASCII 编码中为字节 0x0d
* `\t` 水平制表符 在 ASCII 编码中为字节 0x09
* `\v` 垂直制表符 在 ASCII 编码中为字节 0x0b
*/
static inline int got_simple_escape(int ch) {
/* clang-format off */
#define CASE(ch) case ch: return ch;
switch (ch) {
case '\'': return '\'';
case '\"': return '\"';
case '\?': return '\?';
case '\\': return '\\';
case 'a': return '\a';
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
default: return -1;
}
/* clang-format on */
}
void lex_parse_skip_line(core_stream_t *input, core_pos_t *pos) {
core_stream_t *stream = input;
Assert(stream != null);
core_stream_reset_char(stream);
while (1) {
int ch = core_stream_peek_char(stream);
if (ch == core_stream_eof) {
return;
}
// TODO endline
if (is_next_line(ch)) {
lex_parse_skip_endline(stream, pos);
return;
} else {
core_stream_next_char(stream);
core_pos_next(pos);
}
}
}
void lex_parse_skip_block_comment(core_stream_t *input, core_pos_t *pos) {
core_stream_t *stream = input;
int ch;
core_stream_reset_char(stream);
ch = core_stream_next_char(stream);
core_pos_next(pos);
// FIXME Assertion
Assert(ch == '/');
ch = core_stream_next_char(stream);
core_pos_next(pos);
Assert(ch == '*');
// all ready match `/*`
while (1) {
core_stream_reset_char(stream);
ch = core_stream_peek_char(stream);
if (ch == core_stream_eof) {
LOG_WARN("Unterminated block comment");
return;
}
if (is_next_line(ch)) {
lex_parse_skip_endline(stream, pos);
continue;
}
core_stream_next_char(stream);
core_pos_next(pos);
if (ch == '*') {
ch = core_stream_peek_char(stream);
if (ch == '/') {
core_stream_next_char(stream);
core_pos_next(pos);
return;
}
}
}
}
void lex_parse_skip_whitespace(core_stream_t *input, core_pos_t *pos) {
core_stream_t *stream = input;
Assert(stream != null);
core_stream_reset_char(stream);
while (1) {
int ch = core_stream_next_char(stream);
if (ch == core_stream_eof) {
return;
}
core_pos_next(pos);
}
}
static inline cbool _lex_parse_uint(core_stream_t *input, core_pos_t *pos,
int base, usize *output) {
Assert(input != null && pos != null);
if (input == null || pos == null) {
return false;
}
Assert(base == 2 || base == 8 || base == 10 || base == 16);
core_stream_reset_char(input);
int ch, tmp;
usize n = 0;
usize offset = pos->offset;
while (1) {
ch = core_stream_peek_char(input);
if (ch == core_stream_eof) {
break;
} else if (ch >= 'a' && ch <= 'z') {
tmp = ch - 'a' + 10;
} else if (ch >= 'A' && ch <= 'Z') {
tmp = ch - 'A' + 10;
} else if (ch >= '0' && ch <= '9') {
tmp = ch - '0';
} else {
break;
}
if (tmp >= base) {
LOG_ERROR("Invalid digit");
return false;
}
core_stream_next_char(input);
core_pos_next(pos);
n = n * base + tmp;
// TODO number overflow
}
if (offset == pos->offset) {
// None match any number
return false;
}
*output = n;
return true;
}
/**
* @brief
*
* @param input
* @param pos
* @return int
* https://cppreference.cn/w/c/language/character_constant
*/
int lex_parse_char(core_stream_t *input, core_pos_t *pos) {
core_stream_t *stream = input;
core_stream_reset_char(stream);
int ch = core_stream_peek_char(stream);
int ret = core_stream_eof;
if (ch == core_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
goto ERR;
} else if (ch != '\'') {
LOG_WARN("Unexpected character '%c' at begin", ch);
goto ERR;
}
core_stream_next_char(stream);
core_pos_next(pos);
ch = core_stream_next_char(stream);
core_pos_next(pos);
if (ch == core_stream_eof) {
LOG_WARN("Unexpected EOF at middle");
goto ERR;
} else if (ch == '\\') {
ch = core_stream_next_char(stream);
core_pos_next(pos);
if (ch == '0') {
// 数字转义序列
// \nnn 任意八进制值 码元 nnn
// FIXME 这里如果返回 0 理论上为错误但是恰好与正确值相同
ret = 0;
_lex_parse_uint(stream, pos, 8, (usize *)&ret);
} else if (ch == 'x') {
// TODO https://cppreference.cn/w/c/language/escape
// \xn... 任意十六进制值 码元 n... (任意数量的十六进制数字)
// 通用字符名
TODO();
} else if (ch == 'u' || ch == 'U') {
// \unnnn (C99 起) Unicode 值在允许范围内;
// 可能产生多个码元 码点 U+nnnn
// \Unnnnnnnn (C99 起) Unicode 值在允许范围内;
// 可能产生多个码元 码点 U+nnnnnnnn
TODO();
} else if ((ret = got_simple_escape(ch)) == -1) {
LOG_ERROR("Invalid escape character");
goto ERR;
}
} else {
ret = ch;
}
if ((ch = core_stream_next_char(stream)) != '\'') {
LOG_ERROR("Unclosed character literal '%c' at end, expect `'`", ch);
core_pos_next(pos);
goto ERR;
}
return ret;
ERR:
return core_stream_eof;
}
/**
* @brief
*
* @param input
* @param pos
* @param output
* @return cbool
* https://cppreference.cn/w/c/language/string_literal
*/
cbool lex_parse_string(core_stream_t *input, core_pos_t *pos,
cstring_t *output) {
core_stream_t *stream = input;
core_stream_reset_char(stream);
int ch = core_stream_peek_char(stream);
Assert(cstring_is_empty(output));
if (ch == core_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
goto ERR;
} else if (ch != '"') {
LOG_WARN("Unexpected character '%c' at begin", ch);
goto ERR;
}
core_stream_next_char(stream);
core_pos_next(pos);
cstring_t str = cstring_from_cstr("");
while (1) {
ch = core_stream_peek_char(stream);
if (ch == core_stream_eof) {
LOG_ERROR("Unexpected EOF at string literal");
goto ERR;
} else if (is_next_line(ch)) {
LOG_ERROR("Unexpected newline at string literal");
goto ERR;
} else if (ch == '\\') {
// TODO bad practice and maybe bugs here
core_stream_next_char(stream);
ch = core_stream_next_char(stream);
int val = got_simple_escape(ch);
if (val == -1) {
LOG_ERROR("Invalid escape character it is \\%c [%d]", ch, ch);
} else {
cstring_push(&str, val);
continue;
}
} else if (ch == '"') {
core_stream_next_char(stream);
core_pos_next(pos);
break;
}
core_stream_next_char(stream);
core_pos_next(pos);
cstring_push(&str, ch);
}
*output = str;
return true;
ERR:
cstring_free(&str);
return false;
}
/**
* @brief
*
* @param input
* @param pos
* @param output
* @return cbool
* https://cppreference.cn/w/c/language/integer_constant
*/
cbool lex_parse_number(core_stream_t *input, core_pos_t *pos, usize *output) {
core_stream_t *stream = input;
core_stream_reset_char(stream);
int ch = core_stream_peek_char(stream);
int base = 0;
if (ch == core_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
goto ERR;
} else if (ch == '0') {
ch = core_stream_peek_char(stream);
if (ch == 'x' || ch == 'X') {
base = 16;
core_stream_next_char(stream);
core_pos_next(pos);
core_stream_next_char(stream);
core_pos_next(pos);
} else if (ch == 'b' || ch == 'B') {
// FIXME C23 external integer base
base = 2;
core_stream_next_char(stream);
core_pos_next(pos);
core_stream_next_char(stream);
core_pos_next(pos);
} else if (ch >= '0' && ch <= '7') {
base = 8;
core_stream_next_char(stream);
core_pos_next(pos);
} else if (ch == '9' || ch == '8') {
LOG_ERROR("Invalid digit '%d' in octal literal", ch);
return false;
} else {
base = 10;
}
} else {
base = 10;
}
// 解析整数部分
core_stream_reset_char(stream);
usize n;
if (_lex_parse_uint(stream, pos, base, &n) == false) {
return false;
}
*output = n;
return true;
ERR:
return false;
}
/**
* @brief
*
* @param input
* @param pos
* @param output
* @return cbool
* https://cppreference.cn/w/c/language/identifier
*/
cbool lex_parse_identifier(core_stream_t *input, core_pos_t *pos,
cstring_t *output) {
Assert(cstring_is_empty(output));
core_stream_t *stream = input;
core_stream_reset_char(stream);
int ch = core_stream_peek_char(stream);
if (ch == core_stream_eof) {
LOG_WARN("Unexpected EOF at begin");
} else if (ch == '_' || (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z')) {
while (1) {
cstring_push(output, ch);
core_stream_next_char(stream);
core_pos_next(pos);
ch = core_stream_peek_char(stream);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch == '_') || (ch >= '0' && ch <= '9')) {
continue;
}
break;
}
return true;
}
return false;
}

View File

@@ -0,0 +1,60 @@
// test_char.c
#include <lex_parser.h>
#include <utest/acutest.h>
cbool check_char(const char *str, int expect, int *output) {
log_set_level(&__default_logger_root, 0);
core_pos_t pos = core_pos_init();
core_mem_stream_t mem_stream;
core_stream_t *stream =
core_mem_stream_init(&mem_stream, str, smcc_strlen(str), false);
*output = lex_parse_char(stream, &pos);
return *output == expect;
}
#define CHECK_CHAR_VALID(str, expect) \
do { \
int _output; \
cbool ret = check_char(str, expect, &_output); \
TEST_CHECK(ret == true); \
} while (0)
#define CHECK_CHAR_INVALID(str) \
do { \
int _output; \
check_char(str, core_stream_eof, &_output); \
TEST_CHECK(_output == core_stream_eof); \
} while (0)
void test_simple_char(void) {
TEST_CASE("simple chars");
CHECK_CHAR_VALID("'a'", 'a');
CHECK_CHAR_VALID("'Z'", 'Z');
CHECK_CHAR_VALID("'0'", '0');
CHECK_CHAR_VALID("' '", ' ');
}
void test_escape_char(void) {
TEST_CASE("escape chars");
CHECK_CHAR_VALID("'\\n'", '\n');
CHECK_CHAR_VALID("'\\t'", '\t');
CHECK_CHAR_VALID("'\\r'", '\r');
CHECK_CHAR_VALID("'\\\\'", '\\');
CHECK_CHAR_VALID("'\\''", '\'');
CHECK_CHAR_VALID("'\\\"'", '\"');
}
void test_invalid_char(void) {
TEST_CASE("invalid chars");
CHECK_CHAR_INVALID("'");
CHECK_CHAR_INVALID("''");
CHECK_CHAR_INVALID("'ab'");
CHECK_CHAR_INVALID("'\\'");
}
TEST_LIST = {
{"test_simple_char", test_simple_char},
{"test_escape_char", test_escape_char},
{"test_invalid_char", test_invalid_char},
{NULL, NULL},
};

View File

@@ -0,0 +1,55 @@
// test_identifier.c
#include <lex_parser.h>
#include <utest/acutest.h>
cbool check_identifier(const char *str, const char *expect, cstring_t *output) {
log_set_level(&__default_logger_root, 0);
core_pos_t pos = core_pos_init();
core_mem_stream_t mem_stream;
core_stream_t *stream =
core_mem_stream_init(&mem_stream, str, smcc_strlen(str), false);
cbool ret = lex_parse_identifier(stream, &pos, output);
if (ret && expect) {
return strcmp(output->data, expect) == 0;
}
return ret;
}
#define CHECK_IDENTIFIER_VALID(str, expect) \
do { \
cstring_t _output = cstring_new(); \
cbool ret = check_identifier(str, expect, &_output); \
TEST_CHECK(ret == true); \
TEST_CHECK(strcmp(_output.data, expect) == 0); \
cstring_free(&_output); \
} while (0)
#define CHECK_IDENTIFIER_INVALID(str) \
do { \
cstring_t _output = cstring_new(); \
cbool ret = check_identifier(str, NULL, &_output); \
TEST_CHECK(ret == false); \
cstring_free(&_output); \
} while (0)
void test_valid_identifier(void) {
TEST_CASE("valid identifiers");
CHECK_IDENTIFIER_VALID("variable", "variable");
CHECK_IDENTIFIER_VALID("my_var", "my_var");
CHECK_IDENTIFIER_VALID("_private", "_private");
CHECK_IDENTIFIER_VALID("Var123", "Var123");
CHECK_IDENTIFIER_VALID("a", "a");
}
void test_invalid_identifier(void) {
TEST_CASE("invalid identifiers");
CHECK_IDENTIFIER_INVALID("");
CHECK_IDENTIFIER_INVALID("123var");
}
TEST_LIST = {
{"test_valid_identifier", test_valid_identifier},
{"test_invalid_identifier", test_invalid_identifier},
{NULL, NULL},
};

View File

@@ -0,0 +1,132 @@
#include <lex_parser.h>
#include <utest/acutest.h>
cbool check(const char *str, usize expect, usize *output) {
// TODO maybe have other logger
log_set_level(&__default_logger_root, 0);
core_pos_t pos = core_pos_init();
core_mem_stream_t mem_stream;
core_stream_t *stream =
core_mem_stream_init(&mem_stream, str, smcc_strlen(str), false);
return lex_parse_number(stream, &pos, output);
}
#define CHECK_VALID(str, expect) \
do { \
usize _output; \
cbool ret = check(str, expect, &_output); \
TEST_CHECK(ret == true); \
TEST_CHECK(_output == expect); \
} while (0)
#define CHECK_INVALID(str) \
do { \
usize _output; \
cbool ret = check(str, 0, &_output); \
TEST_CHECK(ret == false); \
} while (0)
void test_simple_hex(void) {
TEST_CASE("lowercase hex");
CHECK_VALID("0xff", 255);
CHECK_VALID("0x0", 0);
CHECK_VALID("0xa", 10);
CHECK_VALID("0xf", 15);
CHECK_VALID("0x1a", 26);
TEST_CASE("uppercase hex");
CHECK_VALID("0xFF", 255);
CHECK_VALID("0xA0", 160);
CHECK_VALID("0xCAFEBABE", 3405691582);
TEST_CASE("mixed case hex");
CHECK_VALID("0xFf", 255);
CHECK_VALID("0xCaFeBaBe", 3405691582);
TEST_CASE("larger hex values");
CHECK_VALID("0xff00", 65280);
CHECK_VALID("0xFFFF", 65535);
TEST_CASE("invalid hex");
CHECK_INVALID("0xG"); // Invalid hex digit
CHECK_INVALID("0xyz"); // Invalid prefix
CHECK_INVALID("0x"); // Incomplete hex
}
void test_simple_oct(void) {
TEST_CASE("basic octal");
CHECK_VALID("00", 0);
CHECK_VALID("01", 1);
CHECK_VALID("07", 7);
TEST_CASE("multi-digit octal");
CHECK_VALID("010", 8);
CHECK_VALID("017", 15);
CHECK_VALID("077", 63);
TEST_CASE("larger octal values");
CHECK_VALID("0177", 127);
CHECK_VALID("0377", 255);
CHECK_VALID("0777", 511);
TEST_CASE("invalid octal");
CHECK_INVALID("08"); // Invalid octal digit
CHECK_INVALID("09"); // Invalid octal digit
}
void test_simple_dec(void) {
TEST_CASE("single digits");
CHECK_VALID("0", 0);
CHECK_VALID("1", 1);
CHECK_VALID("9", 9);
TEST_CASE("multi-digit decimal");
CHECK_VALID("10", 10);
CHECK_VALID("42", 42);
CHECK_VALID("123", 123);
TEST_CASE("larger decimal values");
CHECK_VALID("999", 999);
CHECK_VALID("1234", 1234);
CHECK_VALID("65535", 65535);
}
void test_simple_bin(void) {
TEST_CASE("basic binary");
CHECK_VALID("0b0", 0);
CHECK_VALID("0b1", 1);
TEST_CASE("multi-digit binary");
CHECK_VALID("0b10", 2);
CHECK_VALID("0b11", 3);
CHECK_VALID("0b100", 4);
CHECK_VALID("0b1010", 10);
TEST_CASE("larger binary values");
CHECK_VALID("0b1111", 15);
CHECK_VALID("0b11111111", 255);
CHECK_VALID("0b10101010", 170);
TEST_CASE("invalid binary");
CHECK_INVALID("0b2"); // Invalid binary digit
CHECK_INVALID("0b3"); // Invalid binary digit
CHECK_INVALID("0b"); // Incomplete binary
}
void test_edge_cases(void) {
TEST_CASE("empty string");
CHECK_INVALID(""); // Empty string
TEST_CASE("non-numeric strings");
CHECK_INVALID("abc"); // Non-numeric
CHECK_INVALID("xyz"); // Non-numeric
TEST_CASE("mixed invalid formats");
CHECK_INVALID("0x1G"); // Mixed valid/invalid hex
CHECK_INVALID("0b12"); // Mixed valid/invalid binary
}
TEST_LIST = {
{"test_simple_hex", test_simple_hex}, {"test_simple_oct", test_simple_oct},
{"test_simple_dec", test_simple_dec}, {"test_simple_bin", test_simple_bin},
{"test_edge_cases", test_edge_cases}, {NULL, NULL},
};

View File

@@ -0,0 +1,50 @@
// test_skip_block_comment.c
#include <lex_parser.h>
#include <utest/acutest.h>
void check_skip_block_comment(const char *str, const char *expect_remaining) {
log_set_level(&__default_logger_root, 0);
core_pos_t pos = core_pos_init();
core_mem_stream_t mem_stream;
core_stream_t *stream =
core_mem_stream_init(&mem_stream, str, smcc_strlen(str), false);
lex_parse_skip_block_comment(stream, &pos);
// Check remaining content
char buffer[256] = {0};
int i = 0;
int ch;
while ((ch = core_stream_next_char(stream)) != core_stream_eof && i < 255) {
buffer[i++] = (char)ch;
}
if (expect_remaining) {
TEST_CHECK(strcmp(buffer, expect_remaining) == 0);
}
}
void test_simple_block_comment(void) {
TEST_CASE("simple block comments");
check_skip_block_comment("/* comment */", "");
check_skip_block_comment("/* comment */ int x;", " int x;");
}
void test_multiline_block_comment(void) {
TEST_CASE("multiline block comments");
check_skip_block_comment("/* line1\nline2 */", "");
check_skip_block_comment("/* line1\nline2 */ int x;", " int x;");
}
void test_nested_asterisk_block_comment(void) {
TEST_CASE("nested asterisk block comments");
check_skip_block_comment("/* *** */", "");
check_skip_block_comment("/* *** */ int x;", " int x;");
}
TEST_LIST = {
{"test_simple_block_comment", test_simple_block_comment},
{"test_multiline_block_comment", test_multiline_block_comment},
{"test_nested_asterisk_block_comment", test_nested_asterisk_block_comment},
{NULL, NULL},
};

View File

@@ -0,0 +1,49 @@
// test_skip_line.c
#include <lex_parser.h>
#include <utest/acutest.h>
void check_skip_line(const char *str, const char *expect_remaining) {
log_set_level(&__default_logger_root, 0);
core_pos_t pos = core_pos_init();
core_mem_stream_t mem_stream;
core_stream_t *stream =
core_mem_stream_init(&mem_stream, str, smcc_strlen(str), false);
lex_parse_skip_line(stream, &pos);
// Check remaining content
char buffer[256] = {0};
int i = 0;
int ch;
while ((ch = core_stream_next_char(stream)) != core_stream_eof && i < 255) {
buffer[i++] = (char)ch;
}
if (expect_remaining) {
TEST_CHECK(strcmp(buffer, expect_remaining) == 0);
}
}
void test_simple_line_comment(void) {
TEST_CASE("simple line comments");
check_skip_line("// comment\n", "");
check_skip_line("// comment\nint x;", "int x;");
}
void test_crlf_line_comment(void) {
TEST_CASE("CRLF line comments");
check_skip_line("// comment\r\n", "");
check_skip_line("// comment\r\nint x;", "int x;");
}
void test_eof_line_comment(void) {
TEST_CASE("EOF line comments");
check_skip_line("// comment", "");
}
TEST_LIST = {
{"test_simple_line_comment", test_simple_line_comment},
{"test_crlf_line_comment", test_crlf_line_comment},
{"test_eof_line_comment", test_eof_line_comment},
{NULL, NULL},
};

View File

@@ -0,0 +1,62 @@
// test_string.c
#include <lex_parser.h>
#include <utest/acutest.h>
cbool check_string(const char *str, const char *expect, cstring_t *output) {
log_set_level(&__default_logger_root, 0);
core_pos_t pos = core_pos_init();
core_mem_stream_t mem_stream;
core_stream_t *stream =
core_mem_stream_init(&mem_stream, str, smcc_strlen(str), false);
cbool ret = lex_parse_string(stream, &pos, output);
if (ret && expect) {
return strcmp(output->data, expect) == 0;
}
return ret;
}
#define CHECK_STRING_VALID(str, expect) \
do { \
cstring_t _output = cstring_new(); \
cbool ret = check_string(str, expect, &_output); \
TEST_CHECK(ret == true); \
TEST_CHECK(strcmp(_output.data, expect) == 0); \
cstring_free(&_output); \
} while (0)
#define CHECK_STRING_INVALID(str) \
do { \
cstring_t _output = cstring_new(); \
cbool ret = check_string(str, NULL, &_output); \
TEST_CHECK(ret == false); \
cstring_free(&_output); \
} while (0)
void test_simple_string(void) {
TEST_CASE("simple strings");
CHECK_STRING_VALID("\"\"", "");
CHECK_STRING_VALID("\"hello\"", "hello");
CHECK_STRING_VALID("\"hello world\"", "hello world");
}
void test_escape_string(void) {
TEST_CASE("escape strings");
CHECK_STRING_VALID("\"\\n\"", "\n");
CHECK_STRING_VALID("\"\\t\"", "\t");
CHECK_STRING_VALID("\"\\\"\"", "\"");
CHECK_STRING_VALID("\"Hello\\nWorld\"", "Hello\nWorld");
}
void test_invalid_string(void) {
TEST_CASE("invalid strings");
CHECK_STRING_INVALID("\"unterminated");
CHECK_STRING_INVALID("\"newline\n\"");
}
TEST_LIST = {
{"test_simple_string", test_simple_string},
{"test_escape_string", test_escape_string},
{"test_invalid_string", test_invalid_string},
{NULL, NULL},
};