1 package h
2
3 import (
4 . "github.com/alecthomas/chroma"
5 "github.com/alecthomas/chroma/lexers/internal"
6 )
7
8
9 var Haskell = internal.Register(MustNewLazyLexer(
10 &Config{
11 Name: "Haskell",
12 Aliases: []string{"haskell", "hs"},
13 Filenames: []string{"*.hs"},
14 MimeTypes: []string{"text/x-haskell"},
15 },
16 haskellRules,
17 ))
18
19 func haskellRules() Rules {
20 return Rules{
21 "root": {
22 {`\s+`, Text, nil},
23 {`--(?![!#$%&*+./<=>?@^|_~:\\]).*?$`, CommentSingle, nil},
24 {`\{-`, CommentMultiline, Push("comment")},
25 {`\bimport\b`, KeywordReserved, Push("import")},
26 {`\bmodule\b`, KeywordReserved, Push("module")},
27 {`\berror\b`, NameException, nil},
28 {`\b(case|class|data|default|deriving|do|else|family|if|in|infix[lr]?|instance|let|newtype|of|then|type|where|_)(?!\')\b`, KeywordReserved, nil},
29 {`'[^\\]'`, LiteralStringChar, nil},
30 {`^[_\p{Ll}][\w\']*`, NameFunction, nil},
31 {`'?[_\p{Ll}][\w']*`, Name, nil},
32 {`('')?[\p{Lu}][\w\']*`, KeywordType, nil},
33 {`(')[\p{Lu}][\w\']*`, KeywordType, nil},
34 {`(')\[[^\]]*\]`, KeywordType, nil},
35 {`(')\([^)]*\)`, KeywordType, nil},
36 {`\\(?![:!#$%&*+.\\/<=>?@^|~-]+)`, NameFunction, nil},
37 {`(<-|::|->|=>|=)(?![:!#$%&*+.\\/<=>?@^|~-]+)`, OperatorWord, nil},
38 {`:[:!#$%&*+.\\/<=>?@^|~-]*`, KeywordType, nil},
39 {`[:!#$%&*+.\\/<=>?@^|~-]+`, Operator, nil},
40 {`\d+[eE][+-]?\d+`, LiteralNumberFloat, nil},
41 {`\d+\.\d+([eE][+-]?\d+)?`, LiteralNumberFloat, nil},
42 {`0[oO][0-7]+`, LiteralNumberOct, nil},
43 {`0[xX][\da-fA-F]+`, LiteralNumberHex, nil},
44 {`\d+`, LiteralNumberInteger, nil},
45 {`'`, LiteralStringChar, Push("character")},
46 {`"`, LiteralString, Push("string")},
47 {`\[\]`, KeywordType, nil},
48 {`\(\)`, NameBuiltin, nil},
49 {"[][(),;`{}]", Punctuation, nil},
50 },
51 "import": {
52 {`\s+`, Text, nil},
53 {`"`, LiteralString, Push("string")},
54 {`\)`, Punctuation, Pop(1)},
55 {`qualified\b`, Keyword, nil},
56 {`([\p{Lu}][\w.]*)(\s+)(as)(\s+)([\p{Lu}][\w.]*)`, ByGroups(NameNamespace, Text, Keyword, Text, Name), Pop(1)},
57 {`([\p{Lu}][\w.]*)(\s+)(hiding)(\s+)(\()`, ByGroups(NameNamespace, Text, Keyword, Text, Punctuation), Push("funclist")},
58 {`([\p{Lu}][\w.]*)(\s+)(\()`, ByGroups(NameNamespace, Text, Punctuation), Push("funclist")},
59 {`[\w.]+`, NameNamespace, Pop(1)},
60 },
61 "module": {
62 {`\s+`, Text, nil},
63 {`([\p{Lu}][\w.]*)(\s+)(\()`, ByGroups(NameNamespace, Text, Punctuation), Push("funclist")},
64 {`[\p{Lu}][\w.]*`, NameNamespace, Pop(1)},
65 },
66 "funclist": {
67 {`\s+`, Text, nil},
68 {`[\p{Lu}]\w*`, KeywordType, nil},
69 {`(_[\w\']+|[\p{Ll}][\w\']*)`, NameFunction, nil},
70 {`--(?![!#$%&*+./<=>?@^|_~:\\]).*?$`, CommentSingle, nil},
71 {`\{-`, CommentMultiline, Push("comment")},
72 {`,`, Punctuation, nil},
73 {`[:!#$%&*+.\\/<=>?@^|~-]+`, Operator, nil},
74 {`\(`, Punctuation, Push("funclist", "funclist")},
75 {`\)`, Punctuation, Pop(2)},
76 },
77 "comment": {
78 {`[^-{}]+`, CommentMultiline, nil},
79 {`\{-`, CommentMultiline, Push()},
80 {`-\}`, CommentMultiline, Pop(1)},
81 {`[-{}]`, CommentMultiline, nil},
82 },
83 "character": {
84 {`[^\\']'`, LiteralStringChar, Pop(1)},
85 {`\\`, LiteralStringEscape, Push("escape")},
86 {`'`, LiteralStringChar, Pop(1)},
87 },
88 "string": {
89 {`[^\\"]+`, LiteralString, nil},
90 {`\\`, LiteralStringEscape, Push("escape")},
91 {`"`, LiteralString, Pop(1)},
92 },
93 "escape": {
94 {`[abfnrtv"\'&\\]`, LiteralStringEscape, Pop(1)},
95 {`\^[][\p{Lu}@^_]`, LiteralStringEscape, Pop(1)},
96 {`NUL|SOH|[SE]TX|EOT|ENQ|ACK|BEL|BS|HT|LF|VT|FF|CR|S[OI]|DLE|DC[1-4]|NAK|SYN|ETB|CAN|EM|SUB|ESC|[FGRU]S|SP|DEL`, LiteralStringEscape, Pop(1)},
97 {`o[0-7]+`, LiteralStringEscape, Pop(1)},
98 {`x[\da-fA-F]+`, LiteralStringEscape, Pop(1)},
99 {`\d+`, LiteralStringEscape, Pop(1)},
100 {`\s+\\`, LiteralStringEscape, Pop(1)},
101 },
102 }
103 }
104
View as plain text