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.

dart.js 5.4KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/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 " +
  14. "assert break case continue default in return new deferred async await covariant " +
  15. "try catch finally do else for if switch while import library export " +
  16. "part of show hide is as extension on yield late required").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. hooks: {
  41. "@": function(stream) {
  42. stream.eatWhile(/[\w\$_\.]/);
  43. return "meta";
  44. },
  45. // custom string handling to deal with triple-quoted strings and string interpolation
  46. "'": function(stream, state) {
  47. return tokenString("'", stream, state, false);
  48. },
  49. "\"": function(stream, state) {
  50. return tokenString("\"", stream, state, false);
  51. },
  52. "r": function(stream, state) {
  53. var peek = stream.peek();
  54. if (peek == "'" || peek == "\"") {
  55. return tokenString(stream.next(), stream, state, true);
  56. }
  57. return false;
  58. },
  59. "}": function(_stream, state) {
  60. // "}" is end of interpolation, if interpolation stack is non-empty
  61. if (sizeInterpolationStack(state) > 0) {
  62. state.tokenize = popInterpolationStack(state);
  63. return null;
  64. }
  65. return false;
  66. },
  67. "/": function(stream, state) {
  68. if (!stream.eat("*")) return false
  69. state.tokenize = tokenNestedComment(1)
  70. return state.tokenize(stream, state)
  71. },
  72. token: function(stream, _, style) {
  73. if (style == "variable") {
  74. // Assume uppercase symbols are classes using variable-2
  75. var isUpper = RegExp('^[_$]*[A-Z][a-zA-Z0-9_$]*$','g');
  76. if (isUpper.test(stream.current())) {
  77. return 'variable-2';
  78. }
  79. }
  80. }
  81. }
  82. });
  83. function tokenString(quote, stream, state, raw) {
  84. var tripleQuoted = false;
  85. if (stream.eat(quote)) {
  86. if (stream.eat(quote)) tripleQuoted = true;
  87. else return "string"; //empty string
  88. }
  89. function tokenStringHelper(stream, state) {
  90. var escaped = false;
  91. while (!stream.eol()) {
  92. if (!raw && !escaped && stream.peek() == "$") {
  93. pushInterpolationStack(state);
  94. state.tokenize = tokenInterpolation;
  95. return "string";
  96. }
  97. var next = stream.next();
  98. if (next == quote && !escaped && (!tripleQuoted || stream.match(quote + quote))) {
  99. state.tokenize = null;
  100. break;
  101. }
  102. escaped = !raw && !escaped && next == "\\";
  103. }
  104. return "string";
  105. }
  106. state.tokenize = tokenStringHelper;
  107. return tokenStringHelper(stream, state);
  108. }
  109. function tokenInterpolation(stream, state) {
  110. stream.eat("$");
  111. if (stream.eat("{")) {
  112. // let clike handle the content of ${...},
  113. // we take over again when "}" appears (see hooks).
  114. state.tokenize = null;
  115. } else {
  116. state.tokenize = tokenInterpolationIdentifier;
  117. }
  118. return null;
  119. }
  120. function tokenInterpolationIdentifier(stream, state) {
  121. stream.eatWhile(/[\w_]/);
  122. state.tokenize = popInterpolationStack(state);
  123. return "variable";
  124. }
  125. function tokenNestedComment(depth) {
  126. return function (stream, state) {
  127. var ch
  128. while (ch = stream.next()) {
  129. if (ch == "*" && stream.eat("/")) {
  130. if (depth == 1) {
  131. state.tokenize = null
  132. break
  133. } else {
  134. state.tokenize = tokenNestedComment(depth - 1)
  135. return state.tokenize(stream, state)
  136. }
  137. } else if (ch == "/" && stream.eat("*")) {
  138. state.tokenize = tokenNestedComment(depth + 1)
  139. return state.tokenize(stream, state)
  140. }
  141. }
  142. return "comment"
  143. }
  144. }
  145. CodeMirror.registerHelper("hintWords", "application/dart", keywords.concat(atoms).concat(builtins));
  146. // This is needed to make loading through meta.js work.
  147. CodeMirror.defineMode("dart", function(conf) {
  148. return CodeMirror.getMode(conf, "application/dart");
  149. }, "clike");
  150. });