1 CodeMirror.defineMode("sparql", function(config) {
2 var indentUnit = config.indentUnit;
5 function wordRegexp(words) {
6 return new RegExp("^(?:" + words.join("|") + ")$", "i");
8 var ops = wordRegexp(["str", "lang", "langmatches", "datatype", "bound", "sameterm", "isiri", "isuri",
9 "isblank", "isliteral", "a"]);
10 var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe",
11 "ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional",
12 "graph", "by", "asc", "desc", "as", "having", "undef", "values", "group",
13 "minus", "in", "not", "service", "silent", "using", "insert", "delete", "union",
14 "data", "copy", "to", "move", "add", "create", "drop", "clear", "load"]);
15 var operatorChars = /[*+\-<>=&|]/;
17 function tokenBase(stream, state) {
18 var ch = stream.next();
20 if (ch == "$" || ch == "?") {
21 stream.match(/^[\w\d]*/);
24 else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
25 stream.match(/^[^\s\u00a0>]*>?/);
28 else if (ch == "\"" || ch == "'") {
29 state.tokenize = tokenLiteral(ch);
30 return state.tokenize(stream, state);
32 else if (/[{}\(\),\.;\[\]]/.test(ch)) {
40 else if (operatorChars.test(ch)) {
41 stream.eatWhile(operatorChars);
45 stream.eatWhile(/[\w\d\._\-]/);
49 stream.eatWhile(/[_\w\d]/);
50 if (stream.eat(":")) {
51 stream.eatWhile(/[\w\d_\-]/);
54 var word = stream.current();
57 else if (keywords.test(word))
64 function tokenLiteral(quote) {
65 return function(stream, state) {
66 var escaped = false, ch;
67 while ((ch = stream.next()) != null) {
68 if (ch == quote && !escaped) {
69 state.tokenize = tokenBase;
72 escaped = !escaped && ch == "\\";
78 function pushContext(state, type, col) {
79 state.context = {prev: state.context, indent: state.indent, col: col, type: type};
81 function popContext(state) {
82 state.indent = state.context.indent;
83 state.context = state.context.prev;
87 startState: function() {
88 return {tokenize: tokenBase,
94 token: function(stream, state) {
96 if (state.context && state.context.align == null) state.context.align = false;
97 state.indent = stream.indentation();
99 if (stream.eatSpace()) return null;
100 var style = state.tokenize(stream, state);
102 if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
103 state.context.align = true;
106 if (curPunc == "(") pushContext(state, ")", stream.column());
107 else if (curPunc == "[") pushContext(state, "]", stream.column());
108 else if (curPunc == "{") pushContext(state, "}", stream.column());
109 else if (/[\]\}\)]/.test(curPunc)) {
110 while (state.context && state.context.type == "pattern") popContext(state);
111 if (state.context && curPunc == state.context.type) popContext(state);
113 else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
114 else if (/atom|string|variable/.test(style) && state.context) {
115 if (/[\}\]]/.test(state.context.type))
116 pushContext(state, "pattern", stream.column());
117 else if (state.context.type == "pattern" && !state.context.align) {
118 state.context.align = true;
119 state.context.col = stream.column();
126 indent: function(state, textAfter) {
127 var firstChar = textAfter && textAfter.charAt(0);
128 var context = state.context;
129 if (/[\]\}]/.test(firstChar))
130 while (context && context.type == "pattern") context = context.prev;
132 var closing = context && firstChar == context.type;
135 else if (context.type == "pattern")
137 else if (context.align)
138 return context.col + (closing ? 0 : 1);
140 return context.indent + (closing ? 0 : indentUnit);
145 CodeMirror.defineMIME("application/x-sparql-query", "sparql");