prism-web-idl.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. (function (Prism) {
  2. var id = /(?:\B-|\b_|\b)[A-Za-z][\w-]*(?![\w-])/.source;
  3. var type =
  4. '(?:' +
  5. /\b(?:unsigned\s+)?long\s+long(?![\w-])/.source +
  6. '|' +
  7. /\b(?:unrestricted|unsigned)\s+[a-z]+(?![\w-])/.source +
  8. '|' +
  9. /(?!(?:unrestricted|unsigned)\b)/.source + id + /(?:\s*<(?:[^<>]|<[^<>]*>)*>)?/.source +
  10. ')' + /(?:\s*\?)?/.source;
  11. var typeInside = {};
  12. Prism.languages['web-idl'] = {
  13. 'comment': {
  14. pattern: /\/\/.*|\/\*[\s\S]*?\*\//,
  15. greedy: true
  16. },
  17. 'string': {
  18. pattern: /"[^"]*"/,
  19. greedy: true
  20. },
  21. 'namespace': {
  22. pattern: RegExp(/(\bnamespace\s+)/.source + id),
  23. lookbehind: true,
  24. },
  25. 'class-name': [
  26. {
  27. pattern: /(^|[^\w-])(?:iterable|maplike|setlike)\s*<(?:[^<>]|<[^<>]*>)*>/,
  28. lookbehind: true,
  29. inside: typeInside
  30. },
  31. {
  32. pattern: RegExp(/(\b(?:attribute|const|deleter|getter|optional|setter)\s+)/.source + type),
  33. lookbehind: true,
  34. inside: typeInside
  35. },
  36. {
  37. // callback return type
  38. pattern: RegExp('(' + /\bcallback\s+/.source + id + /\s*=\s*/.source + ')' + type),
  39. lookbehind: true,
  40. inside: typeInside
  41. },
  42. {
  43. // typedef
  44. pattern: RegExp(/(\btypedef\b\s*)/.source + type),
  45. lookbehind: true,
  46. inside: typeInside
  47. },
  48. {
  49. pattern: RegExp(/(\b(?:callback|dictionary|enum|interface(?:\s+mixin)?)\s+)(?!(?:interface|mixin)\b)/.source + id),
  50. lookbehind: true,
  51. },
  52. {
  53. // inheritance
  54. pattern: RegExp(/(:\s*)/.source + id),
  55. lookbehind: true,
  56. },
  57. // includes and implements
  58. RegExp(id + /(?=\s+(?:implements|includes)\b)/.source),
  59. {
  60. pattern: RegExp(/(\b(?:implements|includes)\s+)/.source + id),
  61. lookbehind: true,
  62. },
  63. {
  64. // function return type, parameter types, and dictionary members
  65. pattern: RegExp(type + '(?=' + /\s*(?:\.{3}\s*)?/.source + id + /\s*[(),;=]/.source + ')'),
  66. inside: typeInside
  67. },
  68. ],
  69. 'builtin': /\b(?:ArrayBuffer|BigInt64Array|BigUint64Array|ByteString|DOMString|DataView|Float32Array|Float64Array|FrozenArray|Int16Array|Int32Array|Int8Array|ObservableArray|Promise|USVString|Uint16Array|Uint32Array|Uint8Array|Uint8ClampedArray)\b/,
  70. 'keyword': [
  71. /\b(?:async|attribute|callback|const|constructor|deleter|dictionary|enum|getter|implements|includes|inherit|interface|mixin|namespace|null|optional|or|partial|readonly|required|setter|static|stringifier|typedef|unrestricted)\b/,
  72. // type keywords
  73. /\b(?:any|bigint|boolean|byte|double|float|iterable|long|maplike|object|octet|record|sequence|setlike|short|symbol|undefined|unsigned|void)\b/
  74. ],
  75. 'boolean': /\b(?:false|true)\b/,
  76. 'number': {
  77. pattern: /(^|[^\w-])-?(?:0x[0-9a-f]+|(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?|NaN|Infinity)(?![\w-])/i,
  78. lookbehind: true
  79. },
  80. 'operator': /\.{3}|[=:?<>-]/,
  81. 'punctuation': /[(){}[\].,;]/
  82. };
  83. for (var key in Prism.languages['web-idl']) {
  84. if (key !== 'class-name') {
  85. typeInside[key] = Prism.languages['web-idl'][key];
  86. }
  87. }
  88. Prism.languages['webidl'] = Prism.languages['web-idl'];
  89. }(Prism));