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