完善了scc_parse_type函数以正确解析基本类型,修复了条件表达式解析逻辑, 实现了for循环中声明和表达式的混合处理,并添加了对赋值语句和复杂表达式的支持。 fix(parser): 修复内存泄漏和解析器状态管理问题 修复了当tok参数为null时的内存泄漏问题,在标签语句解析中正确处理解析器状态回退, 并改进了表达式和声明的错误处理机制。 test(parser): 更新单元测试以验证修复的功能 更新了返回语句的测试值,添加了包含变量声明、赋值语句和复杂表达式的综合测试用例, 验证了赋值运算符的右结合性和复杂表达式的解析正确性。
74 lines
1.9 KiB
C
74 lines
1.9 KiB
C
#ifndef __SCC_PARSER_UTILS_H__
|
|
#define __SCC_PARSER_UTILS_H__
|
|
|
|
#include "scc_parser.h"
|
|
|
|
static inline const scc_lexer_tok_t *scc_parser_peek(scc_parser_t *parser) {
|
|
cbool ok = false;
|
|
const scc_lexer_tok_t *tok = null;
|
|
scc_ring_unsafe_peek_ref(*parser->ring, tok, ok);
|
|
if (ok == false) {
|
|
return null;
|
|
}
|
|
return tok;
|
|
}
|
|
|
|
static inline const scc_lexer_tok_t *scc_parser_next(scc_parser_t *parser) {
|
|
cbool ok = false;
|
|
const scc_lexer_tok_t *tok = null;
|
|
scc_ring_unsafe_next_ref(*parser->ring, tok, ok);
|
|
if (ok == false) {
|
|
return null;
|
|
}
|
|
return tok;
|
|
}
|
|
|
|
static inline cbool scc_parser_consume_if(scc_parser_t *parser,
|
|
scc_tok_type_t type) {
|
|
cbool ok = false;
|
|
scc_lexer_tok_t *tok = null;
|
|
scc_ring_unsafe_peek_ref(*parser->ring, tok, ok);
|
|
if (ok == false) {
|
|
return null;
|
|
}
|
|
if (tok->type == type) {
|
|
scc_lexer_tok_drop(tok);
|
|
scc_ring_unsafe_pure_next_consume(*parser->ring);
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static inline void scc_parser_store(scc_parser_t *parser) {
|
|
parser->checkpoint = _scc_ring_probe(*parser->ring);
|
|
}
|
|
|
|
static inline void scc_parser_restore(scc_parser_t *parser) {
|
|
_scc_ring_probe(*parser->ring) = parser->checkpoint;
|
|
}
|
|
|
|
static inline cbool scc_parser_next_consume(scc_parser_t *parser,
|
|
scc_lexer_tok_t *tok) {
|
|
cbool ok = false;
|
|
scc_lexer_tok_t *raw_tok_ref = null;
|
|
scc_ring_unsafe_next_ref_consume(*parser->ring, raw_tok_ref, ok);
|
|
if (tok == null) {
|
|
scc_lexer_tok_drop(raw_tok_ref);
|
|
} else {
|
|
scc_lexer_tok_move(tok, raw_tok_ref);
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
static inline void scc_parser_commit(scc_parser_t *parser) {
|
|
// Memory leak
|
|
scc_ring_consume(*parser->ring);
|
|
}
|
|
|
|
static inline void scc_parser_reset(scc_parser_t *parser) {
|
|
scc_ring_reset(*parser->ring);
|
|
}
|
|
|
|
#endif /* __SCC_PARSER_UTILS_H__ */
|