3 pygments.lexers.ml |
3 pygments.lexers.ml |
4 ~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for ML family languages. |
6 Lexers for ML family languages. |
7 |
7 |
8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import re |
12 import re |
13 |
13 |
14 from pygments.lexer import RegexLexer, include, bygroups, default, words |
14 from pygments.lexer import RegexLexer, include, bygroups, default, words |
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
16 Number, Punctuation, Error |
16 Number, Punctuation, Error |
17 |
17 |
18 __all__ = ['SMLLexer', 'OcamlLexer', 'OpaLexer', 'ReasonLexer'] |
18 __all__ = ['SMLLexer', 'OcamlLexer', 'OpaLexer', 'ReasonLexer', 'FStarLexer'] |
19 |
19 |
20 |
20 |
21 class SMLLexer(RegexLexer): |
21 class SMLLexer(RegexLexer): |
22 """ |
22 """ |
23 For the Standard ML language. |
23 For the Standard ML language. |
778 aliases = ['reason', "reasonml"] |
779 aliases = ['reason', "reasonml"] |
779 filenames = ['*.re', '*.rei'] |
780 filenames = ['*.re', '*.rei'] |
780 mimetypes = ['text/x-reasonml'] |
781 mimetypes = ['text/x-reasonml'] |
781 |
782 |
782 keywords = ( |
783 keywords = ( |
783 'as', 'assert', 'begin', 'class', 'constraint', 'do', 'done', 'downto', |
784 'as', 'assert', 'begin', 'class', 'constraint', 'do', 'done', 'downto', |
784 'else', 'end', 'exception', 'external', 'false', 'for', 'fun', 'esfun', |
785 'else', 'end', 'exception', 'external', 'false', 'for', 'fun', 'esfun', |
785 'function', 'functor', 'if', 'in', 'include', 'inherit', 'initializer', 'lazy', |
786 'function', 'functor', 'if', 'in', 'include', 'inherit', 'initializer', 'lazy', |
786 'let', 'switch', 'module', 'pub', 'mutable', 'new', 'nonrec', 'object', 'of', |
787 'let', 'switch', 'module', 'pub', 'mutable', 'new', 'nonrec', 'object', 'of', |
787 'open', 'pri', 'rec', 'sig', 'struct', 'then', 'to', 'true', 'try', |
788 'open', 'pri', 'rec', 'sig', 'struct', 'then', 'to', 'true', 'try', |
788 'type', 'val', 'virtual', 'when', 'while', 'with' |
789 'type', 'val', 'virtual', 'when', 'while', 'with', |
789 ) |
790 ) |
790 keyopts = ( |
791 keyopts = ( |
791 '!=', '#', '&', '&&', r'\(', r'\)', r'\*', r'\+', ',', '-', |
792 '!=', '#', '&', '&&', r'\(', r'\)', r'\*', r'\+', ',', '-', |
792 r'-\.', '=>', r'\.', r'\.\.', r'\.\.\.', ':', '::', ':=', ':>', ';', ';;', '<', |
793 r'-\.', '=>', r'\.', r'\.\.', r'\.\.\.', ':', '::', ':=', ':>', ';', ';;', '<', |
793 '<-', '=', '>', '>]', r'>\}', r'\?', r'\?\?', r'\[', r'\[<', r'\[>', |
794 '<-', '=', '>', '>]', r'>\}', r'\?', r'\?\?', r'\[', r'\[<', r'\[>', |
794 r'\[\|', ']', '_', '`', r'\{', r'\{<', r'\|\|', r'\|', r'\|]', r'\}', '~' |
795 r'\[\|', ']', '_', '`', r'\{', r'\{<', r'\|', r'\|\|', r'\|]', r'\}', '~' |
795 ) |
796 ) |
796 |
797 |
797 operators = r'[!$%&*+\./:<=>?@^|~-]' |
798 operators = r'[!$%&*+\./:<=>?@^|~-]' |
798 word_operators = ('and', 'asr', 'land', 'lor', 'lsl', 'lsr', 'lxor', 'mod', 'or') |
799 word_operators = ('and', 'asr', 'land', 'lor', 'lsl', 'lsr', 'lxor', 'mod', 'or') |
799 prefix_syms = r'[!?~]' |
800 prefix_syms = r'[!?~]' |
810 (r'\s+', Text), |
811 (r'\s+', Text), |
811 (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo), |
812 (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo), |
812 (r'\b([A-Z][\w\']*)(?=\s*\.)', Name.Namespace, 'dotted'), |
813 (r'\b([A-Z][\w\']*)(?=\s*\.)', Name.Namespace, 'dotted'), |
813 (r'\b([A-Z][\w\']*)', Name.Class), |
814 (r'\b([A-Z][\w\']*)', Name.Class), |
814 (r'//.*?\n', Comment.Single), |
815 (r'//.*?\n', Comment.Single), |
815 (r'\/\*(?![\/])', Comment.Multiline, 'comment'), |
816 (r'\/\*(?!/)', Comment.Multiline, 'comment'), |
816 (r'\b(%s)\b' % '|'.join(keywords), Keyword), |
817 (r'\b(%s)\b' % '|'.join(keywords), Keyword), |
817 (r'(%s)' % '|'.join(keyopts[::-1]), Operator.Word), |
818 (r'(%s)' % '|'.join(keyopts[::-1]), Operator.Word), |
818 (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator), |
819 (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator), |
819 (r'\b(%s)\b' % '|'.join(word_operators), Operator.Word), |
820 (r'\b(%s)\b' % '|'.join(word_operators), Operator.Word), |
820 (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type), |
821 (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type), |
835 (r'"', String.Double, 'string'), |
836 (r'"', String.Double, 'string'), |
836 |
837 |
837 (r'[~?][a-z][\w\']*:', Name.Variable), |
838 (r'[~?][a-z][\w\']*:', Name.Variable), |
838 ], |
839 ], |
839 'comment': [ |
840 'comment': [ |
840 (r'[^\/*]+', Comment.Multiline), |
841 (r'[^/*]+', Comment.Multiline), |
841 (r'\/\*', Comment.Multiline, '#push'), |
842 (r'\/\*', Comment.Multiline, '#push'), |
842 (r'\*\/', Comment.Multiline, '#pop'), |
843 (r'\*\/', Comment.Multiline, '#pop'), |
843 (r'[\*]', Comment.Multiline), |
844 (r'\*', Comment.Multiline), |
844 ], |
845 ], |
845 'string': [ |
846 'string': [ |
846 (r'[^\\"]+', String.Double), |
847 (r'[^\\"]+', String.Double), |
847 include('escape-sequence'), |
848 include('escape-sequence'), |
848 (r'\\\n', String.Double), |
849 (r'\\\n', String.Double), |
855 (r'[A-Z][\w\']*', Name.Class, '#pop'), |
856 (r'[A-Z][\w\']*', Name.Class, '#pop'), |
856 (r'[a-z_][\w\']*', Name, '#pop'), |
857 (r'[a-z_][\w\']*', Name, '#pop'), |
857 default('#pop'), |
858 default('#pop'), |
858 ], |
859 ], |
859 } |
860 } |
|
861 |
|
862 |
|
863 class FStarLexer(RegexLexer): |
|
864 """ |
|
865 For the F* language (https://www.fstar-lang.org/). |
|
866 .. versionadded:: 2.7 |
|
867 """ |
|
868 |
|
869 name = 'FStar' |
|
870 aliases = ['fstar'] |
|
871 filenames = ['*.fst', '*.fsti'] |
|
872 mimetypes = ['text/x-fstar'] |
|
873 |
|
874 keywords = ( |
|
875 'abstract', 'attributes', 'noeq', 'unopteq', 'and' |
|
876 'begin', 'by', 'default', 'effect', 'else', 'end', 'ensures', |
|
877 'exception', 'exists', 'false', 'forall', 'fun', 'function', 'if', |
|
878 'in', 'include', 'inline', 'inline_for_extraction', 'irreducible', |
|
879 'logic', 'match', 'module', 'mutable', 'new', 'new_effect', 'noextract', |
|
880 'of', 'open', 'opaque', 'private', 'range_of', 'reifiable', |
|
881 'reify', 'reflectable', 'requires', 'set_range_of', 'sub_effect', |
|
882 'synth', 'then', 'total', 'true', 'try', 'type', 'unfold', 'unfoldable', |
|
883 'val', 'when', 'with', 'not' |
|
884 ) |
|
885 decl_keywords = ('let', 'rec') |
|
886 assume_keywords = ('assume', 'admit', 'assert', 'calc') |
|
887 keyopts = ( |
|
888 r'~', r'-', r'/\\', r'\\/', r'<:', r'<@', r'\(\|', r'\|\)', r'#', r'u#', |
|
889 r'&', r'\(', r'\)', r'\(\)', r',', r'~>', r'->', r'<-', r'<--', r'<==>', |
|
890 r'==>', r'\.', r'\?', r'\?\.', r'\.\[', r'\.\(', r'\.\(\|', r'\.\[\|', |
|
891 r'\{:pattern', r':', r'::', r':=', r';', r';;', r'=', r'%\[', r'!\{', |
|
892 r'\[', r'\[@', r'\[\|', r'\|>', r'\]', r'\|\]', r'\{', r'\|', r'\}', r'\$' |
|
893 ) |
|
894 |
|
895 operators = r'[!$%&*+\./:<=>?@^|~-]' |
|
896 prefix_syms = r'[!?~]' |
|
897 infix_syms = r'[=<>@^|&+\*/$%-]' |
|
898 primitives = ('unit', 'int', 'float', 'bool', 'string', 'char', 'list', 'array') |
|
899 |
|
900 tokens = { |
|
901 'escape-sequence': [ |
|
902 (r'\\[\\"\'ntbr]', String.Escape), |
|
903 (r'\\[0-9]{3}', String.Escape), |
|
904 (r'\\x[0-9a-fA-F]{2}', String.Escape), |
|
905 ], |
|
906 'root': [ |
|
907 (r'\s+', Text), |
|
908 (r'false|true|False|True|\(\)|\[\]', Name.Builtin.Pseudo), |
|
909 (r'\b([A-Z][\w\']*)(?=\s*\.)', Name.Namespace, 'dotted'), |
|
910 (r'\b([A-Z][\w\']*)', Name.Class), |
|
911 (r'\(\*(?![)])', Comment, 'comment'), |
|
912 (r'^\/\/.+$', Comment), |
|
913 (r'\b(%s)\b' % '|'.join(keywords), Keyword), |
|
914 (r'\b(%s)\b' % '|'.join(assume_keywords), Name.Exception), |
|
915 (r'\b(%s)\b' % '|'.join(decl_keywords), Keyword.Declaration), |
|
916 (r'(%s)' % '|'.join(keyopts[::-1]), Operator), |
|
917 (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator), |
|
918 (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type), |
|
919 |
|
920 (r"[^\W\d][\w']*", Name), |
|
921 |
|
922 (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float), |
|
923 (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex), |
|
924 (r'0[oO][0-7][0-7_]*', Number.Oct), |
|
925 (r'0[bB][01][01_]*', Number.Bin), |
|
926 (r'\d[\d_]*', Number.Integer), |
|
927 |
|
928 (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'", |
|
929 String.Char), |
|
930 (r"'.'", String.Char), |
|
931 (r"'", Keyword), # a stray quote is another syntax element |
|
932 (r"\`([\w\'.]+)\`", Operator.Word), # for infix applications |
|
933 (r"\`", Keyword), # for quoting |
|
934 (r'"', String.Double, 'string'), |
|
935 |
|
936 (r'[~?][a-z][\w\']*:', Name.Variable), |
|
937 ], |
|
938 'comment': [ |
|
939 (r'[^(*)]+', Comment), |
|
940 (r'\(\*', Comment, '#push'), |
|
941 (r'\*\)', Comment, '#pop'), |
|
942 (r'[(*)]', Comment), |
|
943 ], |
|
944 'string': [ |
|
945 (r'[^\\"]+', String.Double), |
|
946 include('escape-sequence'), |
|
947 (r'\\\n', String.Double), |
|
948 (r'"', String.Double, '#pop'), |
|
949 ], |
|
950 'dotted': [ |
|
951 (r'\s+', Text), |
|
952 (r'\.', Punctuation), |
|
953 (r'[A-Z][\w\']*(?=\s*\.)', Name.Namespace), |
|
954 (r'[A-Z][\w\']*', Name.Class, '#pop'), |
|
955 (r'[a-z_][\w\']*', Name, '#pop'), |
|
956 default('#pop'), |
|
957 ], |
|
958 } |