jsonnet.py 5.4 KB

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