Skip to content

Commit

Permalink
Break out query_term_t parser
Browse files Browse the repository at this point in the history
Makes non_join_query_term_t a derivative of query_term_t and prepares
the term parser for upcoming INTERSECTS processing.

Issue #78
  • Loading branch information
jaypipes committed May 29, 2018
1 parent a373aeb commit 6937708
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 11 deletions.
1 change: 1 addition & 0 deletions libsqltoast/include/sqltoast/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ std::ostream& operator<< (std::ostream& out, const position_expression_t& pe);
std::ostream& operator<< (std::ostream& out, const query_expression_t& qe);
std::ostream& operator<< (std::ostream& out, const query_specification_non_join_query_primary_t& primary);
std::ostream& operator<< (std::ostream& out, const query_specification_t& qs);
std::ostream& operator<< (std::ostream& out, const query_term_t& qt);
std::ostream& operator<< (std::ostream& out, const row_value_constructor_element_t& rvce);
std::ostream& operator<< (std::ostream& out, const row_value_constructor_t& rvc);
std::ostream& operator<< (std::ostream& out, const row_value_expression_t& rve);
Expand Down
5 changes: 3 additions & 2 deletions libsqltoast/include/sqltoast/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ typedef struct joined_table_query_term : query_term_t {
} joined_table_query_term_t;

typedef struct non_join_query_expression : query_expression_t {
std::unique_ptr<non_join_query_term_t> term;
// Guaranteed to be static_castable to non_join_query_term_t.
std::unique_ptr<query_term_t> term;
non_join_query_expression(
std::unique_ptr<non_join_query_term_t>& term) :
std::unique_ptr<query_term_t>& term) :
query_expression_t(QUERY_COMPONENT_TYPE_NON_JOIN),
term(std::move(term))
{}
Expand Down
6 changes: 5 additions & 1 deletion libsqltoast/src/parser/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ bool parse_non_join_query_expression(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_expression_t>& out);
bool parse_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_term_t>& out);
bool parse_non_join_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<non_join_query_term_t>& out);
std::unique_ptr<query_term_t>& out);
bool parse_non_join_query_primary(
parse_context_t& ctx,
token_t& cur_tok,
Expand Down
49 changes: 41 additions & 8 deletions libsqltoast/src/parser/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ namespace sqltoast {
// <non-join query expression>
// | <joined table>
//
// <query term> ::=
// <non-join query term>
// | <joined table>
//
// <query primary> ::=
// <non-join query primary>
// | <joined table>
Expand Down Expand Up @@ -55,14 +51,51 @@ bool parse_non_join_query_expression(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_expression_t>& out) {
std::unique_ptr<non_join_query_term_t> njqt;
std::unique_ptr<query_term_t> query_term;

if (! parse_non_join_query_term(ctx, cur_tok, njqt))
if (! parse_non_join_query_term(ctx, cur_tok, query_term))
return false;
// TODO(jaypipes): Handle UNION and EXCEPT
if (ctx.opts.disable_statement_construction)
return true;
out = std::make_unique<non_join_query_expression_t>(njqt);
out = std::make_unique<non_join_query_expression_t>(query_term);
return true;
}

// <query term> ::=
// <non-join query term>
// | <joined table>
bool parse_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_term_t>& out) {
lexer_t& lex = ctx.lexer;
parse_position_t start = lex.cursor;
token_t start_tok = lex.current_token;
std::unique_ptr<joined_table_t> joined_table;
if (parse_non_join_query_term(ctx, cur_tok, out))
return true;
if (ctx.result.code == PARSE_SYNTAX_ERROR)
return false;

// Reset cursor to before parsing of joined table attempt.
lex.cursor = start;
lex.current_token = cur_tok = start_tok;
if (! parse_joined_table(ctx, cur_tok, joined_table))
goto err_expect_joined_table;
goto push_joined_table_term;
err_expect_joined_table:
{
std::stringstream estr;
estr << "Expected <joined table> but found "
<< cur_tok << std::endl;
create_syntax_error_marker(ctx, estr);
return false;
}
push_joined_table_term:
if (ctx.opts.disable_statement_construction)
return true;
out = std::make_unique<joined_table_query_term_t>(joined_table);
return true;
}

Expand All @@ -73,7 +106,7 @@ bool parse_non_join_query_expression(
bool parse_non_join_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<non_join_query_term_t>& out) {
std::unique_ptr<query_term_t>& out) {
std::unique_ptr<non_join_query_primary_t> njqp;

if (! parse_non_join_query_primary(ctx, cur_tok, njqp))
Expand Down
20 changes: 20 additions & 0 deletions libsqltoast/src/print/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ std::ostream& operator<< (std::ostream& out, const non_join_query_expression_t&
return out;
}

std::ostream& operator<< (std::ostream& out, const query_term_t& term) {
switch (term.query_component_type) {
case QUERY_COMPONENT_TYPE_NON_JOIN:
{
const non_join_query_term_t& sub =
static_cast<const non_join_query_term_t&>(term);
out << sub;
}
break;
case QUERY_COMPONENT_TYPE_JOINED_TABLE:
{
const joined_table_query_term_t& sub =
static_cast<const joined_table_query_term_t&>(term);
out << sub;
}
break;
}
return out;
}

std::ostream& operator<< (std::ostream& out, const non_join_query_term_t& term) {
out << *term.primary;
return out;
Expand Down
19 changes: 19 additions & 0 deletions sqltoaster/print/yaml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::non_join_query_e
to_yaml(ptr, out, *qe.term);
}

void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_term_t& term) {
switch (term.query_component_type) {
case sqltoast::QUERY_COMPONENT_TYPE_NON_JOIN:
{
const sqltoast::non_join_query_term_t& sub =
static_cast<const sqltoast::non_join_query_term_t&>(term);
to_yaml(ptr, out, sub);
}
break;
case sqltoast::QUERY_COMPONENT_TYPE_JOINED_TABLE:
{
const sqltoast::joined_table_query_term_t& sub =
static_cast<const sqltoast::joined_table_query_term_t&>(term);
to_yaml(ptr, out, sub);
}
break;
}
}

void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::non_join_query_term_t& term) {
to_yaml(ptr, out, *term.primary);
}
Expand Down
1 change: 1 addition & 0 deletions sqltoaster/print/yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::quantified_compa
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_expression_t& qe);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_specification_non_join_query_primary_t& primary);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_specification_t& query);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_term_t& term);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::row_value_constructor_element_t& rvce, bool list_item);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::row_value_constructor_t& rvc);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::row_value_expression_t& rve);
Expand Down

0 comments on commit 6937708

Please sign in to comment.