| 1 |
var curPunc; |
| 2 |
|
| 3 |
function wordRegexp(words) { |
| 4 |
return new RegExp("^(?:" + words.join("|") + ")$", "i"); |
| 5 |
} |
| 6 |
var ops = wordRegexp([]); |
| 7 |
var keywords = wordRegexp(["@prefix", "@base", "a"]); |
| 8 |
var operatorChars = /[*+\-<>=&|]/; |
| 9 |
|
| 10 |
function tokenBase(stream, state) { |
| 11 |
var ch = stream.next(); |
| 12 |
curPunc = null; |
| 13 |
if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) { |
| 14 |
stream.match(/^[^\s\u00a0>]*>?/); |
| 15 |
return "atom"; |
| 16 |
} |
| 17 |
else if (ch == "\"" || ch == "'") { |
| 18 |
state.tokenize = tokenLiteral(ch); |
| 19 |
return state.tokenize(stream, state); |
| 20 |
} |
| 21 |
else if (/[{}\(\),\.;\[\]]/.test(ch)) { |
| 22 |
curPunc = ch; |
| 23 |
return null; |
| 24 |
} |
| 25 |
else if (ch == "#") { |
| 26 |
stream.skipToEnd(); |
| 27 |
return "comment"; |
| 28 |
} |
| 29 |
else if (operatorChars.test(ch)) { |
| 30 |
stream.eatWhile(operatorChars); |
| 31 |
return null; |
| 32 |
} |
| 33 |
else if (ch == ":") { |
| 34 |
return "operator"; |
| 35 |
} else { |
| 36 |
stream.eatWhile(/[_\w\d]/); |
| 37 |
if(stream.peek() == ":") { |
| 38 |
return "variableName.special"; |
| 39 |
} else { |
| 40 |
var word = stream.current(); |
| 41 |
|
| 42 |
if(keywords.test(word)) { |
| 43 |
return "meta"; |
| 44 |
} |
| 45 |
|
| 46 |
if(ch >= "A" && ch <= "Z") { |
| 47 |
return "comment"; |
| 48 |
} else { |
| 49 |
return "keyword"; |
| 50 |
} |
| 51 |
} |
| 52 |
var word = stream.current(); |
| 53 |
if (ops.test(word)) |
| 54 |
return null; |
| 55 |
else if (keywords.test(word)) |
| 56 |
return "meta"; |
| 57 |
else |
| 58 |
return "variable"; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
function tokenLiteral(quote) { |
| 63 |
return function(stream, state) { |
| 64 |
var escaped = false, ch; |
| 65 |
while ((ch = stream.next()) != null) { |
| 66 |
if (ch == quote && !escaped) { |
| 67 |
state.tokenize = tokenBase; |
| 68 |
break; |
| 69 |
} |
| 70 |
escaped = !escaped && ch == "\\"; |
| 71 |
} |
| 72 |
return "string"; |
| 73 |
}; |
| 74 |
} |
| 75 |
|
| 76 |
function pushContext(state, type, col) { |
| 77 |
state.context = {prev: state.context, indent: state.indent, col: col, type: type}; |
| 78 |
} |
| 79 |
function popContext(state) { |
| 80 |
state.indent = state.context.indent; |
| 81 |
state.context = state.context.prev; |
| 82 |
} |
| 83 |
|
| 84 |
export const turtle = { |
| 85 |
name: "turtle", |
| 86 |
startState: function() { |
| 87 |
return {tokenize: tokenBase, |
| 88 |
context: null, |
| 89 |
indent: 0, |
| 90 |
col: 0}; |
| 91 |
}, |
| 92 |
|
| 93 |
token: function(stream, state) { |
| 94 |
if (stream.sol()) { |
| 95 |
if (state.context && state.context.align == null) state.context.align = false; |
| 96 |
state.indent = stream.indentation(); |
| 97 |
} |
| 98 |
if (stream.eatSpace()) return null; |
| 99 |
var style = state.tokenize(stream, state); |
| 100 |
|
| 101 |
if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") { |
| 102 |
state.context.align = true; |
| 103 |
} |
| 104 |
|
| 105 |
if (curPunc == "(") pushContext(state, ")", stream.column()); |
| 106 |
else if (curPunc == "[") pushContext(state, "]", stream.column()); |
| 107 |
else if (curPunc == "{") pushContext(state, "}", stream.column()); |
| 108 |
else if (/[\]\}\)]/.test(curPunc)) { |
| 109 |
while (state.context && state.context.type == "pattern") popContext(state); |
| 110 |
if (state.context && curPunc == state.context.type) popContext(state); |
| 111 |
} |
| 112 |
else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state); |
| 113 |
else if (/atom|string|variable/.test(style) && state.context) { |
| 114 |
if (/[\}\]]/.test(state.context.type)) |
| 115 |
pushContext(state, "pattern", stream.column()); |
| 116 |
else if (state.context.type == "pattern" && !state.context.align) { |
| 117 |
state.context.align = true; |
| 118 |
state.context.col = stream.column(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return style; |
| 123 |
}, |
| 124 |
|
| 125 |
indent: function(state, textAfter, cx) { |
| 126 |
var firstChar = textAfter && textAfter.charAt(0); |
| 127 |
var context = state.context; |
| 128 |
if (/[\]\}]/.test(firstChar)) |
| 129 |
while (context && context.type == "pattern") context = context.prev; |
| 130 |
|
| 131 |
var closing = context && firstChar == context.type; |
| 132 |
if (!context) |
| 133 |
return 0; |
| 134 |
else if (context.type == "pattern") |
| 135 |
return context.col; |
| 136 |
else if (context.align) |
| 137 |
return context.col + (closing ? 0 : 1); |
| 138 |
else |
| 139 |
return context.indent + (closing ? 0 : cx.unit); |
| 140 |
}, |
| 141 |
|
| 142 |
languageData: { |
| 143 |
commentTokens: {line: "#"} |
| 144 |
} |
| 145 |
}; |