dart.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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 get native operator set typedef with enum throw rethrow " +
  14. "assert break case continue default in return new deferred async await " +
  15. "try catch finally do else for if switch while import library export " +
  16. "part of show hide is as").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".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. }
  73. });
  74. function tokenString(quote, stream, state, raw) {
  75. var tripleQuoted = false;
  76. if (stream.eat(quote)) {
  77. if (stream.eat(quote)) tripleQuoted = true;
  78. else return "string"; //empty string
  79. }
  80. function tokenStringHelper(stream, state) {
  81. var escaped = false;
  82. while (!stream.eol()) {
  83. if (!raw && !escaped && stream.peek() == "$") {
  84. pushInterpolationStack(state);
  85. state.tokenize = tokenInterpolation;
  86. return "string";
  87. }
  88. var next = stream.next();
  89. if (next == quote && !escaped && (!tripleQuoted || stream.match(quote + quote))) {
  90. state.tokenize = null;
  91. break;
  92. }
  93. escaped = !raw && !escaped && next == "\\";
  94. }
  95. return "string";
  96. }
  97. state.tokenize = tokenStringHelper;
  98. return tokenStringHelper(stream, state);
  99. }
  100. function tokenInterpolation(stream, state) {
  101. stream.eat("$");
  102. if (stream.eat("{")) {
  103. // let clike handle the content of ${...},
  104. // we take over again when "}" appears (see hooks).
  105. state.tokenize = null;
  106. } else {
  107. state.tokenize = tokenInterpolationIdentifier;
  108. }
  109. return null;
  110. }
  111. function tokenInterpolationIdentifier(stream, state) {
  112. stream.eatWhile(/[\w_]/);
  113. state.tokenize = popInterpolationStack(state);
  114. return "variable";
  115. }
  116. function tokenNestedComment(depth) {
  117. return function (stream, state) {
  118. var ch
  119. while (ch = stream.next()) {
  120. if (ch == "*" && stream.eat("/")) {
  121. if (depth == 1) {
  122. state.tokenize = null
  123. break
  124. } else {
  125. state.tokenize = tokenNestedComment(depth - 1)
  126. return state.tokenize(stream, state)
  127. }
  128. } else if (ch == "/" && stream.eat("*")) {
  129. state.tokenize = tokenNestedComment(depth + 1)
  130. return state.tokenize(stream, state)
  131. }
  132. }
  133. return "comment"
  134. }
  135. }
  136. CodeMirror.registerHelper("hintWords", "application/dart", keywords.concat(atoms).concat(builtins));
  137. // This is needed to make loading through meta.js work.
  138. CodeMirror.defineMode("dart", function(conf) {
  139. return CodeMirror.getMode(conf, "application/dart");
  140. }, "clike");
  141. });