ini.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Language: TOML, also INI
  3. Description: TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics.
  4. Contributors: Guillaume Gomez <guillaume1.gomez@gmail.com>
  5. Category: common, config
  6. Website: https://github.com/toml-lang/toml
  7. */
  8. function ini(hljs) {
  9. const regex = hljs.regex;
  10. const NUMBERS = {
  11. className: 'number',
  12. relevance: 0,
  13. variants: [
  14. { begin: /([+-]+)?[\d]+_[\d_]+/ },
  15. { begin: hljs.NUMBER_RE }
  16. ]
  17. };
  18. const COMMENTS = hljs.COMMENT();
  19. COMMENTS.variants = [
  20. {
  21. begin: /;/,
  22. end: /$/
  23. },
  24. {
  25. begin: /#/,
  26. end: /$/
  27. }
  28. ];
  29. const VARIABLES = {
  30. className: 'variable',
  31. variants: [
  32. { begin: /\$[\w\d"][\w\d_]*/ },
  33. { begin: /\$\{(.*?)\}/ }
  34. ]
  35. };
  36. const LITERALS = {
  37. className: 'literal',
  38. begin: /\bon|off|true|false|yes|no\b/
  39. };
  40. const STRINGS = {
  41. className: "string",
  42. contains: [ hljs.BACKSLASH_ESCAPE ],
  43. variants: [
  44. {
  45. begin: "'''",
  46. end: "'''",
  47. relevance: 10
  48. },
  49. {
  50. begin: '"""',
  51. end: '"""',
  52. relevance: 10
  53. },
  54. {
  55. begin: '"',
  56. end: '"'
  57. },
  58. {
  59. begin: "'",
  60. end: "'"
  61. }
  62. ]
  63. };
  64. const ARRAY = {
  65. begin: /\[/,
  66. end: /\]/,
  67. contains: [
  68. COMMENTS,
  69. LITERALS,
  70. VARIABLES,
  71. STRINGS,
  72. NUMBERS,
  73. 'self'
  74. ],
  75. relevance: 0
  76. };
  77. const BARE_KEY = /[A-Za-z0-9_-]+/;
  78. const QUOTED_KEY_DOUBLE_QUOTE = /"(\\"|[^"])*"/;
  79. const QUOTED_KEY_SINGLE_QUOTE = /'[^']*'/;
  80. const ANY_KEY = regex.either(
  81. BARE_KEY, QUOTED_KEY_DOUBLE_QUOTE, QUOTED_KEY_SINGLE_QUOTE
  82. );
  83. const DOTTED_KEY = regex.concat(
  84. ANY_KEY, '(\\s*\\.\\s*', ANY_KEY, ')*',
  85. regex.lookahead(/\s*=\s*[^#\s]/)
  86. );
  87. return {
  88. name: 'TOML, also INI',
  89. aliases: [ 'toml' ],
  90. case_insensitive: true,
  91. illegal: /\S/,
  92. contains: [
  93. COMMENTS,
  94. {
  95. className: 'section',
  96. begin: /\[+/,
  97. end: /\]+/
  98. },
  99. {
  100. begin: DOTTED_KEY,
  101. className: 'attr',
  102. starts: {
  103. end: /$/,
  104. contains: [
  105. COMMENTS,
  106. ARRAY,
  107. LITERALS,
  108. VARIABLES,
  109. STRINGS,
  110. NUMBERS
  111. ]
  112. }
  113. }
  114. ]
  115. };
  116. }
  117. export { ini as default };