hcl.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export var conf = {
  6. comments: {
  7. lineComment: '//',
  8. blockComment: ['/*', '*/']
  9. },
  10. brackets: [
  11. ['{', '}'],
  12. ['[', ']'],
  13. ['(', ')']
  14. ],
  15. autoClosingPairs: [
  16. { open: '{', close: '}' },
  17. { open: '[', close: ']' },
  18. { open: '(', close: ')' },
  19. { open: '"', close: '"', notIn: ['string'] }
  20. ],
  21. surroundingPairs: [
  22. { open: '{', close: '}' },
  23. { open: '[', close: ']' },
  24. { open: '(', close: ')' },
  25. { open: '"', close: '"' }
  26. ]
  27. };
  28. export var language = {
  29. defaultToken: '',
  30. tokenPostfix: '.hcl',
  31. keywords: [
  32. 'var',
  33. 'local',
  34. 'path',
  35. 'for_each',
  36. 'any',
  37. 'string',
  38. 'number',
  39. 'bool',
  40. 'true',
  41. 'false',
  42. 'null',
  43. 'if ',
  44. 'else ',
  45. 'endif ',
  46. 'for ',
  47. 'in',
  48. 'endfor'
  49. ],
  50. operators: [
  51. '=',
  52. '>=',
  53. '<=',
  54. '==',
  55. '!=',
  56. '+',
  57. '-',
  58. '*',
  59. '/',
  60. '%',
  61. '&&',
  62. '||',
  63. '!',
  64. '<',
  65. '>',
  66. '?',
  67. '...',
  68. ':'
  69. ],
  70. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  71. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  72. terraformFunctions: /(abs|ceil|floor|log|max|min|pow|signum|chomp|format|formatlist|indent|join|lower|regex|regexall|replace|split|strrev|substr|title|trimspace|upper|chunklist|coalesce|coalescelist|compact|concat|contains|distinct|element|flatten|index|keys|length|list|lookup|map|matchkeys|merge|range|reverse|setintersection|setproduct|setunion|slice|sort|transpose|values|zipmap|base64decode|base64encode|base64gzip|csvdecode|jsondecode|jsonencode|urlencode|yamldecode|yamlencode|abspath|dirname|pathexpand|basename|file|fileexists|fileset|filebase64|templatefile|formatdate|timeadd|timestamp|base64sha256|base64sha512|bcrypt|filebase64sha256|filebase64sha512|filemd5|filemd1|filesha256|filesha512|md5|rsadecrypt|sha1|sha256|sha512|uuid|uuidv5|cidrhost|cidrnetmask|cidrsubnet|tobool|tolist|tomap|tonumber|toset|tostring)/,
  73. terraformMainBlocks: /(module|data|terraform|resource|provider|variable|output|locals)/,
  74. tokenizer: {
  75. root: [
  76. // highlight main blocks
  77. [
  78. /^@terraformMainBlocks([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)(\{)/,
  79. ['type', '', 'string', '', 'string', '', '@brackets']
  80. ],
  81. // highlight all the remaining blocks
  82. [
  83. /(\w+[ \t]+)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)(\{)/,
  84. ['identifier', '', 'string', '', 'string', '', '@brackets']
  85. ],
  86. // highlight block
  87. [
  88. /(\w+[ \t]+)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)([\w-]+|"[\w-]+"|)(=)(\{)/,
  89. ['identifier', '', 'string', '', 'operator', '', '@brackets']
  90. ],
  91. // terraform general highlight - shared with expressions
  92. { include: '@terraform' }
  93. ],
  94. terraform: [
  95. // highlight terraform functions
  96. [/@terraformFunctions(\()/, ['type', '@brackets']],
  97. // all other words are variables or keywords
  98. [
  99. /[a-zA-Z_]\w*-*/,
  100. {
  101. cases: {
  102. '@keywords': { token: 'keyword.$0' },
  103. '@default': 'variable'
  104. }
  105. }
  106. ],
  107. { include: '@whitespace' },
  108. { include: '@heredoc' },
  109. // delimiters and operators
  110. [/[{}()\[\]]/, '@brackets'],
  111. [/[<>](?!@symbols)/, '@brackets'],
  112. [
  113. /@symbols/,
  114. {
  115. cases: {
  116. '@operators': 'operator',
  117. '@default': ''
  118. }
  119. }
  120. ],
  121. // numbers
  122. [/\d*\d+[eE]([\-+]?\d+)?/, 'number.float'],
  123. [/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float'],
  124. [/\d[\d']*/, 'number'],
  125. [/\d/, 'number'],
  126. [/[;,.]/, 'delimiter'],
  127. // strings
  128. [/"/, 'string', '@string'],
  129. [/'/, 'invalid']
  130. ],
  131. heredoc: [
  132. [
  133. /<<[-]*\s*["]?([\w\-]+)["]?/,
  134. { token: 'string.heredoc.delimiter', next: '@heredocBody.$1' }
  135. ]
  136. ],
  137. heredocBody: [
  138. [
  139. /([\w\-]+)$/,
  140. {
  141. cases: {
  142. '$1==$S2': [
  143. {
  144. token: 'string.heredoc.delimiter',
  145. next: '@popall'
  146. }
  147. ],
  148. '@default': 'string.heredoc'
  149. }
  150. }
  151. ],
  152. [/./, 'string.heredoc']
  153. ],
  154. whitespace: [
  155. [/[ \t\r\n]+/, ''],
  156. [/\/\*/, 'comment', '@comment'],
  157. [/\/\/.*$/, 'comment'],
  158. [/#.*$/, 'comment']
  159. ],
  160. comment: [
  161. [/[^\/*]+/, 'comment'],
  162. [/\*\//, 'comment', '@pop'],
  163. [/[\/*]/, 'comment']
  164. ],
  165. string: [
  166. [/\$\{/, { token: 'delimiter', next: '@stringExpression' }],
  167. [/[^\\"\$]+/, 'string'],
  168. [/@escapes/, 'string.escape'],
  169. [/\\./, 'string.escape.invalid'],
  170. [/"/, 'string', '@popall']
  171. ],
  172. stringInsideExpression: [
  173. [/[^\\"]+/, 'string'],
  174. [/@escapes/, 'string.escape'],
  175. [/\\./, 'string.escape.invalid'],
  176. [/"/, 'string', '@pop']
  177. ],
  178. stringExpression: [
  179. [/\}/, { token: 'delimiter', next: '@pop' }],
  180. [/"/, 'string', '@stringInsideExpression'],
  181. { include: '@terraform' }
  182. ]
  183. }
  184. };