| 1 |
function wordRegexp(words) { |
| 2 |
return new RegExp("^((" + words.join(")|(") + "))\\b", "i"); |
| 3 |
} |
| 4 |
|
| 5 |
var singleOperators = new RegExp("^[\\+\\-\\*/&#!_?\\\\<>=\\'\\[\\]]"); |
| 6 |
var doubleOperators = new RegExp("^(('=)|(<=)|(>=)|('>)|('<)|([[)|(]])|(^$))"); |
| 7 |
var singleDelimiters = new RegExp("^[\\.,:]"); |
| 8 |
var brackets = new RegExp("[()]"); |
| 9 |
var identifiers = new RegExp("^[%A-Za-z][A-Za-z0-9]*"); |
| 10 |
var commandKeywords = ["break","close","do","else","for","goto", "halt", "hang", "if", "job","kill","lock","merge","new","open", "quit", "read", "set", "tcommit", "trollback", "tstart", "use", "view", "write", "xecute", "b","c","d","e","f","g", "h", "i", "j","k","l","m","n","o", "q", "r", "s", "tc", "tro", "ts", "u", "v", "w", "x"]; |
| 11 |
// The following list includes intrinsic functions _and_ special variables |
| 12 |
var intrinsicFuncsWords = ["\\$ascii", "\\$char", "\\$data", "\\$ecode", "\\$estack", "\\$etrap", "\\$extract", "\\$find", "\\$fnumber", "\\$get", "\\$horolog", "\\$io", "\\$increment", "\\$job", "\\$justify", "\\$length", "\\$name", "\\$next", "\\$order", "\\$piece", "\\$qlength", "\\$qsubscript", "\\$query", "\\$quit", "\\$random", "\\$reverse", "\\$select", "\\$stack", "\\$test", "\\$text", "\\$translate", "\\$view", "\\$x", "\\$y", "\\$a", "\\$c", "\\$d", "\\$e", "\\$ec", "\\$es", "\\$et", "\\$f", "\\$fn", "\\$g", "\\$h", "\\$i", "\\$j", "\\$l", "\\$n", "\\$na", "\\$o", "\\$p", "\\$q", "\\$ql", "\\$qs", "\\$r", "\\$re", "\\$s", "\\$st", "\\$t", "\\$tr", "\\$v", "\\$z"]; |
| 13 |
var intrinsicFuncs = wordRegexp(intrinsicFuncsWords); |
| 14 |
var command = wordRegexp(commandKeywords); |
| 15 |
|
| 16 |
function tokenBase(stream, state) { |
| 17 |
if (stream.sol()) { |
| 18 |
state.label = true; |
| 19 |
state.commandMode = 0; |
| 20 |
} |
| 21 |
|
| 22 |
// The <space> character has meaning in MUMPS. Ignoring consecutive |
| 23 |
// spaces would interfere with interpreting whether the next non-space |
| 24 |
// character belongs to the command or argument context. |
| 25 |
|
| 26 |
// Examine each character and update a mode variable whose interpretation is: |
| 27 |
// >0 => command 0 => argument <0 => command post-conditional |
| 28 |
var ch = stream.peek(); |
| 29 |
|
| 30 |
if (ch == " " || ch == "\t") { // Pre-process <space> |
| 31 |
state.label = false; |
| 32 |
if (state.commandMode == 0) |
| 33 |
state.commandMode = 1; |
| 34 |
else if ((state.commandMode < 0) || (state.commandMode == 2)) |
| 35 |
state.commandMode = 0; |
| 36 |
} else if ((ch != ".") && (state.commandMode > 0)) { |
| 37 |
if (ch == ":") |
| 38 |
state.commandMode = -1; // SIS - Command post-conditional |
| 39 |
else |
| 40 |
state.commandMode = 2; |
| 41 |
} |
| 42 |
|
| 43 |
// Do not color parameter list as line tag |
| 44 |
if ((ch === "(") || (ch === "\u0009")) |
| 45 |
state.label = false; |
| 46 |
|
| 47 |
// MUMPS comment starts with ";" |
| 48 |
if (ch === ";") { |
| 49 |
stream.skipToEnd(); |
| 50 |
return "comment"; |
| 51 |
} |
| 52 |
|
| 53 |
// Number Literals // SIS/RLM - MUMPS permits canonic number followed by concatenate operator |
| 54 |
if (stream.match(/^[-+]?\d+(\.\d+)?([eE][-+]?\d+)?/)) |
| 55 |
return "number"; |
| 56 |
|
| 57 |
// Handle Strings |
| 58 |
if (ch == '"') { |
| 59 |
if (stream.skipTo('"')) { |
| 60 |
stream.next(); |
| 61 |
return "string"; |
| 62 |
} else { |
| 63 |
stream.skipToEnd(); |
| 64 |
return "error"; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// Handle operators and Delimiters |
| 69 |
if (stream.match(doubleOperators) || stream.match(singleOperators)) |
| 70 |
return "operator"; |
| 71 |
|
| 72 |
// Prevents leading "." in DO block from falling through to error |
| 73 |
if (stream.match(singleDelimiters)) |
| 74 |
return null; |
| 75 |
|
| 76 |
if (brackets.test(ch)) { |
| 77 |
stream.next(); |
| 78 |
return "bracket"; |
| 79 |
} |
| 80 |
|
| 81 |
if (state.commandMode > 0 && stream.match(command)) |
| 82 |
return "controlKeyword"; |
| 83 |
|
| 84 |
if (stream.match(intrinsicFuncs)) |
| 85 |
return "builtin"; |
| 86 |
|
| 87 |
if (stream.match(identifiers)) |
| 88 |
return "variable"; |
| 89 |
|
| 90 |
// Detect dollar-sign when not a documented intrinsic function |
| 91 |
// "^" may introduce a GVN or SSVN - Color same as function |
| 92 |
if (ch === "$" || ch === "^") { |
| 93 |
stream.next(); |
| 94 |
return "builtin"; |
| 95 |
} |
| 96 |
|
| 97 |
// MUMPS Indirection |
| 98 |
if (ch === "@") { |
| 99 |
stream.next(); |
| 100 |
return "string.special"; |
| 101 |
} |
| 102 |
|
| 103 |
if (/[\w%]/.test(ch)) { |
| 104 |
stream.eatWhile(/[\w%]/); |
| 105 |
return "variable"; |
| 106 |
} |
| 107 |
|
| 108 |
// Handle non-detected items |
| 109 |
stream.next(); |
| 110 |
return "error"; |
| 111 |
} |
| 112 |
|
| 113 |
export const mumps = { |
| 114 |
name: "mumps", |
| 115 |
startState: function() { |
| 116 |
return { |
| 117 |
label: false, |
| 118 |
commandMode: 0 |
| 119 |
}; |
| 120 |
}, |
| 121 |
|
| 122 |
token: function(stream, state) { |
| 123 |
var style = tokenBase(stream, state); |
| 124 |
if (state.label) return "tag"; |
| 125 |
return style; |
| 126 |
} |
| 127 |
}; |