all repos — NoPaste @ 29b774f090102303e43cf939b38ac2083e62d9f1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/webidl/webidl.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
 14function wordRegexp(words) {
 15  return new RegExp("^((" + words.join(")|(") + "))\\b");
 16};
 17
 18var builtinArray = [
 19  "Clamp",
 20  "Constructor",
 21  "EnforceRange",
 22  "Exposed",
 23  "ImplicitThis",
 24  "Global", "PrimaryGlobal",
 25  "LegacyArrayClass",
 26  "LegacyUnenumerableNamedProperties",
 27  "LenientThis",
 28  "NamedConstructor",
 29  "NewObject",
 30  "NoInterfaceObject",
 31  "OverrideBuiltins",
 32  "PutForwards",
 33  "Replaceable",
 34  "SameObject",
 35  "TreatNonObjectAsNull",
 36  "TreatNullAs",
 37    "EmptyString",
 38  "Unforgeable",
 39  "Unscopeable"
 40];
 41var builtins = wordRegexp(builtinArray);
 42
 43var typeArray = [
 44  "unsigned", "short", "long",                  // UnsignedIntegerType
 45  "unrestricted", "float", "double",            // UnrestrictedFloatType
 46  "boolean", "byte", "octet",                   // Rest of PrimitiveType
 47  "Promise",                                    // PromiseType
 48  "ArrayBuffer", "DataView", "Int8Array", "Int16Array", "Int32Array",
 49  "Uint8Array", "Uint16Array", "Uint32Array", "Uint8ClampedArray",
 50  "Float32Array", "Float64Array",               // BufferRelatedType
 51  "ByteString", "DOMString", "USVString", "sequence", "object", "RegExp",
 52  "Error", "DOMException", "FrozenArray",       // Rest of NonAnyType
 53  "any",                                        // Rest of SingleType
 54  "void"                                        // Rest of ReturnType
 55];
 56var types = wordRegexp(typeArray);
 57
 58var keywordArray = [
 59  "attribute", "callback", "const", "deleter", "dictionary", "enum", "getter",
 60  "implements", "inherit", "interface", "iterable", "legacycaller", "maplike",
 61  "partial", "required", "serializer", "setlike", "setter", "static",
 62  "stringifier", "typedef",                     // ArgumentNameKeyword except
 63                                                // "unrestricted"
 64  "optional", "readonly", "or"
 65];
 66var keywords = wordRegexp(keywordArray);
 67
 68var atomArray = [
 69  "true", "false",                              // BooleanLiteral
 70  "Infinity", "NaN",                            // FloatLiteral
 71  "null"                                        // Rest of ConstValue
 72];
 73var atoms = wordRegexp(atomArray);
 74
 75CodeMirror.registerHelper("hintWords", "webidl",
 76    builtinArray.concat(typeArray).concat(keywordArray).concat(atomArray));
 77
 78var startDefArray = ["callback", "dictionary", "enum", "interface"];
 79var startDefs = wordRegexp(startDefArray);
 80
 81var endDefArray = ["typedef"];
 82var endDefs = wordRegexp(endDefArray);
 83
 84var singleOperators = /^[:<=>?]/;
 85var integers = /^-?([1-9][0-9]*|0[Xx][0-9A-Fa-f]+|0[0-7]*)/;
 86var floats = /^-?(([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+-]?[0-9]+)?|[0-9]+[Ee][+-]?[0-9]+)/;
 87var identifiers = /^_?[A-Za-z][0-9A-Z_a-z-]*/;
 88var identifiersEnd = /^_?[A-Za-z][0-9A-Z_a-z-]*(?=\s*;)/;
 89var strings = /^"[^"]*"/;
 90var multilineComments = /^\/\*.*?\*\//;
 91var multilineCommentsStart = /^\/\*.*/;
 92var multilineCommentsEnd = /^.*?\*\//;
 93
 94function readToken(stream, state) {
 95  // whitespace
 96  if (stream.eatSpace()) return null;
 97
 98  // comment
 99  if (state.inComment) {
100    if (stream.match(multilineCommentsEnd)) {
101      state.inComment = false;
102      return "comment";
103    }
104    stream.skipToEnd();
105    return "comment";
106  }
107  if (stream.match("//")) {
108    stream.skipToEnd();
109    return "comment";
110  }
111  if (stream.match(multilineComments)) return "comment";
112  if (stream.match(multilineCommentsStart)) {
113    state.inComment = true;
114    return "comment";
115  }
116
117  // integer and float
118  if (stream.match(/^-?[0-9\.]/, false)) {
119    if (stream.match(integers) || stream.match(floats)) return "number";
120  }
121
122  // string
123  if (stream.match(strings)) return "string";
124
125  // identifier
126  if (state.startDef && stream.match(identifiers)) return "def";
127
128  if (state.endDef && stream.match(identifiersEnd)) {
129    state.endDef = false;
130    return "def";
131  }
132
133  if (stream.match(keywords)) return "keyword";
134
135  if (stream.match(types)) {
136    var lastToken = state.lastToken;
137    var nextToken = (stream.match(/^\s*(.+?)\b/, false) || [])[1];
138
139    if (lastToken === ":" || lastToken === "implements" ||
140        nextToken === "implements" || nextToken === "=") {
141      // Used as identifier
142      return "builtin";
143    } else {
144      // Used as type
145      return "variable-3";
146    }
147  }
148
149  if (stream.match(builtins)) return "builtin";
150  if (stream.match(atoms)) return "atom";
151  if (stream.match(identifiers)) return "variable";
152
153  // other
154  if (stream.match(singleOperators)) return "operator";
155
156  // unrecognized
157  stream.next();
158  return null;
159};
160
161CodeMirror.defineMode("webidl", function() {
162  return {
163    startState: function() {
164      return {
165        // Is in multiline comment
166        inComment: false,
167        // Last non-whitespace, matched token
168        lastToken: "",
169        // Next token is a definition
170        startDef: false,
171        // Last token of the statement is a definition
172        endDef: false
173      };
174    },
175    token: function(stream, state) {
176      var style = readToken(stream, state);
177
178      if (style) {
179        var cur = stream.current();
180        state.lastToken = cur;
181        if (style === "keyword") {
182          state.startDef = startDefs.test(cur);
183          state.endDef = state.endDef || endDefs.test(cur);
184        } else {
185          state.startDef = false;
186        }
187      }
188
189      return style;
190    }
191  };
192});
193
194CodeMirror.defineMIME("text/x-webidl", "webidl");
195});