Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
5 lars 1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://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"]);
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(/^[\w\d]*/);
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 (operatorChars.test(ch)) {
64
      stream.eatWhile(operatorChars);
65
      return "operator";
66
    }
67
    else if (ch == ":") {
68
      stream.eatWhile(/[\w\d\._\-]/);
69
      return "atom";
70
    }
71
    else if (ch == "@") {
72
      stream.eatWhile(/[a-z\d\-]/i);
73
      return "meta";
74
    }
75
    else {
76
      stream.eatWhile(/[_\w\d]/);
77
      if (stream.eat(":")) {
78
        stream.eatWhile(/[\w\d_\-]/);
79
        return "atom";
80
      }
81
      var word = stream.current();
82
      if (ops.test(word))
83
        return "builtin";
84
      else if (keywords.test(word))
85
        return "keyword";
86
      else
87
        return "variable";
88
    }
89
  }
90
 
91
  function tokenLiteral(quote) {
92
    return function(stream, state) {
93
      var escaped = false, ch;
94
      while ((ch = stream.next()) != null) {
95
        if (ch == quote && !escaped) {
96
          state.tokenize = tokenBase;
97
          break;
98
        }
99
        escaped = !escaped && ch == "\\";
100
      }
101
      return "string";
102
    };
103
  }
104
 
105
  function pushContext(state, type, col) {
106
    state.context = {prev: state.context, indent: state.indent, col: col, type: type};
107
  }
108
  function popContext(state) {
109
    state.indent = state.context.indent;
110
    state.context = state.context.prev;
111
  }
112
 
113
  return {
114
    startState: function() {
115
      return {tokenize: tokenBase,
116
              context: null,
117
              indent: 0,
118
              col: 0};
119
    },
120
 
121
    token: function(stream, state) {
122
      if (stream.sol()) {
123
        if (state.context && state.context.align == null) state.context.align = false;
124
        state.indent = stream.indentation();
125
      }
126
      if (stream.eatSpace()) return null;
127
      var style = state.tokenize(stream, state);
128
 
129
      if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
130
        state.context.align = true;
131
      }
132
 
133
      if (curPunc == "(") pushContext(state, ")", stream.column());
134
      else if (curPunc == "[") pushContext(state, "]", stream.column());
135
      else if (curPunc == "{") pushContext(state, "}", stream.column());
136
      else if (/[\]\}\)]/.test(curPunc)) {
137
        while (state.context && state.context.type == "pattern") popContext(state);
138
        if (state.context && curPunc == state.context.type) popContext(state);
139
      }
140
      else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
141
      else if (/atom|string|variable/.test(style) && state.context) {
142
        if (/[\}\]]/.test(state.context.type))
143
          pushContext(state, "pattern", stream.column());
144
        else if (state.context.type == "pattern" && !state.context.align) {
145
          state.context.align = true;
146
          state.context.col = stream.column();
147
        }
148
      }
149
 
150
      return style;
151
    },
152
 
153
    indent: function(state, textAfter) {
154
      var firstChar = textAfter && textAfter.charAt(0);
155
      var context = state.context;
156
      if (/[\]\}]/.test(firstChar))
157
        while (context && context.type == "pattern") context = context.prev;
158
 
159
      var closing = context && firstChar == context.type;
160
      if (!context)
161
        return 0;
162
      else if (context.type == "pattern")
163
        return context.col;
164
      else if (context.align)
165
        return context.col + (closing ? 0 : 1);
166
      else
167
        return context.indent + (closing ? 0 : indentUnit);
168
    }
169
  };
170
});
171
 
172
CodeMirror.defineMIME("application/sparql-query", "sparql");
173
 
174
});