增加注释
This commit is contained in:
parent
8baab25c44
commit
95ac9d4177
@ -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 ts Token 流
|
||||
* @return 函数体中的语句节点列表
|
||||
* @param ctx 上下文对象,包含词法流与全局状态
|
||||
* @param ts 当前的 Token 流(词法单元序列)
|
||||
* @return 包含函数体所有语句的列表
|
||||
*/
|
||||
private List<StatementNode> parseFunctionBody(ParserContext ctx, TokenStream ts) {
|
||||
// 匹配 body: 起始标记
|
||||
ts.expect("body");
|
||||
ts.expect(":");
|
||||
ts.expectType(TokenType.NEWLINE);
|
||||
@ -214,20 +226,26 @@ public class FunctionParser implements TopLevelParser {
|
||||
List<StatementNode> body = new ArrayList<>();
|
||||
|
||||
while (true) {
|
||||
// 跳过空行,确保处理有效语句
|
||||
if (ts.peek().getType() == TokenType.NEWLINE) {
|
||||
ts.next(); // 跳过空行
|
||||
ts.next();
|
||||
continue;
|
||||
}
|
||||
|
||||
// 若遇到 end,则说明函数体结束,跳出循环
|
||||
if ("end".equals(ts.peek().getLexeme())) {
|
||||
break;
|
||||
}
|
||||
|
||||
// 根据当前行的关键字选择对应的语句解析器
|
||||
String keyword = ts.peek().getLexeme();
|
||||
|
||||
// 调用语句解析器解析当前语句,加入函数体列表中
|
||||
StatementNode statement = StatementParserFactory.get(keyword).parse(ctx);
|
||||
body.add(statement);
|
||||
}
|
||||
|
||||
// 匹配函数体结束标记 end body
|
||||
ts.expect("end");
|
||||
ts.expect("body");
|
||||
ts.expectType(TokenType.NEWLINE);
|
||||
@ -235,6 +253,7 @@ public class FunctionParser implements TopLevelParser {
|
||||
return body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 匹配函数定义结束标志(end function)
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user