Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMN-1266 Add FORALL statements #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/definitions/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ pub const TOKENS: Tokens<'_> = Tokens {
T!("float"),
T!("follows"),
T!("for"),
T!("forall"),
T!("force"),
T!("foreign"),
T!("forward"),
Expand Down Expand Up @@ -344,6 +345,7 @@ pub const TOKENS: Tokens<'_> = Tokens {
T!("row"),
T!("rowid"),
T!("rowtype"),
T!("save"),
T!("scale"),
T!("schema"),
T!("scope"),
Expand Down Expand Up @@ -432,6 +434,7 @@ pub const SYNTAX_NODES: &'_ [SyntaxNode<'_>] = &[
S!("bind_var", "A bind variable, e.g. `:OLD`"),
S!("block", "A node that marks a block"),
S!("block_statement", "A node that marks an individual statement inside a block"),
S!("bounds_clause", "A node that contains a full bounds clause"),
S!("bulk_into_clause", "A node containing a BULK COLLECT INTO clause"),
S!("calc_meas_clause", "A node containing a calc meas clause"),
S!("case_stmt", "A node containing a CASE statement"),
Expand Down Expand Up @@ -472,6 +475,7 @@ pub const SYNTAX_NODES: &'_ [SyntaxNode<'_>] = &[
S!("expression", "Holds a generic SQL logic/arithmetic expression"),
S!("filter_clause", "A node that contains a full filter clause"),
S!("filter_clauses", "A node that contains a full filter clauses"),
S!("forall_stmt", "A node that contains a full forall statement"),
S!("for_loop", "A node containing a FOR LOOP"),
S!("func_decl_in_type", "A node containing a func_decl_in_type"),
S!("function", "A node that marks a full CREATE [..] FUNCTION block"),
Expand Down
6 changes: 5 additions & 1 deletion crates/source_gen/src/lexer/generated.rs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions crates/source_gen/src/syntax/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub enum SyntaxKind {
Block,
#[doc = "A node that marks an individual statement inside a block"]
BlockStatement,
#[doc = "A node that contains a full bounds clause"]
BoundsClause,
#[doc = "A node containing a BULK COLLECT INTO clause"]
BulkIntoClause,
#[doc = "A node containing a calc meas clause"]
Expand Down Expand Up @@ -126,6 +128,8 @@ pub enum SyntaxKind {
FilterClause,
#[doc = "A node that contains a full filter clauses"]
FilterClauses,
#[doc = "A node that contains a full forall statement"]
ForallStmt,
#[doc = "A node containing a FOR LOOP"]
ForLoop,
#[doc = "A node containing a func_decl_in_type"]
Expand Down Expand Up @@ -471,6 +475,7 @@ impl From<TokenKind> for SyntaxKind {
TokenKind::FloatKw => SyntaxKind::Keyword,
TokenKind::FollowsKw => SyntaxKind::Keyword,
TokenKind::ForKw => SyntaxKind::Keyword,
TokenKind::ForallKw => SyntaxKind::Keyword,
TokenKind::ForceKw => SyntaxKind::Keyword,
TokenKind::ForeignKw => SyntaxKind::Keyword,
TokenKind::ForwardKw => SyntaxKind::Keyword,
Expand Down Expand Up @@ -625,6 +630,7 @@ impl From<TokenKind> for SyntaxKind {
TokenKind::RowKw => SyntaxKind::Keyword,
TokenKind::RowidKw => SyntaxKind::Keyword,
TokenKind::RowtypeKw => SyntaxKind::Keyword,
TokenKind::SaveKw => SyntaxKind::Keyword,
TokenKind::ScaleKw => SyntaxKind::Keyword,
TokenKind::SchemaKw => SyntaxKind::Keyword,
TokenKind::ScopeKw => SyntaxKind::Keyword,
Expand Down
6 changes: 5 additions & 1 deletion src/grammar/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use source_gen::T;

use super::commit::parse_commit;
use super::loops::{parse_continue_stmt, parse_exit_stmt, parse_loop};
use super::{parse_cte, parse_cursor, parse_dml, parse_execute_immediate, parse_raise_stmt};
use super::{
parse_cte, parse_cursor, parse_dml, parse_execute_immediate, parse_forall_stmt,
parse_raise_stmt,
};

/// Parses a complete block.
pub fn parse_block(p: &mut Parser) {
Expand Down Expand Up @@ -64,6 +67,7 @@ pub(super) fn parse_stmt(p: &mut Parser) {
T![raise] => parse_raise_stmt(p),
T![delete] | T![update] => parse_dml(p),
T![commit] => parse_commit(p),
T![forall] => parse_forall_stmt(p),
current_token => {
if !(opt_assignment_stmt(p) || opt_procedure_call(p)) {
p.error(ParseErrorType::ExpectedStatement(current_token));
Expand Down
116 changes: 116 additions & 0 deletions src/grammar/forall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::Parser;

use super::{parse_dml, parse_expr, parse_ident};
use source_gen::{lexer::TokenKind, syntax::SyntaxKind, T};

pub(crate) fn parse_forall_stmt(p: &mut Parser) {
p.start(SyntaxKind::ForallStmt);
p.expect(T![forall]);
parse_ident(p, 1..2);
p.expect(T![in]);
parse_bounds_clause(p);
if p.eat(T![save]) {
p.expect(T![exceptions]);
}
parse_dml(p);
p.eat(T![;]);
p.finish();
}

pub(crate) fn parse_bounds_clause(p: &mut Parser) {
p.start(SyntaxKind::BoundsClause);
match p.current() {
T![values] => {
p.expect(T![values]);
p.expect(T![of]);
parse_ident(p, 1..2);
}
T![indices] => {
p.expect(T![indices]);
p.expect(T![of]);
parse_ident(p, 1..2);
if p.eat(T![between]) {
parse_expr(p);
p.expect(T![and]);
parse_expr(p);
}
}
_ => {
parse_expr(p);
p.expect(T![iter_range]);
parse_expr(p);
}
}
p.finish();
}

#[cfg(test)]
mod tests {
use expect_test::expect;

use crate::grammar::tests::{check, parse};

use super::*;

#[test]
fn test_parse_forall() {
check(
parse(
"FORALL i IN depts.FIRST..depts.LAST
DELETE FROM employees_temp
WHERE department_id = depts(i);",
parse_forall_stmt,
),
expect![[r#"
[email protected]
[email protected]
[email protected] "FORALL"
[email protected] " "
[email protected]
[email protected] "i"
[email protected] " "
[email protected] "IN"
[email protected] " "
[email protected]
[email protected]
[email protected] "depts"
[email protected] "."
[email protected] "FIRST"
[email protected] ".."
[email protected]
[email protected] "depts"
[email protected] "."
[email protected] "LAST"
[email protected] "\n "
[email protected]
[email protected] "DELETE"
[email protected] " "
[email protected] "FROM"
[email protected] " "
[email protected]
[email protected] "employees_temp"
[email protected] "\n "
[email protected]
[email protected] "WHERE"
[email protected] " "
[email protected]
[email protected]
[email protected] "department_id"
[email protected] " "
[email protected] "="
[email protected] " "
[email protected]
[email protected]
[email protected] "depts"
[email protected] "("
[email protected]
[email protected]
[email protected]
[email protected] "i"
[email protected] ")"
[email protected] ";"
"#]],
vec![],
);
}
}
2 changes: 2 additions & 0 deletions src/grammar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub(crate) use datatype::*;
pub(crate) use dml::*;
pub(crate) use execute_immediate::*;
pub(crate) use expressions::*;
pub(crate) use forall::*;
pub(crate) use function::*;
pub(crate) use function_invocation::*;
pub(crate) use package::*;
Expand Down Expand Up @@ -41,6 +42,7 @@ mod dml;
mod element_spec;
mod execute_immediate;
mod expressions;
mod forall;
mod function;
mod function_invocation;
mod loops;
Expand Down
Loading