reasonml.js 6.6 KB

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