eiffel.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. function chain(newtok, stream, state) {
  86. state.tokenize.push(newtok);
  87. return newtok(stream, state);
  88. }
  89. function tokenBase(stream, state) {
  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. });