tcl.js 4.9 KB

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