Skip to content

Commit adb73ff

Browse files
TimothyGumarijnh
authored andcommitted
[webidl mode] Add
1 parent d6e8f34 commit adb73ff

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed

doc/compress.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ <h2>Script compression helper</h2>
226226
<option value="http://codemirror.net/mode/verilog/verilog.js">verilog.js</option>
227227
<option value="http://codemirror.net/mode/vhdl/vhdl.js">vhdl.js</option>
228228
<option value="http://codemirror.net/mode/vue/vue.js">vue.js</option>
229+
<option value="http://codemirror.net/mode/webidl/webidl.js">webidl.js</option>
229230
<option value="http://codemirror.net/mode/xml/xml.js">xml.js</option>
230231
<option value="http://codemirror.net/mode/xquery/xquery.js">xquery.js</option>
231232
<option value="http://codemirror.net/mode/yaml/yaml.js">yaml.js</option>

mode/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ <h2>Language modes</h2>
149149
<li><a href="verilog/index.html">Verilog/SystemVerilog</a></li>
150150
<li><a href="vhdl/index.html">VHDL</a></li>
151151
<li><a href="vue/index.html">Vue.js app</a></li>
152+
<li><a href="webidl/index.html">Web IDL</a></li>
152153
<li><a href="xml/index.html">XML/HTML</a></li>
153154
<li><a href="xquery/index.html">XQuery</a></li>
154155
<li><a href="yacas/index.html">Yacas</a></li>

mode/meta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
{name: "Turtle", mime: "text/turtle", mode: "turtle", ext: ["ttl"]},
146146
{name: "TypeScript", mime: "application/typescript", mode: "javascript", ext: ["ts"], alias: ["ts"]},
147147
{name: "Twig", mime: "text/x-twig", mode: "twig"},
148+
{name: "Web IDL", mime: "text/x-webidl", mode: "webidl", ext: ["webidl"]},
148149
{name: "VB.NET", mime: "text/x-vb", mode: "vb", ext: ["vb"]},
149150
{name: "VBScript", mime: "text/vbscript", mode: "vbscript", ext: ["vbs"]},
150151
{name: "Velocity", mime: "text/velocity", mode: "velocity", ext: ["vtl"]},

mode/webidl/index.html

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!doctype html>
2+
3+
<title>CodeMirror: Web IDL mode</title>
4+
<meta charset="utf-8">
5+
<link rel="stylesheet" href="../../doc/docs.css">
6+
<link rel="stylesheet" href="../../lib/codemirror.css">
7+
<script src="../../lib/codemirror.js"></script>
8+
<script src="../../addon/edit/matchbrackets.js"></script>
9+
<script src="webidl.js"></script>
10+
<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
11+
12+
<div id="nav">
13+
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id="logo" src="../../doc/logo.png"></a>
14+
15+
<ul>
16+
<li><a href="../../index.html">Home</a>
17+
<li><a href="../../doc/manual.html">Manual</a>
18+
<li><a href="https://github.com/codemirror/codemirror">Code</a>
19+
</ul>
20+
<ul>
21+
<li><a href="../index.html">Language modes</a>
22+
<li><a class="active" href="#">Web IDL</a>
23+
</ul>
24+
</div>
25+
26+
<article>
27+
<h2>Web IDL mode</h2>
28+
29+
<div>
30+
<textarea id="code" name="code">
31+
[NamedConstructor=Image(optional unsigned long width, optional unsigned long height)]
32+
interface HTMLImageElement : HTMLElement {
33+
attribute DOMString alt;
34+
attribute DOMString src;
35+
attribute DOMString srcset;
36+
attribute DOMString sizes;
37+
attribute DOMString? crossOrigin;
38+
attribute DOMString useMap;
39+
attribute boolean isMap;
40+
attribute unsigned long width;
41+
attribute unsigned long height;
42+
readonly attribute unsigned long naturalWidth;
43+
readonly attribute unsigned long naturalHeight;
44+
readonly attribute boolean complete;
45+
readonly attribute DOMString currentSrc;
46+
47+
// also has obsolete members
48+
};
49+
50+
partial interface HTMLImageElement {
51+
attribute DOMString name;
52+
attribute DOMString lowsrc;
53+
attribute DOMString align;
54+
attribute unsigned long hspace;
55+
attribute unsigned long vspace;
56+
attribute DOMString longDesc;
57+
58+
[TreatNullAs=EmptyString] attribute DOMString border;
59+
};
60+
</textarea>
61+
</div>
62+
63+
<script>
64+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
65+
lineNumbers: true,
66+
matchBrackets: true
67+
});
68+
</script>
69+
70+
<p><strong>MIME type defined:</strong> <code>text/x-webidl</code>.</p>
71+
</article>

