Debian Codegraph @

codemirror-legacy-modes/mode/webidl.js

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