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
18 #include "general.h" /* must always come first */
31 K_UNDEFINED
= -1, K_CLASS
, K_METHOD
, K_MODULE
, K_SINGLETON
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
58 static vString
* stringListToScope (const stringList
* list
)
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
));
77 * Attempts to advance 's' past 'literal'.
78 * Returns TRUE if it did, FALSE (and leaves 's' where
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)
89 /* Additionally check that we're at the end of a token. */
90 if ( ! (next_char
== 0 || isspace (next_char
) || next_char
== '('))
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
[] = {
107 "!", "~", "+@", "-@",
113 "<=", "<", ">", ">=",
114 "<=>", "==", "===", "!=", "=~", "!~",
118 for (i
= 0; RUBY_OPERATORS
[i
] != 0; ++i
)
120 if (canMatch (cp
, RUBY_OPERATORS
[i
]))
122 vStringCatS (name
, RUBY_OPERATORS
[i
]);
130 * Emits a tag for the given 'name' of kind 'kind' at the current nesting.
132 static void emitRubyTag (vString
* name
, rubyKind kind
)
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
;
149 stringListAdd (nesting
, vStringNewCopy (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
))
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
) ? "_.?!=" : "_";
186 /* Check for an anonymous (singleton) class such as "class << HTTP". */
187 if (kind
== K_CLASS
&& **cp
== '<' && *(*cp
+ 1) == '<')
192 /* Check for operators such as "def []=(key, val)". */
193 if (kind
== K_METHOD
|| kind
== K_SINGLETON
)
195 if (parseRubyOperator (name
, cp
))
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
);
209 if (kind
== K_METHOD
)
211 /* Recognize singleton methods. */
212 if (last_char
== '.')
214 vStringTerminate (name
);
216 return parseIdentifier (cp
, name
, K_SINGLETON
);
219 /* Recognize characters which mark the end of a method name. */
220 if (charIsIn (last_char
, "?!="))
229 static void readAndEmitTag (const unsigned char** cp
, rubyKind expected_kind
)
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)
255 * For now, we don't create any.
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:
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
;
297 if (canMatch (&cp
, "=end"))
299 inMultiLineComment
= FALSE
;
303 skipWhitespace (&cp
);
305 /* Avoid mistakenly starting a scope for modifiers such as
309 * FIXME: this is fooled by code such as
317 * FIXME: we're also fooled if someone does something heinous such as
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
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
);
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
354 if (inMultiLineComment
|| isspace (*cp
))
360 /* FIXME: this is wrong, but there *probably* won't be a
361 * definition after an interpolated string (where # doesn't
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')
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
;
398 /* vi:set tabstop=4 shiftwidth=4: */