eiffel.js 3.7 KB

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