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