1 // ruby.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_RUBY_PARSER
5 #define RAGEL_RUBY_PARSER
7 #include "ragel_parser_macros.h"
9 // the name of the language
10 const char *RUBY_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.
52 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
56 (nonnewline - ws) @comment
58 ruby_comment = ruby_line_comment | ruby_block_comment;
62 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
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]? [(\[{] @code (
91 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
95 [^\r\n\f\t )\]}\\] @code
101 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
105 [^\r\n\f\t `\\] @code
107 '\\' nonnewline @code
111 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
115 [^\r\n\f\t /\\] @code
117 '\\' nonnewline @code
119 # TODO: true literal array and command detection
120 # See TODO above about literal string detection
122 '%' [wrx] [(\[{] @code (
123 newline %{ entity = INTERNAL_NL; } %ruby_ccallback
127 [^\r\n\f\t )\]}\\] @code
129 '\\' nonnewline @code
131 # TODO: heredoc detection
132 # This is impossible with current Ragel. We need to extract what the end
133 # delimiter should be from the heredoc and search up to it on a new line.
136 ruby_sq_str | ruby_dq_str | ruby_lit_str | ruby_cmd_str | ruby_regex |
140 spaces ${ entity = RUBY_SPACE; } => ruby_ccallback;
143 newline ${ entity = NEWLINE; } => ruby_ccallback;
144 ^space ${ entity = RUBY_ANY; } => ruby_ccallback;
149 action ruby_ecallback {
150 callback(RUBY_LANG, entity, cint(ts), cint(te));
153 ruby_entity := 'TODO:';
156 /************************* Required for every parser *************************/
158 /* Parses a string buffer with Ruby code.
160 * @param *buffer The string to parse.
161 * @param length The length of the string to parse.
162 * @param count Integer flag specifying whether or not to count lines. If yes,
163 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
164 * machine optimized for returning entity positions.
165 * @param *callback Callback function. If count is set, callback is called for
166 * every line of code, comment, or blank with 'lcode', 'lcomment', and
167 * 'lblank' respectively. Otherwise callback is called for each entity found.
169 void parse_ruby(char *buffer, int length, int count,
170 void (*callback) (const char *lang, const char *entity, int start, int end)
175 cs = (count) ? ruby_en_ruby_line : ruby_en_ruby_entity;
178 // if no newline at EOF; callback contents of last line
179 if (count) { process_last_line(RUBY_LANG) }
184 /*****************************************************************************/