1 CodeMirror.defineMode("turtle", function(config) {
2 var indentUnit = config.indentUnit;
5 function wordRegexp(words) {
6 return new RegExp("^(?:" + words.join("|") + ")$", "i");
8 var ops = wordRegexp([]);
9 var keywords = wordRegexp(["@prefix", "@base", "a"]);
10 var operatorChars = /[*+\-<>=&|]/;
12 function tokenBase(stream, state) {
13 var ch = stream.next();
15 if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
16 stream.match(/^[^\s\u00a0>]*>?/);
19 else if (ch == "\"" || ch == "'") {
20 state.tokenize = tokenLiteral(ch);
21 return state.tokenize(stream, state);
23 else if (/[{}\(\),\.;\[\]]/.test(ch)) {
31 else if (operatorChars.test(ch)) {
32 stream.eatWhile(operatorChars);
38 stream.eatWhile(/[_\w\d]/);
39 if(stream.peek() == ":") {
42 var word = stream.current();
44 if(keywords.test(word)) {
48 if(ch >= "A" && ch <= "Z") {
54 var word = stream.current();
57 else if (keywords.test(word))
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;
72 escaped = !escaped && ch == "\\";
78 function pushContext(state, type, col) {
79 state.context = {prev: state.context, indent: state.indent, col: col, type: type};
81 function popContext(state) {
82 state.indent = state.context.indent;
83 state.context = state.context.prev;
87 startState: function() {
88 return {tokenize: tokenBase,
94 token: function(stream, state) {
96 if (state.context && state.context.align == null) state.context.align = false;
97 state.indent = stream.indentation();
99 if (stream.eatSpace()) return null;
100 var style = state.tokenize(stream, state);
102 if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
103 state.context.align = true;
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);
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();
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;
132 var closing = context && firstChar == context.type;
135 else if (context.type == "pattern")
137 else if (context.align)
138 return context.col + (closing ? 0 : 1);
140 return context.indent + (closing ? 0 : indentUnit);
145 CodeMirror.defineMIME("text/turtle", "turtle");