From 1feb271c8eeed4df9177dee29b9390bfa6e0757f Mon Sep 17 00:00:00 2001 From: Sreeni Viswanadha Date: Fri, 5 Feb 2021 12:35:18 -0800 Subject: [PATCH] Added the unparser to the C++ version as well. --- parser/cpp/AstNode.h | 32 +- parser/cpp/SqlParserVisitor.h | 562 ++++++++++++++ parser/cpp/build.sh | 1 + parser/cpp/compile.sh | 2 +- parser/cpp/javacc-options.txt | 5 +- parser/cpp/main.cc | 13 +- parser/cpp/unparser.cc | 118 +++ parser/cpp/unparser.h | 44 ++ .../com/facebook/coresql/parser/Unparser.java | 734 +----------------- pom.xml | 38 +- 10 files changed, 804 insertions(+), 745 deletions(-) create mode 100644 parser/cpp/SqlParserVisitor.h create mode 100644 parser/cpp/unparser.cc create mode 100644 parser/cpp/unparser.h diff --git a/parser/cpp/AstNode.h b/parser/cpp/AstNode.h index 67a92b8..0fdf838 100644 --- a/parser/cpp/AstNode.h +++ b/parser/cpp/AstNode.h @@ -20,10 +20,36 @@ class AstNode : public SimpleNode { Token* endToken; virtual ~AstNode() {} AstNode(int id) : SimpleNode(id) {} + AstNode(SqlParser* parser, int id) : SimpleNode(parser, id) {} - int NumChildren() { return jjtGetNumChildren(); } - AstNode* GetChild(int i) { return static_cast(jjtGetChild(i)); } - JJString toString(const JJString& prefix) const { + + int kind() const { return id; } + + int numChildren() const { return jjtGetNumChildren(); } + + AstNode* getChild(int i) const { return static_cast(jjtGetChild(i)); } + + std::string getImage() const { return numChildren() == 0 ? beginToken->image : string(""); } + + void VisitNode(SqlParserVisitor* visitor) const { jjtAccept(visitor, nullptr); } + + void VisitChildren(SqlParserVisitor* visitor) const { jjtChildrenAccept(visitor, nullptr); } + + const AstNode* firstChild() const { return numChildren() > 0 ? getChild(0) : nullptr; } + + const AstNode* lastChild() const { return numChildren() > 0 ? getChild(numChildren() - 1) : nullptr; } + + const AstNode* getFirstChildOfKind(int kind) const { + for (int i = 0; i < numChildren(); i++) { + if (getChild(i)->kind() == kind) { + return getChild(i); + } + } + + return nullptr; + } + + std::string toString(const JJString& prefix) const { return SimpleNode::toString(prefix) + string(" (") + std::to_string(beginToken->beginLine) + diff --git a/parser/cpp/SqlParserVisitor.h b/parser/cpp/SqlParserVisitor.h new file mode 100644 index 0000000..340313d --- /dev/null +++ b/parser/cpp/SqlParserVisitor.h @@ -0,0 +1,562 @@ +/* Generated By:JavaCC: Do not edit this line. SqlParserVisitor.h Version 7.0.10 */ +#ifndef SQLPARSERVISITOR_H +#define SQLPARSERVISITOR_H + +#include "JavaCC.h" +#include "SqlParserTree.h" +namespace commonsql { +namespace parser { +class SqlParserVisitor +{ + public: + virtual void visit(const SimpleNode *node, void * data) = 0; + virtual void visitCompilationUnit(const CompilationUnit *node, void * data) = 0; + virtual void visitUnaryExpression(const UnaryExpression *node, void * data) = 0; + virtual void visitUnsignedNumericLiteral(const UnsignedNumericLiteral *node, void * data) = 0; + virtual void visitCharStringLiteral(const CharStringLiteral *node, void * data) = 0; + virtual void visitDateLiteral(const DateLiteral *node, void * data) = 0; + virtual void visitTimeLiteral(const TimeLiteral *node, void * data) = 0; + virtual void visitTimestampLiteral(const TimestampLiteral *node, void * data) = 0; + virtual void visitIntervalLiteral(const IntervalLiteral *node, void * data) = 0; + virtual void visitBooleanLiteral(const BooleanLiteral *node, void * data) = 0; + virtual void visitUnsupported(const Unsupported *node, void * data) = 0; + virtual void visitIdentifier(const Identifier *node, void * data) = 0; + virtual void visitTableName(const TableName *node, void * data) = 0; + virtual void visitSchemaName(const SchemaName *node, void * data) = 0; + virtual void visitCatalogName(const CatalogName *node, void * data) = 0; + virtual void visitSchemaQualifiedName(const SchemaQualifiedName *node, void * data) = 0; + virtual void visitPredefinedType(const PredefinedType *node, void * data) = 0; + virtual void visitRowType(const RowType *node, void * data) = 0; + virtual void visitArrayType(const ArrayType *node, void * data) = 0; + virtual void visitFieldDefinition(const FieldDefinition *node, void * data) = 0; + virtual void visitParenthesizedExpression(const ParenthesizedExpression *node, void * data) = 0; + virtual void visitBuiltinValue(const BuiltinValue *node, void * data) = 0; + virtual void visitNullLiteral(const NullLiteral *node, void * data) = 0; + virtual void visitArrayLiteral(const ArrayLiteral *node, void * data) = 0; + virtual void visitQualifiedName(const QualifiedName *node, void * data) = 0; + virtual void visitGroupingOperation(const GroupingOperation *node, void * data) = 0; + virtual void visitWindowFunction(const WindowFunction *node, void * data) = 0; + virtual void visitRowNumber(const RowNumber *node, void * data) = 0; + virtual void visitRankFunction(const RankFunction *node, void * data) = 0; + virtual void visitNtileFunction(const NtileFunction *node, void * data) = 0; + virtual void visitLeadOrLag(const LeadOrLag *node, void * data) = 0; + virtual void visitNullTreatment(const NullTreatment *node, void * data) = 0; + virtual void visitFirstOrLastValueFunction(const FirstOrLastValueFunction *node, void * data) = 0; + virtual void visitNullIf(const NullIf *node, void * data) = 0; + virtual void visitCoalesce(const Coalesce *node, void * data) = 0; + virtual void visitCaseExpression(const CaseExpression *node, void * data) = 0; + virtual void visitWhenClause(const WhenClause *node, void * data) = 0; + virtual void visitElseClause(const ElseClause *node, void * data) = 0; + virtual void visitWhenOperand(const WhenOperand *node, void * data) = 0; + virtual void visitSearchedCaseOperand(const SearchedCaseOperand *node, void * data) = 0; + virtual void visitCastEpression(const CastEpression *node, void * data) = 0; + virtual void visitFieldReference(const FieldReference *node, void * data) = 0; + virtual void visitFunctionCall(const FunctionCall *node, void * data) = 0; + virtual void visitAggregationFunction(const AggregationFunction *node, void * data) = 0; + virtual void visitUnused(const Unused *node, void * data) = 0; + virtual void visitLambda(const Lambda *node, void * data) = 0; + virtual void visitArrayElement(const ArrayElement *node, void * data) = 0; + virtual void visitAdd(const Add *node, void * data) = 0; + virtual void visitSubtract(const Subtract *node, void * data) = 0; + virtual void visitMultiply(const Multiply *node, void * data) = 0; + virtual void visitDivide(const Divide *node, void * data) = 0; + virtual void visitMod(const Mod *node, void * data) = 0; + virtual void visitTimeZoneField(const TimeZoneField *node, void * data) = 0; + virtual void visitConcatenation(const Concatenation *node, void * data) = 0; + virtual void visitAdditiveEpression(const AdditiveEpression *node, void * data) = 0; + virtual void visitMultiplicativeExpression(const MultiplicativeExpression *node, void * data) = 0; + virtual void visitOrExpression(const OrExpression *node, void * data) = 0; + virtual void visitAndExpression(const AndExpression *node, void * data) = 0; + virtual void visitNotExpression(const NotExpression *node, void * data) = 0; + virtual void visitIsExpression(const IsExpression *node, void * data) = 0; + virtual void visitRowExpression(const RowExpression *node, void * data) = 0; + virtual void visitRowExression(const RowExression *node, void * data) = 0; + virtual void visitValues(const Values *node, void * data) = 0; + virtual void visitTableExpression(const TableExpression *node, void * data) = 0; + virtual void visitFromClause(const FromClause *node, void * data) = 0; + virtual void visitCommaJoin(const CommaJoin *node, void * data) = 0; + virtual void visitJoin(const Join *node, void * data) = 0; + virtual void visitTableSample(const TableSample *node, void * data) = 0; + virtual void visitAliasedTable(const AliasedTable *node, void * data) = 0; + virtual void visitAlias(const Alias *node, void * data) = 0; + virtual void visitUnnest(const Unnest *node, void * data) = 0; + virtual void visitColumnNames(const ColumnNames *node, void * data) = 0; + virtual void visitOnClause(const OnClause *node, void * data) = 0; + virtual void visitUsingClause(const UsingClause *node, void * data) = 0; + virtual void visitWhereClause(const WhereClause *node, void * data) = 0; + virtual void visitGroupbyClause(const GroupbyClause *node, void * data) = 0; + virtual void visitRollup(const Rollup *node, void * data) = 0; + virtual void visitCube(const Cube *node, void * data) = 0; + virtual void visitGroupingSets(const GroupingSets *node, void * data) = 0; + virtual void visitHavingClause(const HavingClause *node, void * data) = 0; + virtual void visitWindowSpecification(const WindowSpecification *node, void * data) = 0; + virtual void visitPartitionByClause(const PartitionByClause *node, void * data) = 0; + virtual void visitOrderByClause(const OrderByClause *node, void * data) = 0; + virtual void visitWindowFrameUnits(const WindowFrameUnits *node, void * data) = 0; + virtual void visitWindowFrameExtent(const WindowFrameExtent *node, void * data) = 0; + virtual void visitUnboundedPreceding(const UnboundedPreceding *node, void * data) = 0; + virtual void visitCurrentRow(const CurrentRow *node, void * data) = 0; + virtual void visitWindowFramePreceding(const WindowFramePreceding *node, void * data) = 0; + virtual void visitWindowFrameBetween(const WindowFrameBetween *node, void * data) = 0; + virtual void visitUnboundedFollowing(const UnboundedFollowing *node, void * data) = 0; + virtual void visitWindowFrameFollowing(const WindowFrameFollowing *node, void * data) = 0; + virtual void visitSelect(const Select *node, void * data) = 0; + virtual void visitSelectList(const SelectList *node, void * data) = 0; + virtual void visitSelectItem(const SelectItem *node, void * data) = 0; + virtual void visitStar(const Star *node, void * data) = 0; + virtual void visitQuerySpecification(const QuerySpecification *node, void * data) = 0; + virtual void visitWithClause(const WithClause *node, void * data) = 0; + virtual void visitCte(const Cte *node, void * data) = 0; + virtual void visitSetOperation(const SetOperation *node, void * data) = 0; + virtual void visitSubquery(const Subquery *node, void * data) = 0; + virtual void visitComparison(const Comparison *node, void * data) = 0; + virtual void visitBetween(const Between *node, void * data) = 0; + virtual void visitInPredicate(const InPredicate *node, void * data) = 0; + virtual void visitInvalueList(const InvalueList *node, void * data) = 0; + virtual void visitLike(const Like *node, void * data) = 0; + virtual void visitIsNull(const IsNull *node, void * data) = 0; + virtual void visitQuantifiedComparison(const QuantifiedComparison *node, void * data) = 0; + virtual void visitExists(const Exists *node, void * data) = 0; + virtual void visitIsDistinct(const IsDistinct *node, void * data) = 0; + virtual void visitInvervalQualifier(const InvervalQualifier *node, void * data) = 0; + virtual void visitNonSecondField(const NonSecondField *node, void * data) = 0; + virtual void visitSecondField(const SecondField *node, void * data) = 0; + virtual void visitNonSecondDateTimeField(const NonSecondDateTimeField *node, void * data) = 0; + virtual void visitLanguageClause(const LanguageClause *node, void * data) = 0; + virtual void visitArgumentList(const ArgumentList *node, void * data) = 0; + virtual void visitNamedArgument(const NamedArgument *node, void * data) = 0; + virtual void visitSetQuantifier(const SetQuantifier *node, void * data) = 0; + virtual void visitFilterClause(const FilterClause *node, void * data) = 0; + virtual void visitSortSpecificationList(const SortSpecificationList *node, void * data) = 0; + virtual void visitSortSpecification(const SortSpecification *node, void * data) = 0; + virtual void visitOrderingDirection(const OrderingDirection *node, void * data) = 0; + virtual void visitNullOrdering(const NullOrdering *node, void * data) = 0; + virtual void visitCreateSchema(const CreateSchema *node, void * data) = 0; + virtual void visitUnsuppoerted(const Unsuppoerted *node, void * data) = 0; + virtual void visitTableDefinition(const TableDefinition *node, void * data) = 0; + virtual void visitInsert(const Insert *node, void * data) = 0; + virtual void visitDirectSqlStatement(const DirectSqlStatement *node, void * data) = 0; + virtual void visitUseStatement(const UseStatement *node, void * data) = 0; + virtual void visitLambdaBody(const LambdaBody *node, void * data) = 0; + virtual void visitLambdaParams(const LambdaParams *node, void * data) = 0; + virtual void visitLimitClause(const LimitClause *node, void * data) = 0; + virtual void visitParameterizedType(const ParameterizedType *node, void * data) = 0; + virtual void visitMapType(const MapType *node, void * data) = 0; + virtual void visitTryExpression(const TryExpression *node, void * data) = 0; + virtual void visitCastExpression(const CastExpression *node, void * data) = 0; + virtual ~SqlParserVisitor() { } +}; +class SqlParserDefaultVisitor : public SqlParserVisitor { +public: + virtual void defaultVisit(const SimpleNode *node, void * data) = 0; + virtual void visit(const SimpleNode *node, void * data) { + defaultVisit(node, data); +} + virtual void visitCompilationUnit(const CompilationUnit *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnaryExpression(const UnaryExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnsignedNumericLiteral(const UnsignedNumericLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCharStringLiteral(const CharStringLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitDateLiteral(const DateLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTimeLiteral(const TimeLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTimestampLiteral(const TimestampLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitIntervalLiteral(const IntervalLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitBooleanLiteral(const BooleanLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnsupported(const Unsupported *node, void * data) { + defaultVisit(node, data); + } + virtual void visitIdentifier(const Identifier *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTableName(const TableName *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSchemaName(const SchemaName *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCatalogName(const CatalogName *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSchemaQualifiedName(const SchemaQualifiedName *node, void * data) { + defaultVisit(node, data); + } + virtual void visitPredefinedType(const PredefinedType *node, void * data) { + defaultVisit(node, data); + } + virtual void visitRowType(const RowType *node, void * data) { + defaultVisit(node, data); + } + virtual void visitArrayType(const ArrayType *node, void * data) { + defaultVisit(node, data); + } + virtual void visitFieldDefinition(const FieldDefinition *node, void * data) { + defaultVisit(node, data); + } + virtual void visitParenthesizedExpression(const ParenthesizedExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitBuiltinValue(const BuiltinValue *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNullLiteral(const NullLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitArrayLiteral(const ArrayLiteral *node, void * data) { + defaultVisit(node, data); + } + virtual void visitQualifiedName(const QualifiedName *node, void * data) { + defaultVisit(node, data); + } + virtual void visitGroupingOperation(const GroupingOperation *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWindowFunction(const WindowFunction *node, void * data) { + defaultVisit(node, data); + } + virtual void visitRowNumber(const RowNumber *node, void * data) { + defaultVisit(node, data); + } + virtual void visitRankFunction(const RankFunction *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNtileFunction(const NtileFunction *node, void * data) { + defaultVisit(node, data); + } + virtual void visitLeadOrLag(const LeadOrLag *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNullTreatment(const NullTreatment *node, void * data) { + defaultVisit(node, data); + } + virtual void visitFirstOrLastValueFunction(const FirstOrLastValueFunction *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNullIf(const NullIf *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCoalesce(const Coalesce *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCaseExpression(const CaseExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWhenClause(const WhenClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitElseClause(const ElseClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWhenOperand(const WhenOperand *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSearchedCaseOperand(const SearchedCaseOperand *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCastEpression(const CastEpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitFieldReference(const FieldReference *node, void * data) { + defaultVisit(node, data); + } + virtual void visitFunctionCall(const FunctionCall *node, void * data) { + defaultVisit(node, data); + } + virtual void visitAggregationFunction(const AggregationFunction *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnused(const Unused *node, void * data) { + defaultVisit(node, data); + } + virtual void visitLambda(const Lambda *node, void * data) { + defaultVisit(node, data); + } + virtual void visitArrayElement(const ArrayElement *node, void * data) { + defaultVisit(node, data); + } + virtual void visitAdd(const Add *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSubtract(const Subtract *node, void * data) { + defaultVisit(node, data); + } + virtual void visitMultiply(const Multiply *node, void * data) { + defaultVisit(node, data); + } + virtual void visitDivide(const Divide *node, void * data) { + defaultVisit(node, data); + } + virtual void visitMod(const Mod *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTimeZoneField(const TimeZoneField *node, void * data) { + defaultVisit(node, data); + } + virtual void visitConcatenation(const Concatenation *node, void * data) { + defaultVisit(node, data); + } + virtual void visitAdditiveEpression(const AdditiveEpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitMultiplicativeExpression(const MultiplicativeExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitOrExpression(const OrExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitAndExpression(const AndExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNotExpression(const NotExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitIsExpression(const IsExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitRowExpression(const RowExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitRowExression(const RowExression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitValues(const Values *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTableExpression(const TableExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitFromClause(const FromClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCommaJoin(const CommaJoin *node, void * data) { + defaultVisit(node, data); + } + virtual void visitJoin(const Join *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTableSample(const TableSample *node, void * data) { + defaultVisit(node, data); + } + virtual void visitAliasedTable(const AliasedTable *node, void * data) { + defaultVisit(node, data); + } + virtual void visitAlias(const Alias *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnnest(const Unnest *node, void * data) { + defaultVisit(node, data); + } + virtual void visitColumnNames(const ColumnNames *node, void * data) { + defaultVisit(node, data); + } + virtual void visitOnClause(const OnClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUsingClause(const UsingClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWhereClause(const WhereClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitGroupbyClause(const GroupbyClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitRollup(const Rollup *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCube(const Cube *node, void * data) { + defaultVisit(node, data); + } + virtual void visitGroupingSets(const GroupingSets *node, void * data) { + defaultVisit(node, data); + } + virtual void visitHavingClause(const HavingClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWindowSpecification(const WindowSpecification *node, void * data) { + defaultVisit(node, data); + } + virtual void visitPartitionByClause(const PartitionByClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitOrderByClause(const OrderByClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWindowFrameUnits(const WindowFrameUnits *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWindowFrameExtent(const WindowFrameExtent *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnboundedPreceding(const UnboundedPreceding *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCurrentRow(const CurrentRow *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWindowFramePreceding(const WindowFramePreceding *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWindowFrameBetween(const WindowFrameBetween *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnboundedFollowing(const UnboundedFollowing *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWindowFrameFollowing(const WindowFrameFollowing *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSelect(const Select *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSelectList(const SelectList *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSelectItem(const SelectItem *node, void * data) { + defaultVisit(node, data); + } + virtual void visitStar(const Star *node, void * data) { + defaultVisit(node, data); + } + virtual void visitQuerySpecification(const QuerySpecification *node, void * data) { + defaultVisit(node, data); + } + virtual void visitWithClause(const WithClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCte(const Cte *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSetOperation(const SetOperation *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSubquery(const Subquery *node, void * data) { + defaultVisit(node, data); + } + virtual void visitComparison(const Comparison *node, void * data) { + defaultVisit(node, data); + } + virtual void visitBetween(const Between *node, void * data) { + defaultVisit(node, data); + } + virtual void visitInPredicate(const InPredicate *node, void * data) { + defaultVisit(node, data); + } + virtual void visitInvalueList(const InvalueList *node, void * data) { + defaultVisit(node, data); + } + virtual void visitLike(const Like *node, void * data) { + defaultVisit(node, data); + } + virtual void visitIsNull(const IsNull *node, void * data) { + defaultVisit(node, data); + } + virtual void visitQuantifiedComparison(const QuantifiedComparison *node, void * data) { + defaultVisit(node, data); + } + virtual void visitExists(const Exists *node, void * data) { + defaultVisit(node, data); + } + virtual void visitIsDistinct(const IsDistinct *node, void * data) { + defaultVisit(node, data); + } + virtual void visitInvervalQualifier(const InvervalQualifier *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNonSecondField(const NonSecondField *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSecondField(const SecondField *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNonSecondDateTimeField(const NonSecondDateTimeField *node, void * data) { + defaultVisit(node, data); + } + virtual void visitLanguageClause(const LanguageClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitArgumentList(const ArgumentList *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNamedArgument(const NamedArgument *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSetQuantifier(const SetQuantifier *node, void * data) { + defaultVisit(node, data); + } + virtual void visitFilterClause(const FilterClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSortSpecificationList(const SortSpecificationList *node, void * data) { + defaultVisit(node, data); + } + virtual void visitSortSpecification(const SortSpecification *node, void * data) { + defaultVisit(node, data); + } + virtual void visitOrderingDirection(const OrderingDirection *node, void * data) { + defaultVisit(node, data); + } + virtual void visitNullOrdering(const NullOrdering *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCreateSchema(const CreateSchema *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUnsuppoerted(const Unsuppoerted *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTableDefinition(const TableDefinition *node, void * data) { + defaultVisit(node, data); + } + virtual void visitInsert(const Insert *node, void * data) { + defaultVisit(node, data); + } + virtual void visitDirectSqlStatement(const DirectSqlStatement *node, void * data) { + defaultVisit(node, data); + } + virtual void visitUseStatement(const UseStatement *node, void * data) { + defaultVisit(node, data); + } + virtual void visitLambdaBody(const LambdaBody *node, void * data) { + defaultVisit(node, data); + } + virtual void visitLambdaParams(const LambdaParams *node, void * data) { + defaultVisit(node, data); + } + virtual void visitLimitClause(const LimitClause *node, void * data) { + defaultVisit(node, data); + } + virtual void visitParameterizedType(const ParameterizedType *node, void * data) { + defaultVisit(node, data); + } + virtual void visitMapType(const MapType *node, void * data) { + defaultVisit(node, data); + } + virtual void visitTryExpression(const TryExpression *node, void * data) { + defaultVisit(node, data); + } + virtual void visitCastExpression(const CastExpression *node, void * data) { + defaultVisit(node, data); + } + ~SqlParserDefaultVisitor() { } +}; +} +} +#endif +/* JavaCC - OriginalChecksum=0c633070e7b369af1b643ef0d353747a (do not edit this line) */ diff --git a/parser/cpp/build.sh b/parser/cpp/build.sh index 41877ca..3ce5e9e 100755 --- a/parser/cpp/build.sh +++ b/parser/cpp/build.sh @@ -2,5 +2,6 @@ echo 'Generating parser ...' mvn clean generate-sources +mv target/generated-sources/javacc/SqlParserVisitor.h . echo 'Compiling ...' ./compile.sh diff --git a/parser/cpp/compile.sh b/parser/cpp/compile.sh index 5ddb59b..eb56981 100755 --- a/parser/cpp/compile.sh +++ b/parser/cpp/compile.sh @@ -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 diff --git a/parser/cpp/javacc-options.txt b/parser/cpp/javacc-options.txt index dcf1abe..2e41073 100644 --- a/parser/cpp/javacc-options.txt +++ b/parser/cpp/javacc-options.txt @@ -14,6 +14,7 @@ options { NODE_PREFIX = ""; MULTI = true; VISITOR = true; + VISITOR_METHOD_NAME_INCLUDES_TYPE_NAME = true; } PARSER_BEGIN(SqlParser) @@ -96,8 +97,8 @@ PARSER_BEGIN(SqlParser) // For some nodes, the node is opened after some children are already created. Reset the begin for those to be // the begin of the left-most child. - if (astNode->NumChildren() > 0) { - Token* t0 = astNode->GetChild(0)->beginToken; + if (astNode->numChildren() > 0) { + Token* t0 = astNode->getChild(0)->beginToken; if (t0->beginLine < t->beginLine || (t0->beginLine == t->beginLine && t0->beginColumn < t->beginColumn)) { astNode->beginToken = t0; } diff --git a/parser/cpp/main.cc b/parser/cpp/main.cc index c46465a..53bce08 100644 --- a/parser/cpp/main.cc +++ b/parser/cpp/main.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include "SqlParserConstants.h" #include "CharStream.h" @@ -11,8 +12,7 @@ #include "SqlParser.h" #include "SqlParserTokenManager.h" #include "parser.h" - -#include +#include "unparser.h" using namespace commonsql::parser; using namespace std; @@ -39,21 +39,26 @@ int main(int argc, char **argv) { double time; start = clock(); - for (int i = 0; i < 1; i++) { CharStream *stream = new CharStream(s.c_str(), s.size() - 1, 1, 1); SqlParserTokenManager *scanner = new SqlParserTokenManager(stream); SqlParser parser(scanner); parser.setErrorHandler(new ParseErrorHandler()); + for (int i = 0; i < 10; i++) { + stream = new CharStream(s.c_str(), s.size() - 1, 1, 1); + scanner->ReInit(stream); + parser.ReInit(scanner); parser.compilation_unit(); SimpleNode *root = (SimpleNode*)parser.jjtree.peekNode(); if (root) { JAVACC_STRING_TYPE buffer; root->dumpToBuffer(" ", "\n", &buffer); printf("%s\n", buffer.c_str()); + + printf("Unparse:\n%s\n", unparse(static_cast(root)).c_str()); } } finish = clock(); time = (double(finish)-double(start))/CLOCKS_PER_SEC; - printf ("Avg parsing time: %lfms\n", (time*1000)/1); + printf ("Avg parsing time: %lfms\n", (time*1000)/10); } diff --git a/parser/cpp/unparser.cc b/parser/cpp/unparser.cc new file mode 100644 index 0000000..e6e2661 --- /dev/null +++ b/parser/cpp/unparser.cc @@ -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(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()); +} + +} +} \ No newline at end of file diff --git a/parser/cpp/unparser.h b/parser/cpp/unparser.h new file mode 100644 index 0000000..e75fbda --- /dev/null +++ b/parser/cpp/unparser.h @@ -0,0 +1,44 @@ +#ifndef __UNPARSE_H__ +#define __UNPARSE_H__ + +#include +#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 diff --git a/parser/src/main/java/com/facebook/coresql/parser/Unparser.java b/parser/src/main/java/com/facebook/coresql/parser/Unparser.java index 690b00c..132124d 100644 --- a/parser/src/main/java/com/facebook/coresql/parser/Unparser.java +++ b/parser/src/main/java/com/facebook/coresql/parser/Unparser.java @@ -16,7 +16,7 @@ import static com.facebook.coresql.parser.SqlParserConstants.EOF; public class Unparser - extends com.facebook.coresql.parser.SqlParserDefaultVisitor + extends SqlParserDefaultVisitor { protected StringBuilder stringBuilder = new StringBuilder(); private Token lastToken = new Token(); @@ -113,736 +113,4 @@ public void defaultVisit(SimpleNode node, Void data) printToken(astNode.endToken); } } - - @Override - public void visit(CompilationUnit node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(UnaryExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(UnsignedNumericLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CharStringLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(DateLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(TimeLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(TimestampLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(IntervalLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(BooleanLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Unsupported node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Identifier node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(TableName node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SchemaName node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CatalogName node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SchemaQualifiedName node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(ArrayType node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(FieldDefinition node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(ParenthesizedExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(BuiltinValue node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NullLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(ArrayLiteral node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(QualifiedName node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(GroupingOperation node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WindowFunction node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NullTreatment node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NullIf node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Coalesce node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CaseExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WhenClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(ElseClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WhenOperand node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SearchedCaseOperand node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CastEpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(FieldReference node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(AggregationFunction node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(FunctionCall node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Unused node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Lambda node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(ArrayElement node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Add node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Subtract node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Multiply node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Divide node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Mod node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(TimeZoneField node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Concatenation node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(AdditiveEpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(MultiplicativeExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(OrExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(AndExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NotExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(IsExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(RowExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(RowExression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Values node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(TableExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(FromClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CommaJoin node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Join node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(TableSample node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(AliasedTable node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Alias node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Unnest node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(ColumnNames node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(OnClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(UsingClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WhereClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(GroupbyClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Rollup node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Cube node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(GroupingSets node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(HavingClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(PartitionByClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(OrderByClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WindowFrameUnits node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WindowFrameExtent node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(UnboundedPreceding node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CurrentRow node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WindowFramePreceding node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WindowFrameBetween node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(UnboundedFollowing node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WindowFrameFollowing node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Select node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SelectList node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SelectItem node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Star node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(QuerySpecification node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(WithClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Cte node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SetOperation node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Subquery node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Comparison node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Between node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(InPredicate node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(InvalueList node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Like node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(IsNull node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(QuantifiedComparison node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Exists node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(IsDistinct node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(InvervalQualifier node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NonSecondField node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SecondField node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NonSecondDateTimeField node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(LanguageClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(ArgumentList node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NamedArgument node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SetQuantifier node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(FilterClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SortSpecificationList node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(SortSpecification node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(OrderingDirection node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(NullOrdering node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CreateSchema node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(Unsuppoerted node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(UseStatement node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(LambdaBody node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(LambdaParams node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(LimitClause node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(MapType node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(TryExpression node, Void data) - { - defaultVisit(node, data); - } - - @Override - public void visit(CastExpression node, Void data) - { - defaultVisit(node, data); - } } diff --git a/pom.xml b/pom.xml index fad61bc..11bc77d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,8 @@ com.facebook.airlift airbase - 99 + 100 + com.facebook.presto @@ -18,7 +19,7 @@ https://github.com/prestodb/sql - ${project.parent.basedir} + ${project.basedir} @@ -106,6 +107,39 @@ + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.8 + true + + ossrh + https://oss.sonatype.org/ + + + + + oss-release + + + true + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + org.apache.maven.plugins + maven-gpg-plugin + + + + +