refactor(parser): Step 4 - Extract items module
- Create items module with global_vars, functions, static_items - Move parse_global_var from mod.rs to items/global_vars.rs - Move parse_function_declaration to items/functions.rs - Move parse_static_declaration and parse_static_function to items/static_items.rs - Clean up mod.rs by removing 197 lines of code - Build passes successfully Next: Step 5 - Final cleanup and documentation
This commit is contained in:
79
src/parser/items/functions.rs
Normal file
79
src/parser/items/functions.rs
Normal file
@ -0,0 +1,79 @@
|
||||
/*!
|
||||
* Function declaration parsing
|
||||
*/
|
||||
|
||||
use crate::parser::{NyashParser, ParseError};
|
||||
use crate::parser::common::ParserUtils;
|
||||
use crate::tokenizer::TokenType;
|
||||
use crate::ast::{ASTNode, Span};
|
||||
use crate::must_advance;
|
||||
|
||||
impl NyashParser {
|
||||
/// function宣言をパース: function name(params) { body }
|
||||
pub fn parse_function_declaration(&mut self) -> Result<ASTNode, ParseError> {
|
||||
self.consume(TokenType::FUNCTION)?;
|
||||
|
||||
// 関数名を取得
|
||||
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,
|
||||
});
|
||||
};
|
||||
|
||||
// パラメータリストをパース
|
||||
self.consume(TokenType::LPAREN)?;
|
||||
let mut params = Vec::new();
|
||||
|
||||
while !self.match_token(&TokenType::RPAREN) && !self.is_at_end() {
|
||||
must_advance!(self, _unused, "function declaration parameter parsing");
|
||||
|
||||
if let TokenType::IDENTIFIER(param) = &self.current_token().token_type {
|
||||
params.push(param.clone());
|
||||
self.advance();
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
self.consume(TokenType::RPAREN)?;
|
||||
|
||||
// 関数本体をパース
|
||||
self.consume(TokenType::LBRACE)?;
|
||||
self.skip_newlines();
|
||||
|
||||
let mut body = Vec::new();
|
||||
while !self.match_token(&TokenType::RBRACE) && !self.is_at_end() {
|
||||
self.skip_newlines();
|
||||
if !self.match_token(&TokenType::RBRACE) {
|
||||
body.push(self.parse_statement()?);
|
||||
}
|
||||
}
|
||||
|
||||
self.consume(TokenType::RBRACE)?;
|
||||
|
||||
Ok(ASTNode::FunctionDeclaration {
|
||||
name,
|
||||
params,
|
||||
body,
|
||||
is_static: false, // 通常の関数は静的でない
|
||||
is_override: false, // デフォルトは非オーバーライド
|
||||
span: Span::unknown(),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user