mode/webidl/webidl.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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"));
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+
14+
function wordRegexp(words) {
15+
return new RegExp("^((" + words.join(")|(") + "))\\b");
16+
};
17+
18+
var 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+
];
41+
var builtins = wordRegexp(builtinArray);
42+
43+
var 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+
];
56+
var types = wordRegexp(typeArray);
57+
58+
var 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+
];
66+
var keywords = wordRegexp(keywordArray);
67+
68+
var atomArray = [
69+
"true", "false", // BooleanLiteral
70+
"Infinity", "NaN", // FloatLiteral
71+
"null" // Rest of ConstValue
72+
];
73+
var atoms = wordRegexp(atomArray);
74+
75+
CodeMirror.registerHelper("hintWords", "webidl",
76+
builtinArray.concat(typeArray).concat(keywordArray).concat(atomArray));
77+
78+
var startDefArray = ["callback", "dictionary", "enum", "interface"];
79+
var startDefs = wordRegexp(startDefArray);
80+
81+
var endDefArray = ["typedef"];
82+
var endDefs = wordRegexp(endDefArray);
83+
84+
var singleOperators = /^[:<=>?]/;
85+
var integers = /^-?([1-9][0-9]*|0[Xx][0-9A-Fa-f]+|0[0-7]*)/;
86+
var floats = /^-?(([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+-]?[0-9]+)?|[0-9]+[Ee][+-]?[0-9]+)/;
87+
var identifiers = /^_?[A-Za-z][0-9A-Z_a-z-]*/;
88+
var strings = /^"[^"]*"/;
89+
var multilineComments = /^\/\*.*?\*\//;
90+
var multilineCommentsStart = /^\/\*.*/;
91+
var multilineCommentsEnd = /^.*?\*\//;
92+
93+
function readToken(stream, state) {
94+
// whitespace
95+
if (stream.eatSpace()) return null;
96+
97+
// comment
98+
if (state.inComment) {
99+
if (stream.match(multilineCommentsEnd)) {
100+
state.inComment = false;
101+
return "comment";
102+
}
103+
stream.skipToEnd();
104+
return "comment";
105+
}
106+
if (stream.match("//")) {
107+
stream.skipToEnd();
108+
return "comment";
109+
}
110+
if (stream.match(multilineComments)) return "comment";
111+
if (stream.match(multilineCommentsStart)) {
112+
state.inComment = true;
113+
return "comment";
114+
}
115+
116+
// integer and float
117+
if (stream.match(/^-?[0-9\.]/, false)) {
118+
if (stream.match(integers) || stream.match(floats)) return "number";
119+
}
120+
121+
// string
122+
if (stream.match(strings)) return "string";
123+
124+
// identifier
125+
var pos = stream.pos;
126+
if (stream.match(identifiers)) {
127+
if (state.startDef) return "def";
128+
if (state.endDef && stream.match(/^\s*;/, false)) {
129+
state.endDef = false;
130+
return "def";
131+
}
132+
stream.pos = pos;
133+
}
134+
135+
if (stream.match(keywords)) return "keyword";
136+
137+
if (stream.match(types)) {
138+
var lastToken = state.lastToken;
139+
var nextToken = (stream.match(/^\s*(.+?)\b/, false) || [])[1];
140+
141+
if (lastToken === ":" || lastToken === "implements" ||
142+
nextToken === "implements" || nextToken === "=") {
143+
// Used as identifier
144+
return "builtin";
145+
} else {
146+
// Used as type
147+
return "variable-3";
148+
}
149+
}
150+
151+
if (stream.match(builtins)) return "builtin";
152+
if (stream.match(atoms)) return "atom";
153+
if (stream.match(identifiers)) return "variable";
154+
155+
// other
156+
if (stream.match(singleOperators)) return "operator";
157+
158+
// unrecognized
159+
stream.next();
160+
return null;
161+
};
162+
163+
CodeMirror.defineMode("webidl", function() {
164+
return {
165+
startState: function() {
166+
return {
167+
// Is in multiline comment
168+
inComment: false,
169+
// Last non-whitespace, matched token
170+
lastToken: "",
171+
// Next token is a definition
172+
startDef: false,
173+
// Last token of the statement is a definition
174+
endDef: false
175+
};
176+
},
177+
token: function(stream, state) {
178+
var style = readToken(stream, state);
179+
180+
if (style) {
181+
var cur = stream.current();
182+
state.lastToken = cur;
183+
if (style === "keyword") {
184+
state.startDef = startDefs.test(cur);
185+
state.endDef = state.endDef || endDefs.test(cur);
186+
} else {
187+
state.startDef = false;
188+
}
189+
}
190+
191+
return style;
192+
}
193+
};
194+
});
195+
196+
CodeMirror.defineMIME("text/x-webidl", "webidl");
197+
});

0 commit comments

Comments
 (0)