5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("sparql", function(config) {
15
var indentUnit = config.indentUnit;
18
function wordRegexp(words) {
19
return new RegExp("^(?:" + words.join("|") + ")$", "i");
21
var ops = wordRegexp(["str", "lang", "langmatches", "datatype", "bound", "sameterm", "isiri", "isuri",
22
"iri", "uri", "bnode", "count", "sum", "min", "max", "avg", "sample",
23
"group_concat", "rand", "abs", "ceil", "floor", "round", "concat", "substr", "strlen",
24
"replace", "ucase", "lcase", "encode_for_uri", "contains", "strstarts", "strends",
25
"strbefore", "strafter", "year", "month", "day", "hours", "minutes", "seconds",
26
"timezone", "tz", "now", "uuid", "struuid", "md5", "sha1", "sha256", "sha384",
27
"sha512", "coalesce", "if", "strlang", "strdt", "isnumeric", "regex", "exists",
28
"isblank", "isliteral", "a", "bind"]);
29
var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe",
30
"ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional",
31
"graph", "by", "asc", "desc", "as", "having", "undef", "values", "group",
32
"minus", "in", "not", "service", "silent", "using", "insert", "delete", "union",
33
"true", "false", "with",
34
"data", "copy", "to", "move", "add", "create", "drop", "clear", "load"]);
35
var operatorChars = /[*+\-<>=&|\^\/!\?]/;
37
function tokenBase(stream, state) {
38
var ch = stream.next();
40
if (ch == "$" || ch == "?") {
41
if(ch == "?" && stream.match(/\s/, false)){
44
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]*/);
47
else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
48
stream.match(/^[^\s\u00a0>]*>?/);
51
else if (ch == "\"" || ch == "'") {
52
state.tokenize = tokenLiteral(ch);
53
return state.tokenize(stream, state);
55
else if (/[{}\(\),\.;\[\]]/.test(ch)) {
63
else if (ch === "^") {
65
if (ch === "^") stream.eat("^");
66
else stream.eatWhile(operatorChars);
69
else if (operatorChars.test(ch)) {
70
stream.eatWhile(operatorChars);
78
stream.eatWhile(/[a-z\d\-]/i);
82
stream.eatWhile(/[_\w\d]/);
83
if (stream.eat(":")) {
87
var word = stream.current();
90
else if (keywords.test(word))
97
function eatPnLocal(stream) {
98
stream.match(/(\.(?=[\w_\-\\%])|[:\w_-]|\\[-\\_~.!$&'()*+,;=/?#@%]|%[a-f\d][a-f\d])+/i);
101
function tokenLiteral(quote) {
102
return function(stream, state) {
103
var escaped = false, ch;
104
while ((ch = stream.next()) != null) {
105
if (ch == quote && !escaped) {
106
state.tokenize = tokenBase;
109
escaped = !escaped && ch == "\\";
115
function pushContext(state, type, col) {
116
state.context = {prev: state.context, indent: state.indent, col: col, type: type};
118
function popContext(state) {
119
state.indent = state.context.indent;
120
state.context = state.context.prev;
124
startState: function() {
125
return {tokenize: tokenBase,
131
token: function(stream, state) {
133
if (state.context && state.context.align == null) state.context.align = false;
134
state.indent = stream.indentation();
136
if (stream.eatSpace()) return null;
137
var style = state.tokenize(stream, state);
139
if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
140
state.context.align = true;
143
if (curPunc == "(") pushContext(state, ")", stream.column());
144
else if (curPunc == "[") pushContext(state, "]", stream.column());
145
else if (curPunc == "{") pushContext(state, "}", stream.column());
146
else if (/[\]\}\)]/.test(curPunc)) {
147
while (state.context && state.context.type == "pattern") popContext(state);
148
if (state.context && curPunc == state.context.type) {
150
if (curPunc == "}" && state.context && state.context.type == "pattern")
154
else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
155
else if (/atom|string|variable/.test(style) && state.context) {
156
if (/[\}\]]/.test(state.context.type))
157
pushContext(state, "pattern", stream.column());
158
else if (state.context.type == "pattern" && !state.context.align) {
159
state.context.align = true;
160
state.context.col = stream.column();
167
indent: function(state, textAfter) {
168
var firstChar = textAfter && textAfter.charAt(0);
169
var context = state.context;
170
if (/[\]\}]/.test(firstChar))
171
while (context && context.type == "pattern") context = context.prev;
173
var closing = context && firstChar == context.type;
176
else if (context.type == "pattern")
178
else if (context.align)
179
return context.col + (closing ? 0 : 1);
181
return context.indent + (closing ? 0 : indentUnit);
188
CodeMirror.defineMIME("application/sparql-query", "sparql");