| 1 |
var wordRegexp = function(words) { |
| 2 |
return new RegExp("^(?:" + words.join("|") + ")$", "i"); |
| 3 |
}; |
| 4 |
|
| 5 |
var tokenBase = function(stream/*, state*/) { |
| 6 |
curPunc = null; |
| 7 |
var ch = stream.next(); |
| 8 |
if (ch ==='"') { |
| 9 |
stream.match(/^.*?"/); |
| 10 |
return "string"; |
| 11 |
} |
| 12 |
if (ch === "'") { |
| 13 |
stream.match(/^.*?'/); |
| 14 |
return "string"; |
| 15 |
} |
| 16 |
if (/[{}\(\),\.;\[\]]/.test(ch)) { |
| 17 |
curPunc = ch; |
| 18 |
return "punctuation"; |
| 19 |
} else if (ch === "/" && stream.eat("/")) { |
| 20 |
stream.skipToEnd(); |
| 21 |
return "comment"; |
| 22 |
} else if (operatorChars.test(ch)) { |
| 23 |
stream.eatWhile(operatorChars); |
| 24 |
return null; |
| 25 |
} else { |
| 26 |
stream.eatWhile(/[_\w\d]/); |
| 27 |
if (stream.eat(":")) { |
| 28 |
stream.eatWhile(/[\w\d_\-]/); |
| 29 |
return "atom"; |
| 30 |
} |
| 31 |
var word = stream.current(); |
| 32 |
if (funcs.test(word)) return "builtin"; |
| 33 |
if (preds.test(word)) return "def"; |
| 34 |
if (keywords.test(word) || systemKeywords.test(word)) return "keyword"; |
| 35 |
return "variable"; |
| 36 |
} |
| 37 |
}; |
| 38 |
var pushContext = function(state, type, col) { |
| 39 |
return state.context = { |
| 40 |
prev: state.context, |
| 41 |
indent: state.indent, |
| 42 |
col: col, |
| 43 |
type: type |
| 44 |
}; |
| 45 |
}; |
| 46 |
var popContext = function(state) { |
| 47 |
state.indent = state.context.indent; |
| 48 |
return state.context = state.context.prev; |
| 49 |
}; |
| 50 |
var curPunc; |
| 51 |
var funcs = wordRegexp(["abs", "acos", "allShortestPaths", "asin", "atan", "atan2", "avg", "ceil", "coalesce", "collect", "cos", "cot", "count", "degrees", "e", "endnode", "exp", "extract", "filter", "floor", "haversin", "head", "id", "keys", "labels", "last", "left", "length", "log", "log10", "lower", "ltrim", "max", "min", "node", "nodes", "percentileCont", "percentileDisc", "pi", "radians", "rand", "range", "reduce", "rel", "relationship", "relationships", "replace", "reverse", "right", "round", "rtrim", "shortestPath", "sign", "sin", "size", "split", "sqrt", "startnode", "stdev", "stdevp", "str", "substring", "sum", "tail", "tan", "timestamp", "toFloat", "toInt", "toString", "trim", "type", "upper"]); |
| 52 |
var preds = wordRegexp(["all", "and", "any", "contains", "exists", "has", "in", "none", "not", "or", "single", "xor"]); |
| 53 |
var keywords = wordRegexp(["as", "asc", "ascending", "assert", "by", "case", "commit", "constraint", "create", "csv", "cypher", "delete", "desc", "descending", "detach", "distinct", "drop", "else", "end", "ends", "explain", "false", "fieldterminator", "foreach", "from", "headers", "in", "index", "is", "join", "limit", "load", "match", "merge", "null", "on", "optional", "order", "periodic", "profile", "remove", "return", "scan", "set", "skip", "start", "starts", "then", "true", "union", "unique", "unwind", "using", "when", "where", "with", "call", "yield"]); |
| 54 |
var systemKeywords = wordRegexp(["access", "active", "assign", "all", "alter", "as", "catalog", "change", "copy", "create", "constraint", "constraints", "current", "database", "databases", "dbms", "default", "deny", "drop", "element", "elements", "exists", "from", "grant", "graph", "graphs", "if", "index", "indexes", "label", "labels", "management", "match", "name", "names", "new", "node", "nodes", "not", "of", "on", "or", "password", "populated", "privileges", "property", "read", "relationship", "relationships", "remove", "replace", "required", "revoke", "role", "roles", "set", "show", "start", "status", "stop", "suspended", "to", "traverse", "type", "types", "user", "users", "with", "write"]); |
| 55 |
var operatorChars = /[*+\-<>=&|~%^]/; |
| 56 |
|
| 57 |
export const cypher = { |
| 58 |
name: "cypher", |
| 59 |
startState: function() { |
| 60 |
return { |
| 61 |
tokenize: tokenBase, |
| 62 |
context: null, |
| 63 |
indent: 0, |
| 64 |
col: 0 |
| 65 |
}; |
| 66 |
}, |
| 67 |
token: function(stream, state) { |
| 68 |
if (stream.sol()) { |
| 69 |
if (state.context && (state.context.align == null)) { |
| 70 |
state.context.align = false; |
| 71 |
} |
| 72 |
state.indent = stream.indentation(); |
| 73 |
} |
| 74 |
if (stream.eatSpace()) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
var style = state.tokenize(stream, state); |
| 78 |
if (style !== "comment" && state.context && (state.context.align == null) && state.context.type !== "pattern") { |
| 79 |
state.context.align = true; |
| 80 |
} |
| 81 |
if (curPunc === "(") { |
| 82 |
pushContext(state, ")", stream.column()); |
| 83 |
} else if (curPunc === "[") { |
| 84 |
pushContext(state, "]", stream.column()); |
| 85 |
} else if (curPunc === "{") { |
| 86 |
pushContext(state, "}", stream.column()); |
| 87 |
} else if (/[\]\}\)]/.test(curPunc)) { |
| 88 |
while (state.context && state.context.type === "pattern") { |
| 89 |
popContext(state); |
| 90 |
} |
| 91 |
if (state.context && curPunc === state.context.type) { |
| 92 |
popContext(state); |
| 93 |
} |
| 94 |
} else if (curPunc === "." && state.context && state.context.type === "pattern") { |
| 95 |
popContext(state); |
| 96 |
} else if (/atom|string|variable/.test(style) && state.context) { |
| 97 |
if (/[\}\]]/.test(state.context.type)) { |
| 98 |
pushContext(state, "pattern", stream.column()); |
| 99 |
} else if (state.context.type === "pattern" && !state.context.align) { |
| 100 |
state.context.align = true; |
| 101 |
state.context.col = stream.column(); |
| 102 |
} |
| 103 |
} |
| 104 |
return style; |
| 105 |
}, |
| 106 |
indent: function(state, textAfter, cx) { |
| 107 |
var firstChar = textAfter && textAfter.charAt(0); |
| 108 |
var context = state.context; |
| 109 |
if (/[\]\}]/.test(firstChar)) { |
| 110 |
while (context && context.type === "pattern") { |
| 111 |
context = context.prev; |
| 112 |
} |
| 113 |
} |
| 114 |
var closing = context && firstChar === context.type; |
| 115 |
if (!context) return 0; |
| 116 |
if (context.type === "keywords") return null |
| 117 |
if (context.align) return context.col + (closing ? 0 : 1); |
| 118 |
return context.indent + (closing ? 0 : cx.unit); |
| 119 |
} |
| 120 |
}; |