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-2017 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2019 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'] |
18 __all__ = ['SMLLexer', 'OcamlLexer', 'OpaLexer', 'ReasonLexer'] |
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. |
28 name = 'Standard ML' |
28 name = 'Standard ML' |
29 aliases = ['sml'] |
29 aliases = ['sml'] |
30 filenames = ['*.sml', '*.sig', '*.fun'] |
30 filenames = ['*.sml', '*.sig', '*.fun'] |
31 mimetypes = ['text/x-standardml', 'application/x-standardml'] |
31 mimetypes = ['text/x-standardml', 'application/x-standardml'] |
32 |
32 |
33 alphanumid_reserved = set(( |
33 alphanumid_reserved = { |
34 # Core |
34 # Core |
35 'abstype', 'and', 'andalso', 'as', 'case', 'datatype', 'do', 'else', |
35 'abstype', 'and', 'andalso', 'as', 'case', 'datatype', 'do', 'else', |
36 'end', 'exception', 'fn', 'fun', 'handle', 'if', 'in', 'infix', |
36 'end', 'exception', 'fn', 'fun', 'handle', 'if', 'in', 'infix', |
37 'infixr', 'let', 'local', 'nonfix', 'of', 'op', 'open', 'orelse', |
37 'infixr', 'let', 'local', 'nonfix', 'of', 'op', 'open', 'orelse', |
38 'raise', 'rec', 'then', 'type', 'val', 'with', 'withtype', 'while', |
38 'raise', 'rec', 'then', 'type', 'val', 'with', 'withtype', 'while', |
39 # Modules |
39 # Modules |
40 'eqtype', 'functor', 'include', 'sharing', 'sig', 'signature', |
40 'eqtype', 'functor', 'include', 'sharing', 'sig', 'signature', |
41 'struct', 'structure', 'where', |
41 'struct', 'structure', 'where', |
42 )) |
42 } |
43 |
43 |
44 symbolicid_reserved = set(( |
44 symbolicid_reserved = { |
45 # Core |
45 # Core |
46 ':', r'\|', '=', '=>', '->', '#', |
46 ':', r'\|', '=', '=>', '->', '#', |
47 # Modules |
47 # Modules |
48 ':>', |
48 ':>', |
49 )) |
49 } |
50 |
50 |
51 nonid_reserved = set(('(', ')', '[', ']', '{', '}', ',', ';', '...', '_')) |
51 nonid_reserved = {'(', ')', '[', ']', '{', '}', ',', ';', '...', '_'} |
52 |
52 |
53 alphanumid_re = r"[a-zA-Z][\w']*" |
53 alphanumid_re = r"[a-zA-Z][\w']*" |
54 symbolicid_re = r"[!%&$#+\-/:<=>?@\\~`^|*]+" |
54 symbolicid_re = r"[!%&$#+\-/:<=>?@\\~`^|*]+" |
55 |
55 |
56 # A character constant is a sequence of the form #s, where s is a string |
56 # A character constant is a sequence of the form #s, where s is a string |
765 'html-comment': [ |
764 'html-comment': [ |
766 (r'-->', Comment, '#pop'), |
765 (r'-->', Comment, '#pop'), |
767 (r'[^\-]+|-', Comment), |
766 (r'[^\-]+|-', Comment), |
768 ], |
767 ], |
769 } |
768 } |
|
769 |
|
770 class ReasonLexer(RegexLexer): |
|
771 """ |
|
772 For the ReasonML language (https://reasonml.github.io/). |
|
773 |
|
774 .. versionadded:: 2.6 |
|
775 """ |
|
776 |
|
777 name = 'ReasonML' |
|
778 aliases = ['reason', "reasonml"] |
|
779 filenames = ['*.re', '*.rei'] |
|
780 mimetypes = ['text/x-reasonml'] |
|
781 |
|
782 keywords = ( |
|
783 'as', 'assert', 'begin', 'class', 'constraint', 'do', 'done', 'downto', |
|
784 'else', 'end', 'exception', 'external', 'false', 'for', 'fun', 'esfun', |
|
785 'function', 'functor', 'if', 'in', 'include', 'inherit', 'initializer', 'lazy', |
|
786 'let', 'switch', 'module', 'pub', 'mutable', 'new', 'nonrec', 'object', 'of', |
|
787 'open', 'pri', 'rec', 'sig', 'struct', 'then', 'to', 'true', 'try', |
|
788 'type', 'val', 'virtual', 'when', 'while', 'with' |
|
789 ) |
|
790 keyopts = ( |
|
791 '!=', '#', '&', '&&', r'\(', r'\)', r'\*', r'\+', ',', '-', |
|
792 r'-\.', '=>', r'\.', r'\.\.', r'\.\.\.', ':', '::', ':=', ':>', ';', ';;', '<', |
|
793 '<-', '=', '>', '>]', r'>\}', r'\?', r'\?\?', r'\[', r'\[<', r'\[>', |
|
794 r'\[\|', ']', '_', '`', r'\{', r'\{<', r'\|\|', r'\|', r'\|]', r'\}', '~' |
|
795 ) |
|
796 |
|
797 operators = r'[!$%&*+\./:<=>?@^|~-]' |
|
798 word_operators = ('and', 'asr', 'land', 'lor', 'lsl', 'lsr', 'lxor', 'mod', 'or') |
|
799 prefix_syms = r'[!?~]' |
|
800 infix_syms = r'[=<>@^|&+\*/$%-]' |
|
801 primitives = ('unit', 'int', 'float', 'bool', 'string', 'char', 'list', 'array') |
|
802 |
|
803 tokens = { |
|
804 'escape-sequence': [ |
|
805 (r'\\[\\"\'ntbr]', String.Escape), |
|
806 (r'\\[0-9]{3}', String.Escape), |
|
807 (r'\\x[0-9a-fA-F]{2}', String.Escape), |
|
808 ], |
|
809 'root': [ |
|
810 (r'\s+', Text), |
|
811 (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo), |
|
812 (r'\b([A-Z][\w\']*)(?=\s*\.)', Name.Namespace, 'dotted'), |
|
813 (r'\b([A-Z][\w\']*)', Name.Class), |
|
814 (r'//.*?\n', Comment.Single), |
|
815 (r'\/\*(?![\/])', Comment.Multiline, 'comment'), |
|
816 (r'\b(%s)\b' % '|'.join(keywords), Keyword), |
|
817 (r'(%s)' % '|'.join(keyopts[::-1]), Operator.Word), |
|
818 (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(primitives), Keyword.Type), |
|
821 |
|
822 (r"[^\W\d][\w']*", Name), |
|
823 |
|
824 (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float), |
|
825 (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex), |
|
826 (r'0[oO][0-7][0-7_]*', Number.Oct), |
|
827 (r'0[bB][01][01_]*', Number.Bin), |
|
828 (r'\d[\d_]*', Number.Integer), |
|
829 |
|
830 (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'", |
|
831 String.Char), |
|
832 (r"'.'", String.Char), |
|
833 (r"'", Keyword), |
|
834 |
|
835 (r'"', String.Double, 'string'), |
|
836 |
|
837 (r'[~?][a-z][\w\']*:', Name.Variable), |
|
838 ], |
|
839 'comment': [ |
|
840 (r'[^\/*]+', Comment.Multiline), |
|
841 (r'\/\*', Comment.Multiline, '#push'), |
|
842 (r'\*\/', Comment.Multiline, '#pop'), |
|
843 (r'[\*]', Comment.Multiline), |
|
844 ], |
|
845 'string': [ |
|
846 (r'[^\\"]+', String.Double), |
|
847 include('escape-sequence'), |
|
848 (r'\\\n', String.Double), |
|
849 (r'"', String.Double, '#pop'), |
|
850 ], |
|
851 'dotted': [ |
|
852 (r'\s+', Text), |
|
853 (r'\.', Punctuation), |
|
854 (r'[A-Z][\w\']*(?=\s*\.)', Name.Namespace), |
|
855 (r'[A-Z][\w\']*', Name.Class, '#pop'), |
|
856 (r'[a-z_][\w\']*', Name, '#pop'), |
|
857 default('#pop'), |
|
858 ], |
|
859 } |