e2f36d5a05800fcdec4b5bccd3eada3c360d3e53dda0907badd7124621cb9e7a5768d35cf2f0aec8d02b5e70f4dd4547c9291a6b76e265a34d7bf95e22d89c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../clike/clike"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../clike/clike"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var keywords = ("this super static final const abstract class extends external factory " +
  13. "implements mixin get native set typedef with enum throw rethrow assert break case " +
  14. "continue default in return new deferred async await covariant try catch finally " +
  15. "do else for if switch while import library export part of show hide is as extension " +
  16. "on yield late required sealed base interface when").split(" ");
  17. var blockKeywords = "try catch finally do else for if switch while".split(" ");
  18. var atoms = "true false null".split(" ");
  19. var builtins = "void bool num int double dynamic var String Null Never".split(" ");
  20. function set(words) {
  21. var obj = {};
  22. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  23. return obj;
  24. }
  25. function pushInterpolationStack(state) {
  26. (state.interpolationStack || (state.interpolationStack = [])).push(state.tokenize);
  27. }
  28. function popInterpolationStack(state) {
  29. return (state.interpolationStack || (state.interpolationStack = [])).pop();
  30. }
  31. function sizeInterpolationStack(state) {
  32. return state.interpolationStack ? state.interpolationStack.length : 0;
  33. }
  34. CodeMirror.defineMIME("application/dart", {
  35. name: "clike",
  36. keywords: set(keywords),
  37. blockKeywords: set(blockKeywords),
  38. builtin: set(builtins),
  39. atoms: set(atoms),
  40. // clike numbers without the suffixes, and with '_' separators.
  41. number: /^(?:0x[a-f\d_]+|(?:[\d_]+\.?[\d_]*|\.[\d_]+)(?:e[-+]?[\d_]+)?)/i,
  42. hooks: {
  43. "@": function(stream) {
  44. stream.eatWhile(/[\w\$_\.]/);
  45. return "meta";
  46. },
  47. // custom string handling to deal with triple-quoted strings and string interpolation
  48. "'": function(stream, state) {
  49. return tokenString("'", stream, state, false);
  50. },
  51. "\"": function(stream, state) {
  52. return tokenString("\"", stream, state, false);
  53. },
  54. "r": function(stream, state) {
  55. var peek = stream.peek();
  56. if (peek == "'" || peek == "\"") {
  57. return tokenString(stream.next(), stream, state, true);
  58. }
  59. return false;
  60. },
  61. "}": function(_stream, state) {
  62. // "}" is end of interpolation, if interpolation stack is non-empty
  63. if (sizeInterpolationStack(state) > 0) {
  64. state.tokenize = popInterpolationStack(state);
  65. return null;
  66. }
  67. return false;
  68. },
  69. "/": function(stream, state) {
  70. if (!stream.eat("*")) return false
  71. state.tokenize = tokenNestedComment(1)
  72. return state.tokenize(stream, state)
  73. },
  74. token: function(stream, _, style) {
  75. if (style == "variable") {
  76. // Assume uppercase symbols are classes using variable-2
  77. var isUpper = RegExp('^[_$]*[A-Z][a-zA-Z0-9_$]*$','g');
  78. if (isUpper.test(stream.current())) {
  79. return 'variable-2';
  80. }
  81. }
  82. }
  83. }
  84. });
  85. function tokenString(quote, stream, state, raw) {
  86. var tripleQuoted = false;
  87. if (stream.eat(quote)) {
  88. if (stream.eat(quote)) tripleQuoted = true;
  89. else return "string"; //empty string
  90. }
  91. function tokenStringHelper(stream, state) {
  92. var escaped = false;
  93. while (!stream.eol()) {
  94. if (!raw && !escaped && stream.peek() == "$") {
  95. pushInterpolationStack(state);
  96. state.tokenize = tokenInterpolation;
  97. return "string";
  98. }
  99. var next = stream.next();
  100. if (next == quote && !escaped && (!tripleQuoted || stream.match(quote + quote))) {
  101. state.tokenize = null;
  102. break;
  103. }
  104. escaped = !raw && !escaped && next == "\\";
  105. }
  106. return "string";
  107. }
  108. state.tokenize = tokenStringHelper;
  109. return tokenStringHelper(stream, state);
  110. }
  111. function tokenInterpolation(stream, state) {
  112. stream.eat("$");
  113. if (stream.eat("{")) {
  114. // let clike handle the content of ${...},
  115. // we take over again when "}" appears (see hooks).
  116. state.tokenize = null;
  117. } else {
  118. state.tokenize = tokenInterpolationIdentifier;
  119. }
  120. return null;
  121. }
  122. function tokenInterpolationIdentifier(stream, state) {
  123. stream.eatWhile(/[\w_]/);
  124. state.tokenize = popInterpolationStack(state);
  125. return "variable";
  126. }
  127. function tokenNestedComment(depth) {
  128. return function (stream, state) {
  129. var ch
  130. while (ch = stream.next()) {
  131. if (ch == "*" && stream.eat("/")) {
  132. if (depth == 1) {
  133. state.tokenize = null
  134. break
  135. } else {
  136. state.tokenize = tokenNestedComment(depth - 1)
  137. return state.tokenize(stream, state)
  138. }
  139. } else if (ch == "/" && stream.eat("*")) {
  140. state.tokenize = tokenNestedComment(depth + 1)
  141. return state.tokenize(stream, state)
  142. }
  143. }
  144. return "comment"
  145. }
  146. }
  147. CodeMirror.registerHelper("hintWords", "application/dart", keywords.concat(atoms).concat(builtins));
  148. // This is needed to make loading through meta.js work.
  149. CodeMirror.defineMode("dart", function(conf) {
  150. return CodeMirror.getMode(conf, "application/dart");
  151. }, "clike");
  152. });