abnf.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {...(RegExp | string) } args
  16. * @returns {string}
  17. */
  18. function concat(...args) {
  19. const joined = args.map((x) => source(x)).join("");
  20. return joined;
  21. }
  22. /*
  23. Language: Augmented Backus-Naur Form
  24. Author: Alex McKibben <alex@nullscope.net>
  25. Website: https://tools.ietf.org/html/rfc5234
  26. Audit: 2020
  27. */
  28. /** @type LanguageFn */
  29. function abnf(hljs) {
  30. const regexes = {
  31. ruleDeclaration: /^[a-zA-Z][a-zA-Z0-9-]*/,
  32. unexpectedChars: /[!@#$^&',?+~`|:]/
  33. };
  34. const keywords = [
  35. "ALPHA",
  36. "BIT",
  37. "CHAR",
  38. "CR",
  39. "CRLF",
  40. "CTL",
  41. "DIGIT",
  42. "DQUOTE",
  43. "HEXDIG",
  44. "HTAB",
  45. "LF",
  46. "LWSP",
  47. "OCTET",
  48. "SP",
  49. "VCHAR",
  50. "WSP"
  51. ];
  52. const commentMode = hljs.COMMENT(/;/, /$/);
  53. const terminalBinaryMode = {
  54. className: "symbol",
  55. begin: /%b[0-1]+(-[0-1]+|(\.[0-1]+)+){0,1}/
  56. };
  57. const terminalDecimalMode = {
  58. className: "symbol",
  59. begin: /%d[0-9]+(-[0-9]+|(\.[0-9]+)+){0,1}/
  60. };
  61. const terminalHexadecimalMode = {
  62. className: "symbol",
  63. begin: /%x[0-9A-F]+(-[0-9A-F]+|(\.[0-9A-F]+)+){0,1}/
  64. };
  65. const caseSensitivityIndicatorMode = {
  66. className: "symbol",
  67. begin: /%[si]/
  68. };
  69. const ruleDeclarationMode = {
  70. className: "attribute",
  71. begin: concat(regexes.ruleDeclaration, /(?=\s*=)/)
  72. };
  73. return {
  74. name: 'Augmented Backus-Naur Form',
  75. illegal: regexes.unexpectedChars,
  76. keywords: keywords,
  77. contains: [
  78. ruleDeclarationMode,
  79. commentMode,
  80. terminalBinaryMode,
  81. terminalDecimalMode,
  82. terminalHexadecimalMode,
  83. caseSensitivityIndicatorMode,
  84. hljs.QUOTE_STRING_MODE,
  85. hljs.NUMBER_MODE
  86. ]
  87. };
  88. }
  89. module.exports = abnf;