2 * Copyright (c) 2000-2005, 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 makefiles.
13 #include "general.h" /* must always come first */
33 static kindOption MakeKinds
[] = {
34 { TRUE
, 'm', "macro", "macros"},
35 { TRUE
, 't', "target", "targets"}
39 * FUNCTION DEFINITIONS
42 static int nextChar (void)
54 static void skipLine (void)
59 while (c
!= EOF
&& c
!= '\n');
64 static int skipToNonWhite (int c
)
66 while (c
!= '\n' && isspace (c
))
71 static boolean
isIdentifier (int c
)
73 return (boolean
)(c
!= '\0' && (isalnum (c
) || strchr (".-_/$(){}%", c
) != NULL
));
76 static boolean
isSpecialTarget (vString
*const name
)
79 /* All special targets begin with '.'. */
80 if (vStringLength (name
) < 1 || vStringChar (name
, i
++) != '.') {
83 while (i
< vStringLength (name
)) {
84 char ch
= vStringChar (name
, i
++);
85 if (ch
!= '_' && !isupper (ch
))
93 static void newTarget (vString
*const name
)
95 /* Ignore GNU Make's "special targets". */
96 if (isSpecialTarget (name
))
100 makeSimpleTag (name
, MakeKinds
, K_TARGET
);
103 static void newMacro (vString
*const name
)
105 makeSimpleTag (name
, MakeKinds
, K_MACRO
);
108 static void readIdentifier (const int first
, vString
*const id
)
113 while (isIdentifier (c
) || (depth
> 0 && c
!= EOF
&& c
!= '\n'))
115 if (c
== '(' || c
== '{')
117 else if (depth
> 0 && (c
== ')' || c
== '}'))
123 vStringTerminate (id
);
126 static void findMakeTags (void)
128 stringList
*identifiers
= stringListNew ();
129 boolean newline
= TRUE
;
130 boolean in_define
= FALSE
;
131 boolean in_rule
= FALSE
;
132 boolean variable_possible
= TRUE
;
135 while ((c
= nextChar ()) != EOF
)
141 if (c
== '\t' || (c
= skipToNonWhite (c
)) == '#')
143 skipLine (); /* skip rule or comment */
149 stringListClear (identifiers
);
150 variable_possible
= (boolean
)(!in_rule
);
155 else if (isspace (c
))
159 else if (variable_possible
&& c
== '?')
163 variable_possible
= (c
== '=');
165 else if (variable_possible
&& c
== ':' &&
166 stringListCount (identifiers
) > 0)
173 for (i
= 0; i
< stringListCount (identifiers
); i
++)
174 newTarget (stringListItem (identifiers
, i
));
175 stringListClear (identifiers
);
179 else if (variable_possible
&& c
== '=' &&
180 stringListCount (identifiers
) == 1)
182 newMacro (stringListItem (identifiers
, 0));
186 else if (variable_possible
&& isIdentifier (c
))
188 vString
*name
= vStringNew ();
189 readIdentifier (c
, name
);
190 stringListAdd (identifiers
, name
);
192 if (stringListCount (identifiers
) == 1)
194 if (in_define
&& ! strcmp (vStringValue (name
), "endef"))
198 else if (! strcmp (vStringValue (name
), "define"))
201 c
= skipToNonWhite (nextChar ());
203 /* all remaining characters on the line are the name -- even spaces */
204 while (c
!= EOF
&& c
!= '\n')
206 vStringPut (name
, c
);
211 vStringTerminate (name
);
212 vStringStripTrailing (name
);
215 else if (! strcmp (vStringValue (name
), "export"))
216 stringListClear (identifiers
);
220 variable_possible
= FALSE
;
222 stringListDelete (identifiers
);
225 extern parserDefinition
* MakefileParser (void)
227 static const char *const patterns
[] = { "[Mm]akefile", "GNUmakefile", NULL
};
228 static const char *const extensions
[] = { "mak", "mk", NULL
};
229 parserDefinition
* const def
= parserNew ("Make");
230 def
->kinds
= MakeKinds
;
231 def
->kindCount
= KIND_COUNT (MakeKinds
);
232 def
->patterns
= patterns
;
233 def
->extensions
= extensions
;
234 def
->parser
= findMakeTags
;
238 /* vi:set tabstop=4 shiftwidth=4: */