Update HACKING
[geany-mirror.git] / ctags / parsers / lua.c
blob608b71dfa097c3e4467ee3c91e6a397d2cd11503
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 kindDefinition LuaKinds [] = {
30 { true, 'f', "function", "functions" }
34 * FUNCTION DEFINITIONS
38 * Helper function.
39 * Returns 1 if line looks like a line of Lua code.
41 * TODO: Recognize UNIX bang notation.
42 * (Lua treat first line as a comment if it starts with #!)
45 static bool is_a_code_line (const unsigned char *line)
47 bool result;
48 const unsigned char *p = line;
49 while (isspace ((int) *p))
50 p++;
51 if (p [0] == '\0')
52 result = false;
53 else if (p [0] == '-' && p [1] == '-')
54 result = false;
55 else
56 result = true;
57 return result;
60 static void extract_name (const char *begin, const char *end, vString *name)
62 if (begin != NULL && end != NULL && begin < end)
64 const char *cp;
66 while (isspace ((int) *begin))
67 begin++;
68 while (isspace ((int) *end))
69 end--;
70 if (begin < end)
72 for (cp = begin ; cp != end; cp++)
73 vStringPut (name, (int) *cp);
75 makeSimpleTag (name, K_FUNCTION);
76 vStringClear (name);
81 static void findLuaTags (void)
83 vString *name = vStringNew ();
84 const unsigned char *line;
86 while ((line = readLineFromInputFile ()) != NULL)
88 const char *p, *q;
90 if (! is_a_code_line (line))
91 continue;
93 p = (const char*) strstr ((const char*) line, "function");
94 if (p == NULL)
95 continue;
97 q = strchr ((const char*) line, '=');
99 if (q == NULL) {
100 p = p + 9; /* skip the `function' word */
101 q = strchr ((const char*) p, '(');
102 extract_name (p, q, name);
103 } else if (*(q+1) != '=') { /* ignore `if type(v) == "function" then ...' */
104 p = (const char*) &line[0];
105 extract_name (p, q, name);
108 vStringDelete (name);
111 extern parserDefinition* LuaParser (void)
113 static const char* const extensions [] = { "lua", NULL };
114 parserDefinition* def = parserNew ("Lua");
115 def->kindTable = LuaKinds;
116 def->kindCount = ARRAY_SIZE (LuaKinds);
117 def->extensions = extensions;
118 def->parser = findLuaTags;
119 return def;