mscgen.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. "attributes" : ["label", "idurl", "id", "url", "linecolor", "linecolour", "textcolor", "textcolour", "textbgcolor", "textbgcolour", "arclinecolor", "arclinecolour", "arctextcolor", "arctextcolour", "arctextbgcolor", "arctextbgcolour", "arcskip"],
  24. "brackets" : ["\\{", "\\}"], // [ and ] are brackets too, but these get handled in with lists
  25. "arcsWords" : ["note", "abox", "rbox", "box"],
  26. "arcsOthers" : ["\\|\\|\\|", "\\.\\.\\.", "---", "--", "<->", "==", "<<=>>", "<=>", "\\.\\.", "<<>>", "::", "<:>", "->", "=>>", "=>", ">>", ":>", "<-", "<<=", "<=", "<<", "<:", "x-", "-x"],
  27. "singlecomment" : ["//", "#"],
  28. "operators" : ["="]
  29. },
  30. xu: {
  31. "keywords" : ["msc"],
  32. "options" : ["hscale", "width", "arcgradient", "wordwraparcs", "watermark"],
  33. "attributes" : ["label", "idurl", "id", "url", "linecolor", "linecolour", "textcolor", "textcolour", "textbgcolor", "textbgcolour", "arclinecolor", "arclinecolour", "arctextcolor", "arctextcolour", "arctextbgcolor", "arctextbgcolour", "arcskip"],
  34. "brackets" : ["\\{", "\\}"], // [ and ] are brackets too, but these get handled in with lists
  35. "arcsWords" : ["note", "abox", "rbox", "box", "alt", "else", "opt", "break", "par", "seq", "strict", "neg", "critical", "ignore", "consider", "assert", "loop", "ref", "exc"],
  36. "arcsOthers" : ["\\|\\|\\|", "\\.\\.\\.", "---", "--", "<->", "==", "<<=>>", "<=>", "\\.\\.", "<<>>", "::", "<:>", "->", "=>>", "=>", ">>", ":>", "<-", "<<=", "<=", "<<", "<:", "x-", "-x"],
  37. "singlecomment" : ["//", "#"],
  38. "operators" : ["="]
  39. },
  40. msgenny: {
  41. "keywords" : null,
  42. "options" : ["hscale", "width", "arcgradient", "wordwraparcs", "watermark"],
  43. "attributes" : null,
  44. "brackets" : ["\\{", "\\}"],
  45. "arcsWords" : ["note", "abox", "rbox", "box", "alt", "else", "opt", "break", "par", "seq", "strict", "neg", "critical", "ignore", "consider", "assert", "loop", "ref", "exc"],
  46. "arcsOthers" : ["\\|\\|\\|", "\\.\\.\\.", "---", "--", "<->", "==", "<<=>>", "<=>", "\\.\\.", "<<>>", "::", "<:>", "->", "=>>", "=>", ">>", ":>", "<-", "<<=", "<=", "<<", "<:", "x-", "-x"],
  47. "singlecomment" : ["//", "#"],
  48. "operators" : ["="]
  49. }
  50. }
  51. CodeMirror.defineMode("mscgen", function(_, modeConfig) {
  52. var language = languages[modeConfig && modeConfig.language || "mscgen"]
  53. return {
  54. startState: startStateFn,
  55. copyState: copyStateFn,
  56. token: produceTokenFunction(language),
  57. lineComment : "#",
  58. blockCommentStart : "/*",
  59. blockCommentEnd : "*/"
  60. };
  61. });
  62. CodeMirror.defineMIME("text/x-mscgen", "mscgen");
  63. CodeMirror.defineMIME("text/x-xu", {name: "mscgen", language: "xu"});
  64. CodeMirror.defineMIME("text/x-msgenny", {name: "mscgen", language: "msgenny"});
  65. function wordRegexpBoundary(pWords) {
  66. return new RegExp("\\b(" + pWords.join("|") + ")\\b", "i");
  67. }
  68. function wordRegexp(pWords) {
  69. return new RegExp("(" + pWords.join("|") + ")", "i");
  70. }
  71. function startStateFn() {
  72. return {
  73. inComment : false,
  74. inString : false,
  75. inAttributeList : false,
  76. inScript : false
  77. };
  78. }
  79. function copyStateFn(pState) {
  80. return {
  81. inComment : pState.inComment,
  82. inString : pState.inString,
  83. inAttributeList : pState.inAttributeList,
  84. inScript : pState.inScript
  85. };
  86. }
  87. function produceTokenFunction(pConfig) {
  88. return function(pStream, pState) {
  89. if (pStream.match(wordRegexp(pConfig.brackets), true, true)) {
  90. return "bracket";
  91. }
  92. /* comments */
  93. if (!pState.inComment) {
  94. if (pStream.match(/\/\*[^\*\/]*/, true, true)) {
  95. pState.inComment = true;
  96. return "comment";
  97. }
  98. if (pStream.match(wordRegexp(pConfig.singlecomment), true, true)) {
  99. pStream.skipToEnd();
  100. return "comment";
  101. }
  102. }
  103. if (pState.inComment) {
  104. if (pStream.match(/[^\*\/]*\*\//, true, true))
  105. pState.inComment = false;
  106. else
  107. pStream.skipToEnd();
  108. return "comment";
  109. }
  110. /* strings */
  111. if (!pState.inString && pStream.match(/\"(\\\"|[^\"])*/, true, true)) {
  112. pState.inString = true;
  113. return "string";
  114. }
  115. if (pState.inString) {
  116. if (pStream.match(/[^\"]*\"/, true, true))
  117. pState.inString = false;
  118. else
  119. pStream.skipToEnd();
  120. return "string";
  121. }
  122. /* keywords & operators */
  123. if (!!pConfig.keywords && pStream.match(wordRegexpBoundary(pConfig.keywords), true, true))
  124. return "keyword";
  125. if (pStream.match(wordRegexpBoundary(pConfig.options), true, true))
  126. return "keyword";
  127. if (pStream.match(wordRegexpBoundary(pConfig.arcsWords), true, true))
  128. return "keyword";
  129. if (pStream.match(wordRegexp(pConfig.arcsOthers), true, true))
  130. return "keyword";
  131. if (!!pConfig.operators && pStream.match(wordRegexp(pConfig.operators), true, true))
  132. return "operator";
  133. /* attribute lists */
  134. if (!pConfig.inAttributeList && !!pConfig.attributes && pStream.match(/\[/, true, true)) {
  135. pConfig.inAttributeList = true;
  136. return "bracket";
  137. }
  138. if (pConfig.inAttributeList) {
  139. if (pConfig.attributes !== null && pStream.match(wordRegexpBoundary(pConfig.attributes), true, true)) {
  140. return "attribute";
  141. }
  142. if (pStream.match(/]/, true, true)) {
  143. pConfig.inAttributeList = false;
  144. return "bracket";
  145. }
  146. }
  147. pStream.next();
  148. return "base";
  149. };
  150. }
  151. });