@neo4j-cypher/extract-statements
Version:
Utility function to extract statements from a cypher query
123 lines (120 loc) • 3.87 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ReferencesListener = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _antlr4Simple = require("@neo4j-cypher/antlr4-simple");
var _editorSupport = require("@neo4j-cypher/editor-support");
class Index {
constructor() {
(0, _defineProperty2.default)(this, "names", {});
(0, _defineProperty2.default)(this, "namesByQuery", []);
(0, _defineProperty2.default)(this, "referencesByName", {});
(0, _defineProperty2.default)(this, "referencesByQueryAndName", []);
}
addQuery() {
this.namesByQuery.push([]);
this.referencesByQueryAndName.push({});
}
add(ctx, addName = true) {
const queryIndex = this.namesByQuery.length - 1;
const text = ctx.getText();
if (addName) {
this.names[text] = true;
this.namesByQuery[queryIndex][text] = true;
}
this.referencesByName[text] = [...(this.referencesByName[text] || []), ctx];
const index = this.referencesByQueryAndName[queryIndex];
index[text] = [...(index[text] || []), ctx];
}
/**
* Variables have specific rules, because they participate in autocompletion.
* We should not add to the names list variables that are in expression.
*/
addVariable(ctx) {
let addName = true;
// If variable is inside atom, then variable is inside expression.
// Therefore, variables is node defined here.
const parent = ctx.parentCtx;
if (parent && parent instanceof _editorSupport.CypherTypes.ATOM_CONTEXT) {
addName = false;
}
this.add(ctx, addName);
}
}
class ReferencesListener extends _antlr4Simple.CypherListener {
constructor() {
super();
(0, _defineProperty2.default)(this, "queries", []);
(0, _defineProperty2.default)(this, "queriesAndCommands", []);
(0, _defineProperty2.default)(this, "statements", []);
(0, _defineProperty2.default)(this, "raw", []);
(0, _defineProperty2.default)(this, "indexes", new Map());
(0, _defineProperty2.default)(this, "inConsoleCommand", false);
_editorSupport.CypherTypes.SYMBOLIC_CONTEXTS.forEach(sc => {
this.indexes.set(sc, new Index(sc));
});
}
enterRaw(ctx) {
this.raw.push(ctx);
}
exitRaw(ctx) {
if (this.raw.length === 0) {
this.raw.push(ctx);
}
}
enterCypherPart(ctx) {
this.statements.push(ctx);
}
exitCypher(ctx) {
if (this.statements.length === 0) {
this.statements.push(ctx);
}
}
enterCypherConsoleCommand(ctx) {
this.queriesAndCommands.push(ctx);
this.indexes.forEach(index => index.addQuery());
this.inConsoleCommand = true;
}
exitCypherConsoleCommand() {
this.inConsoleCommand = false;
}
enterCypherQuery(ctx) {
this.queries.push(ctx);
this.queriesAndCommands.push(ctx);
this.indexes.forEach(index => index.addQuery());
}
exitVariable(ctx) {
if (this.inConsoleCommand) {
return;
}
this.indexes.get(_editorSupport.CypherTypes.VARIABLE_CONTEXT).addVariable(ctx);
}
exitLabelName(ctx) {
if (this.inConsoleCommand) {
return;
}
this.indexes.get(_editorSupport.CypherTypes.LABEL_NAME_CONTEXT).add(ctx);
}
exitRelTypeName(ctx) {
if (this.inConsoleCommand) {
return;
}
this.indexes.get(_editorSupport.CypherTypes.RELATIONSHIP_TYPE_NAME_CONTEXT).add(ctx);
}
exitPropertyKeyName(ctx) {
if (this.inConsoleCommand) {
return;
}
this.indexes.get(_editorSupport.CypherTypes.PROPERTY_KEY_NAME_CONTEXT).add(ctx);
}
exitParameterName(ctx) {
if (this.inConsoleCommand) {
return;
}
this.indexes.get(_editorSupport.CypherTypes.PARAMETER_NAME_CONTEXT).add(ctx);
}
}
exports.ReferencesListener = ReferencesListener;