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