openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

sparql.js (6335B)


      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", "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(/^[\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) {
    139           popContext(state);
    140           if (curPunc == "}" && state.context && state.context.type == "pattern")
    141             popContext(state);
    142         }
    143       }
    144       else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
    145       else if (/atom|string|variable/.test(style) && state.context) {
    146         if (/[\}\]]/.test(state.context.type))
    147           pushContext(state, "pattern", stream.column());
    148         else if (state.context.type == "pattern" && !state.context.align) {
    149           state.context.align = true;
    150           state.context.col = stream.column();
    151         }
    152       }
    153 
    154       return style;
    155     },
    156 
    157     indent: function(state, textAfter) {
    158       var firstChar = textAfter && textAfter.charAt(0);
    159       var context = state.context;
    160       if (/[\]\}]/.test(firstChar))
    161         while (context && context.type == "pattern") context = context.prev;
    162 
    163       var closing = context && firstChar == context.type;
    164       if (!context)
    165         return 0;
    166       else if (context.type == "pattern")
    167         return context.col;
    168       else if (context.align)
    169         return context.col + (closing ? 0 : 1);
    170       else
    171         return context.indent + (closing ? 0 : indentUnit);
    172     },
    173 
    174     lineComment: "#"
    175   };
    176 });
    177 
    178 CodeMirror.defineMIME("application/sparql-query", "sparql");
    179 
    180 });