2025-08-16 12:24:23 +09:00
|
|
|
/*!
|
|
|
|
|
* Function declaration parsing
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use crate::ast::{ASTNode, Span};
|
2025-09-17 07:43:07 +09:00
|
|
|
use crate::parser::common::ParserUtils;
|
|
|
|
|
use crate::parser::{NyashParser, ParseError};
|
|
|
|
|
use crate::tokenizer::TokenType;
|
2025-08-16 12:24:23 +09:00
|
|
|
|
|
|
|
|
impl NyashParser {
|
|
|
|
|
/// function宣言をパース: function name(params) { body }
|
|
|
|
|
pub fn parse_function_declaration(&mut self) -> Result<ASTNode, ParseError> {
|
|
|
|
|
self.consume(TokenType::FUNCTION)?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
// 関数名を取得
|
|
|
|
|
let name = if let TokenType::IDENTIFIER(name) = &self.current_token().token_type {
|
|
|
|
|
let name = name.clone();
|
|
|
|
|
self.advance();
|
|
|
|
|
name
|
|
|
|
|
} else {
|
|
|
|
|
let line = self.current_token().line;
|
|
|
|
|
return Err(ParseError::UnexpectedToken {
|
|
|
|
|
found: self.current_token().token_type.clone(),
|
|
|
|
|
expected: "function name".to_string(),
|
|
|
|
|
line,
|
|
|
|
|
});
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
// パラメータリストをパース
|
|
|
|
|
self.consume(TokenType::LPAREN)?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
2025-12-24 07:44:50 +09:00
|
|
|
// Phase 285A1.5: Use shared helper to prevent parser hangs on unsupported type annotations
|
|
|
|
|
let params = crate::parser::common::params::parse_param_name_list(self, "function")?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
self.consume(TokenType::RPAREN)?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
2025-09-19 02:07:38 +09:00
|
|
|
// 関数本体をパース(共通ブロックヘルパー)
|
|
|
|
|
let body = self.parse_block_statements()?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
Ok(ASTNode::FunctionDeclaration {
|
|
|
|
|
name,
|
|
|
|
|
params,
|
|
|
|
|
body,
|
2025-09-17 07:43:07 +09:00
|
|
|
is_static: false, // 通常の関数は静的でない
|
2025-08-16 12:24:23 +09:00
|
|
|
is_override: false, // デフォルトは非オーバーライド
|
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
}
|