增加注释

This commit is contained in:
Luke 2025-04-24 22:54:27 +08:00
parent 8baab25c44
commit 95ac9d4177

View File

@ -200,13 +200,25 @@ public class FunctionParser implements TopLevelParser {
} }
/** /**
* 解析函数体 body 区块包括多条语句直到 end body * 解析函数体body部分提取语句并构建 {@link StatementNode} 列表
* <p>
* 语法结构形如
* <pre>{@code
* body:
* declare x:int = 5
* if x > 0
* ...
* end body
* }</pre>
* <p>
* 该方法将每一条语句委托给 {@link StatementParserFactory} 根据关键字调度解析器
* *
* @param ctx 语法分析上下文 * @param ctx 上下文对象包含词法流与全局状态
* @param ts Token * @param ts 当前的 Token 词法单元序列
* @return 函数体中的语句节点列表 * @return 包含函数体所有语句的列表
*/ */
private List<StatementNode> parseFunctionBody(ParserContext ctx, TokenStream ts) { private List<StatementNode> parseFunctionBody(ParserContext ctx, TokenStream ts) {
// 匹配 body: 起始标记
ts.expect("body"); ts.expect("body");
ts.expect(":"); ts.expect(":");
ts.expectType(TokenType.NEWLINE); ts.expectType(TokenType.NEWLINE);
@ -214,20 +226,26 @@ public class FunctionParser implements TopLevelParser {
List<StatementNode> body = new ArrayList<>(); List<StatementNode> body = new ArrayList<>();
while (true) { while (true) {
// 跳过空行确保处理有效语句
if (ts.peek().getType() == TokenType.NEWLINE) { if (ts.peek().getType() == TokenType.NEWLINE) {
ts.next(); // 跳过空行 ts.next();
continue; continue;
} }
// 若遇到 end则说明函数体结束跳出循环
if ("end".equals(ts.peek().getLexeme())) { if ("end".equals(ts.peek().getLexeme())) {
break; break;
} }
// 根据当前行的关键字选择对应的语句解析器
String keyword = ts.peek().getLexeme(); String keyword = ts.peek().getLexeme();
// 调用语句解析器解析当前语句加入函数体列表中
StatementNode statement = StatementParserFactory.get(keyword).parse(ctx); StatementNode statement = StatementParserFactory.get(keyword).parse(ctx);
body.add(statement); body.add(statement);
} }
// 匹配函数体结束标记 end body
ts.expect("end"); ts.expect("end");
ts.expect("body"); ts.expect("body");
ts.expectType(TokenType.NEWLINE); ts.expectType(TokenType.NEWLINE);
@ -235,6 +253,7 @@ public class FunctionParser implements TopLevelParser {
return body; return body;
} }
/** /**
* 匹配函数定义结束标志end function * 匹配函数定义结束标志end function
* *