tcl.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. } else if (/\d/.test(ch)) {
  49. stream.eatWhile(/[\w\.]/);
  50. return "number";
  51. } else if (ch == "#") {
  52. if (stream.eat("*"))
  53. return chain(stream, state, tokenComment);
  54. if (ch == "#" && stream.match(/ *\[ *\[/))
  55. return chain(stream, state, tokenUnparsed);
  56. stream.skipToEnd();
  57. return "comment";
  58. } else if (ch == '"') {
  59. stream.skipTo(/"/);
  60. return "comment";
  61. } else if (ch == "$") {
  62. stream.eatWhile(/[$_a-z0-9A-Z\.{:]/);
  63. stream.eatWhile(/}/);
  64. state.beforeParams = true;
  65. return "builtin";
  66. } else if (isOperatorChar.test(ch)) {
  67. stream.eatWhile(isOperatorChar);
  68. return "comment";
  69. } else {
  70. stream.eatWhile(/[\w\$_{}\xa1-\uffff]/);
  71. var word = stream.current().toLowerCase();
  72. if (keywords && keywords.propertyIsEnumerable(word))
  73. return "keyword";
  74. if (functions && functions.propertyIsEnumerable(word)) {
  75. state.beforeParams = true;
  76. return "keyword";
  77. }
  78. return null;
  79. }
  80. }
  81. function tokenString(quote) {
  82. return function(stream, state) {
  83. var escaped = false, next, end = false;
  84. while ((next = stream.next()) != null) {
  85. if (next == quote && !escaped) {
  86. end = true;
  87. break;
  88. }
  89. escaped = !escaped && next == "\\";
  90. }
  91. if (end) state.tokenize = tokenBase;
  92. return "string";
  93. };
  94. }
  95. function tokenComment(stream, state) {
  96. var maybeEnd = false, ch;
  97. while (ch = stream.next()) {
  98. if (ch == "#" && maybeEnd) {
  99. state.tokenize = tokenBase;
  100. break;
  101. }
  102. maybeEnd = (ch == "*");
  103. }
  104. return "comment";
  105. }
  106. function tokenUnparsed(stream, state) {
  107. var maybeEnd = 0, ch;
  108. while (ch = stream.next()) {
  109. if (ch == "#" && maybeEnd == 2) {
  110. state.tokenize = tokenBase;
  111. break;
  112. }
  113. if (ch == "]")
  114. maybeEnd++;
  115. else if (ch != " ")
  116. maybeEnd = 0;
  117. }
  118. return "meta";
  119. }
  120. return {
  121. startState: function() {
  122. return {
  123. tokenize: tokenBase,
  124. beforeParams: false,
  125. inParams: false
  126. };
  127. },
  128. token: function(stream, state) {
  129. if (stream.eatSpace()) return null;
  130. return state.tokenize(stream, state);
  131. },
  132. lineComment: "#"
  133. };
  134. });
  135. CodeMirror.defineMIME("text/x-tcl", "tcl");
  136. });