| 1 |
function parseWords(str) { |
| 2 |
var obj = {}, words = str.split(" "); |
| 3 |
for (var i = 0; i < words.length; ++i) obj[words[i]] = true; |
| 4 |
return obj; |
| 5 |
} |
| 6 |
var keywords = parseWords("Tcl safe after append array auto_execok auto_import auto_load " + |
| 7 |
"auto_mkindex auto_mkindex_old auto_qualify auto_reset bgerror " + |
| 8 |
"binary break catch cd close concat continue dde eof encoding error " + |
| 9 |
"eval exec exit expr fblocked fconfigure fcopy file fileevent filename " + |
| 10 |
"filename flush for foreach format gets glob global history http if " + |
| 11 |
"incr info interp join lappend lindex linsert list llength load lrange " + |
| 12 |
"lreplace lsearch lset lsort memory msgcat namespace open package parray " + |
| 13 |
"pid pkg::create pkg_mkIndex proc puts pwd re_syntax read regex regexp " + |
| 14 |
"registry regsub rename resource return scan seek set socket source split " + |
| 15 |
"string subst switch tcl_endOfWord tcl_findLibrary tcl_startOfNextWord " + |
| 16 |
"tcl_wordBreakAfter tcl_startOfPreviousWord tcl_wordBreakBefore tcltest " + |
| 17 |
"tclvars tell time trace unknown unset update uplevel upvar variable " + |
| 18 |
"vwait"); |
| 19 |
var functions = parseWords("if elseif else and not or eq ne in ni for foreach while switch"); |
| 20 |
var isOperatorChar = /[+\-*&%=<>!?^\/\|]/; |
| 21 |
function chain(stream, state, f) { |
| 22 |
state.tokenize = f; |
| 23 |
return f(stream, state); |
| 24 |
} |
| 25 |
function tokenBase(stream, state) { |
| 26 |
var beforeParams = state.beforeParams; |
| 27 |
state.beforeParams = false; |
| 28 |
var ch = stream.next(); |
| 29 |
if ((ch == '"' || ch == "'") && state.inParams) { |
| 30 |
return chain(stream, state, tokenString(ch)); |
| 31 |
} else if (/[\[\]{}\(\),;\.]/.test(ch)) { |
| 32 |
if (ch == "(" && beforeParams) state.inParams = true; |
| 33 |
else if (ch == ")") state.inParams = false; |
| 34 |
return null; |
| 35 |
} else if (/\d/.test(ch)) { |
| 36 |
stream.eatWhile(/[\w\.]/); |
| 37 |
return "number"; |
| 38 |
} else if (ch == "#") { |
| 39 |
if (stream.eat("*")) |
| 40 |
return chain(stream, state, tokenComment); |
| 41 |
if (ch == "#" && stream.match(/ *\[ *\[/)) |
| 42 |
return chain(stream, state, tokenUnparsed); |
| 43 |
stream.skipToEnd(); |
| 44 |
return "comment"; |
| 45 |
} else if (ch == '"') { |
| 46 |
stream.skipTo(/"/); |
| 47 |
return "comment"; |
| 48 |
} else if (ch == "$") { |
| 49 |
stream.eatWhile(/[$_a-z0-9A-Z\.{:]/); |
| 50 |
stream.eatWhile(/}/); |
| 51 |
state.beforeParams = true; |
| 52 |
return "builtin"; |
| 53 |
} else if (isOperatorChar.test(ch)) { |
| 54 |
stream.eatWhile(isOperatorChar); |
| 55 |
return "comment"; |
| 56 |
} else { |
| 57 |
stream.eatWhile(/[\w\$_{}\xa1-\uffff]/); |
| 58 |
var word = stream.current().toLowerCase(); |
| 59 |
if (keywords && keywords.propertyIsEnumerable(word)) |
| 60 |
return "keyword"; |
| 61 |
if (functions && functions.propertyIsEnumerable(word)) { |
| 62 |
state.beforeParams = true; |
| 63 |
return "keyword"; |
| 64 |
} |
| 65 |
return null; |
| 66 |
} |
| 67 |
} |
| 68 |
function tokenString(quote) { |
| 69 |
return function(stream, state) { |
| 70 |
var escaped = false, next, end = false; |
| 71 |
while ((next = stream.next()) != null) { |
| 72 |
if (next == quote && !escaped) { |
| 73 |
end = true; |
| 74 |
break; |
| 75 |
} |
| 76 |
escaped = !escaped && next == "\\"; |
| 77 |
} |
| 78 |
if (end) state.tokenize = tokenBase; |
| 79 |
return "string"; |
| 80 |
}; |
| 81 |
} |
| 82 |
function tokenComment(stream, state) { |
| 83 |
var maybeEnd = false, ch; |
| 84 |
while (ch = stream.next()) { |
| 85 |
if (ch == "#" && maybeEnd) { |
| 86 |
state.tokenize = tokenBase; |
| 87 |
break; |
| 88 |
} |
| 89 |
maybeEnd = (ch == "*"); |
| 90 |
} |
| 91 |
return "comment"; |
| 92 |
} |
| 93 |
function tokenUnparsed(stream, state) { |
| 94 |
var maybeEnd = 0, ch; |
| 95 |
while (ch = stream.next()) { |
| 96 |
if (ch == "#" && maybeEnd == 2) { |
| 97 |
state.tokenize = tokenBase; |
| 98 |
break; |
| 99 |
} |
| 100 |
if (ch == "]") |
| 101 |
maybeEnd++; |
| 102 |
else if (ch != " ") |
| 103 |
maybeEnd = 0; |
| 104 |
} |
| 105 |
return "meta"; |
| 106 |
} |
| 107 |
export const tcl = { |
| 108 |
name: "tcl", |
| 109 |
startState: function() { |
| 110 |
return { |
| 111 |
tokenize: tokenBase, |
| 112 |
beforeParams: false, |
| 113 |
inParams: false |
| 114 |
}; |
| 115 |
}, |
| 116 |
token: function(stream, state) { |
| 117 |
if (stream.eatSpace()) return null; |
| 118 |
return state.tokenize(stream, state); |
| 119 |
}, |
| 120 |
languageData: { |
| 121 |
commentTokens: {line: "#"} |
| 122 |
} |
| 123 |
}; |