Skip to content

Commit

Permalink
feat: add exit statement (#48)
Browse files Browse the repository at this point in the history
* feat: add exit statement

* test: add test

* fix: fix compile

* fix: fix case

* fix: fix comment
  • Loading branch information
dl239 authored Feb 7, 2023
1 parent 88b6989 commit 8605f2f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions zetasql/parser/ast_node_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ enum ASTNodeKind {
AST_WINDOW_ATTRIBUTE_INST_NOT_IN_WINDOW,
AST_WINDOW_ATTRIBUTE_LIST,
AST_LIKE_TABLE_CLAUSE,
AST_EXIT_STATEMENT,
kLastASTNodeKind = AST_LIKE_TABLE_CLAUSE,
};

Expand Down
15 changes: 15 additions & 0 deletions zetasql/parser/bison_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,8 @@ using zetasql::ASTDropStatement;
%token KW_STORED "STORED"
%token KW_STORING "STORING"
%token KW_STOP "STOP"
%token KW_QUIT "QUIT"
%token KW_EXIT "EXIT"
%token KW_SYSTEM "SYSTEM"
%token KW_SYSTEM_TIME "SYSTEM_TIME"
%token KW_TABLE "TABLE"
Expand Down Expand Up @@ -1135,6 +1137,7 @@ using zetasql::ASTDropStatement;
%type <node> model_clause
%type <node> module_statement
%type <node> use_statement
%type <node> exit_statement
%type <node> nested_dml_statement
%type <expression> new_constructor
%type <node> new_constructor_arg
Expand Down Expand Up @@ -1614,6 +1617,7 @@ sql_statement_body:
| load_statement
| into_statement
| stop_statement
| exit_statement
;

query_statement:
Expand Down Expand Up @@ -7603,6 +7607,8 @@ keyword_as_identifier:
| "STORED"
| "STORING"
| "STOP"
| "QUIT"
| "EXIT"
| "SYSTEM"
| "SYSTEM_TIME"
| "TABLE"
Expand Down Expand Up @@ -8426,6 +8432,11 @@ stop_statement:
}
;

exit_statement:
"EXIT" { $$ = MAKE_NODE(ASTExitStatement, @$); }
| "QUIT" { $$ = MAKE_NODE(ASTExitStatement, @$); }
;

opt_drop_mode:
"RESTRICT" { $$ = zetasql::ASTDropStatement::DropMode::RESTRICT; }
| "CASCADE" { $$ = zetasql::ASTDropStatement::DropMode::CASCADE; }
Expand Down Expand Up @@ -9108,6 +9119,10 @@ next_statement_kind_without_hint:
{ $$ = zetasql::ASTUseStatement::kConcreteNodeKind; }
| "STOP"
{ $$ = zetasql::ASTStopStatement::kConcreteNodeKind; }
| "EXIT"
{ $$ = zetasql::ASTExitStatement::kConcreteNodeKind; }
| "QUIT"
{ $$ = zetasql::ASTExitStatement::kConcreteNodeKind; }
;

%%
Expand Down
2 changes: 2 additions & 0 deletions zetasql/parser/flex_tokenizer.l
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ status { return BisonParserImpl::token::KW_STATUS; }
stored { return BisonParserImpl::token::KW_STORED; }
storing { return BisonParserImpl::token::KW_STORING; }
stop { return BisonParserImpl::token::KW_STOP; }
quit { return BisonParserImpl::token::KW_QUIT; }
exit { return BisonParserImpl::token::KW_EXIT; }
struct { return BisonParserImpl::token::KW_STRUCT; }
system { return BisonParserImpl::token::KW_SYSTEM; }
system_time { return BisonParserImpl::token::KW_SYSTEM_TIME; }
Expand Down
2 changes: 2 additions & 0 deletions zetasql/parser/keywords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"source", KW_SOURCE},
{"storing", KW_STORING},
{"stop", KW_STOP},
{"exit", KW_EXIT},
{"quit", KW_QUIT},
{"sql", KW_SQL},
{"stable", KW_STABLE},
{"start", KW_START},
Expand Down
1 change: 1 addition & 0 deletions zetasql/parser/parse_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ static absl::flat_hash_map<ASTNodeKind, std::string> CreateNodeNamesMap() {
map[AST_SELECT_INTO_STATEMENT] = "SelectIntoStatement";
map[AST_ESCAPED_EXPRESSION] = "EscapedExpression";
map[AST_STOP_STATEMENT] = "StopStatement";
map[AST_EXIT_STATEMENT] = "ExitStatement";
map[AST_CONFIG_CLAUSE] = "ConfigClause";
map[AST_WITH_WEIGHT] = "WithWeight";
map[AST_WITH_PARTITION_COLUMNS_CLAUSE] = "WithPartitionColumnsClause";
Expand Down
14 changes: 14 additions & 0 deletions zetasql/parser/parse_tree_manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,20 @@ class ASTStopStatement final : public ASTStatement {
const ASTTargetName* target_name_ = nullptr;
};

class ASTExitStatement final : public ASTStatement {
public:
static constexpr ASTNodeKind kConcreteNodeKind = AST_EXIT_STATEMENT;

ASTExitStatement() : ASTStatement(kConcreteNodeKind) {}
void Accept(ParseTreeVisitor* visitor, void* data) const override;
zetasql_base::StatusOr<VisitResult> Accept(
NonRecursiveParseTreeVisitor* visitor) const override;

private:
void InitFields() final {}
};


// Represents a RENAME statement.
class ASTRenameStatement final : public ASTStatement {
public:
Expand Down
27 changes: 27 additions & 0 deletions zetasql/parser/testdata/exit.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
EXIT;
--
ExitStatement [0-4]
--
EXIT
==

QUIT;
--
ExitStatement [0-4]
--
EXIT
==

QUIT ;
--
ExitStatement [0-4]
--
EXIT
==

EXIT aaa;
--
ERROR: Syntax error: Expected end of input but got identifier "aaa" [at 1:6]
EXIT aaa;
^
==
4 changes: 4 additions & 0 deletions zetasql/parser/unparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,10 @@ void Unparser::visitASTStopStatement(const ASTStopStatement* node, void* data) {
node->target_name()->Accept(this, data);
}

void Unparser::visitASTExitStatement(const ASTExitStatement* node, void* data) {
print("EXIT");
}

void Unparser::visitASTBeginStatement(
const ASTBeginStatement* node, void* data) {
print("BEGIN TRANSACTION");
Expand Down
1 change: 1 addition & 0 deletions zetasql/parser/unparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class Unparser : public ParseTreeVisitor {
void* data) override;
void visitASTDeployStatement(const ASTDeployStatement* node, void* data) override;
void visitASTStopStatement(const ASTStopStatement* node, void* data) override;
void visitASTExitStatement(const ASTExitStatement* node, void* data) override;
void visitASTBeginStatement(const ASTBeginStatement* node,
void* data) override;
void visitASTTransactionIsolationLevel(
Expand Down

0 comments on commit 8605f2f

Please sign in to comment.