Skip to content

Commit fb25886

Browse files
committed
Added ASTERISK
1 parent 0751109 commit fb25886

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

examples/main_pgsql_example.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ int main() {
1010
"SELECT name FROM users;",
1111
"INSERT INTO products VALUES ('a new gadget');",
1212
"QUIT;",
13-
"SELECT * FROM;",
14-
"INSERT INTO logs VALUES (no_quotes_here);"
13+
"SELECT * FROM tablenameA;",
14+
"SELECT * FROM tablenameB",
15+
"SELECT * FROM;",
16+
"INSERT INTO logs VALUES (no_quotes_here);"
1517
};
1618

1719
for (const auto& query : queries) {

include/pgsql_parser/pgsql_ast.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// include/pgsql_parser/pgsql_ast.h
12
#ifndef PGSQL_PARSER_AST_H
23
#define PGSQL_PARSER_AST_H
34

@@ -6,7 +7,7 @@
67
#include <iostream>
78
#include <algorithm> // For potential string manipulations
89

9-
namespace PgsqlParser { // Changed namespace
10+
namespace PgsqlParser {
1011

1112
// Enum for different types of AST nodes
1213
enum class NodeType {
@@ -15,7 +16,8 @@ enum class NodeType {
1516
NODE_SELECT_STATEMENT,
1617
NODE_INSERT_STATEMENT,
1718
NODE_IDENTIFIER,
18-
NODE_STRING_LITERAL
19+
NODE_STRING_LITERAL,
20+
NODE_ASTERISK // New node type for '*' in SELECT
1921
};
2022

2123
// Basic AST Node
@@ -57,6 +59,7 @@ inline void print_ast(const AstNode* node, int indent = 0) {
5759
case NodeType::NODE_INSERT_STATEMENT: type_str = "INSERT_STMT"; break;
5860
case NodeType::NODE_IDENTIFIER: type_str = "IDENTIFIER"; break;
5961
case NodeType::NODE_STRING_LITERAL: type_str = "STRING_LITERAL"; break;
62+
case NodeType::NODE_ASTERISK: type_str = "ASTERISK"; break; // Handle new type
6063
default: type_str = "UNHANDLED_TYPE(" + std::to_string(static_cast<int>(node->type)) + ")"; break;
6164
}
6265
std::cout << "Type: " << type_str << ", Value: '" << node->value << "'" << std::endl;

src/pgsql_parser/pgsql_lexer.l

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
return TOKEN_STRING_LITERAL;
4747
}
4848

49+
"*" { return TOKEN_ASTERISK; }
50+
4951
"(" { return TOKEN_LPAREN; }
5052
")" { return TOKEN_RPAREN; }
5153
";" { return TOKEN_SEMICOLON; }

src/pgsql_parser/pgsql_parser.y

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
/* src/pgsql_parser/pgsql_parser.y */
12
%code requires {
23
namespace PgsqlParser {
34
struct AstNode;
45
}
56
#include <string>
67
}
78

9+
810
%{ // C PROLOGUE - This code is now part of a C++ compilation unit
911
#include "pgsql_parser/pgsql_parser.h" // For PgsqlParser::Parser context & yyscan_t
1012

@@ -36,15 +38,14 @@ int pgsql_yylex(union PGSQL_YYSTYPE* yylval_param, yyscan_t yyscanner, PgsqlPars
3638
PgsqlParser::AstNode* node_val;
3739
}
3840

39-
/* Declare tokens from the lexer */
4041
%token TOKEN_SELECT TOKEN_FROM TOKEN_INSERT TOKEN_INTO TOKEN_VALUES
4142
%token TOKEN_LPAREN TOKEN_RPAREN TOKEN_SEMICOLON
43+
%token TOKEN_ASTERISK
4244

4345
%token <str_val> TOKEN_QUIT
4446
%token <str_val> TOKEN_IDENTIFIER
4547
%token <str_val> TOKEN_STRING_LITERAL
4648

47-
/* Declare non-terminals and the type of their semantic value */
4849
%type <node_val> statement
4950
%type <node_val> simple_statement
5051
%type <node_val> command_statement
@@ -53,9 +54,11 @@ int pgsql_yylex(union PGSQL_YYSTYPE* yylval_param, yyscan_t yyscanner, PgsqlPars
5354
%type <node_val> identifier_node
5455
%type <node_val> string_literal_node
5556
%type <node_val> value_for_insert
57+
%type <node_val> select_list_item
58+
%type <node_val> optional_semicolon /* <<< NEW TYPE (though it produces no value for AST) */
5659

5760

58-
%start query_list /* The start symbol of our grammar */
61+
%start query_list
5962

6063
%%
6164

@@ -64,6 +67,12 @@ query_list:
6467
| query_list statement { /* Manages last statement's AST. */ }
6568
;
6669

70+
/* NEW RULE for optional semicolon */
71+
optional_semicolon:
72+
TOKEN_SEMICOLON { $$ = nullptr; /* Or some placeholder if needed, but usually not for this */ }
73+
| /* empty */ { $$ = nullptr; } /* Allows nothing, produces no AST node for itself */
74+
;
75+
6776
statement:
6877
simple_statement { $$ = $1; if (parser_context) parser_context->internal_set_ast($1); }
6978
| select_statement { $$ = $1; if (parser_context) parser_context->internal_set_ast($1); }
@@ -75,9 +84,10 @@ simple_statement:
7584
;
7685

7786
command_statement:
78-
TOKEN_QUIT TOKEN_SEMICOLON {
87+
TOKEN_QUIT optional_semicolon {
7988
$$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_COMMAND, std::move(*$1));
8089
delete $1;
90+
// $2 (optional_semicolon) doesn't contribute a meaningful value here
8191
}
8292
;
8393

@@ -99,11 +109,19 @@ string_literal_node:
99109
}
100110
;
101111

112+
select_list_item:
113+
identifier_node { $$ = $1; }
114+
| TOKEN_ASTERISK {
115+
$$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_ASTERISK, "*");
116+
}
117+
;
118+
102119
select_statement:
103-
TOKEN_SELECT identifier_node TOKEN_FROM identifier_node TOKEN_SEMICOLON {
120+
TOKEN_SELECT select_list_item TOKEN_FROM identifier_node optional_semicolon { /* MODIFIED */
104121
$$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_SELECT_STATEMENT);
105122
$$->addChild($2);
106123
$$->addChild($4);
124+
// $5 (optional_semicolon) doesn't contribute
107125
}
108126
;
109127

@@ -112,14 +130,16 @@ value_for_insert:
112130
;
113131

114132
insert_statement:
115-
TOKEN_INSERT TOKEN_INTO identifier_node TOKEN_VALUES TOKEN_LPAREN value_for_insert TOKEN_RPAREN TOKEN_SEMICOLON {
133+
TOKEN_INSERT TOKEN_INTO identifier_node TOKEN_VALUES TOKEN_LPAREN value_for_insert TOKEN_RPAREN optional_semicolon {
116134
$$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_INSERT_STATEMENT);
117135
$$->addChild($3);
118136
$$->addChild($6);
137+
// $8 (optional_semicolon) doesn't contribute
119138
}
120139
;
121140

122141
%%
123142

124143
// pgsql_yyerror is defined in pgsql_parser.cpp with C linkage,
125144
// and declared in pgsql_parser.h. Bison will generate a call to it.
145+

0 commit comments

Comments
 (0)