Skip to content

Commit

Permalink
update parse_table()
Browse files Browse the repository at this point in the history
  • Loading branch information
kitta65 committed Jul 27, 2023
1 parent 8e38323 commit 596268c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::cst::NodeType;
use crate::error::{BQ2CSTError, BQ2CSTResult};
use crate::token::Token;

#[derive(Clone)]
pub struct Parser {
position: usize,
leading_comment_indices: Vec<usize>,
Expand Down Expand Up @@ -1034,7 +1035,27 @@ impl Parser {
}
}
if statement_flg {
group = self.parse_select_statement(false, false)?;
let org = self.clone();
group = match self.parse_select_statement(false, false) {
Ok(stmt) => stmt,

// maybe that is a table quoted by ()! not a select statement!
Err(_) => {
// restore original state
self.position = org.position;
self.leading_comment_indices = org.leading_comment_indices;
self.trailing_comment_indices = org.trailing_comment_indices;
self.tokens = org.tokens;

// retry
let mut group = self.construct_node(NodeType::GroupedExpr)?;
self.next_token()?; // ( -> table
group.push_node("expr", self.parse_table(true)?);
self.next_token()?; // -> )
group.push_node("rparen", self.construct_node(NodeType::Symbol)?);
group
}
}
} else {
group = self.construct_node(NodeType::GroupedExpr)?;
self.next_token()?; // ( -> expr
Expand Down Expand Up @@ -1486,6 +1507,12 @@ impl Parser {
self.next_token()?; // ( -> SELECT
node.push_node("stmt", self.parse_select_statement(false, true)?);
self.next_token()?; // stmt -> )
if !self.get_token(0)?.is(")") {
return Err(BQ2CSTError::from_token(
self.get_token(0)?,
"expected )".to_string(),
));
}
node.push_node("rparen", self.construct_node(NodeType::Symbol)?);
while self
.get_token(1)?
Expand Down
45 changes: 45 additions & 0 deletions src/parser/tests/tests_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,51 @@ SELECT * FROM (
)
",
"\
self: SELECT (SelectStatement)
exprs:
- self: * (Asterisk)
from:
self: FROM (KeywordWithExpr)
expr:
self: ( (GroupedExpr)
expr:
self: JOIN (JoinOperator)
join_type:
self: CROSS (Keyword)
left:
self: ( (GroupedStatement)
alias:
self: a (Identifier)
as:
self: AS (Keyword)
rparen:
self: ) (Symbol)
stmt:
self: SELECT (SelectStatement)
exprs:
- self: 1 (NumericLiteral)
alias:
self: one (Identifier)
as:
self: AS (Keyword)
right:
self: ( (GroupedStatement)
alias:
self: b (Identifier)
as:
self: AS (Keyword)
rparen:
self: ) (Symbol)
stmt:
self: SELECT (SelectStatement)
exprs:
- self: 2 (NumericLiteral)
alias:
self: two (Identifier)
as:
self: AS (Keyword)
rparen:
self: ) (Symbol)
",
0,
)),
Expand Down

0 comments on commit 596268c

Please sign in to comment.