| 1 |
var curPunc; |
| 2 |
|
| 3 |
function wordRegexp(words) { |
| 4 |
return new RegExp("^(?:" + words.join("|") + ")$", "i"); |
| 5 |
} |
| 6 |
var ops = wordRegexp(["str", "lang", "langmatches", "datatype", "bound", "sameterm", "isiri", "isuri", |
| 7 |
"iri", "uri", "bnode", "count", "sum", "min", "max", "avg", "sample", |
| 8 |
"group_concat", "rand", "abs", "ceil", "floor", "round", "concat", "substr", "strlen", |
| 9 |
"replace", "ucase", "lcase", "encode_for_uri", "contains", "strstarts", "strends", |
| 10 |
"strbefore", "strafter", "year", "month", "day", "hours", "minutes", "seconds", |
| 11 |
"timezone", "tz", "now", "uuid", "struuid", "md5", "sha1", "sha256", "sha384", |
| 12 |
"sha512", "coalesce", "if", "strlang", "strdt", "isnumeric", "regex", "exists", |
| 13 |
"isblank", "isliteral", "a", "bind"]); |
| 14 |
var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe", |
| 15 |
"ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional", |
| 16 |
"graph", "by", "asc", "desc", "as", "having", "undef", "values", "group", |
| 17 |
"minus", "in", "not", "service", "silent", "using", "insert", "delete", "union", |
| 18 |
"true", "false", "with", |
| 19 |
"data", "copy", "to", "move", "add", "create", "drop", "clear", "load", "into"]); |
| 20 |
var operatorChars = /[*+\-<>=&|\^\/!\?]/; |
| 21 |
var PN_CHARS = "[A-Za-z_\\-0-9]"; |
| 22 |
var PREFIX_START = new RegExp("[A-Za-z]"); |
| 23 |
var PREFIX_REMAINDER = new RegExp("((" + PN_CHARS + "|\\.)*(" + PN_CHARS + "))?:"); |
| 24 |
|
| 25 |
function tokenBase(stream, state) { |
| 26 |
var ch = stream.next(); |
| 27 |
curPunc = null; |
| 28 |
if (ch == "$" || ch == "?") { |
| 29 |
if(ch == "?" && stream.match(/\s/, false)){ |
| 30 |
return "operator"; |
| 31 |
} |
| 32 |
stream.match(/^[A-Za-z0-9_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD][A-Za-z0-9_\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]*/); |
| 33 |
return "variableName.local"; |
| 34 |
} |
| 35 |
else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) { |
| 36 |
stream.match(/^[^\s\u00a0>]*>?/); |
| 37 |
return "atom"; |
| 38 |
} |
| 39 |
else if (ch == "\"" || ch == "'") { |
| 40 |
state.tokenize = tokenLiteral(ch); |
| 41 |
return state.tokenize(stream, state); |
| 42 |
} |
| 43 |
else if (/[{}\(\),\.;\[\]]/.test(ch)) { |
| 44 |
curPunc = ch; |
| 45 |
return "bracket"; |
| 46 |
} |
| 47 |
else if (ch == "#") { |
| 48 |
stream.skipToEnd(); |
| 49 |
return "comment"; |
| 50 |
} |
| 51 |
else if (operatorChars.test(ch)) { |
| 52 |
return "operator"; |
| 53 |
} |
| 54 |
else if (ch == ":") { |
| 55 |
eatPnLocal(stream); |
| 56 |
return "atom"; |
| 57 |
} |
| 58 |
else if (ch == "@") { |
| 59 |
stream.eatWhile(/[a-z\d\-]/i); |
| 60 |
return "meta"; |
| 61 |
} |
| 62 |
else if (PREFIX_START.test(ch) && stream.match(PREFIX_REMAINDER)) { |
| 63 |
eatPnLocal(stream); |
| 64 |
return "atom"; |
| 65 |
} |
| 66 |
stream.eatWhile(/[_\w\d]/); |
| 67 |
var word = stream.current(); |
| 68 |
if (ops.test(word)) |
| 69 |
return "builtin"; |
| 70 |
else if (keywords.test(word)) |
| 71 |
return "keyword"; |
| 72 |
else |
| 73 |
return "variable"; |
| 74 |
} |
| 75 |
|
| 76 |
function eatPnLocal(stream) { |
| 77 |
stream.match(/(\.(?=[\w_\-\\%])|[:\w_-]|\\[-\\_~.!$&'()*+,;=/?#@%]|%[a-f\d][a-f\d])+/i); |
| 78 |
} |
| 79 |
|
| 80 |
function tokenLiteral(quote) { |
| 81 |
return function(stream, state) { |
| 82 |
var escaped = false, ch; |
| 83 |
while ((ch = stream.next()) != null) { |
| 84 |
if (ch == quote && !escaped) { |
| 85 |
state.tokenize = tokenBase; |
| 86 |
break; |
| 87 |
} |
| 88 |
escaped = !escaped && ch == "\\"; |
| 89 |
} |
| 90 |
return "string"; |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
function pushContext(state, type, col) { |
| 95 |
state.context = {prev: state.context, indent: state.indent, col: col, type: type}; |
| 96 |
} |
| 97 |
function popContext(state) { |
| 98 |
state.indent = state.context.indent; |
| 99 |
state.context = state.context.prev; |
| 100 |
} |
| 101 |
|
| 102 |
export const sparql = { |
| 103 |
name: "sparql", |
| 104 |
|
| 105 |
startState: function() { |
| 106 |
return {tokenize: tokenBase, |
| 107 |
context: null, |
| 108 |
indent: 0, |
| 109 |
col: 0}; |
| 110 |
}, |
| 111 |
|
| 112 |
token: function(stream, state) { |
| 113 |
if (stream.sol()) { |
| 114 |
if (state.context && state.context.align == null) state.context.align = false; |
| 115 |
state.indent = stream.indentation(); |
| 116 |
} |
| 117 |
if (stream.eatSpace()) return null; |
| 118 |
var style = state.tokenize(stream, state); |
| 119 |
|
| 120 |
if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") { |
| 121 |
state.context.align = true; |
| 122 |
} |
| 123 |
|
| 124 |
if (curPunc == "(") pushContext(state, ")", stream.column()); |
| 125 |
else if (curPunc == "[") pushContext(state, "]", stream.column()); |
| 126 |
else if (curPunc == "{") pushContext(state, "}", stream.column()); |
| 127 |
else if (/[\]\}\)]/.test(curPunc)) { |
| 128 |
while (state.context && state.context.type == "pattern") popContext(state); |
| 129 |
if (state.context && curPunc == state.context.type) { |
| 130 |
popContext(state); |
| 131 |
if (curPunc == "}" && state.context && state.context.type == "pattern") |
| 132 |
popContext(state); |
| 133 |
} |
| 134 |
} |
| 135 |
else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state); |
| 136 |
else if (/atom|string|variable/.test(style) && state.context) { |
| 137 |
if (/[\}\]]/.test(state.context.type)) |
| 138 |
pushContext(state, "pattern", stream.column()); |
| 139 |
else if (state.context.type == "pattern" && !state.context.align) { |
| 140 |
state.context.align = true; |
| 141 |
state.context.col = stream.column(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return style; |
| 146 |
}, |
| 147 |
|
| 148 |
indent: function(state, textAfter, cx) { |
| 149 |
var firstChar = textAfter && textAfter.charAt(0); |
| 150 |
var context = state.context; |
| 151 |
if (/[\]\}]/.test(firstChar)) |
| 152 |
while (context && context.type == "pattern") context = context.prev; |
| 153 |
|
| 154 |
var closing = context && firstChar == context.type; |
| 155 |
if (!context) |
| 156 |
return 0; |
| 157 |
else if (context.type == "pattern") |
| 158 |
return context.col; |
| 159 |
else if (context.align) |
| 160 |
return context.col + (closing ? 0 : 1); |
| 161 |
else |
| 162 |
return context.indent + (closing ? 0 : cx.unit); |
| 163 |
}, |
| 164 |
|
| 165 |
languageData: { |
| 166 |
commentTokens: {line: "#"} |
| 167 |
} |
| 168 |
}; |
| 169 |
|