groovy.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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('groovy', function (config) {
  15. function words(str) {
  16. var obj = {},
  17. words = str.split(' ')
  18. for (var i = 0; i < words.length; ++i) obj[words[i]] = true
  19. return obj
  20. }
  21. var keywords = words(
  22. 'abstract as assert boolean break byte case catch char class const continue def default ' +
  23. 'do double else enum extends final finally float for goto if implements import in ' +
  24. 'instanceof int interface long native new package private protected public return ' +
  25. 'short static strictfp super switch synchronized threadsafe throw throws trait transient ' +
  26. 'try void volatile while'
  27. )
  28. var blockKeywords = words('catch class def do else enum finally for if interface switch trait try while')
  29. var standaloneKeywords = words('return break continue')
  30. var atoms = words('null true false this')
  31. var curPunc
  32. function tokenBase(stream, state) {
  33. var ch = stream.next()
  34. if (ch == '"' || ch == "'") {
  35. return startString(ch, stream, state)
  36. }
  37. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  38. curPunc = ch
  39. return null
  40. }
  41. if (/\d/.test(ch)) {
  42. stream.eatWhile(/[\w\.]/)
  43. if (stream.eat(/eE/)) {
  44. stream.eat(/\+\-/)
  45. stream.eatWhile(/\d/)
  46. }
  47. return 'number'
  48. }
  49. if (ch == '/') {
  50. if (stream.eat('*')) {
  51. state.tokenize.push(tokenComment)
  52. return tokenComment(stream, state)
  53. }
  54. if (stream.eat('/')) {
  55. stream.skipToEnd()
  56. return 'comment'
  57. }
  58. if (expectExpression(state.lastToken, false)) {
  59. return startString(ch, stream, state)
  60. }
  61. }
  62. if (ch == '-' && stream.eat('>')) {
  63. curPunc = '->'
  64. return null
  65. }
  66. if (/[+\-*&%=<>!?|\/~]/.test(ch)) {
  67. stream.eatWhile(/[+\-*&%=<>|~]/)
  68. return 'operator'
  69. }
  70. stream.eatWhile(/[\w\$_]/)
  71. if (ch == '@') {
  72. stream.eatWhile(/[\w\$_\.]/)
  73. return 'meta'
  74. }
  75. if (state.lastToken == '.') return 'property'
  76. if (stream.eat(':')) {
  77. curPunc = 'proplabel'
  78. return 'property'
  79. }
  80. var cur = stream.current()
  81. if (atoms.propertyIsEnumerable(cur)) {
  82. return 'atom'
  83. }
  84. if (keywords.propertyIsEnumerable(cur)) {
  85. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = 'newstatement'
  86. else if (standaloneKeywords.propertyIsEnumerable(cur)) curPunc = 'standalone'
  87. return 'keyword'
  88. }
  89. return 'variable'
  90. }
  91. tokenBase.isBase = true
  92. function startString(quote, stream, state) {
  93. var tripleQuoted = false
  94. if (quote != '/' && stream.eat(quote)) {
  95. if (stream.eat(quote)) tripleQuoted = true
  96. else return 'string'
  97. }
  98. function t(stream, state) {
  99. var escaped = false,
  100. next,
  101. end = !tripleQuoted
  102. while ((next = stream.next()) != null) {
  103. if (next == quote && !escaped) {
  104. if (!tripleQuoted) {
  105. break
  106. }
  107. if (stream.match(quote + quote)) {
  108. end = true
  109. break
  110. }
  111. }
  112. if (quote == '"' && next == '$' && !escaped && stream.eat('{')) {
  113. state.tokenize.push(tokenBaseUntilBrace())
  114. return 'string'
  115. }
  116. escaped = !escaped && next == '\\'
  117. }
  118. if (end) state.tokenize.pop()
  119. return 'string'
  120. }
  121. state.tokenize.push(t)
  122. return t(stream, state)
  123. }
  124. function tokenBaseUntilBrace() {
  125. var depth = 1
  126. function t(stream, state) {
  127. if (stream.peek() == '}') {
  128. depth--
  129. if (depth == 0) {
  130. state.tokenize.pop()
  131. return state.tokenize[state.tokenize.length - 1](stream, state)
  132. }
  133. } else if (stream.peek() == '{') {
  134. depth++
  135. }
  136. return tokenBase(stream, state)
  137. }
  138. t.isBase = true
  139. return t
  140. }
  141. function tokenComment(stream, state) {
  142. var maybeEnd = false,
  143. ch
  144. while ((ch = stream.next())) {
  145. if (ch == '/' && maybeEnd) {
  146. state.tokenize.pop()
  147. break
  148. }
  149. maybeEnd = ch == '*'
  150. }
  151. return 'comment'
  152. }
  153. function expectExpression(last, newline) {
  154. return !last || last == 'operator' || last == '->' || /[\.\[\{\(,;:]/.test(last) || last == 'newstatement' || last == 'keyword' || last == 'proplabel' || (last == 'standalone' && !newline)
  155. }
  156. function Context(indented, column, type, align, prev) {
  157. this.indented = indented
  158. this.column = column
  159. this.type = type
  160. this.align = align
  161. this.prev = prev
  162. }
  163. function pushContext(state, col, type) {
  164. return (state.context = new Context(state.indented, col, type, null, state.context))
  165. }
  166. function popContext(state) {
  167. var t = state.context.type
  168. if (t == ')' || t == ']' || t == '}') state.indented = state.context.indented
  169. return (state.context = state.context.prev)
  170. }
  171. // Interface
  172. return {
  173. startState: function (basecolumn) {
  174. return {
  175. tokenize: [tokenBase],
  176. context: new Context((basecolumn || 0) - config.indentUnit, 0, 'top', false),
  177. indented: 0,
  178. startOfLine: true,
  179. lastToken: null,
  180. }
  181. },
  182. token: function (stream, state) {
  183. var ctx = state.context
  184. if (stream.sol()) {
  185. if (ctx.align == null) ctx.align = false
  186. state.indented = stream.indentation()
  187. state.startOfLine = true
  188. // Automatic semicolon insertion
  189. if (ctx.type == 'statement' && !expectExpression(state.lastToken, true)) {
  190. popContext(state)
  191. ctx = state.context
  192. }
  193. }
  194. if (stream.eatSpace()) return null
  195. curPunc = null
  196. var style = state.tokenize[state.tokenize.length - 1](stream, state)
  197. if (style == 'comment') return style
  198. if (ctx.align == null) ctx.align = true
  199. if ((curPunc == ';' || curPunc == ':') && ctx.type == 'statement') popContext(state)
  200. // Handle indentation for {x -> \n ... }
  201. else if (curPunc == '->' && ctx.type == 'statement' && ctx.prev.type == '}') {
  202. popContext(state)
  203. state.context.align = false
  204. } else if (curPunc == '{') pushContext(state, stream.column(), '}')
  205. else if (curPunc == '[') pushContext(state, stream.column(), ']')
  206. else if (curPunc == '(') pushContext(state, stream.column(), ')')
  207. else if (curPunc == '}') {
  208. while (ctx.type == 'statement') ctx = popContext(state)
  209. if (ctx.type == '}') ctx = popContext(state)
  210. while (ctx.type == 'statement') ctx = popContext(state)
  211. } else if (curPunc == ctx.type) popContext(state)
  212. else if (ctx.type == '}' || ctx.type == 'top' || (ctx.type == 'statement' && curPunc == 'newstatement')) pushContext(state, stream.column(), 'statement')
  213. state.startOfLine = false
  214. state.lastToken = curPunc || style
  215. return style
  216. },
  217. indent: function (state, textAfter) {
  218. if (!state.tokenize[state.tokenize.length - 1].isBase) return CodeMirror.Pass
  219. var firstChar = textAfter && textAfter.charAt(0),
  220. ctx = state.context
  221. if (ctx.type == 'statement' && !expectExpression(state.lastToken, true)) ctx = ctx.prev
  222. var closing = firstChar == ctx.type
  223. if (ctx.type == 'statement') return ctx.indented + (firstChar == '{' ? 0 : config.indentUnit)
  224. else if (ctx.align) return ctx.column + (closing ? 0 : 1)
  225. else return ctx.indented + (closing ? 0 : config.indentUnit)
  226. },
  227. electricChars: '{}',
  228. closeBrackets: { triples: '\'"' },
  229. fold: 'brace',
  230. blockCommentStart: '/*',
  231. blockCommentEnd: '*/',
  232. lineComment: '//',
  233. }
  234. })
  235. CodeMirror.defineMIME('text/x-groovy', 'groovy')
  236. })