ini.js 3.1 KB

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