From ea8391ae9d519169320ae50393078f64e02e4f85 Mon Sep 17 00:00:00 2001 From: myyrakle Date: Fri, 9 Aug 2024 23:46:30 +0900 Subject: [PATCH] =?UTF-8?q?[#148]=20parse=5Fexpression=20=EC=84=9C?= =?UTF-8?q?=EB=B8=8C=EC=BF=BC=EB=A6=AC=20=ED=8C=8C=EC=8B=B1=20=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser/implements/dml/select.rs | 15 ++++++++++++++- src/parser/test/expressions.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/parser/implements/dml/select.rs b/src/parser/implements/dml/select.rs index f3c31db..b1021ca 100644 --- a/src/parser/implements/dml/select.rs +++ b/src/parser/implements/dml/select.rs @@ -55,6 +55,11 @@ impl Parser { // from 없는 select절로 간주. 종료. return Ok(query_builder.build()); } + Token::RightParentheses => { + // from 없는 select절로 간주. 종료. + self.unget_next_token(current_token); + return Ok(query_builder.build()); + } Token::Comma => continue, Token::Operator(OperatorToken::Asterisk) => { query_builder = @@ -129,6 +134,10 @@ impl Parser { Token::SemiColon => { return Ok(query_builder.build()); } + Token::RightParentheses => { + self.unget_next_token(current_token); + return Ok(query_builder.build()); + } Token::Comma => continue, Token::Having | Token::Limit | Token::Offset | Token::Order => { self.unget_next_token(current_token); @@ -219,6 +228,10 @@ impl Parser { Token::SemiColon => { return Ok(query_builder.build()); } + Token::RightParentheses => { + self.unget_next_token(current_token); + return Ok(query_builder.build()); + } Token::Comma => continue, Token::Group | Token::Limit | Token::Offset => { self.unget_next_token(current_token); @@ -313,7 +326,7 @@ impl Parser { // 현재 select_item은 종료된 것으로 판단. Ok(select_item.build()) } - Token::SemiColon => { + Token::SemiColon | Token::RightParentheses => { self.unget_next_token(current_token); // 현재 select_item은 종료된 것으로 판단. Ok(select_item.build()) diff --git a/src/parser/test/expressions.rs b/src/parser/test/expressions.rs index 1cb9a61..f0f226f 100644 --- a/src/parser/test/expressions.rs +++ b/src/parser/test/expressions.rs @@ -8,6 +8,8 @@ use crate::ast::dml::expressions::not_between::NotBetweenExpression; use crate::ast::dml::expressions::operators::{BinaryOperator, UnaryOperator}; use crate::ast::dml::expressions::parentheses::ParenthesesExpression; use crate::ast::dml::expressions::unary::UnaryOperatorExpression; +use crate::ast::dml::parts::select_item::SelectItem; +use crate::ast::dml::select::SelectQuery; use crate::ast::types::{ConditionalFunction, SQLExpression, UserDefinedFunction}; use crate::lexer::predule::OperatorToken; use crate::lexer::tokens::Token; @@ -577,6 +579,33 @@ fn test_parse_expression() { .into(), want_error: false, }, + TestCase { + name: r#"(SELECT 1) + 10"#.into(), + input: vec![ + Token::LeftParentheses, + Token::Select, + Token::Integer(1), + Token::RightParentheses, + Token::Operator(OperatorToken::Plus), + Token::Integer(10), + ], + expected: BinaryOperatorExpression { + operator: BinaryOperator::Add, + lhs: SQLExpression::Subquery( + SelectQuery::builder() + .add_select_item( + SelectItem::builder() + .set_item(SQLExpression::Integer(1)) + .build(), + ) + .build() + .into(), + ), + rhs: SQLExpression::Integer(10), + } + .into(), + want_error: false, + }, TestCase { name: "오류: * 5".into(), input: vec![Token::Operator(OperatorToken::Asterisk), Token::Integer(5)],