manual: added documentation about replacement of 'untitled.ext' with filename (#1804)
[geany-mirror.git] / ctags / parsers / lua.c
blob86e80d68c88c5bb19eeccbc02623cd981c906ca5
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 version 2 or (at your option) any later version.
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 "routines.h"
20 #include "vstring.h"
23 * DATA DEFINITIONS
25 typedef enum {
26 K_FUNCTION
27 } luaKind;
29 static kindOption LuaKinds [] = {
30 { true, 'f', "function", "functions" }
34 * FUNCTION DEFINITIONS
37 /* for debugging purposes */
38 static void CTAGS_ATTR_UNUSED print_string (char *p, char *q)
40 for ( ; p != q; p++)
41 fprintf (errout, "%c", *p);
42 fprintf (errout, "\n");
46 * Helper function.
47 * Returns 1 if line looks like a line of Lua code.
49 * TODO: Recognize UNIX bang notation.
50 * (Lua treat first line as a comment if it starts with #!)
53 static bool is_a_code_line (const unsigned char *line)
55 bool result;
56 const unsigned char *p = line;
57 while (isspace ((int) *p))
58 p++;
59 if (p [0] == '\0')
60 result = false;
61 else if (p [0] == '-' && p [1] == '-')
62 result = false;
63 else
64 result = true;
65 return result;
68 static void extract_name (const char *begin, const char *end, vString *name)
70 if (begin != NULL && end != NULL && begin < end)
72 const char *cp;
74 while (isspace ((int) *begin))
75 begin++;
76 while (isspace ((int) *end))
77 end--;
78 if (begin < end)
80 for (cp = begin ; cp != end; cp++)
81 vStringPut (name, (int) *cp);
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 = readLineFromInputFile ()) != 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 = ARRAY_SIZE (LuaKinds);
125 def->extensions = extensions;
126 def->parser = findLuaTags;
127 return def;