webidl.js 5.6 KB

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