UNPKG

rhombic

Version:

SQL parsing, lineage extraction and manipulation

1,118 lines 82.6 kB
"use strict"; // Auto-generated by generateSerialedGrammar.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.serializedGrammar = void 0; exports.serializedGrammar = [ { type: "Rule", name: "statement", orgText: "function () {\n _this.OR([{ ALT: function () { return _this.SUBRULE(_this.query); } }]);\n }", definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "NonTerminal", name: "query", idx: 0 } ] } ] } ] }, { type: "Rule", name: "query", orgText: "function () {\n _this.OR([\n { ALT: function () { return _this.SUBRULE(_this.values); } },\n {\n ALT: function () {\n _this.SUBRULE(_this.select);\n _this.OPTION(function () {\n _this.SUBRULE(_this.orderBy);\n });\n _this.OPTION1(function () {\n _this.CONSUME(Limit);\n _this.OR1([\n { ALT: function () { return _this.CONSUME(IntegerValue); } },\n { ALT: function () { return _this.CONSUME(All); } }\n ]);\n });\n }\n }\n ]);\n _this.OPTION2(function () { return _this.CONSUME(SemiColon); });\n }", definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "NonTerminal", name: "values", idx: 0 } ] }, { type: "Flat", definition: [ { type: "NonTerminal", name: "select", idx: 0 }, { type: "Option", idx: 0, definition: [ { type: "NonTerminal", name: "orderBy", idx: 0 } ] }, { type: "Option", idx: 1, definition: [ { type: "Terminal", name: "Limit", label: "Limit", idx: 0, pattern: "LIMIT" }, { type: "Alternation", idx: 1, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "IntegerValue", label: "IntegerValue", idx: 0, pattern: "0|[1-9]\\d*" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "All", label: "All", idx: 0, pattern: "ALL" } ] } ] } ] } ] } ] }, { type: "Option", idx: 2, definition: [ { type: "Terminal", name: "SemiColon", label: "SemiColon", idx: 0, pattern: ";" } ] } ] }, { type: "Rule", name: "expression", orgText: "function () {\n _this.OR([\n { ALT: function () { return _this.CONSUME(IntegerValue); } },\n { ALT: function () { return _this.CONSUME(StringValue); } },\n { ALT: function () { return _this.CONSUME(Null); } },\n {\n ALT: function () {\n _this.CONSUME(LParen);\n _this.MANY_SEP({\n SEP: Comma,\n DEF: function () { return _this.SUBRULE(_this.expression); }\n });\n _this.CONSUME(RParen);\n }\n },\n {\n ALT: function () { return _this.SUBRULE(_this.columnPrimary); }\n },\n {\n ALT: function () {\n _this.CONSUME(FunctionIdentifier), _this.CONSUME1(LParen);\n _this.MANY_SEP1({\n SEP: Comma,\n DEF: function () { return _this.SUBRULE1(_this.expression); }\n });\n _this.CONSUME1(RParen);\n }\n },\n {\n ALT: function () { return _this.SUBRULE(_this.cast); }\n }\n ]);\n }", definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "IntegerValue", label: "IntegerValue", idx: 0, pattern: "0|[1-9]\\d*" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "StringValue", label: "StringValue", idx: 0, pattern: "((`[^`]*(`))+)|((\\[[^\\]]*(\\]))(\\][^\\]]*(\\]))*)|((\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*(\"))+)|(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('))+)|((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('))+)" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "Null", label: "Null", idx: 0, pattern: "null" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "LParen", label: "LParen", idx: 0, pattern: "\\(" }, { type: "RepetitionWithSeparator", idx: 0, separator: { type: "Terminal", name: "Comma", label: "Comma", idx: 1, pattern: "," }, definition: [ { type: "NonTerminal", name: "expression", idx: 0 } ] }, { type: "Terminal", name: "RParen", label: "RParen", idx: 0, pattern: "\\)" } ] }, { type: "Flat", definition: [ { type: "NonTerminal", name: "columnPrimary", idx: 0 } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "FunctionIdentifier", label: "FunctionIdentifier", idx: 0 }, { type: "Terminal", name: "LParen", label: "LParen", idx: 1, pattern: "\\(" }, { type: "RepetitionWithSeparator", idx: 1, separator: { type: "Terminal", name: "Comma", label: "Comma", idx: 1, pattern: "," }, definition: [ { type: "NonTerminal", name: "expression", idx: 1 } ] }, { type: "Terminal", name: "RParen", label: "RParen", idx: 1, pattern: "\\)" } ] }, { type: "Flat", definition: [ { type: "NonTerminal", name: "cast", idx: 0 } ] } ] } ] }, { type: "Rule", name: "cast", orgText: "function () {\n _this.CONSUME(Cast);\n _this.CONSUME(LParen);\n _this.SUBRULE(_this.expression);\n _this.CONSUME(As);\n _this.SUBRULE(_this.type);\n _this.OPTION(function () {\n _this.CONSUME1(LParen);\n _this.CONSUME(IntegerValue); // precision\n _this.OPTION1(function () {\n _this.CONSUME(Comma);\n _this.CONSUME1(IntegerValue); // scale\n });\n _this.CONSUME1(RParen);\n });\n _this.CONSUME(RParen);\n }", definition: [ { type: "Terminal", name: "Cast", label: "Cast", idx: 0, pattern: "CAST" }, { type: "Terminal", name: "LParen", label: "LParen", idx: 0, pattern: "\\(" }, { type: "NonTerminal", name: "expression", idx: 0 }, { type: "Terminal", name: "As", label: "As", idx: 0, pattern: "AS" }, { type: "NonTerminal", name: "type", idx: 0 }, { type: "Option", idx: 0, definition: [ { type: "Terminal", name: "LParen", label: "LParen", idx: 1, pattern: "\\(" }, { type: "Terminal", name: "IntegerValue", label: "IntegerValue", idx: 0, pattern: "0|[1-9]\\d*" }, { type: "Option", idx: 1, definition: [ { type: "Terminal", name: "Comma", label: "Comma", idx: 0, pattern: "," }, { type: "Terminal", name: "IntegerValue", label: "IntegerValue", idx: 1, pattern: "0|[1-9]\\d*" } ] }, { type: "Terminal", name: "RParen", label: "RParen", idx: 1, pattern: "\\)" } ] }, { type: "Terminal", name: "RParen", label: "RParen", idx: 0, pattern: "\\)" } ] }, { type: "Rule", name: "orderBy", orgText: "function () {\n _this.CONSUME(OrderBy);\n _this.AT_LEAST_ONE_SEP({\n SEP: Comma,\n DEF: function () { return _this.SUBRULE(_this.orderItem); }\n });\n }", definition: [ { type: "Terminal", name: "OrderBy", label: "OrderBy", idx: 0, pattern: "ORDER BY" }, { type: "RepetitionMandatoryWithSeparator", idx: 0, separator: { type: "Terminal", name: "Comma", label: "Comma", idx: 1, pattern: "," }, definition: [ { type: "NonTerminal", name: "orderItem", idx: 0 } ] } ] }, { type: "Rule", name: "type", orgText: "function () {\n _this.OR([{ ALT: function () { return _this.CONSUME(SqlTypeName); } }]);\n _this.OPTION(function () {\n _this.MANY(function () {\n _this.CONSUME(CollectionTypeName);\n });\n });\n }", definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "SqlTypeName", label: "SqlTypeName", idx: 0, pattern: "CHAR(ACTER)?( VARYING)?|VARCHAR|DATE|TIME(STAMP)?|CHARACTER SET|GEOMETRY|DEC(IMAL)?|NUMERIC|INT(EGER)?|BOOLEAN|BINARY( VARYING)?|VARBINARY|TINYINT|SMALLINT|BIGINT|REAL|DOUBLE|FLOAT|ANY" } ] } ] }, { type: "Option", idx: 0, definition: [ { type: "Repetition", idx: 0, definition: [ { type: "Terminal", name: "CollectionTypeName", label: "CollectionTypeName", idx: 0, pattern: "ARRAY|MULTISET" } ] } ] } ] }, { type: "Rule", name: "valueExpression", orgText: "function () {\n _this.OR([\n { ALT: function () { return _this.CONSUME(IntegerValue); } },\n { ALT: function () { return _this.CONSUME(StringValue); } },\n { ALT: function () { return _this.CONSUME(BooleanValue); } },\n { ALT: function () { return _this.CONSUME(DateValue); } }\n ]);\n }", definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "IntegerValue", label: "IntegerValue", idx: 0, pattern: "0|[1-9]\\d*" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "StringValue", label: "StringValue", idx: 0, pattern: "((`[^`]*(`))+)|((\\[[^\\]]*(\\]))(\\][^\\]]*(\\]))*)|((\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*(\"))+)|(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('))+)|((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('))+)" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "BooleanValue", label: "BooleanValue", idx: 0, pattern: "TRUE|FALSE" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "DateValue", label: "DateValue", idx: 0, pattern: "DATE '\\d{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|3[0-1])'" } ] } ] } ] }, { type: "Rule", name: "booleanExpression", orgText: "function () {\n _this.OR([\n {\n ALT: function () {\n _this.CONSUME(LParen);\n _this.SUBRULE(_this.booleanExpression);\n _this.CONSUME(RParen);\n }\n },\n {\n ALT: function () { return _this.SUBRULE1(_this.booleanExpressionValue); }\n }\n ]);\n _this.OPTION(function () {\n _this.OR1([\n { ALT: function () { return _this.CONSUME(Or); } },\n { ALT: function () { return _this.CONSUME(And); } }\n ]);\n _this.SUBRULE2(_this.booleanExpression);\n });\n }", definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "LParen", label: "LParen", idx: 0, pattern: "\\(" }, { type: "NonTerminal", name: "booleanExpression", idx: 0 }, { type: "Terminal", name: "RParen", label: "RParen", idx: 0, pattern: "\\)" } ] }, { type: "Flat", definition: [ { type: "NonTerminal", name: "booleanExpressionValue", idx: 1 } ] } ] }, { type: "Option", idx: 0, definition: [ { type: "Alternation", idx: 1, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "Or", label: "Or", idx: 0, pattern: "OR" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "And", label: "And", idx: 0, pattern: "AND" } ] } ] }, { type: "NonTerminal", name: "booleanExpression", idx: 2 } ] } ] }, { type: "Rule", name: "booleanExpressionValue", orgText: "function () {\n _this.SUBRULE(_this.columnPrimary);\n _this.OR([\n {\n ALT: function () {\n // Binary operation\n _this.CONSUME(BinaryOperator);\n _this.OR1([\n { ALT: function () { return _this.SUBRULE1(_this.valueExpression); } },\n { ALT: function () { return _this.SUBRULE2(_this.columnPrimary); } }\n ]);\n }\n },\n {\n ALT: function () {\n // Multival operation\n _this.CONSUME(MultivalOperator);\n _this.CONSUME1(LParen);\n _this.AT_LEAST_ONE_SEP({\n SEP: Comma,\n DEF: function () {\n _this.OR2([\n { ALT: function () { return _this.SUBRULE3(_this.valueExpression); } },\n { ALT: function () { return _this.SUBRULE4(_this.columnPrimary); } }\n ]);\n }\n });\n _this.CONSUME1(RParen);\n }\n },\n {\n ALT: function () {\n // Unary operation\n _this.OR3([\n { ALT: function () { return _this.CONSUME(IsNull); } },\n { ALT: function () { return _this.CONSUME(IsNotNull); } }\n ]);\n }\n }\n ]);\n }", definition: [ { type: "NonTerminal", name: "columnPrimary", idx: 0 }, { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "BinaryOperator", label: "BinaryOperator", idx: 0, pattern: "=|>=?|<=?|\\!=|LIKE" }, { type: "Alternation", idx: 1, definition: [ { type: "Flat", definition: [ { type: "NonTerminal", name: "valueExpression", idx: 1 } ] }, { type: "Flat", definition: [ { type: "NonTerminal", name: "columnPrimary", idx: 2 } ] } ] } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "MultivalOperator", label: "MultivalOperator", idx: 0, pattern: "NOT IN|IN" }, { type: "Terminal", name: "LParen", label: "LParen", idx: 1, pattern: "\\(" }, { type: "RepetitionMandatoryWithSeparator", idx: 0, separator: { type: "Terminal", name: "Comma", label: "Comma", idx: 1, pattern: "," }, definition: [ { type: "Alternation", idx: 2, definition: [ { type: "Flat", definition: [ { type: "NonTerminal", name: "valueExpression", idx: 3 } ] }, { type: "Flat", definition: [ { type: "NonTerminal", name: "columnPrimary", idx: 4 } ] } ] } ] }, { type: "Terminal", name: "RParen", label: "RParen", idx: 1, pattern: "\\)" } ] }, { type: "Flat", definition: [ { type: "Alternation", idx: 3, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "IsNull", label: "IsNull", idx: 0, pattern: "IS NULL" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "IsNotNull", label: "IsNotNull", idx: 0, pattern: "IS NOT NULL" } ] } ] } ] } ] } ] }, { type: "Rule", name: "orderItem", orgText: "function () {\n _this.SUBRULE(_this.expression);\n _this.OPTION(function () {\n _this.OR([\n { ALT: function () { return _this.CONSUME(Asc); } },\n { ALT: function () { return _this.CONSUME(Desc); } }\n ]);\n });\n _this.OPTION1(function () {\n _this.OR1([\n {\n ALT: function () {\n _this.CONSUME(Nulls);\n _this.CONSUME(First);\n }\n },\n {\n ALT: function () {\n _this.CONSUME1(Nulls);\n _this.CONSUME(Last);\n }\n }\n ]);\n });\n }", definition: [ { type: "NonTerminal", name: "expression", idx: 0 }, { type: "Option", idx: 0, definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "Asc", label: "Asc", idx: 0, pattern: "ASC" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "Desc", label: "Desc", idx: 0, pattern: "DESC" } ] } ] } ] }, { type: "Option", idx: 1, definition: [ { type: "Alternation", idx: 1, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "Nulls", label: "Nulls", idx: 0, pattern: "NULLS" }, { type: "Terminal", name: "First", label: "First", idx: 0, pattern: "FIRST" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "Nulls", label: "Nulls", idx: 1, pattern: "NULLS" }, { type: "Terminal", name: "Last", label: "Last", idx: 0, pattern: "LAST" } ] } ] } ] } ] }, { type: "Rule", name: "select", orgText: "function () {\n _this.CONSUME(Select);\n _this.OPTION(function () { return _this.CONSUME(Stream); });\n _this.OPTION1(function () {\n _this.OR([\n { ALT: function () { return _this.CONSUME(All); } },\n { ALT: function () { return _this.CONSUME(Distinct); } }\n ]);\n });\n _this.SUBRULE(_this.projectionItems);\n // Everything is wrap into `OPTION` to deal with selectWithoutFrom case\n _this.OPTION3(function () {\n _this.CONSUME(From);\n _this.SUBRULE(_this.tableExpression);\n });\n _this.OPTION4(function () {\n _this.SUBRULE(_this.where);\n });\n _this.OPTION5(function () {\n _this.SUBRULE(_this.groupBy);\n });\n }", definition: [ { type: "Terminal", name: "Select", label: "Select", idx: 0, pattern: "SELECT" }, { type: "Option", idx: 0, definition: [ { type: "Terminal", name: "Stream", label: "Stream", idx: 0, pattern: "STREAM" } ] }, { type: "Option", idx: 1, definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Terminal", name: "All", label: "All", idx: 0, pattern: "ALL" } ] }, { type: "Flat", definition: [ { type: "Terminal", name: "Distinct", label: "Distinct", idx: 0, pattern: "DISTINCT" } ] } ] } ] }, { type: "NonTerminal", name: "projectionItems", idx: 0 }, { type: "Option", idx: 3, definition: [ { type: "Terminal", name: "From", label: "From", idx: 0, pattern: "FROM" }, { type: "NonTerminal", name: "tableExpression", idx: 0 } ] }, { type: "Option", idx: 4, definition: [ { type: "NonTerminal", name: "where", idx: 0 } ] }, { type: "Option", idx: 5, definition: [ { type: "NonTerminal", name: "groupBy", idx: 0 } ] } ] }, { type: "Rule", name: "groupBy", orgText: "function () {\n _this.CONSUME(Group);\n _this.CONSUME(By);\n _this.AT_LEAST_ONE_SEP({\n SEP: Comma,\n DEF: function () { return _this.SUBRULE(_this.groupItem); }\n });\n }", definition: [ { type: "Terminal", name: "Group", label: "Group", idx: 0, pattern: "GROUP" }, { type: "Terminal", name: "By", label: "By", idx: 0, pattern: "BY" }, { type: "RepetitionMandatoryWithSeparator", idx: 0, separator: { type: "Terminal", name: "Comma", label: "Comma", idx: 1, pattern: "," }, definition: [ { type: "NonTerminal", name: "groupItem", idx: 0 } ] } ] }, { type: "Rule", name: "where", orgText: "function () {\n _this.CONSUME(Where);\n _this.SUBRULE(_this.booleanExpression);\n }", definition: [ { type: "Terminal", name: "Where", label: "Where", idx: 0, pattern: "WHERE" }, { type: "NonTerminal", name: "booleanExpression", idx: 0 } ] }, { type: "Rule", name: "projectionItems", orgText: "function () {\n _this.AT_LEAST_ONE_SEP({\n SEP: Comma,\n DEF: function () { return _this.SUBRULE(_this.projectionItem); }\n });\n }", definition: [ { type: "RepetitionMandatoryWithSeparator", idx: 0, separator: { type: "Terminal", name: "Comma", label: "Comma", idx: 1, pattern: "," }, definition: [ { type: "NonTerminal", name: "projectionItem", idx: 0 } ] } ] }, { type: "Rule", name: "projectionItem", orgText: "function () {\n _this.OR([\n {\n ALT: function () {\n _this.OPTION1(function () {\n _this.CONSUME1(Identifier);\n _this.CONSUME(Period);\n });\n _this.CONSUME(Asterisk);\n }\n },\n {\n ALT: function () {\n _this.SUBRULE(_this.expression);\n _this.OPTION(function () {\n _this.CONSUME(As);\n _this.CONSUME(Identifier);\n });\n }\n }\n ]);\n }", definition: [ { type: "Alternation", idx: 0, definition: [ { type: "Flat", definition: [ { type: "Option", idx: 1, definition: [ { type: "Terminal", name: "Identifier", label: "Identifier", idx: 1, pattern: '[a-zA-Z][\\w]*|"[^"]*"' }, { type: "Terminal", name: "Period", label: "Period", idx: 0, pattern: "\\." } ] }, { type: "Terminal", name: "Asterisk", label: "Asterisk", idx: 0, pattern: "\\*" } ] }, { type: "Flat", definition: [ { type: "NonTerminal", name: "expression", idx: 0 }, { type: "Option", idx: 0, definition: [ { type: "Terminal", name: "As", label: "As", idx: 0, pattern: "AS" }, { type: "Terminal", name: "Identifier", label: "Identifier", idx: 0, pattern: '[a-zA-Z][\\w]*|"[^"]*"' } ] } ] } ] } ] }, { type: "Rule", name: "tableExpression", orgText: "function () {\n // tableReference [, tableReference ]*\n _this.MANY_SEP({\n SEP: Comma,\n DEF: function () { return