2025-08-16 12:24:23 +09:00
|
|
|
|
/*!
|
|
|
|
|
|
* Static declaration parsing
|
|
|
|
|
|
* Handles both static functions and static boxes
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
use crate::ast::{ASTNode, Span};
|
|
|
|
|
|
use crate::must_advance;
|
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 {
|
|
|
|
|
|
/// 静的宣言をパース - 🔥 static function / static box 記法
|
|
|
|
|
|
pub fn parse_static_declaration(&mut self) -> Result<ASTNode, ParseError> {
|
|
|
|
|
|
self.consume(TokenType::STATIC)?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
// 次のトークンで分岐: function か box か
|
|
|
|
|
|
match &self.current_token().token_type {
|
|
|
|
|
|
TokenType::FUNCTION => self.parse_static_function(),
|
|
|
|
|
|
TokenType::BOX => self.parse_static_box(),
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
let line = self.current_token().line;
|
|
|
|
|
|
Err(ParseError::UnexpectedToken {
|
|
|
|
|
|
found: self.current_token().token_type.clone(),
|
|
|
|
|
|
expected: "function or box after static".to_string(),
|
|
|
|
|
|
line,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
/// 静的関数宣言をパース - static function Name() { ... }
|
|
|
|
|
|
fn parse_static_function(&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
|
|
|
|
// 関数名を取得(Box名.関数名の形式をサポート)
|
|
|
|
|
|
let name = if let TokenType::IDENTIFIER(first_part) = &self.current_token().token_type {
|
|
|
|
|
|
let mut full_name = first_part.clone();
|
|
|
|
|
|
self.advance();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
// ドット記法をチェック(例:Math.min)
|
|
|
|
|
|
if self.match_token(&TokenType::DOT) {
|
|
|
|
|
|
self.advance(); // DOTを消費
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
if let TokenType::IDENTIFIER(method_name) = &self.current_token().token_type {
|
|
|
|
|
|
full_name = format!("{}.{}", full_name, method_name);
|
|
|
|
|
|
self.advance();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let line = self.current_token().line;
|
|
|
|
|
|
return Err(ParseError::UnexpectedToken {
|
|
|
|
|
|
found: self.current_token().token_type.clone(),
|
|
|
|
|
|
expected: "method name after dot".to_string(),
|
|
|
|
|
|
line,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
full_name
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let line = self.current_token().line;
|
|
|
|
|
|
return Err(ParseError::UnexpectedToken {
|
|
|
|
|
|
found: self.current_token().token_type.clone(),
|
|
|
|
|
|
expected: "static 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)?;
|
|
|
|
|
|
let mut params = Vec::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
while !self.match_token(&TokenType::RPAREN) && !self.is_at_end() {
|
|
|
|
|
|
must_advance!(self, _unused, "static function parameter parsing");
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
if let TokenType::IDENTIFIER(param) = &self.current_token().token_type {
|
|
|
|
|
|
params.push(param.clone());
|
|
|
|
|
|
self.advance();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-16 12:24:23 +09:00
|
|
|
|
if self.match_token(&TokenType::COMMA) {
|
|
|
|
|
|
self.advance();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if !self.match_token(&TokenType::RPAREN) {
|
|
|
|
|
|
let line = self.current_token().line;
|
|
|
|
|
|
return Err(ParseError::UnexpectedToken {
|
|
|
|
|
|
found: self.current_token().token_type.clone(),
|
|
|
|
|
|
expected: "parameter name".to_string(),
|
|
|
|
|
|
line,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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: true, // 🔥 静的関数フラグを設定
|
2025-08-16 12:24:23 +09:00
|
|
|
|
is_override: false, // デフォルトは非オーバーライド
|
|
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|