From 20ce83b8b0db5e506b2dd5f699d9422378b30571 Mon Sep 17 00:00:00 2001 From: Selfhosting Dev Date: Thu, 25 Sep 2025 06:25:27 +0900 Subject: [PATCH] =?UTF-8?q?parser(cursor):=20Step-2=20begin=20=E2=80=93=20?= =?UTF-8?q?TokenCursor=20primary=20support=20(array=20literal)=20and=20env?= =?UTF-8?q?-guarded=20bridge;=20default=20path=20unchanged?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser/expr_cursor.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/parser/expr_cursor.rs b/src/parser/expr_cursor.rs index 2ab43d87..d7eb0479 100644 --- a/src/parser/expr_cursor.rs +++ b/src/parser/expr_cursor.rs @@ -2,6 +2,7 @@ use crate::ast::{ASTNode, BinaryOperator, LiteralValue, Span}; use crate::parser::cursor::TokenCursor; use crate::parser::ParseError; use crate::tokenizer::TokenType; +use crate::parser::sugar_gate; /// TokenCursorを使用した式パーサー(実験的実装) pub struct ExprParserWithCursor; @@ -145,6 +146,30 @@ impl ExprParserWithCursor { /// プライマリ式をパース fn parse_primary_expr(cursor: &mut TokenCursor) -> Result { match &cursor.current().token_type.clone() { + TokenType::LBRACK => { + // Array literal (sugar gated) + let sugar_on = sugar_gate::is_enabled() + || std::env::var("NYASH_ENABLE_ARRAY_LITERAL").ok().as_deref() == Some("1"); + if !sugar_on { + let line = cursor.current().line; + return Err(ParseError::UnexpectedToken { + found: cursor.current().token_type.clone(), + expected: "enable NYASH_SYNTAX_SUGAR_LEVEL=basic|full or NYASH_ENABLE_ARRAY_LITERAL=1".to_string(), + line, + }); + } + cursor.advance(); // consume '[' + let mut elements: Vec = Vec::new(); + while !cursor.match_token(&TokenType::RBRACK) && !cursor.is_at_end() { + let el = Self::parse_expression(cursor)?; + elements.push(el); + if cursor.match_token(&TokenType::COMMA) { + cursor.advance(); + } + } + cursor.consume(TokenType::RBRACK)?; + Ok(ASTNode::ArrayLiteral { elements, span: Span::unknown() }) + } TokenType::NUMBER(n) => { let value = *n; cursor.advance(); @@ -250,4 +275,4 @@ impl ExprParserWithCursor { span: Span::unknown(), }) } -} \ No newline at end of file +}