nix.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. pygments.lexers.nix
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for the NixOS Nix language.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Literal
  12. __all__ = ['NixLexer']
  13. class NixLexer(RegexLexer):
  14. """
  15. For the Nix language.
  16. """
  17. name = 'Nix'
  18. url = 'http://nixos.org/nix/'
  19. aliases = ['nixos', 'nix']
  20. filenames = ['*.nix']
  21. mimetypes = ['text/x-nix']
  22. version_added = '2.0'
  23. keywords = ['rec', 'with', 'let', 'in', 'inherit', 'assert', 'if',
  24. 'else', 'then', '...']
  25. builtins = ['import', 'abort', 'baseNameOf', 'dirOf', 'isNull', 'builtins',
  26. 'map', 'removeAttrs', 'throw', 'toString', 'derivation']
  27. operators = ['++', '+', '?', '.', '!', '//', '==', '/',
  28. '!=', '&&', '||', '->', '=', '<', '>', '*', '-']
  29. punctuations = ["(", ")", "[", "]", ";", "{", "}", ":", ",", "@"]
  30. tokens = {
  31. 'root': [
  32. # comments starting with #
  33. (r'#.*$', Comment.Single),
  34. # multiline comments
  35. (r'/\*', Comment.Multiline, 'comment'),
  36. # whitespace
  37. (r'\s+', Text),
  38. # keywords
  39. ('({})'.format('|'.join(re.escape(entry) + '\\b' for entry in keywords)), Keyword),
  40. # highlight the builtins
  41. ('({})'.format('|'.join(re.escape(entry) + '\\b' for entry in builtins)),
  42. Name.Builtin),
  43. (r'\b(true|false|null)\b', Name.Constant),
  44. # floats
  45. (r'-?(\d+\.\d*|\.\d+)([eE][-+]?\d+)?', Number.Float),
  46. # integers
  47. (r'-?[0-9]+', Number.Integer),
  48. # paths
  49. (r'[\w.+-]*(\/[\w.+-]+)+', Literal),
  50. (r'~(\/[\w.+-]+)+', Literal),
  51. (r'\<[\w.+-]+(\/[\w.+-]+)*\>', Literal),
  52. # operators
  53. ('({})'.format('|'.join(re.escape(entry) for entry in operators)),
  54. Operator),
  55. # word operators
  56. (r'\b(or|and)\b', Operator.Word),
  57. (r'\{', Punctuation, 'block'),
  58. # punctuations
  59. ('({})'.format('|'.join(re.escape(entry) for entry in punctuations)), Punctuation),
  60. # strings
  61. (r'"', String.Double, 'doublequote'),
  62. (r"''", String.Multiline, 'multiline'),
  63. # urls
  64. (r'[a-zA-Z][a-zA-Z0-9\+\-\.]*\:[\w%/?:@&=+$,\\.!~*\'-]+', Literal),
  65. # names of variables
  66. (r'[\w-]+(?=\s*=)', String.Symbol),
  67. (r'[a-zA-Z_][\w\'-]*', Text),
  68. (r"\$\{", String.Interpol, 'antiquote'),
  69. ],
  70. 'comment': [
  71. (r'[^/*]+', Comment.Multiline),
  72. (r'/\*', Comment.Multiline, '#push'),
  73. (r'\*/', Comment.Multiline, '#pop'),
  74. (r'[*/]', Comment.Multiline),
  75. ],
  76. 'multiline': [
  77. (r"''(\$|'|\\n|\\r|\\t|\\)", String.Escape),
  78. (r"''", String.Multiline, '#pop'),
  79. (r'\$\{', String.Interpol, 'antiquote'),
  80. (r"[^'\$]+", String.Multiline),
  81. (r"\$[^\{']", String.Multiline),
  82. (r"'[^']", String.Multiline),
  83. (r"\$(?=')", String.Multiline),
  84. ],
  85. 'doublequote': [
  86. (r'\\(\\|"|\$|n)', String.Escape),
  87. (r'"', String.Double, '#pop'),
  88. (r'\$\{', String.Interpol, 'antiquote'),
  89. (r'[^"\\\$]+', String.Double),
  90. (r'\$[^\{"]', String.Double),
  91. (r'\$(?=")', String.Double),
  92. (r'\\', String.Double),
  93. ],
  94. 'antiquote': [
  95. (r"\}", String.Interpol, '#pop'),
  96. # TODO: we should probably escape also here ''${ \${
  97. (r"\$\{", String.Interpol, '#push'),
  98. include('root'),
  99. ],
  100. 'block': [
  101. (r"\}", Punctuation, '#pop'),
  102. include('root'),
  103. ],
  104. }
  105. def analyse_text(text):
  106. rv = 0.0
  107. # TODO: let/in
  108. if re.search(r'import.+?<[^>]+>', text):
  109. rv += 0.4
  110. if re.search(r'mkDerivation\s+(\(|\{|rec)', text):
  111. rv += 0.4
  112. if re.search(r'=\s+mkIf\s+', text):
  113. rv += 0.4
  114. if re.search(r'\{[a-zA-Z,\s]+\}:', text):
  115. rv += 0.1
  116. return rv