GPQAPP

Форк
0
190 строк · 6.8 Кб
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: https://codemirror.net/LICENSE
3

4
(function(mod) {
5
  if (typeof exports == "object" && typeof module == "object") // CommonJS
6
    mod(require("../../lib/codemirror"));
7
  else if (typeof define == "function" && define.amd) // AMD
8
    define(["../../lib/codemirror"], mod);
9
  else // Plain browser env
10
    mod(CodeMirror);
11
})(function(CodeMirror) {
12
"use strict";
13

14
CodeMirror.defineMode("sparql", function(config) {
15
  var indentUnit = config.indentUnit;
16
  var curPunc;
17

18
  function wordRegexp(words) {
19
    return new RegExp("^(?:" + words.join("|") + ")$", "i");
20
  }
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 = /[*+\-<>=&|\^\/!\?]/;
36

37
  function tokenBase(stream, state) {
38
    var ch = stream.next();
39
    curPunc = null;
40
    if (ch == "$" || ch == "?") {
41
      if(ch == "?" && stream.match(/\s/, false)){
42
        return "operator";
43
      }
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]*/);
45
      return "variable-2";
46
    }
47
    else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
48
      stream.match(/^[^\s\u00a0>]*>?/);
49
      return "atom";
50
    }
51
    else if (ch == "\"" || ch == "'") {
52
      state.tokenize = tokenLiteral(ch);
53
      return state.tokenize(stream, state);
54
    }
55
    else if (/[{}\(\),\.;\[\]]/.test(ch)) {
56
      curPunc = ch;
57
      return "bracket";
58
    }
59
    else if (ch == "#") {
60
      stream.skipToEnd();
61
      return "comment";
62
    }
63
    else if (ch === "^") {
64
      ch = stream.peek();
65
      if (ch === "^") stream.eat("^");
66
      else stream.eatWhile(operatorChars);
67
      return "operator";
68
    }
69
    else if (operatorChars.test(ch)) {
70
      stream.eatWhile(operatorChars);
71
      return "operator";
72
    }
73
    else if (ch == ":") {
74
      eatPnLocal(stream);
75
      return "atom";
76
    }
77
    else if (ch == "@") {
78
      stream.eatWhile(/[a-z\d\-]/i);
79
      return "meta";
80
    }
81
    else {
82
      stream.eatWhile(/[_\w\d]/);
83
      if (stream.eat(":")) {
84
        eatPnLocal(stream);
85
        return "atom";
86
      }
87
      var word = stream.current();
88
      if (ops.test(word))
89
        return "builtin";
90
      else if (keywords.test(word))
91
        return "keyword";
92
      else
93
        return "variable";
94
    }
95
  }
96

97
  function eatPnLocal(stream) {
98
    stream.match(/(\.(?=[\w_\-\\%])|[:\w_-]|\\[-\\_~.!$&'()*+,;=/?#@%]|%[a-f\d][a-f\d])+/i);
99
  }
100

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;
107
          break;
108
        }
109
        escaped = !escaped && ch == "\\";
110
      }
111
      return "string";
112
    };
113
  }
114

115
  function pushContext(state, type, col) {
116
    state.context = {prev: state.context, indent: state.indent, col: col, type: type};
117
  }
118
  function popContext(state) {
119
    state.indent = state.context.indent;
120
    state.context = state.context.prev;
121
  }
122

123
  return {
124
    startState: function() {
125
      return {tokenize: tokenBase,
126
              context: null,
127
              indent: 0,
128
              col: 0};
129
    },
130

131
    token: function(stream, state) {
132
      if (stream.sol()) {
133
        if (state.context && state.context.align == null) state.context.align = false;
134
        state.indent = stream.indentation();
135
      }
136
      if (stream.eatSpace()) return null;
137
      var style = state.tokenize(stream, state);
138

139
      if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
140
        state.context.align = true;
141
      }
142

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) {
149
          popContext(state);
150
          if (curPunc == "}" && state.context && state.context.type == "pattern")
151
            popContext(state);
152
        }
153
      }
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();
161
        }
162
      }
163

164
      return style;
165
    },
166

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;
172

173
      var closing = context && firstChar == context.type;
174
      if (!context)
175
        return 0;
176
      else if (context.type == "pattern")
177
        return context.col;
178
      else if (context.align)
179
        return context.col + (closing ? 0 : 1);
180
      else
181
        return context.indent + (closing ? 0 : indentUnit);
182
    },
183

184
    lineComment: "#"
185
  };
186
});
187

188
CodeMirror.defineMIME("application/sparql-query", "sparql");
189

190
});
191

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.