From adb73ffa1810807bfedf4a218e8cd1462425df14 Mon Sep 17 00:00:00 2001
From: Timothy Gu <timothygu99@gmail.com>
Date: Sat, 2 Apr 2016 20:25:41 -0700
Subject: [PATCH] [webidl mode] Add

---
 doc/compress.html      |   1 +
 mode/index.html        |   1 +
 mode/meta.js           |   1 +
 mode/webidl/index.html |  71 +++++++++++++++
 mode/webidl/webidl.js  | 197 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 271 insertions(+)
 create mode 100644 mode/webidl/index.html
 create mode 100644 mode/webidl/webidl.js

diff --git a/doc/compress.html b/doc/compress.html
index 538ecbe5f..44e6fea82 100644
--- a/doc/compress.html
+++ b/doc/compress.html
@@ -226,6 +226,7 @@
           <option value="http://codemirror.net/mode/verilog/verilog.js">verilog.js</option>
           <option value="http://codemirror.net/mode/vhdl/vhdl.js">vhdl.js</option>
           <option value="http://codemirror.net/mode/vue/vue.js">vue.js</option>
+          <option value="http://codemirror.net/mode/webidl/webidl.js">webidl.js</option>
           <option value="http://codemirror.net/mode/xml/xml.js">xml.js</option>
           <option value="http://codemirror.net/mode/xquery/xquery.js">xquery.js</option>
           <option value="http://codemirror.net/mode/yaml/yaml.js">yaml.js</option>
diff --git a/mode/index.html b/mode/index.html
index efb64143a..822f04fc6 100644
--- a/mode/index.html
+++ b/mode/index.html
@@ -149,6 +149,7 @@ option.</p>
       <li><a href="verilog/index.html">Verilog/SystemVerilog</a></li>
       <li><a href="vhdl/index.html">VHDL</a></li>
       <li><a href="vue/index.html">Vue.js app</a></li>
+      <li><a href="webidl/index.html">Web IDL</a></li>
       <li><a href="xml/index.html">XML/HTML</a></li>
       <li><a href="xquery/index.html">XQuery</a></li>
       <li><a href="yacas/index.html">Yacas</a></li>
diff --git a/mode/meta.js b/mode/meta.js
index 3539beaa1..dcdcdab9c 100644
--- a/mode/meta.js
+++ b/mode/meta.js
@@ -145,6 +145,7 @@
     {name: "Turtle", mime: "text/turtle", mode: "turtle", ext: ["ttl"]},
     {name: "TypeScript", mime: "application/typescript", mode: "javascript", ext: ["ts"], alias: ["ts"]},
     {name: "Twig", mime: "text/x-twig", mode: "twig"},
+    {name: "Web IDL", mime: "text/x-webidl", mode: "webidl", ext: ["webidl"]},
     {name: "VB.NET", mime: "text/x-vb", mode: "vb", ext: ["vb"]},
     {name: "VBScript", mime: "text/vbscript", mode: "vbscript", ext: ["vbs"]},
     {name: "Velocity", mime: "text/velocity", mode: "velocity", ext: ["vtl"]},
