eiffel.js 5.2 KB

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