github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/mode/dart/dart.js (about)

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