all repos — NoPaste @ 29b774f090102303e43cf939b38ac2083e62d9f1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/yaml/yaml.js (view raw)

  1// CodeMirror, copyright (c) by Marijn Haverbeke and others
  2// Distributed under an MIT license: https://codemirror.net/LICENSE
  3
  4(function(mod) {
  5  if (typeof exports == "object" && typeof module == "object") // CommonJS
  6    mod(require("../../lib/codemirror"));
  7  else if (typeof define == "function" && define.amd) // AMD
  8    define(["../../lib/codemirror"], mod);
  9  else // Plain browser env
 10    mod(CodeMirror);
 11})(function(CodeMirror) {
 12"use strict";
 13
 14CodeMirror.defineMode("yaml", function() {
 15
 16  var cons = ['true', 'false', 'on', 'off', 'yes', 'no'];
 17  var keywordRegex = new RegExp("\\b(("+cons.join(")|(")+"))$", 'i');
 18
 19  return {
 20    token: function(stream, state) {
 21      var ch = stream.peek();
 22      var esc = state.escaped;
 23      state.escaped = false;
 24      /* comments */
 25      if (ch == "#" && (stream.pos == 0 || /\s/.test(stream.string.charAt(stream.pos - 1)))) {
 26        stream.skipToEnd();
 27        return "comment";
 28      }
 29
 30      if (stream.match(/^('([^']|\\.)*'?|"([^"]|\\.)*"?)/))
 31        return "string";
 32
 33      if (state.literal && stream.indentation() > state.keyCol) {
 34        stream.skipToEnd(); return "string";
 35      } else if (state.literal) { state.literal = false; }
 36      if (stream.sol()) {
 37        state.keyCol = 0;
 38        state.pair = false;
 39        state.pairStart = false;
 40        /* document start */
 41        if(stream.match(/---/)) { return "def"; }
 42        /* document end */
 43        if (stream.match(/\.\.\./)) { return "def"; }
 44        /* array list item */
 45        if (stream.match(/\s*-\s+/)) { return 'meta'; }
 46      }
 47      /* inline pairs/lists */
 48      if (stream.match(/^(\{|\}|\[|\])/)) {
 49        if (ch == '{')
 50          state.inlinePairs++;
 51        else if (ch == '}')
 52          state.inlinePairs--;
 53        else if (ch == '[')
 54          state.inlineList++;
 55        else
 56          state.inlineList--;
 57        return 'meta';
 58      }
 59
 60      /* list seperator */
 61      if (state.inlineList > 0 && !esc && ch == ',') {
 62        stream.next();
 63        return 'meta';
 64      }
 65      /* pairs seperator */
 66      if (state.inlinePairs > 0 && !esc && ch == ',') {
 67        state.keyCol = 0;
 68        state.pair = false;
 69        state.pairStart = false;
 70        stream.next();
 71        return 'meta';
 72      }
 73
 74      /* start of value of a pair */
 75      if (state.pairStart) {
 76        /* block literals */
 77        if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
 78        /* references */
 79        if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable-2'; }
 80        /* numbers */
 81        if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
 82        if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
 83        /* keywords */
 84        if (stream.match(keywordRegex)) { return 'keyword'; }
 85      }
 86
 87      /* pairs (associative arrays) -> key */
 88      if (!state.pair && stream.match(/^\s*(?:[,\[\]{}&*!|>'"%@`][^\s'":]|[^,\[\]{}#&*!|>'"%@`])[^#]*?(?=\s*:($|\s))/)) {
 89        state.pair = true;
 90        state.keyCol = stream.indentation();
 91        return "atom";
 92      }
 93      if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
 94
 95      /* nothing found, continue */
 96      state.pairStart = false;
 97      state.escaped = (ch == '\\');
 98      stream.next();
 99      return null;
100    },
101    startState: function() {
102      return {
103        pair: false,
104        pairStart: false,
105        keyCol: 0,
106        inlinePairs: 0,
107        inlineList: 0,
108        literal: false,
109        escaped: false
110      };
111    },
112    lineComment: "#",
113    fold: "indent"
114  };
115});
116
117CodeMirror.defineMIME("text/x-yaml", "yaml");
118CodeMirror.defineMIME("text/yaml", "yaml");
119
120});