jsonnet.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. """
  2. pygments.lexers.jsonnet
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Jsonnet data templating language.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import include, RegexLexer, words
  9. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  10. Punctuation, String, Text, Whitespace
  11. __all__ = ['JsonnetLexer']
  12. jsonnet_token = r'[^\W\d]\w*'
  13. jsonnet_function_token = jsonnet_token + r'(?=\()'
  14. def string_rules(quote_mark):
  15. return [
  16. (rf"[^{quote_mark}\\]", String),
  17. (r"\\.", String.Escape),
  18. (quote_mark, String, '#pop'),
  19. ]
  20. def quoted_field_name(quote_mark):
  21. return [
  22. (rf'([^{quote_mark}\\]|\\.)*{quote_mark}',
  23. Name.Variable, 'field_separator')
  24. ]
  25. class JsonnetLexer(RegexLexer):
  26. """Lexer for Jsonnet source code."""
  27. name = 'Jsonnet'
  28. aliases = ['jsonnet']
  29. filenames = ['*.jsonnet', '*.libsonnet']
  30. url = "https://jsonnet.org"
  31. version_added = ''
  32. tokens = {
  33. # Not used by itself
  34. '_comments': [
  35. (r'(//|#).*\n', Comment.Single),
  36. (r'/\*\*([^/]|/(?!\*))*\*/', String.Doc),
  37. (r'/\*([^/]|/(?!\*))*\*/', Comment),
  38. ],
  39. 'root': [
  40. include('_comments'),
  41. (r"@'.*'", String),
  42. (r'@".*"', String),
  43. (r"'", String, 'singlestring'),
  44. (r'"', String, 'doublestring'),
  45. (r'\|\|\|(.|\n)*\|\|\|', String),
  46. # Jsonnet has no integers, only an IEEE754 64-bit float
  47. (r'[+-]?[0-9]+(.[0-9])?', Number.Float),
  48. # Omit : despite spec because it appears to be used as a field
  49. # separator
  50. (r'[!$~+\-&|^=<>*/%]', Operator),
  51. (r'\{', Punctuation, 'object'),
  52. (r'\[', Punctuation, 'array'),
  53. (r'local\b', Keyword, ('local_name')),
  54. (r'assert\b', Keyword, 'assert'),
  55. (words([
  56. 'assert', 'else', 'error', 'false', 'for', 'if', 'import',
  57. 'importstr', 'in', 'null', 'tailstrict', 'then', 'self',
  58. 'super', 'true',
  59. ], suffix=r'\b'), Keyword),
  60. (r'\s+', Whitespace),
  61. (r'function(?=\()', Keyword, 'function_params'),
  62. (r'std\.' + jsonnet_function_token, Name.Builtin, 'function_args'),
  63. (jsonnet_function_token, Name.Function, 'function_args'),
  64. (jsonnet_token, Name.Variable),
  65. (r'[\.()]', Punctuation),
  66. ],
  67. 'singlestring': string_rules("'"),
  68. 'doublestring': string_rules('"'),
  69. 'array': [
  70. (r',', Punctuation),
  71. (r'\]', Punctuation, '#pop'),
  72. include('root'),
  73. ],
  74. 'local_name': [
  75. (jsonnet_function_token, Name.Function, 'function_params'),
  76. (jsonnet_token, Name.Variable),
  77. (r'\s+', Whitespace),
  78. ('(?==)', Whitespace, ('#pop', 'local_value')),
  79. ],
  80. 'local_value': [
  81. (r'=', Operator),
  82. (r';', Punctuation, '#pop'),
  83. include('root'),
  84. ],
  85. 'assert': [
  86. (r':', Punctuation),
  87. (r';', Punctuation, '#pop'),
  88. include('root'),
  89. ],
  90. 'function_params': [
  91. (jsonnet_token, Name.Variable),
  92. (r'\(', Punctuation),
  93. (r'\)', Punctuation, '#pop'),
  94. (r',', Punctuation),
  95. (r'\s+', Whitespace),
  96. (r'=', Operator, 'function_param_default'),
  97. ],
  98. 'function_args': [
  99. (r'\(', Punctuation),
  100. (r'\)', Punctuation, '#pop'),
  101. (r',', Punctuation),
  102. (r'\s+', Whitespace),
  103. include('root'),
  104. ],
  105. 'object': [
  106. (r'\s+', Whitespace),
  107. (r'local\b', Keyword, 'object_local_name'),
  108. (r'assert\b', Keyword, 'object_assert'),
  109. (r'\[', Operator, 'field_name_expr'),
  110. (fr'(?={jsonnet_token})', Text, 'field_name'),
  111. (r'\}', Punctuation, '#pop'),
  112. (r'"', Name.Variable, 'double_field_name'),
  113. (r"'", Name.Variable, 'single_field_name'),
  114. include('_comments'),
  115. ],
  116. 'field_name': [
  117. (jsonnet_function_token, Name.Function,
  118. ('field_separator', 'function_params')
  119. ),
  120. (jsonnet_token, Name.Variable, 'field_separator'),
  121. ],
  122. 'double_field_name': quoted_field_name('"'),
  123. 'single_field_name': quoted_field_name("'"),
  124. 'field_name_expr': [
  125. (r'\]', Operator, 'field_separator'),
  126. include('root'),
  127. ],
  128. 'function_param_default': [
  129. (r'(?=[,\)])', Whitespace, '#pop'),
  130. include('root'),
  131. ],
  132. 'field_separator': [
  133. (r'\s+', Whitespace),
  134. (r'\+?::?:?', Punctuation, ('#pop', '#pop', 'field_value')),
  135. include('_comments'),
  136. ],
  137. 'field_value': [
  138. (r',', Punctuation, '#pop'),
  139. (r'\}', Punctuation, '#pop:2'),
  140. include('root'),
  141. ],
  142. 'object_assert': [
  143. (r':', Punctuation),
  144. (r',', Punctuation, '#pop'),
  145. include('root'),
  146. ],
  147. 'object_local_name': [
  148. (jsonnet_token, Name.Variable, ('#pop', 'object_local_value')),
  149. (r'\s+', Whitespace),
  150. ],
  151. 'object_local_value': [
  152. (r'=', Operator),
  153. (r',', Punctuation, '#pop'),
  154. (r'\}', Punctuation, '#pop:2'),
  155. include('root'),
  156. ],
  157. }