-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the unparser to the C++ version as well.
- Loading branch information
Sreeni Viswanadha
committed
Mar 22, 2021
1 parent
ffc3e84
commit 1feb271
Showing
10 changed files
with
804 additions
and
745 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
SRC_DIR=./target/generated-sources/javacc | ||
TARGET_DIR=./target | ||
clang++ -fbracket-depth=1024 -funsigned-char -I. -I./$SRC_DIR -o $TARGET_DIR/sqlparser -std=c++11 ParseErrorHandler.cc main.cc $SRC_DIR/*.cc | ||
clang++ -O2 -fbracket-depth=1024 -funsigned-char -I. -I./$SRC_DIR -o $TARGET_DIR/sqlparser -std=c++11 ParseErrorHandler.cc main.cc $SRC_DIR/*.cc unparser.cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2->0 (the "License"); | ||
* you may not use this file except in compliance with the License-> | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www->apache->org/licenses/LICENSE-2->0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied-> | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License-> | ||
*/ | ||
|
||
#include "unparser.h" | ||
|
||
namespace commonsql { | ||
namespace parser { | ||
|
||
void Unparser::printSpecialTokens(const Token* t) | ||
{ | ||
// special tokens are chained in reverse-> | ||
Token* specialToken = t->specialToken; | ||
if (specialToken != nullptr) { | ||
while (specialToken->specialToken != nullptr) { | ||
specialToken = specialToken->specialToken; | ||
} | ||
while (specialToken != nullptr) { | ||
stringBuilder << specialToken->image.c_str(); | ||
specialToken = specialToken->next; | ||
} | ||
} | ||
} | ||
|
||
void Unparser::printTrailingComments() | ||
{ | ||
if (lastToken != nullptr && lastToken->kind == EOF) { | ||
printSpecialTokens(lastToken); | ||
} | ||
} | ||
|
||
void Unparser::printToken(const std::string& s) | ||
{ | ||
stringBuilder << " " << s.c_str() << " "; | ||
} | ||
|
||
void Unparser::printToken(const Token* t) | ||
{ | ||
while (lastToken != t) { | ||
lastToken = lastToken->next; | ||
printSpecialTokens(lastToken); | ||
stringBuilder << lastToken->image; | ||
} | ||
} | ||
|
||
void Unparser::printUptoToken(const Token* t) | ||
{ | ||
while (lastToken->next != t) { | ||
printToken(lastToken->next); | ||
} | ||
} | ||
|
||
void Unparser::moveToEndOfNode(const AstNode* node) | ||
{ | ||
lastToken = node->endToken; | ||
} | ||
|
||
void Unparser::unparseUpto(const AstNode* node) | ||
{ | ||
printUptoToken(node->beginToken); | ||
} | ||
|
||
void Unparser::unparseRestOfTheNode(const AstNode* node) | ||
{ | ||
if (lastToken != node->endToken) { | ||
printUptoToken(node->endToken); | ||
} | ||
} | ||
|
||
void Unparser::defaultVisit(const SimpleNode* node, void* data) | ||
{ | ||
const AstNode* astNode = static_cast<const AstNode*>(node); | ||
const AstNode* firstChild = astNode->firstChild(); | ||
const AstNode* lastChild = astNode->lastChild(); | ||
|
||
// Print the tokens-> | ||
if (firstChild == nullptr || astNode->beginToken != firstChild->beginToken) { | ||
printToken(astNode->beginToken); | ||
} | ||
|
||
astNode->VisitChildren(this); | ||
|
||
if (lastChild != nullptr && astNode->endToken != lastChild->endToken) { | ||
printToken(astNode->endToken); | ||
} | ||
} | ||
|
||
std::string Unparser::unparse(const AstNode* node) | ||
{ | ||
stringBuilder.clear(); | ||
lastToken->next = node->beginToken; | ||
node->VisitNode(this); | ||
printTrailingComments(); | ||
return getUnparsedString(); | ||
} | ||
|
||
std::string unparse(const AstNode* node, Unparser* unparser) | ||
{ | ||
return unparser->unparse(node); | ||
} | ||
|
||
std::string unparse(const AstNode* node) | ||
{ | ||
return unparse(node, new Unparser()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef __UNPARSE_H__ | ||
#define __UNPARSE_H__ | ||
|
||
#include <sstream> | ||
#include "AstNode.h" | ||
#include "SqlParserVisitor.h" | ||
|
||
namespace commonsql { | ||
namespace parser { | ||
|
||
using std::string; | ||
using std::stringstream; | ||
|
||
class Unparser : public SqlParserDefaultVisitor { | ||
private: | ||
Token* lastToken = new Token(); | ||
|
||
virtual void printSpecialTokens(const Token* t); | ||
virtual void printTrailingComments(); | ||
|
||
protected: | ||
std::stringstream stringBuilder; | ||
|
||
virtual void printToken(const std::string& s); | ||
virtual void printToken(const Token* t); | ||
virtual void printUptoToken(const Token* t); | ||
virtual void moveToEndOfNode(const AstNode* node); | ||
virtual void unparseUpto(const AstNode* node); | ||
virtual void unparseRestOfTheNode(const AstNode* node); | ||
virtual const string getUnparsedString() const { return stringBuilder.str(); } | ||
|
||
public: | ||
string unparse(const AstNode* node); | ||
void defaultVisit(const SimpleNode* node, void* data); | ||
virtual ~Unparser() {} | ||
}; | ||
|
||
std::string unparse(const AstNode* node, Unparser* unparser); | ||
std::string unparse(const AstNode* node); | ||
|
||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.