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.

modelica.js 6.8KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Modelica support for CodeMirror, copyright (c) by Lennart Ochel
  4. (function(mod) {
  5. if (typeof exports == "object" && typeof module == "object") // CommonJS
  6. mod(require("../../lib/codemirror"));
  7. else if (typeof define == "function" && define.amd) // AMD
  8. define(["../../lib/codemirror"], mod);
  9. else // Plain browser env
  10. mod(CodeMirror);
  11. })
  12. (function(CodeMirror) {
  13. "use strict";
  14. CodeMirror.defineMode("modelica", function(config, parserConfig) {
  15. var indentUnit = config.indentUnit;
  16. var keywords = parserConfig.keywords || {};
  17. var builtin = parserConfig.builtin || {};
  18. var atoms = parserConfig.atoms || {};
  19. var isSingleOperatorChar = /[;=\(:\),{}.*<>+\-\/^\[\]]/;
  20. var isDoubleOperatorChar = /(:=|<=|>=|==|<>|\.\+|\.\-|\.\*|\.\/|\.\^)/;
  21. var isDigit = /[0-9]/;
  22. var isNonDigit = /[_a-zA-Z]/;
  23. function tokenLineComment(stream, state) {
  24. stream.skipToEnd();
  25. state.tokenize = null;
  26. return "comment";
  27. }
  28. function tokenBlockComment(stream, state) {
  29. var maybeEnd = false, ch;
  30. while (ch = stream.next()) {
  31. if (maybeEnd && ch == "/") {
  32. state.tokenize = null;
  33. break;
  34. }
  35. maybeEnd = (ch == "*");
  36. }
  37. return "comment";
  38. }
  39. function tokenString(stream, state) {
  40. var escaped = false, ch;
  41. while ((ch = stream.next()) != null) {
  42. if (ch == '"' && !escaped) {
  43. state.tokenize = null;
  44. state.sol = false;
  45. break;
  46. }
  47. escaped = !escaped && ch == "\\";
  48. }
  49. return "string";
  50. }
  51. function tokenIdent(stream, state) {
  52. stream.eatWhile(isDigit);
  53. while (stream.eat(isDigit) || stream.eat(isNonDigit)) { }
  54. var cur = stream.current();
  55. if(state.sol && (cur == "package" || cur == "model" || cur == "when" || cur == "connector")) state.level++;
  56. else if(state.sol && cur == "end" && state.level > 0) state.level--;
  57. state.tokenize = null;
  58. state.sol = false;
  59. if (keywords.propertyIsEnumerable(cur)) return "keyword";
  60. else if (builtin.propertyIsEnumerable(cur)) return "builtin";
  61. else if (atoms.propertyIsEnumerable(cur)) return "atom";
  62. else return "variable";
  63. }
  64. function tokenQIdent(stream, state) {
  65. while (stream.eat(/[^']/)) { }
  66. state.tokenize = null;
  67. state.sol = false;
  68. if(stream.eat("'"))
  69. return "variable";
  70. else
  71. return "error";
  72. }
  73. function tokenUnsignedNumber(stream, state) {
  74. stream.eatWhile(isDigit);
  75. if (stream.eat('.')) {
  76. stream.eatWhile(isDigit);
  77. }
  78. if (stream.eat('e') || stream.eat('E')) {
  79. if (!stream.eat('-'))
  80. stream.eat('+');
  81. stream.eatWhile(isDigit);
  82. }
  83. state.tokenize = null;
  84. state.sol = false;
  85. return "number";
  86. }
  87. // Interface
  88. return {
  89. startState: function() {
  90. return {
  91. tokenize: null,
  92. level: 0,
  93. sol: true
  94. };
  95. },
  96. token: function(stream, state) {
  97. if(state.tokenize != null) {
  98. return state.tokenize(stream, state);
  99. }
  100. if(stream.sol()) {
  101. state.sol = true;
  102. }
  103. // WHITESPACE
  104. if(stream.eatSpace()) {
  105. state.tokenize = null;
  106. return null;
  107. }
  108. var ch = stream.next();
  109. // LINECOMMENT
  110. if(ch == '/' && stream.eat('/')) {
  111. state.tokenize = tokenLineComment;
  112. }
  113. // BLOCKCOMMENT
  114. else if(ch == '/' && stream.eat('*')) {
  115. state.tokenize = tokenBlockComment;
  116. }
  117. // TWO SYMBOL TOKENS
  118. else if(isDoubleOperatorChar.test(ch+stream.peek())) {
  119. stream.next();
  120. state.tokenize = null;
  121. return "operator";
  122. }
  123. // SINGLE SYMBOL TOKENS
  124. else if(isSingleOperatorChar.test(ch)) {
  125. state.tokenize = null;
  126. return "operator";
  127. }
  128. // IDENT
  129. else if(isNonDigit.test(ch)) {
  130. state.tokenize = tokenIdent;
  131. }
  132. // Q-IDENT
  133. else if(ch == "'" && stream.peek() && stream.peek() != "'") {
  134. state.tokenize = tokenQIdent;
  135. }
  136. // STRING
  137. else if(ch == '"') {
  138. state.tokenize = tokenString;
  139. }
  140. // UNSIGNED_NUMBER
  141. else if(isDigit.test(ch)) {
  142. state.tokenize = tokenUnsignedNumber;
  143. }
  144. // ERROR
  145. else {
  146. state.tokenize = null;
  147. return "error";
  148. }
  149. return state.tokenize(stream, state);
  150. },
  151. indent: function(state, textAfter) {
  152. if (state.tokenize != null) return CodeMirror.Pass;
  153. var level = state.level;
  154. if(/(algorithm)/.test(textAfter)) level--;
  155. if(/(equation)/.test(textAfter)) level--;
  156. if(/(initial algorithm)/.test(textAfter)) level--;
  157. if(/(initial equation)/.test(textAfter)) level--;
  158. if(/(end)/.test(textAfter)) level--;
  159. if(level > 0)
  160. return indentUnit*level;
  161. else
  162. return 0;
  163. },
  164. blockCommentStart: "/*",
  165. blockCommentEnd: "*/",
  166. lineComment: "//"
  167. };
  168. });
  169. function words(str) {
  170. var obj = {}, words = str.split(" ");
  171. for (var i=0; i<words.length; ++i)
  172. obj[words[i]] = true;
  173. return obj;
  174. }
  175. var modelicaKeywords = "algorithm and annotation assert block break class connect connector constant constrainedby der discrete each else elseif elsewhen encapsulated end enumeration equation expandable extends external false final flow for function if import impure in initial inner input loop model not operator or outer output package parameter partial protected public pure record redeclare replaceable return stream then true type when while within";
  176. var modelicaBuiltin = "abs acos actualStream asin atan atan2 cardinality ceil cos cosh delay div edge exp floor getInstanceName homotopy inStream integer log log10 mod pre reinit rem semiLinear sign sin sinh spatialDistribution sqrt tan tanh";
  177. var modelicaAtoms = "Real Boolean Integer String";
  178. function def(mimes, mode) {
  179. if (typeof mimes == "string")
  180. mimes = [mimes];
  181. var words = [];
  182. function add(obj) {
  183. if (obj)
  184. for (var prop in obj)
  185. if (obj.hasOwnProperty(prop))
  186. words.push(prop);
  187. }
  188. add(mode.keywords);
  189. add(mode.builtin);
  190. add(mode.atoms);
  191. if (words.length) {
  192. mode.helperType = mimes[0];
  193. CodeMirror.registerHelper("hintWords", mimes[0], words);
  194. }
  195. for (var i=0; i<mimes.length; ++i)
  196. CodeMirror.defineMIME(mimes[i], mode);
  197. }
  198. def(["text/x-modelica"], {
  199. name: "modelica",
  200. keywords: words(modelicaKeywords),
  201. builtin: words(modelicaBuiltin),
  202. atoms: words(modelicaAtoms)
  203. });
  204. });