1
0

stex.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Author: Constantin Jucovschi (c.jucovschi@jacobs-university.de)
  3. * Licence: MIT
  4. */
  5. CodeMirror.defineMode("stex", function() {
  6. "use strict";
  7. function pushCommand(state, command) {
  8. state.cmdState.push(command);
  9. }
  10. function peekCommand(state) {
  11. if (state.cmdState.length > 0) {
  12. return state.cmdState[state.cmdState.length - 1];
  13. } else {
  14. return null;
  15. }
  16. }
  17. function popCommand(state) {
  18. var plug = state.cmdState.pop();
  19. if (plug) {
  20. plug.closeBracket();
  21. }
  22. }
  23. // returns the non-default plugin closest to the end of the list
  24. function getMostPowerful(state) {
  25. var context = state.cmdState;
  26. for (var i = context.length - 1; i >= 0; i--) {
  27. var plug = context[i];
  28. if (plug.name == "DEFAULT") {
  29. continue;
  30. }
  31. return plug;
  32. }
  33. return { styleIdentifier: function() { return null; } };
  34. }
  35. function addPluginPattern(pluginName, cmdStyle, styles) {
  36. return function () {
  37. this.name = pluginName;
  38. this.bracketNo = 0;
  39. this.style = cmdStyle;
  40. this.styles = styles;
  41. this.argument = null; // \begin and \end have arguments that follow. These are stored in the plugin
  42. this.styleIdentifier = function() {
  43. return this.styles[this.bracketNo - 1] || null;
  44. };
  45. this.openBracket = function() {
  46. this.bracketNo++;
  47. return "bracket";
  48. };
  49. this.closeBracket = function() {};
  50. };
  51. }
  52. var plugins = {};
  53. plugins["importmodule"] = addPluginPattern("importmodule", "tag", ["string", "builtin"]);
  54. plugins["documentclass"] = addPluginPattern("documentclass", "tag", ["", "atom"]);
  55. plugins["usepackage"] = addPluginPattern("usepackage", "tag", ["atom"]);
  56. plugins["begin"] = addPluginPattern("begin", "tag", ["atom"]);
  57. plugins["end"] = addPluginPattern("end", "tag", ["atom"]);
  58. plugins["DEFAULT"] = function () {
  59. this.name = "DEFAULT";
  60. this.style = "tag";
  61. this.styleIdentifier = this.openBracket = this.closeBracket = function() {};
  62. };
  63. function setState(state, f) {
  64. state.f = f;
  65. }
  66. // called when in a normal (no environment) context
  67. function normal(source, state) {
  68. var plug;
  69. // Do we look like '\command' ? If so, attempt to apply the plugin 'command'
  70. if (source.match(/^\\[a-zA-Z@]+/)) {
  71. var cmdName = source.current().slice(1);
  72. plug = plugins[cmdName] || plugins["DEFAULT"];
  73. plug = new plug();
  74. pushCommand(state, plug);
  75. setState(state, beginParams);
  76. return plug.style;
  77. }
  78. // escape characters
  79. if (source.match(/^\\[$&%#{}_]/)) {
  80. return "tag";
  81. }
  82. // white space control characters
  83. if (source.match(/^\\[,;!\/\\]/)) {
  84. return "tag";
  85. }
  86. // find if we're starting various math modes
  87. if (source.match("\\[")) {
  88. setState(state, function(source, state){ return inMathMode(source, state, "\\]"); });
  89. return "keyword";
  90. }
  91. if (source.match("$$")) {
  92. setState(state, function(source, state){ return inMathMode(source, state, "$$"); });
  93. return "keyword";
  94. }
  95. if (source.match("$")) {
  96. setState(state, function(source, state){ return inMathMode(source, state, "$"); });
  97. return "keyword";
  98. }
  99. var ch = source.next();
  100. if (ch == "%") {
  101. // special case: % at end of its own line; stay in same state
  102. if (!source.eol()) {
  103. setState(state, inCComment);
  104. }
  105. return "comment";
  106. }
  107. else if (ch == '}' || ch == ']') {
  108. plug = peekCommand(state);
  109. if (plug) {
  110. plug.closeBracket(ch);
  111. setState(state, beginParams);
  112. } else {
  113. return "error";
  114. }
  115. return "bracket";
  116. } else if (ch == '{' || ch == '[') {
  117. plug = plugins["DEFAULT"];
  118. plug = new plug();
  119. pushCommand(state, plug);
  120. return "bracket";
  121. }
  122. else if (/\d/.test(ch)) {
  123. source.eatWhile(/[\w.%]/);
  124. return "atom";
  125. }
  126. else {
  127. source.eatWhile(/[\w\-_]/);
  128. plug = getMostPowerful(state);
  129. if (plug.name == 'begin') {
  130. plug.argument = source.current();
  131. }
  132. return plug.styleIdentifier();
  133. }
  134. }
  135. function inCComment(source, state) {
  136. source.skipToEnd();
  137. setState(state, normal);
  138. return "comment";
  139. }
  140. function inMathMode(source, state, endModeSeq) {
  141. if (source.eatSpace()) {
  142. return null;
  143. }
  144. if (source.match(endModeSeq)) {
  145. setState(state, normal);
  146. return "keyword";
  147. }
  148. if (source.match(/^\\[a-zA-Z@]+/)) {
  149. return "tag";
  150. }
  151. if (source.match(/^[a-zA-Z]+/)) {
  152. return "variable-2";
  153. }
  154. // escape characters
  155. if (source.match(/^\\[$&%#{}_]/)) {
  156. return "tag";
  157. }
  158. // white space control characters
  159. if (source.match(/^\\[,;!\/]/)) {
  160. return "tag";
  161. }
  162. // special math-mode characters
  163. if (source.match(/^[\^_&]/)) {
  164. return "tag";
  165. }
  166. // non-special characters
  167. if (source.match(/^[+\-<>|=,\/@!*:;'"`~#?]/)) {
  168. return null;
  169. }
  170. if (source.match(/^(\d+\.\d*|\d*\.\d+|\d+)/)) {
  171. return "number";
  172. }
  173. var ch = source.next();
  174. if (ch == "{" || ch == "}" || ch == "[" || ch == "]" || ch == "(" || ch == ")") {
  175. return "bracket";
  176. }
  177. // eat comments here, because inCComment returns us to normal state!
  178. if (ch == "%") {
  179. if (!source.eol()) {
  180. source.skipToEnd();
  181. }
  182. return "comment";
  183. }
  184. return "error";
  185. }
  186. function beginParams(source, state) {
  187. var ch = source.peek(), lastPlug;
  188. if (ch == '{' || ch == '[') {
  189. lastPlug = peekCommand(state);
  190. lastPlug.openBracket(ch);
  191. source.eat(ch);
  192. setState(state, normal);
  193. return "bracket";
  194. }
  195. if (/[ \t\r]/.test(ch)) {
  196. source.eat(ch);
  197. return null;
  198. }
  199. setState(state, normal);
  200. popCommand(state);
  201. return normal(source, state);
  202. }
  203. return {
  204. startState: function() {
  205. return {
  206. cmdState: [],
  207. f: normal
  208. };
  209. },
  210. copyState: function(s) {
  211. return {
  212. cmdState: s.cmdState.slice(),
  213. f: s.f
  214. };
  215. },
  216. token: function(stream, state) {
  217. return state.f(stream, state);
  218. }
  219. };
  220. });
  221. CodeMirror.defineMIME("text/x-stex", "stex");
  222. CodeMirror.defineMIME("text/x-latex", "stex");