5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("turtle", function(config) {
15
var indentUnit = config.indentUnit;
18
function wordRegexp(words) {
19
return new RegExp("^(?:" + words.join("|") + ")$", "i");
21
var ops = wordRegexp([]);
22
var keywords = wordRegexp(["@prefix", "@base", "a"]);
23
var operatorChars = /[*+\-<>=&|]/;
25
function tokenBase(stream, state) {
26
var ch = stream.next();
28
if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
29
stream.match(/^[^\s\u00a0>]*>?/);
32
else if (ch == "\"" || ch == "'") {
33
state.tokenize = tokenLiteral(ch);
34
return state.tokenize(stream, state);
36
else if (/[{}\(\),\.;\[\]]/.test(ch)) {
44
else if (operatorChars.test(ch)) {
45
stream.eatWhile(operatorChars);
51
stream.eatWhile(/[_\w\d]/);
52
if(stream.peek() == ":") {
55
var word = stream.current();
57
if(keywords.test(word)) {
61
if(ch >= "A" && ch <= "Z") {
67
var word = stream.current();
70
else if (keywords.test(word))
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;
85
escaped = !escaped && ch == "\\";
91
function pushContext(state, type, col) {
92
state.context = {prev: state.context, indent: state.indent, col: col, type: type};
94
function popContext(state) {
95
state.indent = state.context.indent;
96
state.context = state.context.prev;
100
startState: function() {
101
return {tokenize: tokenBase,
107
token: function(stream, state) {
109
if (state.context && state.context.align == null) state.context.align = false;
110
state.indent = stream.indentation();
112
if (stream.eatSpace()) return null;
113
var style = state.tokenize(stream, state);
115
if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
116
state.context.align = true;
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);
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();
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;
145
var closing = context && firstChar == context.type;
148
else if (context.type == "pattern")
150
else if (context.align)
151
return context.col + (closing ? 0 : 1);
153
return context.indent + (closing ? 0 : indentUnit);
160
CodeMirror.defineMIME("text/turtle", "turtle");