ttcn.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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('ttcn', function (config, parserConfig) {
  15. var indentUnit = config.indentUnit,
  16. keywords = parserConfig.keywords || {},
  17. builtin = parserConfig.builtin || {},
  18. timerOps = parserConfig.timerOps || {},
  19. portOps = parserConfig.portOps || {},
  20. configOps = parserConfig.configOps || {},
  21. verdictOps = parserConfig.verdictOps || {},
  22. sutOps = parserConfig.sutOps || {},
  23. functionOps = parserConfig.functionOps || {},
  24. verdictConsts = parserConfig.verdictConsts || {},
  25. booleanConsts = parserConfig.booleanConsts || {},
  26. otherConsts = parserConfig.otherConsts || {},
  27. types = parserConfig.types || {},
  28. visibilityModifiers = parserConfig.visibilityModifiers || {},
  29. templateMatch = parserConfig.templateMatch || {},
  30. multiLineStrings = parserConfig.multiLineStrings,
  31. indentStatements = parserConfig.indentStatements !== false
  32. var isOperatorChar = /[+\-*&@=<>!\/]/
  33. var curPunc
  34. function tokenBase(stream, state) {
  35. var ch = stream.next()
  36. if (ch == '"' || ch == "'") {
  37. state.tokenize = tokenString(ch)
  38. return state.tokenize(stream, state)
  39. }
  40. if (/[\[\]{}\(\),;\\:\?\.]/.test(ch)) {
  41. curPunc = ch
  42. return 'punctuation'
  43. }
  44. if (ch == '#') {
  45. stream.skipToEnd()
  46. return 'atom preprocessor'
  47. }
  48. if (ch == '%') {
  49. stream.eatWhile(/\b/)
  50. return 'atom ttcn3Macros'
  51. }
  52. if (/\d/.test(ch)) {
  53. stream.eatWhile(/[\w\.]/)
  54. return 'number'
  55. }
  56. if (ch == '/') {
  57. if (stream.eat('*')) {
  58. state.tokenize = tokenComment
  59. return tokenComment(stream, state)
  60. }
  61. if (stream.eat('/')) {
  62. stream.skipToEnd()
  63. return 'comment'
  64. }
  65. }
  66. if (isOperatorChar.test(ch)) {
  67. if (ch == '@') {
  68. if (stream.match('try') || stream.match('catch') || stream.match('lazy')) {
  69. return 'keyword'
  70. }
  71. }
  72. stream.eatWhile(isOperatorChar)
  73. return 'operator'
  74. }
  75. stream.eatWhile(/[\w\$_\xa1-\uffff]/)
  76. var cur = stream.current()
  77. if (keywords.propertyIsEnumerable(cur)) return 'keyword'
  78. if (builtin.propertyIsEnumerable(cur)) return 'builtin'
  79. if (timerOps.propertyIsEnumerable(cur)) return 'def timerOps'
  80. if (configOps.propertyIsEnumerable(cur)) return 'def configOps'
  81. if (verdictOps.propertyIsEnumerable(cur)) return 'def verdictOps'
  82. if (portOps.propertyIsEnumerable(cur)) return 'def portOps'
  83. if (sutOps.propertyIsEnumerable(cur)) return 'def sutOps'
  84. if (functionOps.propertyIsEnumerable(cur)) return 'def functionOps'
  85. if (verdictConsts.propertyIsEnumerable(cur)) return 'string verdictConsts'
  86. if (booleanConsts.propertyIsEnumerable(cur)) return 'string booleanConsts'
  87. if (otherConsts.propertyIsEnumerable(cur)) return 'string otherConsts'
  88. if (types.propertyIsEnumerable(cur)) return 'builtin types'
  89. if (visibilityModifiers.propertyIsEnumerable(cur)) return 'builtin visibilityModifiers'
  90. if (templateMatch.propertyIsEnumerable(cur)) return 'atom templateMatch'
  91. return 'variable'
  92. }
  93. function tokenString(quote) {
  94. return function (stream, state) {
  95. var escaped = false,
  96. next,
  97. end = false
  98. while ((next = stream.next()) != null) {
  99. if (next == quote && !escaped) {
  100. var afterQuote = stream.peek()
  101. //look if the character after the quote is like the B in '10100010'B
  102. if (afterQuote) {
  103. afterQuote = afterQuote.toLowerCase()
  104. if (afterQuote == 'b' || afterQuote == 'h' || afterQuote == 'o') stream.next()
  105. }
  106. end = true
  107. break
  108. }
  109. escaped = !escaped && next == '\\'
  110. }
  111. if (end || !(escaped || multiLineStrings)) state.tokenize = null
  112. return 'string'
  113. }
  114. }
  115. function tokenComment(stream, state) {
  116. var maybeEnd = false,
  117. ch
  118. while ((ch = stream.next())) {
  119. if (ch == '/' && maybeEnd) {
  120. state.tokenize = null
  121. break
  122. }
  123. maybeEnd = ch == '*'
  124. }
  125. return 'comment'
  126. }
  127. function Context(indented, column, type, align, prev) {
  128. this.indented = indented
  129. this.column = column
  130. this.type = type
  131. this.align = align
  132. this.prev = prev
  133. }
  134. function pushContext(state, col, type) {
  135. var indent = state.indented
  136. if (state.context && state.context.type == 'statement') indent = state.context.indented
  137. return (state.context = new Context(indent, col, type, null, state.context))
  138. }
  139. function popContext(state) {
  140. var t = state.context.type
  141. if (t == ')' || t == ']' || t == '}') state.indented = state.context.indented
  142. return (state.context = state.context.prev)
  143. }
  144. //Interface
  145. return {
  146. startState: function (basecolumn) {
  147. return {
  148. tokenize: null,
  149. context: new Context((basecolumn || 0) - indentUnit, 0, 'top', false),
  150. indented: 0,
  151. startOfLine: true,
  152. }
  153. },
  154. token: function (stream, state) {
  155. var ctx = state.context
  156. if (stream.sol()) {
  157. if (ctx.align == null) ctx.align = false
  158. state.indented = stream.indentation()
  159. state.startOfLine = true
  160. }
  161. if (stream.eatSpace()) return null
  162. curPunc = null
  163. var style = (state.tokenize || tokenBase)(stream, state)
  164. if (style == 'comment') return style
  165. if (ctx.align == null) ctx.align = true
  166. if ((curPunc == ';' || curPunc == ':' || curPunc == ',') && ctx.type == 'statement') {
  167. popContext(state)
  168. } else if (curPunc == '{') pushContext(state, stream.column(), '}')
  169. else if (curPunc == '[') pushContext(state, stream.column(), ']')
  170. else if (curPunc == '(') pushContext(state, stream.column(), ')')
  171. else if (curPunc == '}') {
  172. while (ctx.type == 'statement') ctx = popContext(state)
  173. if (ctx.type == '}') ctx = popContext(state)
  174. while (ctx.type == 'statement') ctx = popContext(state)
  175. } else if (curPunc == ctx.type) popContext(state)
  176. else if (indentStatements && (((ctx.type == '}' || ctx.type == 'top') && curPunc != ';') || (ctx.type == 'statement' && curPunc == 'newstatement')))
  177. pushContext(state, stream.column(), 'statement')
  178. state.startOfLine = false
  179. return style
  180. },
  181. electricChars: '{}',
  182. blockCommentStart: '/*',
  183. blockCommentEnd: '*/',
  184. lineComment: '//',
  185. fold: 'brace',
  186. }
  187. })
  188. function words(str) {
  189. var obj = {},
  190. words = str.split(' ')
  191. for (var i = 0; i < words.length; ++i) obj[words[i]] = true
  192. return obj
  193. }
  194. function def(mimes, mode) {
  195. if (typeof mimes == 'string') mimes = [mimes]
  196. var words = []
  197. function add(obj) {
  198. if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop)) words.push(prop)
  199. }
  200. add(mode.keywords)
  201. add(mode.builtin)
  202. add(mode.timerOps)
  203. add(mode.portOps)
  204. if (words.length) {
  205. mode.helperType = mimes[0]
  206. CodeMirror.registerHelper('hintWords', mimes[0], words)
  207. }
  208. for (var i = 0; i < mimes.length; ++i) CodeMirror.defineMIME(mimes[i], mode)
  209. }
  210. def(['text/x-ttcn', 'text/x-ttcn3', 'text/x-ttcnpp'], {
  211. name: 'ttcn',
  212. keywords: words(
  213. 'activate address alive all alt altstep and and4b any' +
  214. ' break case component const continue control deactivate' +
  215. ' display do else encode enumerated except exception' +
  216. ' execute extends extension external for from function' +
  217. ' goto group if import in infinity inout interleave' +
  218. ' label language length log match message mixed mod' +
  219. ' modifies module modulepar mtc noblock not not4b nowait' +
  220. ' of on optional or or4b out override param pattern port' +
  221. ' procedure record recursive rem repeat return runs select' +
  222. ' self sender set signature system template testcase to' +
  223. ' type union value valueof var variant while with xor xor4b'
  224. ),
  225. builtin: words(
  226. 'bit2hex bit2int bit2oct bit2str char2int char2oct encvalue' +
  227. ' decomp decvalue float2int float2str hex2bit hex2int' +
  228. ' hex2oct hex2str int2bit int2char int2float int2hex' +
  229. ' int2oct int2str int2unichar isbound ischosen ispresent' +
  230. ' isvalue lengthof log2str oct2bit oct2char oct2hex oct2int' +
  231. ' oct2str regexp replace rnd sizeof str2bit str2float' +
  232. ' str2hex str2int str2oct substr unichar2int unichar2char' +
  233. ' enum2int'
  234. ),
  235. types: words('anytype bitstring boolean char charstring default float' + ' hexstring integer objid octetstring universal verdicttype timer'),
  236. timerOps: words('read running start stop timeout'),
  237. portOps: words('call catch check clear getcall getreply halt raise receive' + ' reply send trigger'),
  238. configOps: words('create connect disconnect done kill killed map unmap'),
  239. verdictOps: words('getverdict setverdict'),
  240. sutOps: words('action'),
  241. functionOps: words('apply derefers refers'),
  242. verdictConsts: words('error fail inconc none pass'),
  243. booleanConsts: words('true false'),
  244. otherConsts: words('null NULL omit'),
  245. visibilityModifiers: words('private public friend'),
  246. templateMatch: words('complement ifpresent subset superset permutation'),
  247. multiLineStrings: true,
  248. })
  249. })