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