ocaml.go

 1package o
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Ocaml lexer.
 9var Ocaml = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "OCaml",
12		Aliases:   []string{"ocaml"},
13		Filenames: []string{"*.ml", "*.mli", "*.mll", "*.mly"},
14		MimeTypes: []string{"text/x-ocaml"},
15	},
16	Rules{
17		"escape-sequence": {
18			{`\\[\\"\'ntbr]`, LiteralStringEscape, nil},
19			{`\\[0-9]{3}`, LiteralStringEscape, nil},
20			{`\\x[0-9a-fA-F]{2}`, LiteralStringEscape, nil},
21		},
22		"root": {
23			{`\s+`, Text, nil},
24			{`false|true|\(\)|\[\]`, NameBuiltinPseudo, nil},
25			{`\b([A-Z][\w\']*)(?=\s*\.)`, NameNamespace, Push("dotted")},
26			{`\b([A-Z][\w\']*)`, NameClass, nil},
27			{`\(\*(?![)])`, Comment, Push("comment")},
28			{`\b(as|assert|begin|class|constraint|do|done|downto|else|end|exception|external|false|for|fun|function|functor|if|in|include|inherit|initializer|lazy|let|match|method|module|mutable|new|object|of|open|private|raise|rec|sig|struct|then|to|true|try|type|value|val|virtual|when|while|with)\b`, Keyword, nil},
29			{"(~|\\}|\\|]|\\||\\{<|\\{|`|_|]|\\[\\||\\[>|\\[<|\\[|\\?\\?|\\?|>\\}|>]|>|=|<-|<|;;|;|:>|:=|::|:|\\.\\.|\\.|->|-\\.|-|,|\\+|\\*|\\)|\\(|&&|&|#|!=)", Operator, nil},
30			{`([=<>@^|&+\*/$%-]|[!?~])?[!$%&*+\./:<=>?@^|~-]`, Operator, nil},
31			{`\b(and|asr|land|lor|lsl|lxor|mod|or)\b`, OperatorWord, nil},
32			{`\b(unit|int|float|bool|string|char|list|array)\b`, KeywordType, nil},
33			{`[^\W\d][\w']*`, Name, nil},
34			{`-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)`, LiteralNumberFloat, nil},
35			{`0[xX][\da-fA-F][\da-fA-F_]*`, LiteralNumberHex, nil},
36			{`0[oO][0-7][0-7_]*`, LiteralNumberOct, nil},
37			{`0[bB][01][01_]*`, LiteralNumberBin, nil},
38			{`\d[\d_]*`, LiteralNumberInteger, nil},
39			{`'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'`, LiteralStringChar, nil},
40			{`'.'`, LiteralStringChar, nil},
41			{`'`, Keyword, nil},
42			{`"`, LiteralStringDouble, Push("string")},
43			{`[~?][a-z][\w\']*:`, NameVariable, nil},
44		},
45		"comment": {
46			{`[^(*)]+`, Comment, nil},
47			{`\(\*`, Comment, Push()},
48			{`\*\)`, Comment, Pop(1)},
49			{`[(*)]`, Comment, nil},
50		},
51		"string": {
52			{`[^\\"]+`, LiteralStringDouble, nil},
53			Include("escape-sequence"),
54			{`\\\n`, LiteralStringDouble, nil},
55			{`"`, LiteralStringDouble, Pop(1)},
56		},
57		"dotted": {
58			{`\s+`, Text, nil},
59			{`\.`, Punctuation, nil},
60			{`[A-Z][\w\']*(?=\s*\.)`, NameNamespace, nil},
61			{`[A-Z][\w\']*`, NameClass, Pop(1)},
62			{`[a-z_][\w\']*`, Name, Pop(1)},
63			Default(Pop(1)),
64		},
65	},
66))