Move code related to various tag file formats into tm_source_file.c
[geany-mirror.git] / tagmanager / ctags / lua.c
blobefb6787e34dff4df1a80430e2cc6734cf26407c5
1 /*
2 * Copyright (c) 2000-2001, Max Ischenko <mfi@ukr.net>.
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License.
7 * This module contains functions for generating tags for Lua language.
8 */
11 * INCLUDE FILES
13 #include "general.h" /* must always come first */
15 #include <string.h>
17 #include "parse.h"
18 #include "read.h"
19 #include "vstring.h"
22 * DATA DEFINITIONS
24 typedef enum {
25 K_FUNCTION
26 } luaKind;
28 static kindOption LuaKinds [] = {
29 { TRUE, 'f', "function", "functions" }
33 * FUNCTION DEFINITIONS
36 /* for debugging purposes */
37 static void UNUSED print_string (char *p, char *q)
39 for ( ; p != q; p++)
40 fprintf (errout, "%c", *p);
41 fprintf (errout, "\n");
45 * Helper function.
46 * Returns 1 if line looks like a line of Lua code.
48 * TODO: Recognize UNIX bang notation.
49 * (Lua treat first line as a comment if it starts with #!)
52 static boolean is_a_code_line (const unsigned char *line)
54 boolean result;
55 const unsigned char *p = line;
56 while (isspace ((int) *p))
57 p++;
58 if (p [0] == '\0')
59 result = FALSE;
60 else if (p [0] == '-' && p [1] == '-')
61 result = FALSE;
62 else
63 result = TRUE;
64 return result;
67 static void extract_name (const char *begin, const char *end, vString *name)
69 if (begin != NULL && end != NULL && begin < end)
71 const char *cp;
73 while (isspace ((int) *begin))
74 begin++;
75 while (isspace ((int) *end))
76 end--;
77 if (begin < end)
79 for (cp = begin ; cp != end; cp++)
80 vStringPut (name, (int) *cp);
81 vStringTerminate (name);
83 makeSimpleTag (name, LuaKinds, K_FUNCTION);
84 vStringClear (name);
89 static void findLuaTags (void)
91 vString *name = vStringNew ();
92 const unsigned char *line;
94 while ((line = fileReadLine ()) != NULL)
96 const char *p, *q;
98 if (! is_a_code_line (line))
99 continue;
101 p = (const char*) strstr ((const char*) line, "function");
102 if (p == NULL)
103 continue;
105 q = strchr ((const char*) line, '=');
107 if (q == NULL) {
108 p = p + 9; /* skip the `function' word */
109 q = strchr ((const char*) p, '(');
110 extract_name (p, q, name);
111 } else if (*(q+1) != '=') { /* ignore `if type(v) == "function" then ...' */
112 p = (const char*) &line[0];
113 extract_name (p, q, name);
116 vStringDelete (name);
119 extern parserDefinition* LuaParser (void)
121 static const char* const extensions [] = { "lua", NULL };
122 parserDefinition* def = parserNew ("Lua");
123 def->kinds = LuaKinds;
124 def->kindCount = KIND_COUNT (LuaKinds);
125 def->extensions = extensions;
126 def->parser = findLuaTags;
127 return def;
130 /* vi:set tabstop=4 shiftwidth=4: */