~ubuntu-branches/ubuntu/gutsy/geany/gutsy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
*
*   Copyright (c) 2000-2001, Darren Hiebert
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License.
*
*   This module contains functions for generating tags for Python language
*   files.
*/

/*
*   INCLUDE FILES
*/
#include "general.h"	/* must always come first */
#include <glib.h>
#include <string.h>

#include "parse.h"
#include "read.h"
#include "vstring.h"

/*
*   DATA DEFINITIONS
*/
typedef enum {
    K_CLASS, K_FUNCTION, K_METHOD
} pythonKind;

static kindOption PythonKinds [] = {
    { TRUE, 'c', "class",    "classes" },
    { TRUE, 'f', "function", "functions" },
    { TRUE, 'm', "member", "methods" }
};

typedef struct _lastClass {
	gchar *name;
	gint indent;
} lastClass;

/*
*   FUNCTION DEFINITIONS
*/


// remove all previous classes with more indent than the current one
static GList *clean_class_list(GList *list, gint indent)
{
	GList *tmp, *tmp2;

	tmp = g_list_first(list);
	while (tmp != NULL)
	{
		if (((lastClass*)tmp->data)->indent >= indent)
		{
			g_free(((lastClass*)tmp->data)->name);
			g_free(tmp->data);
			tmp2 = tmp->next;

			list = g_list_remove(list, tmp->data);
			tmp = tmp2;
		}
		else
		{
			tmp = tmp->next;
		}
	}

	return list;
}


static void findPythonTags (void)
{
    GList *parents = NULL, *tmp; // list of classes which are around the token
    vString *name = vStringNew ();
    gint indent;
    const unsigned char *line;
    boolean inMultilineString = FALSE;

    while ((line = fileReadLine ()) != NULL)
    {
	const unsigned char *cp = line;
	indent = 0;
	while (*cp != '\0')
	{
	    if (*cp=='"' &&
		strncmp ((const char*) cp, "\"\"\"", (size_t) 3) == 0)
	    {
		inMultilineString = (boolean) !inMultilineString;
		cp += 3;
	    }
	    if (*cp=='\'' &&
		strncmp ((const char*) cp, "'''", (size_t) 3) == 0)
	    {
		inMultilineString = (boolean) !inMultilineString;
		cp += 3;
	    }

	    if (inMultilineString)
		++cp;
		else if (isspace ((int) *cp))
		{
			cp++;
			// count indentation amount of current line
			// the indentation has to be made with tabs only _or_ spaces only, if they are mixed
			// the code below gets confused
			indent++;
		}
	    else if (*cp == '#')
		break;
	    else if (strncmp ((const char*) cp, "class", (size_t) 5) == 0)
	    {
			cp += 5;
			if (isspace ((int) *cp))
			{
				GList *last = g_list_last(parents);
				lastClass *lastclass = NULL;
				lastClass *newclass = g_new(lastClass, 1);

				if (last != NULL) lastclass = last->data;

				while (isspace ((int) *cp))
				++cp;
				while (isalnum ((int) *cp)  ||  *cp == '_')
				{
				vStringPut (name, (int) *cp);
				++cp;
				}
				vStringTerminate (name);

				parents = clean_class_list(parents, indent);

				newclass->name = g_strdup(vStringValue(name));
				newclass->indent = indent;
				parents = g_list_append(parents, newclass);
				makeSimpleTag (name, PythonKinds, K_CLASS);
				vStringClear (name);
			}
	    }
	    else if (strncmp ((const char*) cp, "def", (size_t) 3) == 0)
	    {
		cp += 3;
		if (isspace ((int) *cp))
		{
		    GList *last;
		    lastClass *lastclass = NULL;

			parents = clean_class_list(parents, indent);
			last = g_list_last(parents);
			if (last != NULL) lastclass = last->data;

		    while (isspace ((int) *cp))
			++cp;
		    while (isalnum ((int) *cp)  ||  *cp == '_')
		    {
			vStringPut (name, (int) *cp);
			++cp;
		    }
		    vStringTerminate (name);
		    if (!isspace(*line) || lastclass == NULL || strlen(lastclass->name) <= 0)
			makeSimpleTag (name, PythonKinds, K_FUNCTION);
		    else
			makeSimpleScopedTag (name, PythonKinds, K_METHOD,
					     PythonKinds[K_CLASS].name, lastclass->name, "public");
		    vStringClear (name);
		}
	    }
	    else if (*cp != '\0')
	    {
		do
		    ++cp;
		while (isalnum ((int) *cp)  ||  *cp == '_');
	    }
	}
    }
    vStringDelete (name);

    // clear the remaining elements in the list
    tmp = g_list_first(parents);
    while (tmp != NULL)
    {
    	if (tmp->data)
    	{
			g_free(((lastClass*)tmp->data)->name);
			g_free(tmp->data);
    	}
    	tmp = tmp->next;
    }
    g_list_free(parents);
}

extern parserDefinition* PythonParser (void)
{
    static const char *const extensions [] = { "py", "python", NULL };
    parserDefinition* def = parserNew ("Python");
    def->kinds      = PythonKinds;
    def->kindCount  = KIND_COUNT (PythonKinds);
    def->extensions = extensions;
    def->parser     = findPythonTags;
    return def;
}

/* vi:set tabstop=8 shiftwidth=4: */