reasonml.js (7087B)
1 /* 2 Language: ReasonML 3 Description: Reason lets you write simple, fast and quality type safe code while leveraging both the JavaScript & OCaml ecosystems. 4 Website: https://reasonml.github.io 5 Author: Gidi Meir Morris <oss@gidi.io> 6 Category: functional 7 */ 8 function reasonml(hljs) { 9 function orReValues(ops){ 10 return ops 11 .map(function(op) { 12 return op 13 .split('') 14 .map(function(char) { 15 return '\\' + char; 16 }) 17 .join(''); 18 }) 19 .join('|'); 20 } 21 22 var RE_IDENT = '~?[a-z$_][0-9a-zA-Z$_]*'; 23 var RE_MODULE_IDENT = '`?[A-Z$_][0-9a-zA-Z$_]*'; 24 25 var RE_PARAM_TYPEPARAM = '\'?[a-z$_][0-9a-z$_]*'; 26 var RE_PARAM_TYPE = '\s*:\s*[a-z$_][0-9a-z$_]*(\(\s*(' + RE_PARAM_TYPEPARAM + '\s*(,' + RE_PARAM_TYPEPARAM + ')*)?\s*\))?'; 27 var RE_PARAM = RE_IDENT + '(' + RE_PARAM_TYPE + ')?(' + RE_PARAM_TYPE + ')?'; 28 var RE_OPERATOR = "(" + orReValues(['||', '&&', '++', '**', '+.', '*', '/', '*.', '/.', '...', '|>']) + "|==|===)"; 29 var RE_OPERATOR_SPACED = "\\s+" + RE_OPERATOR + "\\s+"; 30 31 var KEYWORDS = { 32 keyword: 33 'and as asr assert begin class constraint do done downto else end exception external ' + 34 'for fun function functor if in include inherit initializer ' + 35 'land lazy let lor lsl lsr lxor match method mod module mutable new nonrec ' + 36 'object of open or private rec sig struct then to try type val virtual when while with', 37 built_in: 38 'array bool bytes char exn|5 float int int32 int64 list lazy_t|5 nativeint|5 ref string unit ', 39 literal: 40 'true false' 41 }; 42 43 var RE_NUMBER = '\\b(0[xX][a-fA-F0-9_]+[Lln]?|' + 44 '0[oO][0-7_]+[Lln]?|' + 45 '0[bB][01_]+[Lln]?|' + 46 '[0-9][0-9_]*([Lln]|(\\.[0-9_]*)?([eE][-+]?[0-9_]+)?)?)'; 47 48 var NUMBER_MODE = { 49 className: 'number', 50 relevance: 0, 51 variants: [ 52 { 53 begin: RE_NUMBER 54 }, 55 { 56 begin: '\\(\\-' + RE_NUMBER + '\\)' 57 } 58 ] 59 }; 60 61 var OPERATOR_MODE = { 62 className: 'operator', 63 relevance: 0, 64 begin: RE_OPERATOR 65 }; 66 var LIST_CONTENTS_MODES = [ 67 { 68 className: 'identifier', 69 relevance: 0, 70 begin: RE_IDENT 71 }, 72 OPERATOR_MODE, 73 NUMBER_MODE 74 ]; 75 76 var MODULE_ACCESS_CONTENTS = [ 77 hljs.QUOTE_STRING_MODE, 78 OPERATOR_MODE, 79 { 80 className: 'module', 81 begin: "\\b" + RE_MODULE_IDENT, returnBegin: true, 82 end: "\.", 83 contains: [ 84 { 85 className: 'identifier', 86 begin: RE_MODULE_IDENT, 87 relevance: 0 88 } 89 ] 90 } 91 ]; 92 93 var PARAMS_CONTENTS = [ 94 { 95 className: 'module', 96 begin: "\\b" + RE_MODULE_IDENT, returnBegin: true, 97 end: "\.", 98 relevance: 0, 99 contains: [ 100 { 101 className: 'identifier', 102 begin: RE_MODULE_IDENT, 103 relevance: 0 104 } 105 ] 106 } 107 ]; 108 109 var PARAMS_MODE = { 110 begin: RE_IDENT, 111 end: '(,|\\n|\\))', 112 relevance: 0, 113 contains: [ 114 OPERATOR_MODE, 115 { 116 className: 'typing', 117 begin: ':', 118 end: '(,|\\n)', 119 returnBegin: true, 120 relevance: 0, 121 contains: PARAMS_CONTENTS 122 } 123 ] 124 }; 125 126 var FUNCTION_BLOCK_MODE = { 127 className: 'function', 128 relevance: 0, 129 keywords: KEYWORDS, 130 variants: [ 131 { 132 begin: '\\s(\\(\\.?.*?\\)|' + RE_IDENT + ')\\s*=>', 133 end: '\\s*=>', 134 returnBegin: true, 135 relevance: 0, 136 contains: [ 137 { 138 className: 'params', 139 variants: [ 140 { 141 begin: RE_IDENT 142 }, 143 { 144 begin: RE_PARAM 145 }, 146 { 147 begin: /\(\s*\)/, 148 } 149 ] 150 } 151 ] 152 }, 153 { 154 begin: '\\s\\(\\.?[^;\\|]*\\)\\s*=>', 155 end: '\\s=>', 156 returnBegin: true, 157 relevance: 0, 158 contains: [ 159 { 160 className: 'params', 161 relevance: 0, 162 variants: [ 163 PARAMS_MODE 164 ] 165 } 166 ] 167 }, 168 { 169 begin: '\\(\\.\\s' + RE_IDENT + '\\)\\s*=>' 170 } 171 ] 172 }; 173 MODULE_ACCESS_CONTENTS.push(FUNCTION_BLOCK_MODE); 174 175 var CONSTRUCTOR_MODE = { 176 className: 'constructor', 177 begin: RE_MODULE_IDENT + '\\(', 178 end: '\\)', 179 illegal: '\\n', 180 keywords: KEYWORDS, 181 contains: [ 182 hljs.QUOTE_STRING_MODE, 183 OPERATOR_MODE, 184 { 185 className: 'params', 186 begin: '\\b' + RE_IDENT 187 } 188 ] 189 }; 190 191 var PATTERN_MATCH_BLOCK_MODE = { 192 className: 'pattern-match', 193 begin: '\\|', 194 returnBegin: true, 195 keywords: KEYWORDS, 196 end: '=>', 197 relevance: 0, 198 contains: [ 199 CONSTRUCTOR_MODE, 200 OPERATOR_MODE, 201 { 202 relevance: 0, 203 className: 'constructor', 204 begin: RE_MODULE_IDENT 205 } 206 ] 207 }; 208 209 var MODULE_ACCESS_MODE = { 210 className: 'module-access', 211 keywords: KEYWORDS, 212 returnBegin: true, 213 variants: [ 214 { 215 begin: "\\b(" + RE_MODULE_IDENT + "\\.)+" + RE_IDENT 216 }, 217 { 218 begin: "\\b(" + RE_MODULE_IDENT + "\\.)+\\(", 219 end: "\\)", 220 returnBegin: true, 221 contains: [ 222 FUNCTION_BLOCK_MODE, 223 { 224 begin: '\\(', 225 end: '\\)', 226 skip: true 227 } 228 ].concat(MODULE_ACCESS_CONTENTS) 229 }, 230 { 231 begin: "\\b(" + RE_MODULE_IDENT + "\\.)+{", 232 end: "}" 233 } 234 ], 235 contains: MODULE_ACCESS_CONTENTS 236 }; 237 238 PARAMS_CONTENTS.push(MODULE_ACCESS_MODE); 239 240 return { 241 name: 'ReasonML', 242 aliases: ['re'], 243 keywords: KEYWORDS, 244 illegal: '(:\\-|:=|\\${|\\+=)', 245 contains: [ 246 hljs.COMMENT('/\\*', '\\*/', { illegal: '^(\\#,\\/\\/)' }), 247 { 248 className: 'character', 249 begin: '\'(\\\\[^\']+|[^\'])\'', 250 illegal: '\\n', 251 relevance: 0 252 }, 253 hljs.QUOTE_STRING_MODE, 254 { 255 className: 'literal', 256 begin: '\\(\\)', 257 relevance: 0 258 }, 259 { 260 className: 'literal', 261 begin: '\\[\\|', 262 end: '\\|\\]', 263 relevance: 0, 264 contains: LIST_CONTENTS_MODES 265 }, 266 { 267 className: 'literal', 268 begin: '\\[', 269 end: '\\]', 270 relevance: 0, 271 contains: LIST_CONTENTS_MODES 272 }, 273 CONSTRUCTOR_MODE, 274 { 275 className: 'operator', 276 begin: RE_OPERATOR_SPACED, 277 illegal: '\\-\\->', 278 relevance: 0 279 }, 280 NUMBER_MODE, 281 hljs.C_LINE_COMMENT_MODE, 282 PATTERN_MATCH_BLOCK_MODE, 283 FUNCTION_BLOCK_MODE, 284 { 285 className: 'module-def', 286 begin: "\\bmodule\\s+" + RE_IDENT + "\\s+" + RE_MODULE_IDENT + "\\s+=\\s+{", 287 end: "}", 288 returnBegin: true, 289 keywords: KEYWORDS, 290 relevance: 0, 291 contains: [ 292 { 293 className: 'module', 294 relevance: 0, 295 begin: RE_MODULE_IDENT 296 }, 297 { 298 begin: '{', 299 end: '}', 300 skip: true 301 } 302 ].concat(MODULE_ACCESS_CONTENTS) 303 }, 304 MODULE_ACCESS_MODE 305 ] 306 }; 307 } 308 309 module.exports = reasonml;