// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"), require("../clike/clike"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror", "../clike/clike"], mod);
else // Plain browser env
})(function(CodeMirror) {
var keywords = ("this super static final const abstract class extends external factory " +
"implements get native operator set typedef with enum throw rethrow " +
"assert break case continue default in return new deferred async await " +
"try catch finally do else for if switch while import library export " +
"part of show hide is as").split(" ");
var blockKeywords = "try catch finally do else for if switch while".split(" ");
var atoms = "true false null".split(" ");
var builtins = "void bool num int double dynamic var String".split(" ");
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
function pushInterpolationStack(state) {
(state.interpolationStack || (state.interpolationStack = [])).push(state.tokenize);
function popInterpolationStack(state) {
return (state.interpolationStack || (state.interpolationStack = [])).pop();
function sizeInterpolationStack(state) {
return state.interpolationStack ? state.interpolationStack.length : 0;
CodeMirror.defineMIME("application/dart", {
blockKeywords: set(blockKeywords),
stream.eatWhile(/[\w\$_\.]/);
// custom string handling to deal with triple-quoted strings and string interpolation
"'": function(stream, state) {
return tokenString("'", stream, state, false);
"\"": function(stream, state) {
return tokenString("\"", stream, state, false);
"r": function(stream, state) {
var peek = stream.peek();
if (peek == "'" || peek == "\"") {
return tokenString(stream.next(), stream, state, true);
"}": function(_stream, state) {
// "}" is end of interpolation, if interpolation stack is non-empty
if (sizeInterpolationStack(state) > 0) {
state.tokenize = popInterpolationStack(state);
"/": function(stream, state) {
if (!stream.eat("*")) return false
state.tokenize = tokenNestedComment(1)
return state.tokenize(stream, state)
function tokenString(quote, stream, state, raw) {
var tripleQuoted = false;
if (stream.eat(quote)) tripleQuoted = true;
else return "string"; //empty string
function tokenStringHelper(stream, state) {
if (!raw && !escaped && stream.peek() == "$") {
pushInterpolationStack(state);
state.tokenize = tokenInterpolation;
var next = stream.next();
if (next == quote && !escaped && (!tripleQuoted || stream.match(quote + quote))) {
escaped = !raw && !escaped && next == "\\";
state.tokenize = tokenStringHelper;
return tokenStringHelper(stream, state);
function tokenInterpolation(stream, state) {
// let clike handle the content of ${...},
// we take over again when "}" appears (see hooks).
state.tokenize = tokenInterpolationIdentifier;
function tokenInterpolationIdentifier(stream, state) {
stream.eatWhile(/[\w_]/);
state.tokenize = popInterpolationStack(state);
function tokenNestedComment(depth) {
return function (stream, state) {
while (ch = stream.next()) {
if (ch == "*" && stream.eat("/")) {
state.tokenize = tokenNestedComment(depth - 1)
return state.tokenize(stream, state)
} else if (ch == "/" && stream.eat("*")) {
state.tokenize = tokenNestedComment(depth + 1)
return state.tokenize(stream, state)
CodeMirror.registerHelper("hintWords", "application/dart", keywords.concat(atoms).concat(builtins));
// This is needed to make loading through meta.js work.
CodeMirror.defineMode("dart", function(conf) {
return CodeMirror.getMode(conf, "application/dart");