Debian Codegraph @

lua.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * $Id: lua.c 443 2006-05-30 04:37:13Z darren $
3 *
4 * Copyright (c) 2000-2001, Max Ischenko <[email protected]>.
5 *
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
8 *
9 * This module contains functions for generating tags for Lua language.
10 */
11
12 /*
13 * INCLUDE FILES
14 */
15 #include "general.h" /* must always come first */
16
17 #include <string.h>
18
19 #include "options.h"
20 #include "parse.h"
21 #include "read.h"
22 #include "vstring.h"
23
24 /*
25 * DATA DEFINITIONS
26 */
27 typedef enum {
28 K_FUNCTION
29 } luaKind;
30
31 static kindOption LuaKinds [] = {
32 { TRUE, 'f', "function", "functions" }
33 };
34
35 /*
36 * FUNCTION DEFINITIONS
37 */
38
39 /* for debugging purposes */
•40 static void __unused print_string (char *p, char *q)
41 {
42 for ( ; p != q; p++)
43 fprintf (errout, "%c", *p);
44 fprintf (errout, "\n");
45 }
46
47 /*
48 * Helper function.
49 * Returns 1 if line looks like a line of Lua code.
50 *
51 * TODO: Recognize UNIX bang notation.
52 * (Lua treat first line as a comment if it starts with #!)
53 *
54 */
55 static boolean is_a_code_line (const unsigned char *line)
56 {
57 boolean result;
58 const unsigned char *p = line;
59 while (isspace ((int) *p))
60 p++;
61 if (p [0] == '\0')
62 result = FALSE;
63 else if (p [0] == '-' && p [1] == '-')
64 result = FALSE;
65 else
66 result = TRUE;
67 return result;
68 }
69
70 static void extract_name (const char *begin, const char *end, vString *name)
71 {
72 if (begin != NULL && end != NULL && begin < end)
73 {
74 const char *cp;
75
76 while (isspace ((int) *begin))
77 begin++;
78 while (isspace ((int) *end))
79 end--;
80 if (begin < end)
81 {
82 for (cp = begin ; cp != end; cp++)
83 vStringPut (name, (int) *cp);
84 vStringTerminate (name);
85
86 makeSimpleTag (name, LuaKinds, K_FUNCTION);
87 vStringClear (name);
88 }
89 }
90 }
91
92 static void findLuaTags (void)
93 {
94 vString *name = vStringNew ();
95 const unsigned char *line;
96
97 while ((line = fileReadLine ()) != NULL)
98 {
99 const char *p, *q;
100
101 if (! is_a_code_line (line))
102 continue;
103
104 p = (const char*) strstr ((const char*) line, "function");
105 if (p == NULL)
106 continue;
107
108 q = strchr ((const char*) line, '=');
109
110 if (q == NULL) {
111 p = p + 9; /* skip the `function' word */
112 q = strchr ((const char*) p, '(');
113 extract_name (p, q, name);
114 } else {
115 p = (const char*) &line[0];
116 extract_name (p, q, name);
117 }
118 }
119 vStringDelete (name);
120 }
121
122 extern parserDefinition* LuaParser (void)
123 {
124 static const char* const extensions [] = { "lua", NULL };
125 parserDefinition* def = parserNew ("Lua");
126 def->kinds = LuaKinds;
127 def->kindCount = KIND_COUNT (LuaKinds);
128 def->extensions = extensions;
129 def->parser = findLuaTags;
130 return def;
131 }
132
133 /* vi:set tabstop=4 shiftwidth=4: */