jsonnet.py 5.5 KB

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