diff options
Diffstat (limited to 'pygments/lexers/functional.py')
-rw-r--r-- | pygments/lexers/functional.py | 125 |
1 files changed, 124 insertions, 1 deletions
diff --git a/pygments/lexers/functional.py b/pygments/lexers/functional.py index 770e6bd9..cb9a4a7f 100644 --- a/pygments/lexers/functional.py +++ b/pygments/lexers/functional.py @@ -18,7 +18,7 @@ from pygments.token import Text, Comment, Operator, Keyword, Name, \ __all__ = ['RacketLexer', 'SchemeLexer', 'CommonLispLexer', 'HaskellLexer', 'AgdaLexer', 'LiterateHaskellLexer', 'LiterateAgdaLexer', 'SMLLexer', 'OcamlLexer', 'ErlangLexer', 'ErlangShellLexer', - 'OpaLexer', 'CoqLexer', 'NewLispLexer', 'ElixirLexer', + 'OpaLexer', 'CoqLexer', 'NewLispLexer', 'NixLexer', 'ElixirLexer', 'ElixirConsoleLexer', 'KokaLexer'] @@ -2359,6 +2359,129 @@ class NewLispLexer(RegexLexer): } +class NixLexer(RegexLexer): + """ + For the `Nix language <http://nixos.org/nix/>`_. + + *New in Pygments 1.7.* + """ + + name = 'Nix' + aliases = ['nixos'] + filenames = ['*.nix'] + mimetypes = ['text/x-nix'] + + flags = re.MULTILINE | re.UNICODE + + keywords = ['rec', 'with', 'let', 'in', 'inherit', 'assert', 'if', + 'else', 'then', '...'] + builtins = ['import', 'abort', 'baseNameOf', 'dirOf', 'isNull', 'builtins', + 'map', 'removeAttrs', 'throw', 'toString', 'derivation'] + operators = ['++', '+', '?', '.', '!', '//', '==', + '!=', '&&', '||', '->', '='] + + tokens = { + 'root': [ + # comments starting with # + (r'#.*$', Comment.Single), + + # multiline comments + (r'/\*', Comment.Multiline, 'comment'), + + # whitespace + (r'\s+', Text), + + # keywords + ('(%s)' % '|'.join(re.escape(entry) + '\\b' for entry in keywords), Keyword), + + # highlight the builtins + ('(%s)' % '|'.join(re.escape(entry) for entry in builtins), + Name.Builtin), + + (r'\b(true|false)\b', Name.Constant), + + # operators + ('(%s)' % '|'.join(re.escape(entry) for entry in operators), + Operator), + + # word operators + (r'(or|and)', Operator.Word), + + + # punctuations + (r'(\(|\)|\[|\]|;|\{|\}|:|@|,)', Punctuation), + + # semicolons + (r';', Punctuation), + + # integers + (r'[0-9]+', Number.Integer), + + # strings + (r'"', String.Double, 'doublequote'), + (r"''", String.Single, 'singlequote'), + + # paths + (r'[a-zA-Z0-9\.\_\-\+]*(\/[a-zA-Z0-9\.\_\-\+]+)+', Literal), + (r'\<[a-zA-Z0-9\.\_\-\+]+(\/[a-zA-Z0-9\.\_\-\+]+)*\>', Literal), + + # urls + (r'[a-zA-Z][a-zA-Z0-9\+\-\.]*\:[a-zA-Z0-9\%\/\?\:\@\&\=\+\$\,\-\_\.\!\~\*\']+', Literal), + + # names of variables + (r'[a-zA-Z\_][a-zA-Z0-9\_\'\-]*', String.Symbol), + + ], + 'comment': [ + (r'[^/\*]+', Comment.Multiline), + (r'/\*', Comment.Multiline, '#push'), + (r'\*/', Comment.Multiline, '#pop'), + (r'[\*/]', Comment.Multiline), + ], + 'singlequote': [ + (r"'''", String.Escape), + (r"''\$\{", String.Escape), + (r"''\n", String.Escape), + (r"''\r", String.Escape), + (r"''\t", String.Escape), + (r"''", String.Single, '#pop'), + (r'\$\{', String.Interpol, 'antiquote'), + (r"[^']", String.Single), + ], + 'doublequote': [ + (r'\\', String.Escape), + (r'\\"', String.Escape), + (r'\\${', String.Escape), + (r'"', String.Double, '#pop'), + (r'\$\{', String.Interpol, 'antiquote'), + (r'[^"]', String.Double), + ], + 'antiquote': [ + (r"}", String.Interpol, '#pop'), + # TODO: we should probably escape also here ''${ \${ + (r"\$\{", String.Interpol, '#push'), + include('root'), + ], + } + + def analyse_text(text): + rv = 0.0 + # TODO: let/in + if re.search(r'import.+?<[^>]+>', text) is not None: + rv += 0.4 + if re.search(r'mkDerivation\s+(\(|\{|rec)', text) is not None: + rv += 0.4 + if re.search(r'with\s+[a-zA-Z\.]+;', text) is not None: + rv += 0.2 + if re.search(r'inherit\s+[a-zA-Z()\.];', text) is not None: + rv += 0.2 + if re.search(r'=\s+mkIf\s+', text) is not None: + rv += 0.4 + if re.search(r'\{[a-zA-Z,\s]+\}:', text) is not None: + rv += 0.1 + return rv + + class ElixirLexer(RegexLexer): """ For the `Elixir language <http://elixir-lang.org>`_. |