tcl.js 4.8 KB

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