haskell.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Language: Haskell
  3. Author: Jeremy Hull <sourdrums@gmail.com>
  4. Contributors: Zena Treep <zena.treep@gmail.com>
  5. Website: https://www.haskell.org
  6. Category: functional
  7. */
  8. function haskell(hljs) {
  9. const COMMENT = {
  10. variants: [
  11. hljs.COMMENT('--', '$'),
  12. hljs.COMMENT(
  13. /\{-/,
  14. /-\}/,
  15. {
  16. contains: ['self']
  17. }
  18. )
  19. ]
  20. };
  21. const PRAGMA = {
  22. className: 'meta',
  23. begin: /\{-#/,
  24. end: /#-\}/
  25. };
  26. const PREPROCESSOR = {
  27. className: 'meta',
  28. begin: '^#',
  29. end: '$'
  30. };
  31. const CONSTRUCTOR = {
  32. className: 'type',
  33. begin: '\\b[A-Z][\\w\']*', // TODO: other constructors (build-in, infix).
  34. relevance: 0
  35. };
  36. const LIST = {
  37. begin: '\\(',
  38. end: '\\)',
  39. illegal: '"',
  40. contains: [
  41. PRAGMA,
  42. PREPROCESSOR,
  43. {
  44. className: 'type',
  45. begin: '\\b[A-Z][\\w]*(\\((\\.\\.|,|\\w+)\\))?'
  46. },
  47. hljs.inherit(hljs.TITLE_MODE, {
  48. begin: '[_a-z][\\w\']*'
  49. }),
  50. COMMENT
  51. ]
  52. };
  53. const RECORD = {
  54. begin: /\{/,
  55. end: /\}/,
  56. contains: LIST.contains
  57. };
  58. return {
  59. name: 'Haskell',
  60. aliases: ['hs'],
  61. keywords:
  62. 'let in if then else case of where do module import hiding ' +
  63. 'qualified type data newtype deriving class instance as default ' +
  64. 'infix infixl infixr foreign export ccall stdcall cplusplus ' +
  65. 'jvm dotnet safe unsafe family forall mdo proc rec',
  66. contains: [
  67. // Top-level constructions.
  68. {
  69. beginKeywords: 'module',
  70. end: 'where',
  71. keywords: 'module where',
  72. contains: [
  73. LIST,
  74. COMMENT
  75. ],
  76. illegal: '\\W\\.|;'
  77. },
  78. {
  79. begin: '\\bimport\\b',
  80. end: '$',
  81. keywords: 'import qualified as hiding',
  82. contains: [
  83. LIST,
  84. COMMENT
  85. ],
  86. illegal: '\\W\\.|;'
  87. },
  88. {
  89. className: 'class',
  90. begin: '^(\\s*)?(class|instance)\\b',
  91. end: 'where',
  92. keywords: 'class family instance where',
  93. contains: [
  94. CONSTRUCTOR,
  95. LIST,
  96. COMMENT
  97. ]
  98. },
  99. {
  100. className: 'class',
  101. begin: '\\b(data|(new)?type)\\b',
  102. end: '$',
  103. keywords: 'data family type newtype deriving',
  104. contains: [
  105. PRAGMA,
  106. CONSTRUCTOR,
  107. LIST,
  108. RECORD,
  109. COMMENT
  110. ]
  111. },
  112. {
  113. beginKeywords: 'default',
  114. end: '$',
  115. contains: [
  116. CONSTRUCTOR,
  117. LIST,
  118. COMMENT
  119. ]
  120. },
  121. {
  122. beginKeywords: 'infix infixl infixr',
  123. end: '$',
  124. contains: [
  125. hljs.C_NUMBER_MODE,
  126. COMMENT
  127. ]
  128. },
  129. {
  130. begin: '\\bforeign\\b',
  131. end: '$',
  132. keywords: 'foreign import export ccall stdcall cplusplus jvm ' +
  133. 'dotnet safe unsafe',
  134. contains: [
  135. CONSTRUCTOR,
  136. hljs.QUOTE_STRING_MODE,
  137. COMMENT
  138. ]
  139. },
  140. {
  141. className: 'meta',
  142. begin: '#!\\/usr\\/bin\\/env\ runhaskell',
  143. end: '$'
  144. },
  145. // "Whitespaces".
  146. PRAGMA,
  147. PREPROCESSOR,
  148. // Literals and names.
  149. // TODO: characters.
  150. hljs.QUOTE_STRING_MODE,
  151. hljs.C_NUMBER_MODE,
  152. CONSTRUCTOR,
  153. hljs.inherit(hljs.TITLE_MODE, {
  154. begin: '^[_a-z][\\w\']*'
  155. }),
  156. COMMENT,
  157. { // No markup, relevance booster
  158. begin: '->|<-'
  159. }
  160. ]
  161. };
  162. }
  163. module.exports = haskell;