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