UNPKG

4.69 kBJavaScriptView Raw
1/*
2Language: Haskell
3Author: Jeremy Hull <sourdrums@gmail.com>
4Contributors: Zena Treep <zena.treep@gmail.com>
5Website: https://www.haskell.org
6Category: functional
7*/
8
9function haskell(hljs) {
10 const COMMENT = {
11 variants: [
12 hljs.COMMENT('--', '$'),
13 hljs.COMMENT(
14 /\{-/,
15 /-\}/,
16 {
17 contains: ['self']
18 }
19 )
20 ]
21 };
22
23 const PRAGMA = {
24 className: 'meta',
25 begin: /\{-#/,
26 end: /#-\}/
27 };
28
29 const PREPROCESSOR = {
30 className: 'meta',
31 begin: '^#',
32 end: '$'
33 };
34
35 const CONSTRUCTOR = {
36 className: 'type',
37 begin: '\\b[A-Z][\\w\']*', // TODO: other constructors (build-in, infix).
38 relevance: 0
39 };
40
41 const LIST = {
42 begin: '\\(',
43 end: '\\)',
44 illegal: '"',
45 contains: [
46 PRAGMA,
47 PREPROCESSOR,
48 {
49 className: 'type',
50 begin: '\\b[A-Z][\\w]*(\\((\\.\\.|,|\\w+)\\))?'
51 },
52 hljs.inherit(hljs.TITLE_MODE, {
53 begin: '[_a-z][\\w\']*'
54 }),
55 COMMENT
56 ]
57 };
58
59 const RECORD = {
60 begin: /\{/,
61 end: /\}/,
62 contains: LIST.contains
63 };
64
65 /* See:
66
67 - https://www.haskell.org/onlinereport/lexemes.html
68 - https://downloads.haskell.org/ghc/9.0.1/docs/html/users_guide/exts/binary_literals.html
69 - https://downloads.haskell.org/ghc/9.0.1/docs/html/users_guide/exts/numeric_underscores.html
70 - https://downloads.haskell.org/ghc/9.0.1/docs/html/users_guide/exts/hex_float_literals.html
71
72 */
73 const decimalDigits = '([0-9]_*)+';
74 const hexDigits = '([0-9a-fA-F]_*)+';
75 const binaryDigits = '([01]_*)+';
76 const octalDigits = '([0-7]_*)+';
77
78 const NUMBER = {
79 className: 'number',
80 relevance: 0,
81 variants: [
82 // decimal floating-point-literal (subsumes decimal-literal)
83 {
84 match: `\\b(${decimalDigits})(\\.(${decimalDigits}))?` + `([eE][+-]?(${decimalDigits}))?\\b`
85 },
86 // hexadecimal floating-point-literal (subsumes hexadecimal-literal)
87 {
88 match: `\\b0[xX]_*(${hexDigits})(\\.(${hexDigits}))?` + `([pP][+-]?(${decimalDigits}))?\\b`
89 },
90 // octal-literal
91 {
92 match: `\\b0[oO](${octalDigits})\\b`
93 },
94 // binary-literal
95 {
96 match: `\\b0[bB](${binaryDigits})\\b`
97 }
98 ]
99 };
100
101 return {
102 name: 'Haskell',
103 aliases: ['hs'],
104 keywords:
105 'let in if then else case of where do module import hiding ' +
106 'qualified type data newtype deriving class instance as default ' +
107 'infix infixl infixr foreign export ccall stdcall cplusplus ' +
108 'jvm dotnet safe unsafe family forall mdo proc rec',
109 contains: [
110 // Top-level constructions.
111 {
112 beginKeywords: 'module',
113 end: 'where',
114 keywords: 'module where',
115 contains: [
116 LIST,
117 COMMENT
118 ],
119 illegal: '\\W\\.|;'
120 },
121 {
122 begin: '\\bimport\\b',
123 end: '$',
124 keywords: 'import qualified as hiding',
125 contains: [
126 LIST,
127 COMMENT
128 ],
129 illegal: '\\W\\.|;'
130 },
131 {
132 className: 'class',
133 begin: '^(\\s*)?(class|instance)\\b',
134 end: 'where',
135 keywords: 'class family instance where',
136 contains: [
137 CONSTRUCTOR,
138 LIST,
139 COMMENT
140 ]
141 },
142 {
143 className: 'class',
144 begin: '\\b(data|(new)?type)\\b',
145 end: '$',
146 keywords: 'data family type newtype deriving',
147 contains: [
148 PRAGMA,
149 CONSTRUCTOR,
150 LIST,
151 RECORD,
152 COMMENT
153 ]
154 },
155 {
156 beginKeywords: 'default',
157 end: '$',
158 contains: [
159 CONSTRUCTOR,
160 LIST,
161 COMMENT
162 ]
163 },
164 {
165 beginKeywords: 'infix infixl infixr',
166 end: '$',
167 contains: [
168 hljs.C_NUMBER_MODE,
169 COMMENT
170 ]
171 },
172 {
173 begin: '\\bforeign\\b',
174 end: '$',
175 keywords: 'foreign import export ccall stdcall cplusplus jvm ' +
176 'dotnet safe unsafe',
177 contains: [
178 CONSTRUCTOR,
179 hljs.QUOTE_STRING_MODE,
180 COMMENT
181 ]
182 },
183 {
184 className: 'meta',
185 begin: '#!\\/usr\\/bin\\/env\ runhaskell',
186 end: '$'
187 },
188 // "Whitespaces".
189 PRAGMA,
190 PREPROCESSOR,
191
192 // Literals and names.
193
194 // TODO: characters.
195 hljs.QUOTE_STRING_MODE,
196 NUMBER,
197 CONSTRUCTOR,
198 hljs.inherit(hljs.TITLE_MODE, {
199 begin: '^[_a-z][\\w\']*'
200 }),
201 COMMENT,
202 { // No markup, relevance booster
203 begin: '->|<-'
204 }
205 ]
206 };
207}
208
209export { haskell as default };