1 // haskell.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_HASKELL_PARSER_H
5 #define OHCOUNT_HASKELL_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *HASKELL_LANG = LANG_HASKELL;
12 // the languages entities
13 const char *haskell_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 HASKELL_SPACE = 0, HASKELL_COMMENT, HASKELL_STRING, HASKELL_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action haskell_ccallback {
40 std_internal_newline(HASKELL_LANG)
43 std_newline(HASKELL_LANG)
47 action haskell_comment_nc_res { nest_count = 0; }
48 action haskell_comment_nc_inc { nest_count++; }
49 action haskell_comment_nc_dec { nest_count--; }
51 # TODO: |-- is not a comment
52 haskell_line_comment = '--' [^>] @{ fhold; } @comment nonnewline*;
53 haskell_nested_block_comment =
54 '{-' >haskell_comment_nc_res @comment (
55 newline %{ entity = INTERNAL_NL; } %haskell_ccallback
59 '{-' @haskell_comment_nc_inc @comment
61 '-}' @haskell_comment_nc_dec @comment
63 (nonnewline - ws) @comment
64 )* :>> ('-}' when { nest_count == 0 }) @comment;
65 haskell_comment = haskell_line_comment | haskell_nested_block_comment;
67 haskell_char = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline) '\'';
70 escaped_newline %{ entity = INTERNAL_NL; } %haskell_ccallback
78 haskell_string = haskell_char | haskell_dq_str;
81 spaces ${ entity = HASKELL_SPACE; } => haskell_ccallback;
84 newline ${ entity = NEWLINE; } => haskell_ccallback;
85 ^space ${ entity = HASKELL_ANY; } => haskell_ccallback;
90 action haskell_ecallback {
91 callback(HASKELL_LANG, haskell_entities[entity], cint(ts), cint(te),
95 haskell_line_comment_entity = '--' [^>] @{ fhold; } nonnewline*;
96 haskell_block_comment_entity = '{-' >haskell_comment_nc_res (
97 '{-' @haskell_comment_nc_inc
99 '-}' @haskell_comment_nc_dec
102 )* :>> ('-}' when { nest_count == 0 });
103 haskell_comment_entity =
104 haskell_line_comment_entity | haskell_block_comment_entity;
107 space+ ${ entity = HASKELL_SPACE; } => haskell_ecallback;
108 haskell_comment_entity ${ entity = HASKELL_COMMENT; } => haskell_ecallback;
114 /************************* Required for every parser *************************/
116 /* Parses a string buffer with Haskell code.
118 * @param *buffer The string to parse.
119 * @param length The length of the string to parse.
120 * @param count Integer flag specifying whether or not to count lines. If yes,
121 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
122 * machine optimized for returning entity positions.
123 * @param *callback Callback function. If count is set, callback is called for
124 * every line of code, comment, or blank with 'lcode', 'lcomment', and
125 * 'lblank' respectively. Otherwise callback is called for each entity found.
127 void parse_haskell(char *buffer, int length, int count,
128 void (*callback) (const char *lang, const char *entity,
129 int s, int e, void *udata),
137 cs = (count) ? haskell_en_haskell_line : haskell_en_haskell_entity;
140 // if no newline at EOF; callback contents of last line
141 if (count) { process_last_line(HASKELL_LANG) }
146 /*****************************************************************************/