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.

149 line
5.2KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. /*
  4. This MUMPS Language script was constructed using vbscript.js as a template.
  5. */
  6. (function(mod) {
  7. if (typeof exports == "object" && typeof module == "object") // CommonJS
  8. mod(require("../../lib/codemirror"));
  9. else if (typeof define == "function" && define.amd) // AMD
  10. define(["../../lib/codemirror"], mod);
  11. else // Plain browser env
  12. mod(CodeMirror);
  13. })(function(CodeMirror) {
  14. "use strict";
  15. CodeMirror.defineMode("mumps", function() {
  16. function wordRegexp(words) {
  17. return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
  18. }
  19. var singleOperators = new RegExp("^[\\+\\-\\*/&#!_?\\\\<>=\\'\\[\\]]");
  20. var doubleOperators = new RegExp("^(('=)|(<=)|(>=)|('>)|('<)|([[)|(]])|(^$))");
  21. var singleDelimiters = new RegExp("^[\\.,:]");
  22. var brackets = new RegExp("[()]");
  23. var identifiers = new RegExp("^[%A-Za-z][A-Za-z0-9]*");
  24. 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"];
  25. // The following list includes intrinsic functions _and_ special variables
  26. 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"];
  27. var intrinsicFuncs = wordRegexp(intrinsicFuncsWords);
  28. var command = wordRegexp(commandKeywords);
  29. function tokenBase(stream, state) {
  30. if (stream.sol()) {
  31. state.label = true;
  32. state.commandMode = 0;
  33. }
  34. // The <space> character has meaning in MUMPS. Ignoring consecutive
  35. // spaces would interfere with interpreting whether the next non-space
  36. // character belongs to the command or argument context.
  37. // Examine each character and update a mode variable whose interpretation is:
  38. // >0 => command 0 => argument <0 => command post-conditional
  39. var ch = stream.peek();
  40. if (ch == " " || ch == "\t") { // Pre-process <space>
  41. state.label = false;
  42. if (state.commandMode == 0)
  43. state.commandMode = 1;
  44. else if ((state.commandMode < 0) || (state.commandMode == 2))
  45. state.commandMode = 0;
  46. } else if ((ch != ".") && (state.commandMode > 0)) {
  47. if (ch == ":")
  48. state.commandMode = -1; // SIS - Command post-conditional
  49. else
  50. state.commandMode = 2;
  51. }
  52. // Do not color parameter list as line tag
  53. if ((ch === "(") || (ch === "\u0009"))
  54. state.label = false;
  55. // MUMPS comment starts with ";"
  56. if (ch === ";") {
  57. stream.skipToEnd();
  58. return "comment";
  59. }
  60. // Number Literals // SIS/RLM - MUMPS permits canonic number followed by concatenate operator
  61. if (stream.match(/^[-+]?\d+(\.\d+)?([eE][-+]?\d+)?/))
  62. return "number";
  63. // Handle Strings
  64. if (ch == '"') {
  65. if (stream.skipTo('"')) {
  66. stream.next();
  67. return "string";
  68. } else {
  69. stream.skipToEnd();
  70. return "error";
  71. }
  72. }
  73. // Handle operators and Delimiters
  74. if (stream.match(doubleOperators) || stream.match(singleOperators))
  75. return "operator";
  76. // Prevents leading "." in DO block from falling through to error
  77. if (stream.match(singleDelimiters))
  78. return null;
  79. if (brackets.test(ch)) {
  80. stream.next();
  81. return "bracket";
  82. }
  83. if (state.commandMode > 0 && stream.match(command))
  84. return "variable-2";
  85. if (stream.match(intrinsicFuncs))
  86. return "builtin";
  87. if (stream.match(identifiers))
  88. return "variable";
  89. // Detect dollar-sign when not a documented intrinsic function
  90. // "^" may introduce a GVN or SSVN - Color same as function
  91. if (ch === "$" || ch === "^") {
  92. stream.next();
  93. return "builtin";
  94. }
  95. // MUMPS Indirection
  96. if (ch === "@") {
  97. stream.next();
  98. return "string-2";
  99. }
  100. if (/[\w%]/.test(ch)) {
  101. stream.eatWhile(/[\w%]/);
  102. return "variable";
  103. }
  104. // Handle non-detected items
  105. stream.next();
  106. return "error";
  107. }
  108. return {
  109. startState: function() {
  110. return {
  111. label: false,
  112. commandMode: 0
  113. };
  114. },
  115. token: function(stream, state) {
  116. var style = tokenBase(stream, state);
  117. if (state.label) return "tag";
  118. return style;
  119. }
  120. };
  121. });
  122. CodeMirror.defineMIME("text/x-mumps", "mumps");
  123. });