[d24f17c] | 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 } re
|
---|
| 19 | * @returns {string}
|
---|
| 20 | */
|
---|
| 21 | function lookahead(re) {
|
---|
| 22 | return concat('(?=', re, ')');
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * @param {...(RegExp | string) } args
|
---|
| 27 | * @returns {string}
|
---|
| 28 | */
|
---|
| 29 | function concat(...args) {
|
---|
| 30 | const joined = args.map((x) => source(x)).join("");
|
---|
| 31 | return joined;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Any of the passed expresssions may match
|
---|
| 36 | *
|
---|
| 37 | * Creates a huge this | this | that | that match
|
---|
| 38 | * @param {(RegExp | string)[] } args
|
---|
| 39 | * @returns {string}
|
---|
| 40 | */
|
---|
| 41 | function either(...args) {
|
---|
| 42 | const joined = '(' + args.map((x) => source(x)).join("|") + ")";
|
---|
| 43 | return joined;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | Language: TOML, also INI
|
---|
| 48 | Description: TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics.
|
---|
| 49 | Contributors: Guillaume Gomez <guillaume1.gomez@gmail.com>
|
---|
| 50 | Category: common, config
|
---|
| 51 | Website: https://github.com/toml-lang/toml
|
---|
| 52 | */
|
---|
| 53 |
|
---|
| 54 | function ini(hljs) {
|
---|
| 55 | const NUMBERS = {
|
---|
| 56 | className: 'number',
|
---|
| 57 | relevance: 0,
|
---|
| 58 | variants: [
|
---|
| 59 | {
|
---|
| 60 | begin: /([+-]+)?[\d]+_[\d_]+/
|
---|
| 61 | },
|
---|
| 62 | {
|
---|
| 63 | begin: hljs.NUMBER_RE
|
---|
| 64 | }
|
---|
| 65 | ]
|
---|
| 66 | };
|
---|
| 67 | const COMMENTS = hljs.COMMENT();
|
---|
| 68 | COMMENTS.variants = [
|
---|
| 69 | {
|
---|
| 70 | begin: /;/,
|
---|
| 71 | end: /$/
|
---|
| 72 | },
|
---|
| 73 | {
|
---|
| 74 | begin: /#/,
|
---|
| 75 | end: /$/
|
---|
| 76 | }
|
---|
| 77 | ];
|
---|
| 78 | const VARIABLES = {
|
---|
| 79 | className: 'variable',
|
---|
| 80 | variants: [
|
---|
| 81 | {
|
---|
| 82 | begin: /\$[\w\d"][\w\d_]*/
|
---|
| 83 | },
|
---|
| 84 | {
|
---|
| 85 | begin: /\$\{(.*?)\}/
|
---|
| 86 | }
|
---|
| 87 | ]
|
---|
| 88 | };
|
---|
| 89 | const LITERALS = {
|
---|
| 90 | className: 'literal',
|
---|
| 91 | begin: /\bon|off|true|false|yes|no\b/
|
---|
| 92 | };
|
---|
| 93 | const STRINGS = {
|
---|
| 94 | className: "string",
|
---|
| 95 | contains: [hljs.BACKSLASH_ESCAPE],
|
---|
| 96 | variants: [
|
---|
| 97 | {
|
---|
| 98 | begin: "'''",
|
---|
| 99 | end: "'''",
|
---|
| 100 | relevance: 10
|
---|
| 101 | },
|
---|
| 102 | {
|
---|
| 103 | begin: '"""',
|
---|
| 104 | end: '"""',
|
---|
| 105 | relevance: 10
|
---|
| 106 | },
|
---|
| 107 | {
|
---|
| 108 | begin: '"',
|
---|
| 109 | end: '"'
|
---|
| 110 | },
|
---|
| 111 | {
|
---|
| 112 | begin: "'",
|
---|
| 113 | end: "'"
|
---|
| 114 | }
|
---|
| 115 | ]
|
---|
| 116 | };
|
---|
| 117 | const ARRAY = {
|
---|
| 118 | begin: /\[/,
|
---|
| 119 | end: /\]/,
|
---|
| 120 | contains: [
|
---|
| 121 | COMMENTS,
|
---|
| 122 | LITERALS,
|
---|
| 123 | VARIABLES,
|
---|
| 124 | STRINGS,
|
---|
| 125 | NUMBERS,
|
---|
| 126 | 'self'
|
---|
| 127 | ],
|
---|
| 128 | relevance: 0
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | const BARE_KEY = /[A-Za-z0-9_-]+/;
|
---|
| 132 | const QUOTED_KEY_DOUBLE_QUOTE = /"(\\"|[^"])*"/;
|
---|
| 133 | const QUOTED_KEY_SINGLE_QUOTE = /'[^']*'/;
|
---|
| 134 | const ANY_KEY = either(
|
---|
| 135 | BARE_KEY, QUOTED_KEY_DOUBLE_QUOTE, QUOTED_KEY_SINGLE_QUOTE
|
---|
| 136 | );
|
---|
| 137 | const DOTTED_KEY = concat(
|
---|
| 138 | ANY_KEY, '(\\s*\\.\\s*', ANY_KEY, ')*',
|
---|
| 139 | lookahead(/\s*=\s*[^#\s]/)
|
---|
| 140 | );
|
---|
| 141 |
|
---|
| 142 | return {
|
---|
| 143 | name: 'TOML, also INI',
|
---|
| 144 | aliases: ['toml'],
|
---|
| 145 | case_insensitive: true,
|
---|
| 146 | illegal: /\S/,
|
---|
| 147 | contains: [
|
---|
| 148 | COMMENTS,
|
---|
| 149 | {
|
---|
| 150 | className: 'section',
|
---|
| 151 | begin: /\[+/,
|
---|
| 152 | end: /\]+/
|
---|
| 153 | },
|
---|
| 154 | {
|
---|
| 155 | begin: DOTTED_KEY,
|
---|
| 156 | className: 'attr',
|
---|
| 157 | starts: {
|
---|
| 158 | end: /$/,
|
---|
| 159 | contains: [
|
---|
| 160 | COMMENTS,
|
---|
| 161 | ARRAY,
|
---|
| 162 | LITERALS,
|
---|
| 163 | VARIABLES,
|
---|
| 164 | STRINGS,
|
---|
| 165 | NUMBERS
|
---|
| 166 | ]
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | ]
|
---|
| 170 | };
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | module.exports = ini;
|
---|