openrat-cms

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

turtle.js (4849B)


      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("turtle", 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([]);
     22   var keywords = wordRegexp(["@prefix", "@base", "a"]);
     23   var operatorChars = /[*+\-<>=&|]/;
     24 
     25   function tokenBase(stream, state) {
     26     var ch = stream.next();
     27     curPunc = null;
     28     if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
     29       stream.match(/^[^\s\u00a0>]*>?/);
     30       return "atom";
     31     }
     32     else if (ch == "\"" || ch == "'") {
     33       state.tokenize = tokenLiteral(ch);
     34       return state.tokenize(stream, state);
     35     }
     36     else if (/[{}\(\),\.;\[\]]/.test(ch)) {
     37       curPunc = ch;
     38       return null;
     39     }
     40     else if (ch == "#") {
     41       stream.skipToEnd();
     42       return "comment";
     43     }
     44     else if (operatorChars.test(ch)) {
     45       stream.eatWhile(operatorChars);
     46       return null;
     47     }
     48     else if (ch == ":") {
     49           return "operator";
     50         } else {
     51       stream.eatWhile(/[_\w\d]/);
     52       if(stream.peek() == ":") {
     53         return "variable-3";
     54       } else {
     55              var word = stream.current();
     56 
     57              if(keywords.test(word)) {
     58                         return "meta";
     59              }
     60 
     61              if(ch >= "A" && ch <= "Z") {
     62                     return "comment";
     63                  } else {
     64                         return "keyword";
     65                  }
     66       }
     67       var word = stream.current();
     68       if (ops.test(word))
     69         return null;
     70       else if (keywords.test(word))
     71         return "meta";
     72       else
     73         return "variable";
     74     }
     75   }
     76 
     77   function tokenLiteral(quote) {
     78     return function(stream, state) {
     79       var escaped = false, ch;
     80       while ((ch = stream.next()) != null) {
     81         if (ch == quote && !escaped) {
     82           state.tokenize = tokenBase;
     83           break;
     84         }
     85         escaped = !escaped && ch == "\\";
     86       }
     87       return "string";
     88     };
     89   }
     90 
     91   function pushContext(state, type, col) {
     92     state.context = {prev: state.context, indent: state.indent, col: col, type: type};
     93   }
     94   function popContext(state) {
     95     state.indent = state.context.indent;
     96     state.context = state.context.prev;
     97   }
     98 
     99   return {
    100     startState: function() {
    101       return {tokenize: tokenBase,
    102               context: null,
    103               indent: 0,
    104               col: 0};
    105     },
    106 
    107     token: function(stream, state) {
    108       if (stream.sol()) {
    109         if (state.context && state.context.align == null) state.context.align = false;
    110         state.indent = stream.indentation();
    111       }
    112       if (stream.eatSpace()) return null;
    113       var style = state.tokenize(stream, state);
    114 
    115       if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
    116         state.context.align = true;
    117       }
    118 
    119       if (curPunc == "(") pushContext(state, ")", stream.column());
    120       else if (curPunc == "[") pushContext(state, "]", stream.column());
    121       else if (curPunc == "{") pushContext(state, "}", stream.column());
    122       else if (/[\]\}\)]/.test(curPunc)) {
    123         while (state.context && state.context.type == "pattern") popContext(state);
    124         if (state.context && curPunc == state.context.type) popContext(state);
    125       }
    126       else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
    127       else if (/atom|string|variable/.test(style) && state.context) {
    128         if (/[\}\]]/.test(state.context.type))
    129           pushContext(state, "pattern", stream.column());
    130         else if (state.context.type == "pattern" && !state.context.align) {
    131           state.context.align = true;
    132           state.context.col = stream.column();
    133         }
    134       }
    135 
    136       return style;
    137     },
    138 
    139     indent: function(state, textAfter) {
    140       var firstChar = textAfter && textAfter.charAt(0);
    141       var context = state.context;
    142       if (/[\]\}]/.test(firstChar))
    143         while (context && context.type == "pattern") context = context.prev;
    144 
    145       var closing = context && firstChar == context.type;
    146       if (!context)
    147         return 0;
    148       else if (context.type == "pattern")
    149         return context.col;
    150       else if (context.align)
    151         return context.col + (closing ? 0 : 1);
    152       else
    153         return context.indent + (closing ? 0 : indentUnit);
    154     },
    155 
    156     lineComment: "#"
    157   };
    158 });
    159 
    160 CodeMirror.defineMIME("text/turtle", "turtle");
    161 
    162 });