asn.1.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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('asn.1', function (config, parserConfig) {
  15. var indentUnit = config.indentUnit,
  16. keywords = parserConfig.keywords || {},
  17. cmipVerbs = parserConfig.cmipVerbs || {},
  18. compareTypes = parserConfig.compareTypes || {},
  19. status = parserConfig.status || {},
  20. tags = parserConfig.tags || {},
  21. storage = parserConfig.storage || {},
  22. modifier = parserConfig.modifier || {},
  23. accessTypes = parserConfig.accessTypes || {},
  24. multiLineStrings = parserConfig.multiLineStrings,
  25. indentStatements = parserConfig.indentStatements !== false
  26. var isOperatorChar = /[\|\^]/
  27. var curPunc
  28. function tokenBase(stream, state) {
  29. var ch = stream.next()
  30. if (ch == '"' || ch == "'") {
  31. state.tokenize = tokenString(ch)
  32. return state.tokenize(stream, state)
  33. }
  34. if (/[\[\]\(\){}:=,;]/.test(ch)) {
  35. curPunc = ch
  36. return 'punctuation'
  37. }
  38. if (ch == '-') {
  39. if (stream.eat('-')) {
  40. stream.skipToEnd()
  41. return 'comment'
  42. }
  43. }
  44. if (/\d/.test(ch)) {
  45. stream.eatWhile(/[\w\.]/)
  46. return 'number'
  47. }
  48. if (isOperatorChar.test(ch)) {
  49. stream.eatWhile(isOperatorChar)
  50. return 'operator'
  51. }
  52. stream.eatWhile(/[\w\-]/)
  53. var cur = stream.current()
  54. if (keywords.propertyIsEnumerable(cur)) return 'keyword'
  55. if (cmipVerbs.propertyIsEnumerable(cur)) return 'variable cmipVerbs'
  56. if (compareTypes.propertyIsEnumerable(cur)) return 'atom compareTypes'
  57. if (status.propertyIsEnumerable(cur)) return 'comment status'
  58. if (tags.propertyIsEnumerable(cur)) return 'variable-3 tags'
  59. if (storage.propertyIsEnumerable(cur)) return 'builtin storage'
  60. if (modifier.propertyIsEnumerable(cur)) return 'string-2 modifier'
  61. if (accessTypes.propertyIsEnumerable(cur)) return 'atom accessTypes'
  62. return 'variable'
  63. }
  64. function tokenString(quote) {
  65. return function (stream, state) {
  66. var escaped = false,
  67. next,
  68. end = false
  69. while ((next = stream.next()) != null) {
  70. if (next == quote && !escaped) {
  71. var afterNext = stream.peek()
  72. //look if the character if the quote is like the B in '10100010'B
  73. if (afterNext) {
  74. afterNext = afterNext.toLowerCase()
  75. if (afterNext == 'b' || afterNext == 'h' || afterNext == 'o') stream.next()
  76. }
  77. end = true
  78. break
  79. }
  80. escaped = !escaped && next == '\\'
  81. }
  82. if (end || !(escaped || multiLineStrings)) state.tokenize = null
  83. return 'string'
  84. }
  85. }
  86. function Context(indented, column, type, align, prev) {
  87. this.indented = indented
  88. this.column = column
  89. this.type = type
  90. this.align = align
  91. this.prev = prev
  92. }
  93. function pushContext(state, col, type) {
  94. var indent = state.indented
  95. if (state.context && state.context.type == 'statement') indent = state.context.indented
  96. return (state.context = new Context(indent, col, type, null, state.context))
  97. }
  98. function popContext(state) {
  99. var t = state.context.type
  100. if (t == ')' || t == ']' || t == '}') state.indented = state.context.indented
  101. return (state.context = state.context.prev)
  102. }
  103. //Interface
  104. return {
  105. startState: function (basecolumn) {
  106. return {
  107. tokenize: null,
  108. context: new Context((basecolumn || 0) - indentUnit, 0, 'top', false),
  109. indented: 0,
  110. startOfLine: true,
  111. }
  112. },
  113. token: function (stream, state) {
  114. var ctx = state.context
  115. if (stream.sol()) {
  116. if (ctx.align == null) ctx.align = false
  117. state.indented = stream.indentation()
  118. state.startOfLine = true
  119. }
  120. if (stream.eatSpace()) return null
  121. curPunc = null
  122. var style = (state.tokenize || tokenBase)(stream, state)
  123. if (style == 'comment') return style
  124. if (ctx.align == null) ctx.align = true
  125. if ((curPunc == ';' || curPunc == ':' || curPunc == ',') && ctx.type == 'statement') {
  126. popContext(state)
  127. } else if (curPunc == '{') pushContext(state, stream.column(), '}')
  128. else if (curPunc == '[') pushContext(state, stream.column(), ']')
  129. else if (curPunc == '(') pushContext(state, stream.column(), ')')
  130. else if (curPunc == '}') {
  131. while (ctx.type == 'statement') ctx = popContext(state)
  132. if (ctx.type == '}') ctx = popContext(state)
  133. while (ctx.type == 'statement') ctx = popContext(state)
  134. } else if (curPunc == ctx.type) popContext(state)
  135. else if (indentStatements && (((ctx.type == '}' || ctx.type == 'top') && curPunc != ';') || (ctx.type == 'statement' && curPunc == 'newstatement')))
  136. pushContext(state, stream.column(), 'statement')
  137. state.startOfLine = false
  138. return style
  139. },
  140. electricChars: '{}',
  141. lineComment: '--',
  142. fold: 'brace',
  143. }
  144. })
  145. function words(str) {
  146. var obj = {},
  147. words = str.split(' ')
  148. for (var i = 0; i < words.length; ++i) obj[words[i]] = true
  149. return obj
  150. }
  151. CodeMirror.defineMIME('text/x-ttcn-asn', {
  152. name: 'asn.1',
  153. keywords: words(
  154. 'DEFINITIONS OBJECTS IF DERIVED INFORMATION ACTION' +
  155. ' REPLY ANY NAMED CHARACTERIZED BEHAVIOUR REGISTERED' +
  156. ' WITH AS IDENTIFIED CONSTRAINED BY PRESENT BEGIN' +
  157. ' IMPORTS FROM UNITS SYNTAX MIN-ACCESS MAX-ACCESS' +
  158. ' MINACCESS MAXACCESS REVISION STATUS DESCRIPTION' +
  159. ' SEQUENCE SET COMPONENTS OF CHOICE DistinguishedName' +
  160. ' ENUMERATED SIZE MODULE END INDEX AUGMENTS EXTENSIBILITY' +
  161. ' IMPLIED EXPORTS'
  162. ),
  163. cmipVerbs: words('ACTIONS ADD GET NOTIFICATIONS REPLACE REMOVE'),
  164. compareTypes: words(
  165. 'OPTIONAL DEFAULT MANAGED MODULE-TYPE MODULE_IDENTITY' +
  166. ' MODULE-COMPLIANCE OBJECT-TYPE OBJECT-IDENTITY' +
  167. ' OBJECT-COMPLIANCE MODE CONFIRMED CONDITIONAL' +
  168. ' SUBORDINATE SUPERIOR CLASS TRUE FALSE NULL' +
  169. ' TEXTUAL-CONVENTION'
  170. ),
  171. status: words('current deprecated mandatory obsolete'),
  172. tags: words('APPLICATION AUTOMATIC EXPLICIT IMPLICIT PRIVATE TAGS' + ' UNIVERSAL'),
  173. storage: words(
  174. 'BOOLEAN INTEGER OBJECT IDENTIFIER BIT OCTET STRING' +
  175. ' UTCTime InterfaceIndex IANAifType CMIP-Attribute' +
  176. ' REAL PACKAGE PACKAGES IpAddress PhysAddress' +
  177. ' NetworkAddress BITS BMPString TimeStamp TimeTicks' +
  178. ' TruthValue RowStatus DisplayString GeneralString' +
  179. ' GraphicString IA5String NumericString' +
  180. ' PrintableString SnmpAdminString TeletexString' +
  181. ' UTF8String VideotexString VisibleString StringStore' +
  182. ' ISO646String T61String UniversalString Unsigned32' +
  183. ' Integer32 Gauge Gauge32 Counter Counter32 Counter64'
  184. ),
  185. modifier: words('ATTRIBUTE ATTRIBUTES MANDATORY-GROUP MANDATORY-GROUPS' + ' GROUP GROUPS ELEMENTS EQUALITY ORDERING SUBSTRINGS' + ' DEFINED'),
  186. accessTypes: words('not-accessible accessible-for-notify read-only' + ' read-create read-write'),
  187. multiLineStrings: true,
  188. })
  189. })