...

Source file src/github.com/alecthomas/chroma/lexers/o/ocaml.go

Documentation: github.com/alecthomas/chroma/lexers/o

     1  package o
     2  
     3  import (
     4  	. "github.com/alecthomas/chroma" // nolint
     5  	"github.com/alecthomas/chroma/lexers/internal"
     6  )
     7  
     8  // Ocaml lexer.
     9  var Ocaml = internal.Register(MustNewLazyLexer(
    10  	&Config{
    11  		Name:      "OCaml",
    12  		Aliases:   []string{"ocaml"},
    13  		Filenames: []string{"*.ml", "*.mli", "*.mll", "*.mly"},
    14  		MimeTypes: []string{"text/x-ocaml"},
    15  	},
    16  	ocamlRules,
    17  ))
    18  
    19  func ocamlRules() Rules {
    20  	return Rules{
    21  		"escape-sequence": {
    22  			{`\\[\\"\'ntbr]`, LiteralStringEscape, nil},
    23  			{`\\[0-9]{3}`, LiteralStringEscape, nil},
    24  			{`\\x[0-9a-fA-F]{2}`, LiteralStringEscape, nil},
    25  		},
    26  		"root": {
    27  			{`\s+`, Text, nil},
    28  			{`false|true|\(\)|\[\]`, NameBuiltinPseudo, nil},
    29  			{`\b([A-Z][\w\']*)(?=\s*\.)`, NameNamespace, Push("dotted")},
    30  			{`\b([A-Z][\w\']*)`, NameClass, nil},
    31  			{`\(\*(?![)])`, Comment, Push("comment")},
    32  			{`\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},
    33  			{"(~|\\}|\\|]|\\||\\{<|\\{|`|_|]|\\[\\||\\[>|\\[<|\\[|\\?\\?|\\?|>\\}|>]|>|=|<-|<|;;|;|:>|:=|::|:|\\.\\.|\\.|->|-\\.|-|,|\\+|\\*|\\)|\\(|&&|&|#|!=)", Operator, nil},
    34  			{`([=<>@^|&+\*/$%-]|[!?~])?[!$%&*+\./:<=>?@^|~-]`, Operator, nil},
    35  			{`\b(and|asr|land|lor|lsl|lxor|mod|or)\b`, OperatorWord, nil},
    36  			{`\b(unit|int|float|bool|string|char|list|array)\b`, KeywordType, nil},
    37  			{`[^\W\d][\w']*`, Name, nil},
    38  			{`-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)`, LiteralNumberFloat, nil},
    39  			{`0[xX][\da-fA-F][\da-fA-F_]*`, LiteralNumberHex, nil},
    40  			{`0[oO][0-7][0-7_]*`, LiteralNumberOct, nil},
    41  			{`0[bB][01][01_]*`, LiteralNumberBin, nil},
    42  			{`\d[\d_]*`, LiteralNumberInteger, nil},
    43  			{`'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'`, LiteralStringChar, nil},
    44  			{`'.'`, LiteralStringChar, nil},
    45  			{`'`, Keyword, nil},
    46  			{`"`, LiteralStringDouble, Push("string")},
    47  			{`[~?][a-z][\w\']*:`, NameVariable, nil},
    48  		},
    49  		"comment": {
    50  			{`[^(*)]+`, Comment, nil},
    51  			{`\(\*`, Comment, Push()},
    52  			{`\*\)`, Comment, Pop(1)},
    53  			{`[(*)]`, Comment, nil},
    54  		},
    55  		"string": {
    56  			{`[^\\"]+`, LiteralStringDouble, nil},
    57  			Include("escape-sequence"),
    58  			{`\\\n`, LiteralStringDouble, nil},
    59  			{`"`, LiteralStringDouble, Pop(1)},
    60  		},
    61  		"dotted": {
    62  			{`\s+`, Text, nil},
    63  			{`\.`, Punctuation, nil},
    64  			{`[A-Z][\w\']*(?=\s*\.)`, NameNamespace, nil},
    65  			{`[A-Z][\w\']*`, NameClass, Pop(1)},
    66  			{`[a-z_][\w\']*`, Name, Pop(1)},
    67  			Default(Pop(1)),
    68  		},
    69  	}
    70  }
    71  

View as plain text