Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / ruby.rl
1 // ruby.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_RUBY_PARSER_H
5 #define OHCOUNT_RUBY_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *RUBY_LANG = LANG_RUBY;
11
12 // the languages entities
13 const char *ruby_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   RUBY_SPACE = 0, RUBY_COMMENT, RUBY_STRING, RUBY_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine ruby;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action ruby_ccallback {
32     switch(entity) {
33     case RUBY_SPACE:
34       ls
35       break;
36     case RUBY_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(RUBY_LANG)
41       break;
42     case NEWLINE:
43       std_newline(RUBY_LANG)
44     }
45   }
46
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.
50   ruby_block_comment =
51     '=begin' @enqueue @comment (
52       newline %{ entity = INTERNAL_NL; } %ruby_ccallback
53       |
54       ws
55       |
56       (nonnewline - ws) @comment
57     )* :>> '=end' @commit;
58   ruby_comment = ruby_line_comment | ruby_block_comment;
59
60   ruby_sq_str =
61     '\'' @enqueue @code (
62       newline %{ entity = INTERNAL_NL; } %ruby_ccallback
63       |
64       ws
65       |
66       [^\r\n\f\t '\\] @code
67       |
68       '\\' nonnewline @code
69     )* '\'' @commit @code;
70   ruby_dq_str =
71     '"' @enqueue @code (
72       newline %{ entity = INTERNAL_NL; } %ruby_ccallback
73       |
74       ws
75       |
76       [^\r\n\f\t "\\] @code
77       |
78       '\\' nonnewline @code
79     )* '"' @commit @code;
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
85   # string.
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.
89   ruby_lit_str =
90     '%' [qQ]? [(\[{] @enqueue @code (
91       newline %{ entity = INTERNAL_NL; } %ruby_ccallback
92       |
93       ws
94       |
95       [^\r\n\f\t )\]}\\] @code
96       |
97       '\\' nonnewline @code
98     )* [)\]}] @commit @code;
99   ruby_cmd_str =
100     '`' @enqueue @code (
101       newline %{ entity = INTERNAL_NL; } %ruby_ccallback
102       |
103       ws
104       |
105       [^\r\n\f\t `\\] @code
106       |
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
112   ruby_lit_other =
113     '%' [wrx] [(\[{] @enqueue @code (
114       newline %{ entity = INTERNAL_NL; } %ruby_ccallback
115       |
116       ws
117       |
118       [^\r\n\f\t )\]}\\] @code
119       |
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.
125   # ruby_heredoc =
126   ruby_string =
127     ruby_sq_str | ruby_dq_str | ruby_lit_str | ruby_cmd_str | ruby_regex |
128     ruby_lit_other;
129
130   ruby_line := |*
131     spaces        ${ entity = RUBY_SPACE; } => ruby_ccallback;
132     ruby_comment;
133     ruby_string;
134     newline       ${ entity = NEWLINE;    } => ruby_ccallback;
135     ^space        ${ entity = RUBY_ANY;   } => ruby_ccallback;
136   *|;
137
138   # Entity machine
139
140   action ruby_ecallback {
141     callback(RUBY_LANG, ruby_entities[entity], cint(ts), cint(te), userdata);
142   }
143
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;
148
149   ruby_entity := |*
150     space+              ${ entity = RUBY_SPACE;   } => ruby_ecallback;
151     ruby_comment_entity ${ entity = RUBY_COMMENT; } => ruby_ecallback;
152     # TODO:
153     ^space;
154   *|;
155 }%%
156
157 /************************* Required for every parser *************************/
158
159 /* Parses a string buffer with Ruby code.
160  *
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.
169  */
170 void parse_ruby(char *buffer, int length, int count,
171                 void (*callback) (const char *lang, const char *entity, int s,
172                                   int e, void *udata),
173                 void *userdata
174   ) {
175   init
176
177   %% write init;
178   cs = (count) ? ruby_en_ruby_line : ruby_en_ruby_entity;
179   %% write exec;
180
181   // if no newline at EOF; callback contents of last line
182   if (count) { process_last_line(RUBY_LANG) }
183 }
184
185 #endif
186
187 /*****************************************************************************/