diff --git a/mode/webidl/index.html b/mode/webidl/index.html
new file mode 100644
index 000000000..1d4112e1c
--- /dev/null
+++ b/mode/webidl/index.html
@@ -0,0 +1,71 @@
+<!doctype html>
+
+<title>CodeMirror: Web IDL mode</title>
+<meta charset="utf-8">
+<link rel="stylesheet" href="../../doc/docs.css">
+<link rel="stylesheet" href="../../lib/codemirror.css">
+<script src="../../lib/codemirror.js"></script>
+<script src="../../addon/edit/matchbrackets.js"></script>
+<script src="webidl.js"></script>
+<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
+
+<div id="nav">
+  <a href="http://codemirror.net"><h1>CodeMirror</h1><img id="logo" src="../../doc/logo.png"></a>
+
+  <ul>
+    <li><a href="../../index.html">Home</a>
+    <li><a href="../../doc/manual.html">Manual</a>
+    <li><a href="https://github.com/codemirror/codemirror">Code</a>
+  </ul>
+  <ul>
+    <li><a href="../index.html">Language modes</a>
+    <li><a class="active" href="#">Web IDL</a>
+  </ul>
+</div>
+
+<article>
+  <h2>Web IDL mode</h2>
+
+  <div>
+<textarea id="code" name="code">
+[NamedConstructor=Image(optional unsigned long width, optional unsigned long height)]
+interface HTMLImageElement : HTMLElement {
+           attribute DOMString alt;
+           attribute DOMString src;
+           attribute DOMString srcset;
+           attribute DOMString sizes;
+           attribute DOMString? crossOrigin;
+           attribute DOMString useMap;
+           attribute boolean isMap;
+           attribute unsigned long width;
+           attribute unsigned long height;
+  readonly attribute unsigned long naturalWidth;
+  readonly attribute unsigned long naturalHeight;
+  readonly attribute boolean complete;
+  readonly attribute DOMString currentSrc;
+
+  // also has obsolete members
+};
+
+partial interface HTMLImageElement {
+  attribute DOMString name;
+  attribute DOMString lowsrc;
+  attribute DOMString align;
+  attribute unsigned long hspace;
+  attribute unsigned long vspace;
+  attribute DOMString longDesc;
+
+  [TreatNullAs=EmptyString] attribute DOMString border;
+};
+</textarea>
+  </div>
+
+  <script>
+    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
+      lineNumbers: true,
+      matchBrackets: true
+    });
+  </script>
+
+  <p><strong>MIME type defined:</strong> <code>text/x-webidl</code>.</p>
+</article>
diff --git a/mode/webidl/webidl.js b/mode/webidl/webidl.js
new file mode 100644
index 000000000..6a60fa23e
--- /dev/null
+++ b/mode/webidl/webidl.js
@@ -0,0 +1,197 @@
+// CodeMirror, copyright (c) by Marijn Haverbeke and others
+// Distributed under an MIT license: http://codemirror.net/LICENSE
+
+(function(mod) {
+  if (typeof exports == "object" && typeof module == "object") // CommonJS
+    mod(require("../../lib/codemirror"));
+  else if (typeof define == "function" && define.amd) // AMD
+    define(["../../lib/codemirror"], mod);
+  else // Plain browser env
+    mod(CodeMirror);
+})(function(CodeMirror) {
+"use strict";
+
+function wordRegexp(words) {
+  return new RegExp("^((" + words.join(")|(") + "))\\b");
+};
+
+var builtinArray = [
+  "Clamp",
+  "Constructor",
+  "EnforceRange",
+  "Exposed",
+  "ImplicitThis",
+  "Global", "PrimaryGlobal",
+  "LegacyArrayClass",
+  "LegacyUnenumerableNamedProperties",
+  "LenientThis",
+  "NamedConstructor",
+  "NewObject",
+  "NoInterfaceObject",
+  "OverrideBuiltins",
+  "PutForwards",
+  "Replaceable",
+  "SameObject",
+  "TreatNonObjectAsNull",
+  "TreatNullAs",
+    "EmptyString",
+  "Unforgeable",
+  "Unscopeable"
+];
+var builtins = wordRegexp(builtinArray);
+
+var typeArray = [
+  "unsigned", "short", "long",                  // UnsignedIntegerType
+  "unrestricted", "float", "double",            // UnrestrictedFloatType
+  "boolean", "byte", "octet",                   // Rest of PrimitiveType
+  "Promise",                                    // PromiseType
+  "ArrayBuffer", "DataView", "Int8Array", "Int16Array", "Int32Array",
+  "Uint8Array", "Uint16Array", "Uint32Array", "Uint8ClampedArray",
+  "Float32Array", "Float64Array",               // BufferRelatedType
+  "ByteString", "DOMString", "USVString", "sequence", "object", "RegExp",
+  "Error", "DOMException", "FrozenArray",       // Rest of NonAnyType
+  "any",                                        // Rest of SingleType
+  "void"                                        // Rest of ReturnType
+];
+var types = wordRegexp(typeArray);
+
+var keywordArray = [
+  "attribute", "callback", "const", "deleter", "dictionary", "enum", "getter",
+  "implements", "inherit", "interface", "iterable", "legacycaller", "maplike",
+  "partial", "required", "serializer", "setlike", "setter", "static",
+  "stringifier", "typedef",                     // ArgumentNameKeyword except
+                                                // "unrestricted"
+  "optional", "readonly", "or"
+];
+var keywords = wordRegexp(keywordArray);
+
+var atomArray = [
+  "true", "false",                              // BooleanLiteral
+  "Infinity", "NaN",                            // FloatLiteral
+  "null"                                        // Rest of ConstValue
+];
+var atoms = wordRegexp(atomArray);
+
+CodeMirror.registerHelper("hintWords", "webidl",
+    builtinArray.concat(typeArray).concat(keywordArray).concat(atomArray));
+
+var startDefArray = ["callback", "dictionary", "enum", "interface"];
+var startDefs = wordRegexp(startDefArray);
+
+var endDefArray = ["typedef"];
+var endDefs = wordRegexp(endDefArray);
+
+var singleOperators = /^[:<=>?]/;
+var integers = /^-?([1-9][0-9]*|0[Xx][0-9A-Fa-f]+|0[0-7]*)/;
+var floats = /^-?(([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+-]?[0-9]+)?|[0-9]+[Ee][+-]?[0-9]+)/;
+var identifiers = /^_?[A-Za-z][0-9A-Z_a-z-]*/;
+var strings = /^"[^"]*"/;
+var multilineComments = /^\/\*.*?\*\//;
+var multilineCommentsStart = /^\/\*.*/;
+var multilineCommentsEnd = /^.*?\*\//;
+
+function readToken(stream, state) {
+  // whitespace
+  if (stream.eatSpace()) return null;
+
+  // comment
+  if (state.inComment) {
+    if (stream.match(multilineCommentsEnd)) {
+      state.inComment = false;
+      return "comment";
+    }
+    stream.skipToEnd();
+    return "comment";
+  }
+  if (stream.match("//")) {
+    stream.skipToEnd();
+    return "comment";
+  }
+  if (stream.match(multilineComments)) return "comment";
+  if (stream.match(multilineCommentsStart)) {
+    state.inComment = true;
+    return "comment";
+  }
+
+  // integer and float
+  if (stream.match(/^-?[0-9\.]/, false)) {
+    if (stream.match(integers) || stream.match(floats)) return "number";
+  }
+
+  // string
+  if (stream.match(strings)) return "string";
+
+  // identifier
+  var pos = stream.pos;
+  if (stream.match(identifiers)) {
+    if (state.startDef) return "def";
+    if (state.endDef && stream.match(/^\s*;/, false)) {
+      state.endDef = false;
+      return "def";
+    }
+    stream.pos = pos;
+  }
+
+  if (stream.match(keywords)) return "keyword";
+
+  if (stream.match(types)) {
+    var lastToken = state.lastToken;
+    var nextToken = (stream.match(/^\s*(.+?)\b/, false) || [])[1];
+
+    if (lastToken === ":" || lastToken === "implements" ||
+        nextToken === "implements" || nextToken === "=") {
+      // Used as identifier
+      return "builtin";
+    } else {
+      // Used as type
+      return "variable-3";
+    }
+  }
+
+  if (stream.match(builtins)) return "builtin";
+  if (stream.match(atoms)) return "atom";
+  if (stream.match(identifiers)) return "variable";
+
+  // other
+  if (stream.match(singleOperators)) return "operator";
+
+  // unrecognized
+  stream.next();
+  return null;
+};
+
+CodeMirror.defineMode("webidl", function() {
+  return {
+    startState: function() {
+      return {
+        // Is in multiline comment
+        inComment: false,
+        // Last non-whitespace, matched token
+        lastToken: "",
+        // Next token is a definition
+        startDef: false,
+        // Last token of the statement is a definition
+        endDef: false
+      };
+    },
+    token: function(stream, state) {
+      var style = readToken(stream, state);
+
+      if (style) {
+        var cur = stream.current();
+        state.lastToken = cur;
+        if (style === "keyword") {
+          state.startDef = startDefs.test(cur);
+          state.endDef = state.endDef || endDefs.test(cur);
+        } else {
+          state.startDef = false;
+        }
+      }
+
+      return style;
+    }
+  };
+});
+
+CodeMirror.defineMIME("text/x-webidl", "webidl");
+});
-- 
GitLab