Debian Codegraph @

ruby.c

raw
1 /*
2 * $Id: ruby.c 571 2007-06-24 23:32:14Z elliotth $
3 *
4 * Copyright (c) 2000-2001, Thaddeus Covert <[email protected]>
5 * Copyright (c) 2002 Matthias Veit <[email protected]>
6 * Copyright (c) 2004 Elliott Hughes <[email protected]>
7 *
8 * This source code is released for free distribution under the terms of the
9 * GNU General Public License.
10 *
11 * This module contains functions for generating tags for Ruby language
12 * files.
13 */
14
15 /*
16 * INCLUDE FILES
17 */
18 #include "general.h" /* must always come first */
19
20 #include <string.h>
21
22 #include "entry.h"
23 #include "parse.h"
24 #include "read.h"
25 #include "vstring.h"
26
27 /*
28 * DATA DECLARATIONS
29 */
30 typedef enum {
31 K_UNDEFINED = -1, K_CLASS, K_METHOD, K_MODULE, K_SINGLETON
32 } rubyKind;
33
34 /*
35 * DATA DEFINITIONS
36 */
37 static kindOption RubyKinds [] = {
38 { TRUE, 'c', "class", "classes" },
39 { TRUE, 'f', "method", "methods" },
40 { TRUE, 'm', "module", "modules" },
41 { TRUE, 'F', "singleton method", "singleton methods" }
42 };
43
44 static stringList* nesting = 0;
45
46 /*
47 * FUNCTION DEFINITIONS
48 */
49
50 /*
51 * Returns a string describing the scope in 'list'.
52 * We record the current scope as a list of entered scopes.
53 * Scopes corresponding to 'if' statements and the like are
54 * represented by empty strings. Scopes corresponding to
55 * modules and classes are represented by the name of the
56 * module or class.
57 */
58 static vString* stringListToScope (const stringList* list)
59 {
60 unsigned int i;
61 unsigned int chunks_output = 0;
62 vString* result = vStringNew ();
63 const unsigned int max = stringListCount (list);
64 for (i = 0; i < max; ++i)
65 {
66 vString* chunk = stringListItem (list, i);
67 if (vStringLength (chunk) > 0)
68 {
69 vStringCatS (result, (chunks_output++ > 0) ? "." : "");
70 vStringCatS (result, vStringValue (chunk));
71 }
72 }
73 return result;
74 }
75
76 /*
77 * Attempts to advance 's' past 'literal'.
78 * Returns TRUE if it did, FALSE (and leaves 's' where
79 * it was) otherwise.
80 */
81 static boolean canMatch (const unsigned char** s, const char* literal)
82 {
83 const int literal_length = strlen (literal);
84 const unsigned char next_char = *(*s + literal_length);
85 if (strncmp ((const char*) *s, literal, literal_length) != 0)
86 {
87 return FALSE;
88 }
89 /* Additionally check that we're at the end of a token. */
90 if ( ! (next_char == 0 || isspace (next_char) || next_char == '('))
91 {
92 return FALSE;
93 }
94 *s += literal_length;
95 return TRUE;
96 }
97
98 /*
99 * Attempts to advance 'cp' past a Ruby operator method name. Returns
100 * TRUE if successful (and copies the name into 'name'), FALSE otherwise.
101 */
102 static boolean parseRubyOperator (vString* name, const unsigned char** cp)
103 {
104 static const char* RUBY_OPERATORS[] = {
105 "[]", "[]=",
106 "**",
107 "!", "~", "+@", "-@",
108 "*", "/", "%",
109 "+", "-",
110 ">>", "<<",
111 "&",
112 "^", "|",
113 "<=", "<", ">", ">=",
114 "<=>", "==", "===", "!=", "=~", "!~",
115 "`",
116 0
117 };
118 int i;
119 for (i = 0; RUBY_OPERATORS[i] != 0; ++i)
120 {
121 if (canMatch (cp, RUBY_OPERATORS[i]))
122 {
123 vStringCatS (name, RUBY_OPERATORS[i]);
124 return TRUE;
125 }
126 }
127 return FALSE;
128 }
129
130 /*
131 * Emits a tag for the given 'name' of kind 'kind' at the current nesting.
132 */
133 static void emitRubyTag (vString* name, rubyKind kind)
134 {
135 tagEntryInfo tag;
136 vString* scope;
137
138 vStringTerminate (name);
139 scope = stringListToScope (nesting);
140
141 initTagEntry (&tag, vStringValue (name));
142 if (vStringLength (scope) > 0) {
143 tag.extensionFields.scope [0] = "class";
144 tag.extensionFields.scope [1] = vStringValue (scope);
145 }
146 tag.kindName = RubyKinds [kind].name;
147 tag.kind = RubyKinds [kind].letter;
148 makeTagEntry (&tag);
149
150 stringListAdd (nesting, vStringNewCopy (name));
151
152 vStringClear (name);
153 vStringDelete (scope);
154 }
155
156 /* Tests whether 'ch' is a character in 'list'. */
157 static boolean charIsIn (char ch, const char* list)
158 {
159 return (strchr (list, ch) != 0);
160 }
161
162 /* Advances 'cp' over leading whitespace. */
163 static void skipWhitespace (const unsigned char** cp)
164 {
165 while (isspace (**cp))
166 {
167 ++*cp;
168 }
169 }
170
171 /*
172 * Copies the characters forming an identifier from *cp into
173 * name, leaving *cp pointing to the character after the identifier.
174 */
175 static rubyKind parseIdentifier (
176 const unsigned char** cp, vString* name, rubyKind kind)
177 {
178 /* Method names are slightly different to class and variable names.
179 * A method name may optionally end with a question mark, exclamation
180 * point or equals sign. These are all part of the name.
181 * A method name may also contain a period if it's a singleton method.
182 */
183 const char* also_ok = (kind == K_METHOD) ? "_.?!=" : "_";
184
185 skipWhitespace (cp);
186
187 /* Check for an anonymous (singleton) class such as "class << HTTP". */
188 if (kind == K_CLASS && **cp == '<' && *(*cp + 1) == '<')
189 {
190 return K_UNDEFINED;
191 }
192
193 /* Check for operators such as "def []=(key, val)". */
194 if (kind == K_METHOD || kind == K_SINGLETON)
195 {
196 if (parseRubyOperator (name, cp))
197 {
198 return kind;
199 }
200 }
201
202 /* Copy the identifier into 'name'. */
203 while (**cp != 0 && (isalnum (**cp) || charIsIn (**cp, also_ok)))
204 {
205 char last_char = **cp;
206
207 vStringPut (name, last_char);
208 ++*cp;
209
210 if (kind == K_METHOD)
211 {
212 /* Recognize singleton methods. */
213 if (last_char == '.')
214 {
215 vStringTerminate (name);
216 vStringClear (name);
217 return parseIdentifier (cp, name, K_SINGLETON);
218 }
219
220 /* Recognize characters which mark the end of a method name. */
221 if (charIsIn (last_char, "?!="))
222 {
223 break;
224 }
225 }
226 }
227 return kind;
228 }
229
230 static void readAndEmitTag (const unsigned char** cp, rubyKind expected_kind)
231 {
232 if (isspace (**cp))
233 {
234 vString *name = vStringNew ();
235 rubyKind actual_kind = parseIdentifier (cp, name, expected_kind);
236
237 if (actual_kind == K_UNDEFINED || vStringLength (name) == 0)
238 {
239 /*
240 * What kind of tags should we create for code like this?
241 *
242 * %w(self.clfloor clfloor).each do |name|
243 * module_eval <<-"end;"
244 * def #{name}(x, y=1)
245 * q, r = x.divmod(y)
246 * q = q.to_i
247 * return q, r
248 * end
249 * end;
250 * end
251 *
252 * Or this?
253 *
254 * class << HTTP
255 *
256 * For now, we don't create any.
257 */
258 }
259 else
260 {
261 emitRubyTag (name, actual_kind);
262 }
263 vStringDelete (name);
264 }
265 }
266
267 static void enterUnnamedScope (void)
268 {
269 stringListAdd (nesting, vStringNewInit (""));
270 }
271
272 static void findRubyTags (void)
273 {
274 const unsigned char *line;
275 boolean inMultiLineComment = FALSE;
276
277 nesting = stringListNew ();
278
279 /* FIXME: this whole scheme is wrong, because Ruby isn't line-based.
280 * You could perfectly well write:
281 *
282 * def
283 * method
284 * puts("hello")
285 * end
286 *
287 * if you wished, and this function would fail to recognize anything.
288 */
289 while ((line = fileReadLine ()) != NULL)
290 {
291 const unsigned char *cp = line;
292
293 if (canMatch (&cp, "=begin"))
294 {
295 inMultiLineComment = TRUE;
296 continue;
297 }
298 if (canMatch (&cp, "=end"))
299 {
300 inMultiLineComment = FALSE;
301 continue;
302 }
303
304 skipWhitespace (&cp);
305
306 /* Avoid mistakenly starting a scope for modifiers such as
307 *
308 * return if <exp>
309 *
310 * FIXME: this is fooled by code such as
311 *
312 * result = if <exp>
313 * <a>
314 * else
315 * <b>
316 * end
317 *
318 * FIXME: we're also fooled if someone does something heinous such as
319 *
320 * puts("hello") \
321 * unless <exp>
322 */
323 if (canMatch (&cp, "case") || canMatch (&cp, "for") ||
324 canMatch (&cp, "if") || canMatch (&cp, "unless") ||
325 canMatch (&cp, "while"))
326 {
327 enterUnnamedScope ();
328 }
329
330 /*
331 * "module M", "class C" and "def m" should only be at the beginning
332 * of a line.
333 */
334 if (canMatch (&cp, "module"))
335 {
336 readAndEmitTag (&cp, K_MODULE);
337 }
338 else if (canMatch (&cp, "class"))
339 {
340 readAndEmitTag (&cp, K_CLASS);
341 }
342 else if (canMatch (&cp, "def"))
343 {
344 readAndEmitTag (&cp, K_METHOD);
345 }
346
347 while (*cp != '\0')
348 {
349 /* FIXME: we don't cope with here documents,
350 * or regular expression literals, or ... you get the idea.
351 * Hopefully, the restriction above that insists on seeing
352 * definitions at the starts of lines should keep us out of
353 * mischief.
354 */
355 if (inMultiLineComment || isspace (*cp))
356 {
357 ++cp;
358 }
359 else if (*cp == '#')
360 {
361 /* FIXME: this is wrong, but there *probably* won't be a
362 * definition after an interpolated string (where # doesn't
363 * mean 'comment').
364 */
365 break;
366 }
367 else if (canMatch (&cp, "begin") || canMatch (&cp, "do"))
368 {
369 enterUnnamedScope ();
370 }
371 else if (canMatch (&cp, "end") && stringListCount (nesting) > 0)
372 {
373 /* Leave the most recent scope. */
374 vStringDelete (stringListLast (nesting));
375 stringListRemoveLast (nesting);
376 }
377 else if (*cp == '"')
378 {
379 /* Skip string literals.
380 * FIXME: should cope with escapes and interpolation.
381 */
382 do {
383 ++cp;
384 } while (*cp != 0 && *cp != '"');
385 }
386 else if (*cp != '\0')
387 {
388 do
389 ++cp;
390 while (isalnum (*cp) || *cp == '_');
391 }
392 }
393 }
394 stringListDelete (nesting);
395 }
396
397 extern parserDefinition* RubyParser (void)
398 {
399 static const char *const extensions [] = { "rb", "ruby", NULL };
400 parserDefinition* def = parserNew ("Ruby");
401 def->kinds = RubyKinds;
402 def->kindCount = KIND_COUNT (RubyKinds);
403 def->extensions = extensions;
404 def->parser = findRubyTags;
405 return def;
406 }
407
408 /* vi:set tabstop=4 shiftwidth=4: */