Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / sparql / sparql.js
1 CodeMirror.defineMode("sparql", function(config) {
2   var indentUnit = config.indentUnit;
3   var curPunc;
4
5   function wordRegexp(words) {
6     return new RegExp("^(?:" + words.join("|") + ")$", "i");
7   }
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 = /[*+\-<>=&|]/;
16
17   function tokenBase(stream, state) {
18     var ch = stream.next();
19     curPunc = null;
20     if (ch == "$" || ch == "?") {
21       stream.match(/^[\w\d]*/);
22       return "variable-2";
23     }
24     else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
25       stream.match(/^[^\s\u00a0>]*>?/);
26       return "atom";
27     }
28     else if (ch == "\"" || ch == "'") {
29       state.tokenize = tokenLiteral(ch);
30       return state.tokenize(stream, state);
31     }
32     else if (/[{}\(\),\.;\[\]]/.test(ch)) {
33       curPunc = ch;
34       return null;
35     }
36     else if (ch == "#") {
37       stream.skipToEnd();
38       return "comment";
39     }
40     else if (operatorChars.test(ch)) {
41       stream.eatWhile(operatorChars);
42       return null;
43     }
44     else if (ch == ":") {
45       stream.eatWhile(/[\w\d\._\-]/);
46       return "atom";
47     }
48     else {
49       stream.eatWhile(/[_\w\d]/);
50       if (stream.eat(":")) {
51         stream.eatWhile(/[\w\d_\-]/);
52         return "atom";
53       }
54       var word = stream.current();
55       if (ops.test(word))
56         return null;
57       else if (keywords.test(word))
58         return "keyword";
59       else
60         return "variable";
61     }
62   }
63
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;
70           break;
71         }
72         escaped = !escaped && ch == "\\";
73       }
74       return "string";
75     };
76   }
77
78   function pushContext(state, type, col) {
79     state.context = {prev: state.context, indent: state.indent, col: col, type: type};
80   }
81   function popContext(state) {
82     state.indent = state.context.indent;
83     state.context = state.context.prev;
84   }
85
86   return {
87     startState: function() {
88       return {tokenize: tokenBase,
89               context: null,
90               indent: 0,
91               col: 0};
92     },
93
94     token: function(stream, state) {
95       if (stream.sol()) {
96         if (state.context && state.context.align == null) state.context.align = false;
97         state.indent = stream.indentation();
98       }
99       if (stream.eatSpace()) return null;
100       var style = state.tokenize(stream, state);
101
102       if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
103         state.context.align = true;
104       }
105
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);
112       }
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();
120         }
121       }
122
123       return style;
124     },
125
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;
131
132       var closing = context && firstChar == context.type;
133       if (!context)
134         return 0;
135       else if (context.type == "pattern")
136         return context.col;
137       else if (context.align)
138         return context.col + (closing ? 0 : 1);
139       else
140         return context.indent + (closing ? 0 : indentUnit);
141     }
142   };
143 });
144
145 CodeMirror.defineMIME("application/x-sparql-query", "sparql");