all repos — NoPaste @ 29b774f090102303e43cf939b38ac2083e62d9f1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/sieve/sieve.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("sieve", function(config) {
 15  function words(str) {
 16    var obj = {}, words = str.split(" ");
 17    for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
 18    return obj;
 19  }
 20
 21  var keywords = words("if elsif else stop require");
 22  var atoms = words("true false not");
 23  var indentUnit = config.indentUnit;
 24
 25  function tokenBase(stream, state) {
 26
 27    var ch = stream.next();
 28    if (ch == "/" && stream.eat("*")) {
 29      state.tokenize = tokenCComment;
 30      return tokenCComment(stream, state);
 31    }
 32
 33    if (ch === '#') {
 34      stream.skipToEnd();
 35      return "comment";
 36    }
 37
 38    if (ch == "\"") {
 39      state.tokenize = tokenString(ch);
 40      return state.tokenize(stream, state);
 41    }
 42
 43    if (ch == "(") {
 44      state._indent.push("(");
 45      // add virtual angel wings so that editor behaves...
 46      // ...more sane incase of broken brackets
 47      state._indent.push("{");
 48      return null;
 49    }
 50
 51    if (ch === "{") {
 52      state._indent.push("{");
 53      return null;
 54    }
 55
 56    if (ch == ")")  {
 57      state._indent.pop();
 58      state._indent.pop();
 59    }
 60
 61    if (ch === "}") {
 62      state._indent.pop();
 63      return null;
 64    }
 65
 66    if (ch == ",")
 67      return null;
 68
 69    if (ch == ";")
 70      return null;
 71
 72
 73    if (/[{}\(\),;]/.test(ch))
 74      return null;
 75
 76    // 1*DIGIT "K" / "M" / "G"
 77    if (/\d/.test(ch)) {
 78      stream.eatWhile(/[\d]/);
 79      stream.eat(/[KkMmGg]/);
 80      return "number";
 81    }
 82
 83    // ":" (ALPHA / "_") *(ALPHA / DIGIT / "_")
 84    if (ch == ":") {
 85      stream.eatWhile(/[a-zA-Z_]/);
 86      stream.eatWhile(/[a-zA-Z0-9_]/);
 87
 88      return "operator";
 89    }
 90
 91    stream.eatWhile(/\w/);
 92    var cur = stream.current();
 93
 94    // "text:" *(SP / HTAB) (hash-comment / CRLF)
 95    // *(multiline-literal / multiline-dotstart)
 96    // "." CRLF
 97    if ((cur == "text") && stream.eat(":"))
 98    {
 99      state.tokenize = tokenMultiLineString;
100      return "string";
101    }
102
103    if (keywords.propertyIsEnumerable(cur))
104      return "keyword";
105
106    if (atoms.propertyIsEnumerable(cur))
107      return "atom";
108
109    return null;
110  }
111
112  function tokenMultiLineString(stream, state)
113  {
114    state._multiLineString = true;
115    // the first line is special it may contain a comment
116    if (!stream.sol()) {
117      stream.eatSpace();
118
119      if (stream.peek() == "#") {
120        stream.skipToEnd();
121        return "comment";
122      }
123
124      stream.skipToEnd();
125      return "string";
126    }
127
128    if ((stream.next() == ".")  && (stream.eol()))
129    {
130      state._multiLineString = false;
131      state.tokenize = tokenBase;
132    }
133
134    return "string";
135  }
136
137  function tokenCComment(stream, state) {
138    var maybeEnd = false, ch;
139    while ((ch = stream.next()) != null) {
140      if (maybeEnd && ch == "/") {
141        state.tokenize = tokenBase;
142        break;
143      }
144      maybeEnd = (ch == "*");
145    }
146    return "comment";
147  }
148
149  function tokenString(quote) {
150    return function(stream, state) {
151      var escaped = false, ch;
152      while ((ch = stream.next()) != null) {
153        if (ch == quote && !escaped)
154          break;
155        escaped = !escaped && ch == "\\";
156      }
157      if (!escaped) state.tokenize = tokenBase;
158      return "string";
159    };
160  }
161
162  return {
163    startState: function(base) {
164      return {tokenize: tokenBase,
165              baseIndent: base || 0,
166              _indent: []};
167    },
168
169    token: function(stream, state) {
170      if (stream.eatSpace())
171        return null;
172
173      return (state.tokenize || tokenBase)(stream, state);
174    },
175
176    indent: function(state, _textAfter) {
177      var length = state._indent.length;
178      if (_textAfter && (_textAfter[0] == "}"))
179        length--;
180
181      if (length <0)
182        length = 0;
183
184      return length * indentUnit;
185    },
186
187    electricChars: "}"
188  };
189});
190
191CodeMirror.defineMIME("application/sieve", "sieve");
192
193});