nix.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """
  2. pygments.lexers.nix
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for the NixOS Nix language.
  5. :copyright: Copyright 2006-2021 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 <http://nixos.org/nix/>`_.
  16. .. versionadded:: 2.0
  17. """
  18. name = 'Nix'
  19. aliases = ['nixos', 'nix']
  20. filenames = ['*.nix']
  21. mimetypes = ['text/x-nix']
  22. flags = re.MULTILINE | re.UNICODE
  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. ('(%s)' % '|'.join(re.escape(entry) + '\\b' for entry in keywords), Keyword),
  40. # highlight the builtins
  41. ('(%s)' % '|'.join(re.escape(entry) + '\\b' for entry in builtins),
  42. Name.Builtin),
  43. (r'\b(true|false|null)\b', Name.Constant),
  44. # operators
  45. ('(%s)' % '|'.join(re.escape(entry) for entry in operators),
  46. Operator),
  47. # word operators
  48. (r'\b(or|and)\b', Operator.Word),
  49. # punctuations
  50. ('(%s)' % '|'.join(re.escape(entry) for entry in punctuations), Punctuation),
  51. # integers
  52. (r'[0-9]+', Number.Integer),
  53. # strings
  54. (r'"', String.Double, 'doublequote'),
  55. (r"''", String.Single, 'singlequote'),
  56. # paths
  57. (r'[\w.+-]*(\/[\w.+-]+)+', Literal),
  58. (r'\<[\w.+-]+(\/[\w.+-]+)*\>', Literal),
  59. # urls
  60. (r'[a-zA-Z][a-zA-Z0-9\+\-\.]*\:[\w%/?:@&=+$,\\.!~*\'-]+', Literal),
  61. # names of variables
  62. (r'[\w-]+\s*=', String.Symbol),
  63. (r'[a-zA-Z_][\w\'-]*', Text),
  64. ],
  65. 'comment': [
  66. (r'[^/*]+', Comment.Multiline),
  67. (r'/\*', Comment.Multiline, '#push'),
  68. (r'\*/', Comment.Multiline, '#pop'),
  69. (r'[*/]', Comment.Multiline),
  70. ],
  71. 'singlequote': [
  72. (r"'''", String.Escape),
  73. (r"''\$\{", String.Escape),
  74. (r"''\n", String.Escape),
  75. (r"''\r", String.Escape),
  76. (r"''\t", String.Escape),
  77. (r"''", String.Single, '#pop'),
  78. (r'\$\{', String.Interpol, 'antiquote'),
  79. (r"[^']", String.Single),
  80. ],
  81. 'doublequote': [
  82. (r'\\', String.Escape),
  83. (r'\\"', String.Escape),
  84. (r'\\$\{', String.Escape),
  85. (r'"', String.Double, '#pop'),
  86. (r'\$\{', String.Interpol, 'antiquote'),
  87. (r'[^"]', String.Double),
  88. ],
  89. 'antiquote': [
  90. (r"\}", String.Interpol, '#pop'),
  91. # TODO: we should probably escape also here ''${ \${
  92. (r"\$\{", String.Interpol, '#push'),
  93. include('root'),
  94. ],
  95. }
  96. def analyse_text(text):
  97. rv = 0.0
  98. # TODO: let/in
  99. if re.search(r'import.+?<[^>]+>', text):
  100. rv += 0.4
  101. if re.search(r'mkDerivation\s+(\(|\{|rec)', text):
  102. rv += 0.4
  103. if re.search(r'=\s+mkIf\s+', text):
  104. rv += 0.4
  105. if re.search(r'\{[a-zA-Z,\s]+\}:', text):
  106. rv += 0.1
  107. return rv