1 // d.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_D_PARSER_H
5 #define OHCOUNT_D_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *D_LANG = LANG_DMD;
12 // the languages entities
13 const char *d_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 D_SPACE = 0, D_COMMENT, D_STRING, D_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
40 std_internal_newline(D_LANG)
47 action d_comment_nc_res { nest_count = 0; }
48 action d_comment_nc_inc { nest_count++; }
49 action d_comment_nc_dec { nest_count--; }
53 escaped_newline %{ entity = INTERNAL_NL; } %d_ccallback
57 (nonnewline - ws) @comment
61 newline %{ entity = INTERNAL_NL; } %d_ccallback
65 (nonnewline - ws) @comment
68 '/+' >d_comment_nc_res @comment (
69 newline %{ entity = INTERNAL_NL; } %d_ccallback
73 '/+' @d_comment_nc_inc @comment
75 '+/' @d_comment_nc_dec @comment
78 )* :>> ('+/' when { nest_count == 0 }) @comment;
79 d_comment = d_line_comment | d_block_comment | d_nested_comment;
83 escaped_newline %{ entity = INTERNAL_NL; } %d_ccallback
93 escaped_newline %{ entity = INTERNAL_NL; } %d_ccallback
103 escaped_newline %{ entity = INTERNAL_NL; } %d_ccallback
109 '\\' nonnewline @code
111 d_string = d_sq_str | d_dq_str | d_bt_str;
114 spaces ${ entity = D_SPACE; } => d_ccallback;
117 newline ${ entity = NEWLINE; } => d_ccallback;
118 ^space ${ entity = D_ANY; } => d_ccallback;
124 callback(D_LANG, d_entities[entity], cint(ts), cint(te), userdata);
127 d_line_comment_entity = '//' (escaped_newline | nonnewline)*;
128 d_block_comment_entity = '/*' any* :>> '*/';
129 d_nested_comment_entity = '/+' >d_comment_nc_res (
130 '/+' @d_comment_nc_inc
132 '+/' @d_comment_nc_dec
135 )* :>> ('+/' when { nest_count == 0 });
136 d_comment_entity = d_line_comment_entity | d_block_comment_entity |
137 d_nested_comment_entity;
140 space+ ${ entity = D_SPACE; } => d_ecallback;
141 d_comment_entity ${ entity = D_COMMENT; } => d_ecallback;
147 /************************* Required for every parser *************************/
149 /* Parses a string buffer with D code.
151 * @param *buffer The string to parse.
152 * @param length The length of the string to parse.
153 * @param count Integer flag specifying whether or not to count lines. If yes,
154 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
155 * machine optimized for returning entity positions.
156 * @param *callback Callback function. If count is set, callback is called for
157 * every line of code, comment, or blank with 'lcode', 'lcomment', and
158 * 'lblank' respectively. Otherwise callback is called for each entity found.
160 void parse_d(char *buffer, int length, int count,
161 void (*callback) (const char *lang, const char *entity, int s,
170 cs = (count) ? d_en_d_line : d_en_d_entity;
173 // if no newline at EOF; callback contents of last line
174 if (count) { process_last_line(D_LANG) }
179 /*****************************************************************************/