mscgen.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // mode(s) for the sequence chart dsl's mscgen, xù and msgenny
  4. // For more information on mscgen, see the site of the original author:
  5. // http://www.mcternan.me.uk/mscgen
  6. //
  7. // This mode for mscgen and the two derivative languages were
  8. // originally made for use in the mscgen_js interpreter
  9. // (https://sverweij.github.io/mscgen_js)
  10. (function(mod) {
  11. if ( typeof exports == "object" && typeof module == "object")// CommonJS
  12. mod(require("../../lib/codemirror"));
  13. else if ( typeof define == "function" && define.amd)// AMD
  14. define(["../../lib/codemirror"], mod);
  15. else// Plain browser env
  16. mod(CodeMirror);
  17. })(function(CodeMirror) {
  18. "use strict";
  19. var languages = {
  20. mscgen: {
  21. "keywords" : ["msc"],
  22. "options" : ["hscale", "width", "arcgradient", "wordwraparcs"],
  23. "constants" : ["true", "false", "on", "off"],
  24. "attributes" : ["label", "idurl", "id", "url", "linecolor", "linecolour", "textcolor", "textcolour", "textbgcolor", "textbgcolour", "arclinecolor", "arclinecolour", "arctextcolor", "arctextcolour", "arctextbgcolor", "arctextbgcolour", "arcskip"],
  25. "brackets" : ["\\{", "\\}"], // [ and ] are brackets too, but these get handled in with lists
  26. "arcsWords" : ["note", "abox", "rbox", "box"],
  27. "arcsOthers" : ["\\|\\|\\|", "\\.\\.\\.", "---", "--", "<->", "==", "<<=>>", "<=>", "\\.\\.", "<<>>", "::", "<:>", "->", "=>>", "=>", ">>", ":>", "<-", "<<=", "<=", "<<", "<:", "x-", "-x"],
  28. "singlecomment" : ["//", "#"],
  29. "operators" : ["="]
  30. },
  31. xu: {
  32. "keywords" : ["msc", "xu"],
  33. "options" : ["hscale", "width", "arcgradient", "wordwraparcs", "watermark"],
  34. "constants" : ["true", "false", "on", "off", "auto"],
  35. "attributes" : ["label", "idurl", "id", "url", "linecolor", "linecolour", "textcolor", "textcolour", "textbgcolor", "textbgcolour", "arclinecolor", "arclinecolour", "arctextcolor", "arctextcolour", "arctextbgcolor", "arctextbgcolour", "arcskip"],
  36. "brackets" : ["\\{", "\\}"], // [ and ] are brackets too, but these get handled in with lists
  37. "arcsWords" : ["note", "abox", "rbox", "box", "alt", "else", "opt", "break", "par", "seq", "strict", "neg", "critical", "ignore", "consider", "assert", "loop", "ref", "exc"],
  38. "arcsOthers" : ["\\|\\|\\|", "\\.\\.\\.", "---", "--", "<->", "==", "<<=>>", "<=>", "\\.\\.", "<<>>", "::", "<:>", "->", "=>>", "=>", ">>", ":>", "<-", "<<=", "<=", "<<", "<:", "x-", "-x"],
  39. "singlecomment" : ["//", "#"],
  40. "operators" : ["="]
  41. },
  42. msgenny: {
  43. "keywords" : null,
  44. "options" : ["hscale", "width", "arcgradient", "wordwraparcs", "watermark"],
  45. "constants" : ["true", "false", "on", "off", "auto"],
  46. "attributes" : null,
  47. "brackets" : ["\\{", "\\}"],
  48. "arcsWords" : ["note", "abox", "rbox", "box", "alt", "else", "opt", "break", "par", "seq", "strict", "neg", "critical", "ignore", "consider", "assert", "loop", "ref", "exc"],
  49. "arcsOthers" : ["\\|\\|\\|", "\\.\\.\\.", "---", "--", "<->", "==", "<<=>>", "<=>", "\\.\\.", "<<>>", "::", "<:>", "->", "=>>", "=>", ">>", ":>", "<-", "<<=", "<=", "<<", "<:", "x-", "-x"],
  50. "singlecomment" : ["//", "#"],
  51. "operators" : ["="]
  52. }
  53. }
  54. CodeMirror.defineMode("mscgen", function(_, modeConfig) {
  55. var language = languages[modeConfig && modeConfig.language || "mscgen"]
  56. return {
  57. startState: startStateFn,
  58. copyState: copyStateFn,
  59. token: produceTokenFunction(language),
  60. lineComment : "#",
  61. blockCommentStart : "/*",
  62. blockCommentEnd : "*/"
  63. };
  64. });
  65. CodeMirror.defineMIME("text/x-mscgen", "mscgen");
  66. CodeMirror.defineMIME("text/x-xu", {name: "mscgen", language: "xu"});
  67. CodeMirror.defineMIME("text/x-msgenny", {name: "mscgen", language: "msgenny"});
  68. function wordRegexpBoundary(pWords) {
  69. return new RegExp("\\b(" + pWords.join("|") + ")\\b", "i");
  70. }
  71. function wordRegexp(pWords) {
  72. return new RegExp("(" + pWords.join("|") + ")", "i");
  73. }
  74. function startStateFn() {
  75. return {
  76. inComment : false,
  77. inString : false,
  78. inAttributeList : false,
  79. inScript : false
  80. };
  81. }
  82. function copyStateFn(pState) {
  83. return {
  84. inComment : pState.inComment,
  85. inString : pState.inString,
  86. inAttributeList : pState.inAttributeList,
  87. inScript : pState.inScript
  88. };
  89. }
  90. function produceTokenFunction(pConfig) {
  91. return function(pStream, pState) {
  92. if (pStream.match(wordRegexp(pConfig.brackets), true, true)) {
  93. return "bracket";
  94. }
  95. /* comments */
  96. if (!pState.inComment) {
  97. if (pStream.match(/\/\*[^\*\/]*/, true, true)) {
  98. pState.inComment = true;
  99. return "comment";
  100. }
  101. if (pStream.match(wordRegexp(pConfig.singlecomment), true, true)) {
  102. pStream.skipToEnd();
  103. return "comment";
  104. }
  105. }
  106. if (pState.inComment) {
  107. if (pStream.match(/[^\*\/]*\*\//, true, true))
  108. pState.inComment = false;
  109. else
  110. pStream.skipToEnd();
  111. return "comment";
  112. }
  113. /* strings */
  114. if (!pState.inString && pStream.match(/\"(\\\"|[^\"])*/, true, true)) {
  115. pState.inString = true;
  116. return "string";
  117. }
  118. if (pState.inString) {
  119. if (pStream.match(/[^\"]*\"/, true, true))
  120. pState.inString = false;
  121. else
  122. pStream.skipToEnd();
  123. return "string";
  124. }
  125. /* keywords & operators */
  126. if (!!pConfig.keywords && pStream.match(wordRegexpBoundary(pConfig.keywords), true, true))
  127. return "keyword";
  128. if (pStream.match(wordRegexpBoundary(pConfig.options), true, true))
  129. return "keyword";
  130. if (pStream.match(wordRegexpBoundary(pConfig.arcsWords), true, true))
  131. return "keyword";
  132. if (pStream.match(wordRegexp(pConfig.arcsOthers), true, true))
  133. return "keyword";
  134. if (!!pConfig.operators && pStream.match(wordRegexp(pConfig.operators), true, true))
  135. return "operator";
  136. if (!!pConfig.constants && pStream.match(wordRegexp(pConfig.constants), true, true))
  137. return "variable";
  138. /* attribute lists */
  139. if (!pConfig.inAttributeList && !!pConfig.attributes && pStream.match(/\[/, true, true)) {
  140. pConfig.inAttributeList = true;
  141. return "bracket";
  142. }
  143. if (pConfig.inAttributeList) {
  144. if (pConfig.attributes !== null && pStream.match(wordRegexpBoundary(pConfig.attributes), true, true)) {
  145. return "attribute";
  146. }
  147. if (pStream.match(/]/, true, true)) {
  148. pConfig.inAttributeList = false;
  149. return "bracket";
  150. }
  151. }
  152. pStream.next();
  153. return "base";
  154. };
  155. }
  156. });