2 * Copyright (c) 2000-2003, Darren Hiebert
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License version 2 or (at your option) any later version.
7 * This module contains functions for generating tags for PERL language
14 #include "general.h" /* must always come first */
26 #define TRACE_PERL_C 0
27 #define TRACE if (TRACE_PERL_C) printf("perl.c:%d: ", __LINE__), printf
39 K_SUBROUTINE_DECLARATION
42 static kindOption PerlKinds
[] = {
43 { true, 'c', "constant", "constants" },
44 { true, 'f', "format", "formats" },
45 { true, 'l', "label", "labels" },
46 { true, 'p', "package", "packages" },
47 { true, 's', "subroutine", "subroutines" },
48 { false, 'd', "subroutineDeclaration", "subroutine declarations" },
52 * FUNCTION DEFINITIONS
55 static bool isIdentifier1 (int c
)
57 return (bool) (isalpha (c
) || c
== '_');
60 static bool isIdentifier (int c
)
62 return (bool) (isalnum (c
) || c
== '_');
65 static bool isPodWord (const char *word
)
70 const char *const pods
[] = {
71 "head1", "head2", "head3", "head4", "over", "item", "back",
72 "pod", "begin", "end", "for"
74 const size_t count
= ARRAY_SIZE (pods
);
75 const char *white
= strpbrk (word
, " \t");
76 const size_t len
= (white
!=NULL
) ? (size_t)(white
-word
) : strlen (word
);
77 char *const id
= (char*) eMalloc (len
+ 1);
79 strncpy (id
, word
, len
);
81 for (i
= 0 ; i
< count
&& ! result
; ++i
)
83 if (strcmp (id
, pods
[i
]) == 0)
92 * Perl subroutine declaration may look like one of the following:
97 * sub abc (proto) :attr;
99 * Note that there may be more than one attribute. Attributes may
100 * have things in parentheses (they look like arguments). Anything
101 * inside of those parentheses goes. Prototypes may contain semi-colons.
102 * The matching end when we encounter (outside of any parentheses) either
103 * a semi-colon (that'd be a declaration) or an left curly brace
106 * This is pretty complicated parsing (plus we all know that only perl can
107 * parse Perl), so we are only promising best effort here.
109 * If we can't determine what this is (due to a file ending, for example),
110 * we will return false.
112 static bool isSubroutineDeclaration (const unsigned char *cp
)
124 else if (true == attr
)
125 return false; /* Invalid attribute name */
148 if (isIdentifier1(*cp
)) {
150 while (isIdentifier (*cp
))
153 goto SUB_DECL_SWITCH
; /* Instead of --cp; */
157 } else if (nparens
) {
164 } while (NULL
!= (cp
= readLineFromInputFile ()));
169 /* Algorithm adapted from from GNU etags.
170 * Perl support by Bart Robinson <lomew@cs.utah.edu>
171 * Perl sub names: look for /^ [ \t\n]sub [ \t\n]+ [^ \t\n{ (]+/
173 static void findPerlTags (void)
175 vString
*name
= vStringNew ();
176 vString
*package
= NULL
;
177 bool skipPodDoc
= false;
178 const unsigned char *line
;
180 while ((line
= readLineFromInputFile ()) != NULL
)
182 bool spaceRequired
= false;
183 bool qualified
= false;
184 const unsigned char *cp
= line
;
185 perlKind kind
= K_NONE
;
190 if (strncmp ((const char*) line
, "=cut", (size_t) 4) == 0)
194 else if (line
[0] == '=')
196 skipPodDoc
= isPodWord ((const char*)line
+ 1);
199 else if (strcmp ((const char*) line
, "__DATA__") == 0)
201 else if (strcmp ((const char*) line
, "__END__") == 0)
203 else if (line
[0] == '#')
206 while (isspace (*cp
))
209 if (strncmp((const char*) cp
, "sub", (size_t) 3) == 0)
211 TRACE("this looks like a sub\n");
214 spaceRequired
= true;
217 else if (strncmp((const char*) cp
, "use", (size_t) 3) == 0)
222 while (*cp
&& isspace (*cp
))
224 if (strncmp((const char*) cp
, "constant", (size_t) 8) != 0)
228 spaceRequired
= true;
231 else if (strncmp((const char*) cp
, "package", (size_t) 7) == 0)
233 /* This will point to space after 'package' so that a tag
235 const unsigned char *space
= cp
+= 7;
238 package
= vStringNew ();
240 vStringClear (package
);
241 while (isspace (*cp
))
243 while ((int) *cp
!= ';' && !isspace ((int) *cp
))
245 vStringPut (package
, (int) *cp
);
248 vStringCatS (package
, "::");
250 cp
= space
; /* Rewind */
252 spaceRequired
= true;
255 else if (strncmp((const char*) cp
, "format", (size_t) 6) == 0)
259 spaceRequired
= true;
264 if (isIdentifier1 (*cp
))
266 const unsigned char *p
= cp
;
267 while (isIdentifier (*p
))
271 if ((int) *p
== ':' && (int) *(p
+ 1) != ':')
277 TRACE("cp0: %s\n", (const char *) cp
);
278 if (spaceRequired
&& *cp
&& !isspace (*cp
))
281 TRACE("cp1: %s\n", (const char *) cp
);
282 while (isspace (*cp
))
285 while (!*cp
|| '#' == *cp
) { /* Gobble up empty lines
287 cp
= readLineFromInputFile ();
290 while (isspace (*cp
))
294 while (isIdentifier (*cp
) || (K_PACKAGE
== kind
&& ':' == *cp
))
296 vStringPut (name
, (int) *cp
);
300 if (K_FORMAT
== kind
&&
301 vStringLength (name
) == 0 && /* cp did not advance */
304 /* format's name is optional. If it's omitted, 'STDOUT'
306 vStringCatS (name
, "STDOUT");
309 TRACE("name: %s\n", name
->buffer
);
311 if (0 == vStringLength(name
)) {
316 if (K_SUBROUTINE
== kind
)
319 * isSubroutineDeclaration() may consume several lines. So
320 * we record line positions.
322 initTagEntry(&e
, vStringValue(name
), &(PerlKinds
[kind
]));
324 if (true == isSubroutineDeclaration(cp
)) {
325 if (true == PerlKinds
[K_SUBROUTINE_DECLARATION
].enabled
) {
326 kind
= K_SUBROUTINE_DECLARATION
;
335 if (isXtagEnabled(XTAG_QUALIFIED_TAGS
) && qualified
&&
336 package
!= NULL
&& vStringLength (package
) > 0)
338 vString
*const qualifiedName
= vStringNew ();
339 vStringCopy (qualifiedName
, package
);
340 vStringCat (qualifiedName
, name
);
341 e
.name
= vStringValue(qualifiedName
);
343 vStringDelete (qualifiedName
);
345 } else if (vStringLength (name
) > 0)
347 makeSimpleTag (name
, PerlKinds
, kind
);
348 if (isXtagEnabled(XTAG_QUALIFIED_TAGS
) && qualified
&&
350 package
!= NULL
&& vStringLength (package
) > 0)
352 vString
*const qualifiedName
= vStringNew ();
353 vStringCopy (qualifiedName
, package
);
354 vStringCat (qualifiedName
, name
);
355 makeSimpleTag (qualifiedName
, PerlKinds
, kind
);
356 vStringDelete (qualifiedName
);
364 vStringDelete (name
);
366 vStringDelete (package
);
369 extern parserDefinition
* PerlParser (void)
371 static const char *const extensions
[] = { "pl", "pm", "plx", "perl", NULL
};
372 parserDefinition
* def
= parserNew ("Perl");
373 def
->kinds
= PerlKinds
;
374 def
->kindCount
= ARRAY_SIZE (PerlKinds
);
375 def
->extensions
= extensions
;
376 def
->parser
= findPerlTags
;