Don't add progress ticks to the statusbar when creating progress data.
[anjuta-git-plugin.git] / tagmanager / ruby.c
blob1ee74dd9cd4b60f27c08c4ab9d255e0952c7b9d7
1 /*
2 * $Id$
4 * Copyright (c) 2000-2001, Thaddeus Covert <sahuagin@mediaone.net>
5 * Copyright (c) 2002 Matthias Veit <matthias_veit@yahoo.de>
6 * Copyright (c) 2004 Elliott Hughes <enh@acm.org>
8 * This source code is released for free distribution under the terms of the
9 * GNU General Public License.
11 * This module contains functions for generating tags for Ruby language
12 * files.
16 * INCLUDE FILES
18 #include "general.h" /* must always come first */
20 #include <string.h>
22 #include "entry.h"
23 #include "parse.h"
24 #include "read.h"
25 #include "vstring.h"
28 * DATA DECLARATIONS
30 typedef enum {
31 K_UNDEFINED = -1, K_CLASS, K_METHOD, K_MODULE, K_SINGLETON
32 } rubyKind;
35 * DATA DEFINITIONS
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" }
44 static stringList* nesting = 0;
47 * FUNCTION DEFINITIONS
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.
58 static vString* stringListToScope (const stringList* list)
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)
66 vString* chunk = stringListItem (list, i);
67 if (vStringLength (chunk) > 0)
69 vStringCatS (result, (chunks_output++ > 0) ? "." : "");
70 vStringCatS (result, vStringValue (chunk));
73 return result;
77 * Attempts to advance 's' past 'literal'.
78 * Returns TRUE if it did, FALSE (and leaves 's' where
79 * it was) otherwise.
81 static boolean canMatch (const unsigned char** s, const char* literal)
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)
87 return FALSE;
89 /* Additionally check that we're at the end of a token. */
90 if ( ! (next_char == 0 || isspace (next_char) || next_char == '('))
92 return FALSE;
94 *s += literal_length;
95 return TRUE;
99 * Attempts to advance 'cp' past a Ruby operator method name. Returns
100 * TRUE if successful (and copies the name into 'name'), FALSE otherwise.
102 static boolean parseRubyOperator (vString* name, const unsigned char** cp)
104 static const char* RUBY_OPERATORS[] = {
105 "[]", "[]=",
106 "**",
107 "!", "~", "+@", "-@",
108 "*", "/", "%",
109 "+", "-",
110 ">>", "<<",
111 "&",
112 "^", "|",
113 "<=", "<", ">", ">=",
114 "<=>", "==", "===", "!=", "=~", "!~",
117 int i;
118 for (i = 0; RUBY_OPERATORS[i] != 0; ++i)
120 if (canMatch (cp, RUBY_OPERATORS[i]))
122 vStringCatS (name, RUBY_OPERATORS[i]);
123 return TRUE;
126 return FALSE;
130 * Emits a tag for the given 'name' of kind 'kind' at the current nesting.
132 static void emitRubyTag (vString* name, rubyKind kind)
134 tagEntryInfo tag;
135 vString* scope;
137 vStringTerminate (name);
138 scope = stringListToScope (nesting);
140 initTagEntry (&tag, vStringValue (name));
141 if (vStringLength (scope) > 0) {
142 tag.extensionFields.scope [0] = "class";
143 tag.extensionFields.scope [1] = vStringValue (scope);
145 tag.kindName = RubyKinds [kind].name;
146 tag.kind = RubyKinds [kind].letter;
147 makeTagEntry (&tag);
149 stringListAdd (nesting, vStringNewCopy (name));
151 vStringClear (name);
152 vStringDelete (scope);
155 /* Tests whether 'ch' is a character in 'list'. */
156 static boolean charIsIn (char ch, const char* list)
158 return (strchr (list, ch) != 0);
161 /* Advances 'cp' over leading whitespace. */
162 static void skipWhitespace (const unsigned char** cp)
164 while (isspace (**cp))
166 ++*cp;
171 * Copies the characters forming an identifier from *cp into
172 * name, leaving *cp pointing to the character after the identifier.
174 static rubyKind parseIdentifier (
175 const unsigned char** cp, vString* name, rubyKind kind)
177 /* Method names are slightly different to class and variable names.
178 * A method name may optionally end with a question mark, exclamation
179 * point or equals sign. These are all part of the name.
180 * A method name may also contain a period if it's a singleton method.
182 const char* also_ok = (kind == K_METHOD) ? "_.?!=" : "_";
184 skipWhitespace (cp);
186 /* Check for an anonymous (singleton) class such as "class << HTTP". */
187 if (kind == K_CLASS && **cp == '<' && *(*cp + 1) == '<')
189 return K_UNDEFINED;
192 /* Check for operators such as "def []=(key, val)". */
193 if (kind == K_METHOD || kind == K_SINGLETON)
195 if (parseRubyOperator (name, cp))
197 return kind;
201 /* Copy the identifier into 'name'. */
202 while (**cp != 0 && (isalnum (**cp) || charIsIn (**cp, also_ok)))
204 char last_char = **cp;
206 vStringPut (name, last_char);
207 ++*cp;
209 if (kind == K_METHOD)
211 /* Recognize singleton methods. */
212 if (last_char == '.')
214 vStringTerminate (name);
215 vStringClear (name);
216 return parseIdentifier (cp, name, K_SINGLETON);
219 /* Recognize characters which mark the end of a method name. */
220 if (charIsIn (last_char, "?!="))
222 break;
226 return kind;
229 static void readAndEmitTag (const unsigned char** cp, rubyKind expected_kind)
231 if (isspace (**cp))
233 vString *name = vStringNew ();
234 rubyKind actual_kind = parseIdentifier (cp, name, expected_kind);
236 if (actual_kind == K_UNDEFINED || vStringLength (name) == 0)
239 * What kind of tags should we create for code like this?
241 * %w(self.clfloor clfloor).each do |name|
242 * module_eval <<-"end;"
243 * def #{name}(x, y=1)
244 * q, r = x.divmod(y)
245 * q = q.to_i
246 * return q, r
247 * end
248 * end;
249 * end
251 * Or this?
253 * class << HTTP
255 * For now, we don't create any.
258 else
260 emitRubyTag (name, actual_kind);
262 vStringDelete (name);
266 static void enterUnnamedScope (void)
268 stringListAdd (nesting, vStringNewInit (""));
271 static void findRubyTags (void)
273 const unsigned char *line;
274 boolean inMultiLineComment = FALSE;
276 nesting = stringListNew ();
278 /* FIXME: this whole scheme is wrong, because Ruby isn't line-based.
279 * You could perfectly well write:
281 * def
282 * method
283 * puts("hello")
284 * end
286 * if you wished, and this function would fail to recognize anything.
288 while ((line = fileReadLine ()) != NULL)
290 const unsigned char *cp = line;
292 if (canMatch (&cp, "=begin"))
294 inMultiLineComment = TRUE;
295 continue;
297 if (canMatch (&cp, "=end"))
299 inMultiLineComment = FALSE;
300 continue;
303 skipWhitespace (&cp);
305 /* Avoid mistakenly starting a scope for modifiers such as
307 * return if <exp>
309 * FIXME: this is fooled by code such as
311 * result = if <exp>
312 * <a>
313 * else
314 * <b>
315 * end
317 * FIXME: we're also fooled if someone does something heinous such as
319 * puts("hello") \
320 * unless <exp>
322 if (canMatch (&cp, "case") || canMatch (&cp, "for") ||
323 canMatch (&cp, "if") || canMatch (&cp, "unless") ||
324 canMatch (&cp, "while"))
326 enterUnnamedScope ();
330 * "module M", "class C" and "def m" should only be at the beginning
331 * of a line.
333 if (canMatch (&cp, "module"))
335 readAndEmitTag (&cp, K_MODULE);
337 else if (canMatch (&cp, "class"))
339 readAndEmitTag (&cp, K_CLASS);
341 else if (canMatch (&cp, "def"))
343 readAndEmitTag (&cp, K_METHOD);
346 while (*cp != '\0')
348 /* FIXME: we don't cope with here documents, or string literals,
349 * or regular expression literals, or ... you get the idea.
350 * Hopefully, the restriction above that insists on seeing
351 * definitions at the starts of lines should keep us out of
352 * mischief.
354 if (inMultiLineComment || isspace (*cp))
356 ++cp;
358 else if (*cp == '#')
360 /* FIXME: this is wrong, but there *probably* won't be a
361 * definition after an interpolated string (where # doesn't
362 * mean 'comment').
364 break;
366 else if (canMatch (&cp, "begin") || canMatch (&cp, "do"))
368 enterUnnamedScope ();
370 else if (canMatch (&cp, "end") && stringListCount (nesting) > 0)
372 /* Leave the most recent scope. */
373 vStringDelete (stringListLast (nesting));
374 stringListRemoveLast (nesting);
376 else if (*cp != '\0')
379 ++cp;
380 while (isalnum (*cp) || *cp == '_');
384 stringListDelete (nesting);
387 extern parserDefinition* RubyParser (void)
389 static const char *const extensions [] = { "rb", "ruby", NULL };
390 parserDefinition* def = parserNew ("Ruby");
391 def->kinds = RubyKinds;
392 def->kindCount = KIND_COUNT (RubyKinds);
393 def->extensions = extensions;
394 def->parser = findRubyTags;
395 return def;
398 /* vi:set tabstop=4 shiftwidth=4: */