r5098
[geany-mirror.git] / tagmanager / lua.c
blob26601eb7e3e7cc98d80e558c4235eb3e8828a82b
1 /*
2 * $Id$
4 * Copyright (c) 2000-2001, Max Ischenko <mfi@ukr.net>.
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for generating tags for Lua language.
13 * INCLUDE FILES
15 #include "general.h" /* must always come first */
17 #include <string.h>
19 #include "parse.h"
20 #include "read.h"
21 #include "vstring.h"
24 * DATA DEFINITIONS
26 typedef enum {
27 K_FUNCTION
28 } luaKind;
30 static kindOption LuaKinds [] = {
31 { TRUE, 'f', "function", "functions" }
35 * FUNCTION DEFINITIONS
38 /* for debugging purposes */
39 static void __unused__ print_string (char *p, char *q)
41 for ( ; p != q; p++)
42 fprintf (errout, "%c", *p);
43 fprintf (errout, "\n");
47 * Helper function.
48 * Returns 1 if line looks like a line of Lua code.
50 * TODO: Recognize UNIX bang notation.
51 * (Lua treat first line as a comment if it starts with #!)
54 static boolean is_a_code_line (const unsigned char *line)
56 boolean result;
57 const unsigned char *p = line;
58 while (isspace ((int) *p))
59 p++;
60 if (p [0] == '\0')
61 result = FALSE;
62 else if (p [0] == '-' && p [1] == '-')
63 result = FALSE;
64 else
65 result = TRUE;
66 return result;
69 static void extract_name (const char *begin, const char *end, vString *name)
71 if (begin != NULL && end != NULL && begin < end)
73 const char *cp;
75 while (isspace ((int) *begin))
76 begin++;
77 while (isspace ((int) *end))
78 end--;
79 if (begin < end)
81 for (cp = begin ; cp != end; cp++)
82 vStringPut (name, (int) *cp);
83 vStringTerminate (name);
85 makeSimpleTag (name, LuaKinds, K_FUNCTION);
86 vStringClear (name);
91 static void findLuaTags (void)
93 vString *name = vStringNew ();
94 const unsigned char *line;
96 while ((line = fileReadLine ()) != NULL)
98 const char *p, *q;
100 if (! is_a_code_line (line))
101 continue;
103 p = (const char*) strstr ((const char*) line, "function");
104 if (p == NULL)
105 continue;
107 q = strchr ((const char*) line, '=');
109 if (q == NULL) {
110 p = p + 9; /* skip the `function' word */
111 q = strchr ((const char*) p, '(');
112 extract_name (p, q, name);
113 } else if (*(q+1) != '=') { /* ignore `if type(v) == "function" then ...' */
114 p = (const char*) &line[0];
115 extract_name (p, q, name);
118 vStringDelete (name);
121 extern parserDefinition* LuaParser (void)
123 static const char* const extensions [] = { "lua", NULL };
124 parserDefinition* def = parserNew ("Lua");
125 def->kinds = LuaKinds;
126 def->kindCount = KIND_COUNT (LuaKinds);
127 def->extensions = extensions;
128 def->parser = findLuaTags;
129 return def;
132 /* vi:set tabstop=4 shiftwidth=4: */