lang-hs.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2009 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview
  16. * Registers a language handler for Haskell.
  17. *
  18. *
  19. * To use, include prettify.js and this file in your HTML page.
  20. * Then put your code in an HTML tag like
  21. * <pre class="prettyprint lang-hs">(my lisp code)</pre>
  22. * The lang-cl class identifies the language as common lisp.
  23. * This file supports the following language extensions:
  24. * lang-cl - Common Lisp
  25. * lang-el - Emacs Lisp
  26. * lang-lisp - Lisp
  27. * lang-scm - Scheme
  28. *
  29. *
  30. * I used http://www.informatik.uni-freiburg.de/~thiemann/haskell/haskell98-report-html/syntax-iso.html
  31. * as the basis, but ignore the way the ncomment production nests since this
  32. * makes the lexical grammar irregular. It might be possible to support
  33. * ncomments using the lookbehind filter.
  34. *
  35. *
  36. * @author mikesamuel@gmail.com
  37. */
  38. PR['registerLangHandler'](
  39. PR['createSimpleLexer'](
  40. [
  41. // Whitespace
  42. // whitechar -> newline | vertab | space | tab | uniWhite
  43. // newline -> return linefeed | return | linefeed | formfeed
  44. [PR['PR_PLAIN'], /^[\t\n\x0B\x0C\r ]+/, null, '\t\n\x0B\x0C\r '],
  45. // Single line double and single-quoted strings.
  46. // char -> ' (graphic<' | \> | space | escape<\&>) '
  47. // string -> " {graphic<" | \> | space | escape | gap}"
  48. // escape -> \ ( charesc | ascii | decimal | o octal
  49. // | x hexadecimal )
  50. // charesc -> a | b | f | n | r | t | v | \ | " | ' | &
  51. [PR['PR_STRING'], /^\"(?:[^\"\\\n\x0C\r]|\\[\s\S])*(?:\"|$)/,
  52. null, '"'],
  53. [PR['PR_STRING'], /^\'(?:[^\'\\\n\x0C\r]|\\[^&])\'?/,
  54. null, "'"],
  55. // decimal -> digit{digit}
  56. // octal -> octit{octit}
  57. // hexadecimal -> hexit{hexit}
  58. // integer -> decimal
  59. // | 0o octal | 0O octal
  60. // | 0x hexadecimal | 0X hexadecimal
  61. // float -> decimal . decimal [exponent]
  62. // | decimal exponent
  63. // exponent -> (e | E) [+ | -] decimal
  64. [PR['PR_LITERAL'],
  65. /^(?:0o[0-7]+|0x[\da-f]+|\d+(?:\.\d+)?(?:e[+\-]?\d+)?)/i,
  66. null, '0123456789']
  67. ],
  68. [
  69. // Haskell does not have a regular lexical grammar due to the nested
  70. // ncomment.
  71. // comment -> dashes [ any<symbol> {any}] newline
  72. // ncomment -> opencom ANYseq {ncomment ANYseq}closecom
  73. // dashes -> '--' {'-'}
  74. // opencom -> '{-'
  75. // closecom -> '-}'
  76. [PR['PR_COMMENT'], /^(?:(?:--+(?:[^\r\n\x0C]*)?)|(?:\{-(?:[^-]|-+[^-\}])*-\}))/],
  77. // reservedid -> case | class | data | default | deriving | do
  78. // | else | if | import | in | infix | infixl | infixr
  79. // | instance | let | module | newtype | of | then
  80. // | type | where | _
  81. [PR['PR_KEYWORD'], /^(?:case|class|data|default|deriving|do|else|if|import|in|infix|infixl|infixr|instance|let|module|newtype|of|then|type|where|_)(?=[^a-zA-Z0-9\']|$)/, null],
  82. // qvarid -> [ modid . ] varid
  83. // qconid -> [ modid . ] conid
  84. // varid -> (small {small | large | digit | ' })<reservedid>
  85. // conid -> large {small | large | digit | ' }
  86. // modid -> conid
  87. // small -> ascSmall | uniSmall | _
  88. // ascSmall -> a | b | ... | z
  89. // uniSmall -> any Unicode lowercase letter
  90. // large -> ascLarge | uniLarge
  91. // ascLarge -> A | B | ... | Z
  92. // uniLarge -> any uppercase or titlecase Unicode letter
  93. [PR['PR_PLAIN'], /^(?:[A-Z][\w\']*\.)*[a-zA-Z][\w\']*/],
  94. // matches the symbol production
  95. [PR['PR_PUNCTUATION'], /^[^\t\n\x0B\x0C\r a-zA-Z0-9\'\"]+/]
  96. ]),
  97. ['hs']);