~ubuntu-branches/ubuntu/trusty/exuberant-ctags/trusty-updates

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
/*
*   $Id: vim.c,v 1.11 2003/12/23 02:27:46 darren Exp $
*
*   Copyright (c) 2000-2003, Darren Hiebert
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License.
*
*   Thanks are due to Jay Glanville for significant improvements.
*
*   This module contains functions for generating tags for user-defined
*   functions for the Vim editor.
*/

/*
*   INCLUDE FILES
*/
#include "general.h"	/* must always come first */

#include <string.h>

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

/*
*   DATA DEFINITIONS
*/
typedef enum {
    K_AUGROUP,
    K_FUNCTION,
    K_VARIABLE
} vimKind;

static kindOption VimKinds [] = {
    { TRUE,  'a', "augroup",  "autocommand groups" },
    { TRUE,  'f', "function", "function definitions" },
    { TRUE,  'v', "variable", "variable definitions" },
};

/*
*   FUNCTION DEFINITIONS
*/

/* This function takes a char pointer, tries to find a scope separator in the
 * string, and if it does, returns a pointer to the character after the colon,
 * and the character defining the scope.
 * If a colon is not found, it returns the original pointer.
 */
static const unsigned char* skipPrefix (const unsigned char* name, int *scope)
{
    const unsigned char* result = name;
    if (scope != NULL)
	*scope = '\0';
    if (name[1] == ':')
    {
	if (scope != NULL)
	    *scope = *name;
	result = name + 2;
    }
    else if (strncasecmp ((const char*) name, "<SID>", (size_t) 5) == 0)
    {
	if (scope != NULL)
	    *scope = *name;
	result = name + 5;
    }
    return result;
}

static void findVimTags (void)
{
    vString *name = vStringNew ();
    const unsigned char *line;
    boolean inFunction = FALSE;
    int scope;

    while ((line = fileReadLine ()) != NULL)
    {
	while (isspace ((int) *line))
	    ++line;
	if ((int) *line == '"')
	    continue;        /* skip comment */
	if (strncmp ((const char*) line, "fu", (size_t) 2) == 0)
	{
	    const unsigned char *cp = line + 1;
            inFunction = TRUE;

	    if ((int) *++cp == 'n'  &&  (int) *++cp == 'c'  &&
		(int) *++cp == 't'  &&  (int) *++cp == 'i'  &&
		(int) *++cp == 'o'  &&  (int) *++cp == 'n')
		    ++cp;
	    if ((int) *cp == '!')
		++cp;
	    if (isspace ((int) *cp))
	    {
		while (isspace ((int) *cp))
		    ++cp;
                cp = skipPrefix (cp, &scope);
		if (isupper ((int) *cp)  ||  scope == 's'  ||  scope == '<')
		{
		    do
		    {
			vStringPut (name, (int) *cp);
			++cp;
		    } while (isalnum ((int) *cp)  ||  *cp == '_');
		    vStringTerminate (name);
		    makeSimpleTag (name, VimKinds, K_FUNCTION);
		    vStringClear (name);
		}
	    }
	}

        if  (strncmp ((const char*) line, "aug", (size_t) 3) == 0)
	{
	    /* Found Autocommand Group (augroup) */
	    const unsigned char *cp = line + 2;
	    if ((int) *++cp == 'r' && (int) *++cp == 'o' &&
		(int) *++cp == 'u' && (int) *++cp == 'p')
		    ++cp;
	    if (isspace ((int) *cp))
	    {
		while (isspace ((int) *cp))
		    ++cp; 
		if (strncasecmp ((const char*) cp, "end", (size_t) 3) != 0)
		{    
		    do
		    {
			vStringPut (name, (int) *cp);
			++cp;
		    } while (isalnum ((int) *cp)  ||  *cp == '_');
		    vStringTerminate (name);
		    makeSimpleTag (name, VimKinds, K_AUGROUP);
		    vStringClear (name);
		}
	    }
	}

        if (strncmp ((const char*) line, "endf", (size_t) 4) == 0)
            inFunction = FALSE;

        if (!inFunction  &&
                strncmp ((const char*) line, "let", (size_t) 3) == 0)
	{
            /* we've found a variable declared outside of a function!! */
	    const unsigned char *cp = line + 3;
            /* get the name */
            if (isspace ((int) *cp))
	    {
                /* deal with spaces, $, @ and & */
                while (!isalnum ((int) *cp))
                    ++cp;
                cp = skipPrefix (cp, &scope);
                do
		{
                    vStringPut (name, (int) *cp);
                    ++cp;
                } while (isalnum ((int) *cp)  ||  *cp == '_');
                vStringTerminate (name);
                makeSimpleTag (name, VimKinds, K_VARIABLE);
                vStringClear (name);
            }
        }
    }
    vStringDelete (name);
}

extern parserDefinition* VimParser (void)
{
    static const char *const extensions [] = { "vim", NULL };
    parserDefinition* def = parserNew ("Vim");
    def->kinds      = VimKinds;
    def->kindCount  = KIND_COUNT (VimKinds);
    def->extensions = extensions;
    def->parser     = findVimTags;
    return def;
}

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