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