e118bfbcede58e676a61a3f36b1e8198896a57d3
[myslice.git] / third-party / codemirror-3.15 / mode / turtle / turtle.js
1 CodeMirror.defineMode("turtle", 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([]);
9   var keywords = wordRegexp(["@prefix", "@base", "a"]);
10   var operatorChars = /[*+\-<>=&|]/;
11
12   function tokenBase(stream, state) {
13     var ch = stream.next();
14     curPunc = null;
15     if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
16       stream.match(/^[^\s\u00a0>]*>?/);
17       return "atom";
18     }
19     else if (ch == "\"" || ch == "'") {
20       state.tokenize = tokenLiteral(ch);
21       return state.tokenize(stream, state);
22     }
23     else if (/[{}\(\),\.;\[\]]/.test(ch)) {
24       curPunc = ch;
25       return null;
26     }
27     else if (ch == "#") {
28       stream.skipToEnd();
29       return "comment";
30     }
31     else if (operatorChars.test(ch)) {
32       stream.eatWhile(operatorChars);
33       return null;
34     }
35     else if (ch == ":") {
36           return "operator";
37         } else {
38       stream.eatWhile(/[_\w\d]/);
39       if(stream.peek() == ":") {
40         return "variable-3";
41       } else {
42              var word = stream.current();
43
44              if(keywords.test(word)) {
45                         return "meta";
46              }
47
48              if(ch >= "A" && ch <= "Z") {
49                     return "comment";
50                  } else {
51                         return "keyword";
52                  }
53       }
54       var word = stream.current();
55       if (ops.test(word))
56         return null;
57       else if (keywords.test(word))
58         return "meta";
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("text/turtle", "turtle");