eiffel.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("eiffel", function() {
  11. function wordObj(words) {
  12. var o = {};
  13. for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true;
  14. return o;
  15. }
  16. var keywords = wordObj([
  17. 'note',
  18. 'across',
  19. 'when',
  20. 'variant',
  21. 'until',
  22. 'unique',
  23. 'undefine',
  24. 'then',
  25. 'strip',
  26. 'select',
  27. 'retry',
  28. 'rescue',
  29. 'require',
  30. 'rename',
  31. 'reference',
  32. 'redefine',
  33. 'prefix',
  34. 'once',
  35. 'old',
  36. 'obsolete',
  37. 'loop',
  38. 'local',
  39. 'like',
  40. 'is',
  41. 'inspect',
  42. 'infix',
  43. 'include',
  44. 'if',
  45. 'frozen',
  46. 'from',
  47. 'external',
  48. 'export',
  49. 'ensure',
  50. 'end',
  51. 'elseif',
  52. 'else',
  53. 'do',
  54. 'creation',
  55. 'create',
  56. 'check',
  57. 'alias',
  58. 'agent',
  59. 'separate',
  60. 'invariant',
  61. 'inherit',
  62. 'indexing',
  63. 'feature',
  64. 'expanded',
  65. 'deferred',
  66. 'class',
  67. 'Void',
  68. 'True',
  69. 'Result',
  70. 'Precursor',
  71. 'False',
  72. 'Current',
  73. 'create',
  74. 'attached',
  75. 'detachable',
  76. 'as',
  77. 'and',
  78. 'implies',
  79. 'not',
  80. 'or'
  81. ]);
  82. var operators = wordObj([":=", "and then","and", "or","<<",">>"]);
  83. var curPunc;
  84. function chain(newtok, stream, state) {
  85. state.tokenize.push(newtok);
  86. return newtok(stream, state);
  87. }
  88. function tokenBase(stream, state) {
  89. curPunc = null;
  90. if (stream.eatSpace()) return null;
  91. var ch = stream.next();
  92. if (ch == '"'||ch == "'") {
  93. return chain(readQuoted(ch, "string"), stream, state);
  94. } else if (ch == "-"&&stream.eat("-")) {
  95. stream.skipToEnd();
  96. return "comment";
  97. } else if (ch == ":"&&stream.eat("=")) {
  98. return "operator";
  99. } else if (/[0-9]/.test(ch)) {
  100. stream.eatWhile(/[xXbBCc0-9\.]/);
  101. stream.eat(/[\?\!]/);
  102. return "ident";
  103. } else if (/[a-zA-Z_0-9]/.test(ch)) {
  104. stream.eatWhile(/[a-zA-Z_0-9]/);
  105. stream.eat(/[\?\!]/);
  106. return "ident";
  107. } else if (/[=+\-\/*^%<>~]/.test(ch)) {
  108. stream.eatWhile(/[=+\-\/*^%<>~]/);
  109. return "operator";
  110. } else {
  111. return null;
  112. }
  113. }
  114. function readQuoted(quote, style, unescaped) {
  115. return function(stream, state) {
  116. var escaped = false, ch;
  117. while ((ch = stream.next()) != null) {
  118. if (ch == quote && (unescaped || !escaped)) {
  119. state.tokenize.pop();
  120. break;
  121. }
  122. escaped = !escaped && ch == "%";
  123. }
  124. return style;
  125. };
  126. }
  127. return {
  128. startState: function() {
  129. return {tokenize: [tokenBase]};
  130. },
  131. token: function(stream, state) {
  132. var style = state.tokenize[state.tokenize.length-1](stream, state);
  133. if (style == "ident") {
  134. var word = stream.current();
  135. style = keywords.propertyIsEnumerable(stream.current()) ? "keyword"
  136. : operators.propertyIsEnumerable(stream.current()) ? "operator"
  137. : /^[A-Z][A-Z_0-9]*$/g.test(word) ? "tag"
  138. : /^0[bB][0-1]+$/g.test(word) ? "number"
  139. : /^0[cC][0-7]+$/g.test(word) ? "number"
  140. : /^0[xX][a-fA-F0-9]+$/g.test(word) ? "number"
  141. : /^([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+)$/g.test(word) ? "number"
  142. : /^[0-9]+$/g.test(word) ? "number"
  143. : "variable";
  144. }
  145. return style;
  146. },
  147. lineComment: "--"
  148. };
  149. });
  150. CodeMirror.defineMIME("text/x-eiffel", "eiffel");
  151. });