You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.6KB

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