1 // ruby.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_RUBY_PARSER_H
5 #define OHCOUNT_RUBY_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *RUBY_LANG = LANG_RUBY;
12 // the languages entities
13 const char *ruby_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 RUBY_SPACE = 0, RUBY_COMMENT, RUBY_STRING, RUBY_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action ruby_ccallback {
40 std_internal_newline(RUBY_LANG)
43 std_newline(RUBY_LANG)
47 ruby_line_comment = '#' @comment nonnewline*;
48 # TODO: detect =begin and =end at start of their lines
49 # Can't do that now because using 'when starts_line' fails a Ragel assertion.
51 '=begin' @enqueue @comment (
52 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
56 (nonnewline - ws) @comment
57 )* :>> '=end' @commit;
58 ruby_comment = ruby_line_comment | ruby_block_comment;
62 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
69 )* '\'' @commit @code;
72 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
80 # TODO: true literal string detection
81 # Turns out any non-alphanum char can be after the initial '%' for a literal
82 # string. I only have '(', '[', '{' for now because they are common(?). Their
83 # respective closing characters need to be escaped though, which is not
84 # accurate; only the single closing character needs to be escaped in a literal
86 # We need to detect which non-alphanum char opens a literal string, somehow
87 # let Ragel know what it is (currently unsupported), and put its respective
88 # closing char in the literal string below.
90 '%' [qQ]? [(\[{] @enqueue @code (
91 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
95 [^\r\n\f\t )\]}\\] @code
98 )* [)\]}] @commit @code;
101 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
105 [^\r\n\f\t `\\] @code
107 '\\' nonnewline @code
108 )* '`' @commit @code;
109 ruby_regex = '/' ([^\r\n\f/\\] | '\\' nonnewline)* '/' @code;
110 # TODO: true literal array and command detection
111 # See TODO above about literal string detection
113 '%' [wrx] [(\[{] @enqueue @code (
114 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
118 [^\r\n\f\t )\]}\\] @code
120 '\\' nonnewline @code
121 )* [)\]}] @commit @code;
122 # TODO: heredoc detection
123 # This is impossible with current Ragel. We need to extract what the end
124 # delimiter should be from the heredoc and search up to it on a new line.
127 ruby_sq_str | ruby_dq_str | ruby_lit_str | ruby_cmd_str | ruby_regex |
131 spaces ${ entity = RUBY_SPACE; } => ruby_ccallback;
134 newline ${ entity = NEWLINE; } => ruby_ccallback;
135 ^space ${ entity = RUBY_ANY; } => ruby_ccallback;
140 action ruby_ecallback {
141 callback(RUBY_LANG, ruby_entities[entity], cint(ts), cint(te), userdata);
144 ruby_line_comment_entity = '#' nonnewline*;
145 ruby_block_comment_entity = ('=' when starts_line) 'begin'
146 any* :>> (('=' when starts_line) 'end');
147 ruby_comment_entity = ruby_line_comment_entity | ruby_block_comment_entity;
150 space+ ${ entity = RUBY_SPACE; } => ruby_ecallback;
151 ruby_comment_entity ${ entity = RUBY_COMMENT; } => ruby_ecallback;
157 /************************* Required for every parser *************************/
159 /* Parses a string buffer with Ruby code.
161 * @param *buffer The string to parse.
162 * @param length The length of the string to parse.
163 * @param count Integer flag specifying whether or not to count lines. If yes,
164 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
165 * machine optimized for returning entity positions.
166 * @param *callback Callback function. If count is set, callback is called for
167 * every line of code, comment, or blank with 'lcode', 'lcomment', and
168 * 'lblank' respectively. Otherwise callback is called for each entity found.
170 void parse_ruby(char *buffer, int length, int count,
171 void (*callback) (const char *lang, const char *entity, int s,
178 cs = (count) ? ruby_en_ruby_line : ruby_en_ruby_entity;
181 // if no newline at EOF; callback contents of last line
182 if (count) { process_last_line(RUBY_LANG) }
187 /*****************************************************************************/