xref: /Universal-ctags/parsers/scheme.c (revision dbeba0347d706a300f7fccd163ee5dbaf6dc71b2)
1 /*
2 *   Copyright (c) 2000-2002, Darren Hiebert
3 *
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.
6 *
7 *   This module contains functions for generating tags for Scheme language
8 *   files.
9 */
10 
11 /*
12 *   INCLUDE FILES
13 */
14 #include "general.h"  /* must always come first */
15 
16 #include <string.h>
17 
18 #include "parse.h"
19 #include "read.h"
20 #include "routines.h"
21 #include "vstring.h"
22 
23 /*
24 *   DATA DEFINITIONS
25 */
26 typedef enum {
27 	K_FUNCTION, K_SET
28 } schemeKind;
29 
30 static kindDefinition SchemeKinds [] = {
31 	{ true, 'f', "function", "functions" },
32 	{ true, 's', "set",      "sets" }
33 };
34 
35 /*
36 *   FUNCTION DEFINITIONS
37 */
38 
39 /* Algorithm adapted from from GNU etags.
40  * Scheme tag functions
41  * look for (def... xyzzy
42  * look for (def... (xyzzy
43  * look for (def ... ((... (xyzzy ....
44  * look for (set! xyzzy
45  */
readIdentifier(vString * const name,const unsigned char * cp)46 static void readIdentifier (vString *const name, const unsigned char *cp)
47 {
48 	const unsigned char *p;
49 	vStringClear (name);
50 	/* Go till you get to white space or a syntactic break */
51 	for (p = cp; *p != '\0' && *p != '(' && *p != ')' && !isspace (*p); p++)
52 		vStringPut (name, (int) *p);
53 }
54 
findSchemeTags(void)55 static void findSchemeTags (void)
56 {
57 	vString *name = vStringNew ();
58 	const unsigned char *line;
59 
60 	while ((line = readLineFromInputFile ()) != NULL)
61 	{
62 		const unsigned char *cp = line;
63 
64 		if (cp [0] == '(' &&
65 			(cp [1] == 'D' || cp [1] == 'd') &&
66 			(cp [2] == 'E' || cp [2] == 'e') &&
67 			(cp [3] == 'F' || cp [3] == 'f'))
68 		{
69 			while (*cp != '\0'  &&  !isspace (*cp))
70 				cp++;
71 			/* Skip over open parens and white space */
72 			do {
73 				while (*cp != '\0' && (isspace (*cp) || *cp == '('))
74 					cp++;
75 				if (*cp == '\0')
76 					cp = line = readLineFromInputFile ();
77 				else
78 					break;
79 			} while (line);
80 			if (line == NULL)
81 				break;
82 			readIdentifier (name, cp);
83 			makeSimpleTag (name, K_FUNCTION);
84 		}
85 		if (cp [0] == '(' &&
86 			(cp [1] == 'S' || cp [1] == 's') &&
87 			(cp [2] == 'E' || cp [2] == 'e') &&
88 			(cp [3] == 'T' || cp [3] == 't') &&
89 			(cp [4] == '!') &&
90 			(isspace (cp [5]) || cp[5] == '\0'))
91 		{
92 			cp += 5;
93 			/* Skip over white space */
94 			do {
95 				while (*cp != '\0' && isspace (*cp))
96 					cp++;
97 				if (*cp == '\0')
98 					cp = line = readLineFromInputFile ();
99 				else
100 					break;
101 			} while (line);
102 			if (line == NULL)
103 				break;
104 			readIdentifier (name, cp);
105 			makeSimpleTag (name, K_SET);
106 		}
107 	}
108 	vStringDelete (name);
109 }
110 
SchemeParser(void)111 extern parserDefinition* SchemeParser (void)
112 {
113 	static const char *const extensions [] = {
114 		"SCM", "SM", "sch", "scheme", "scm", "sm", "rkt", NULL
115 	};
116 	static const char *const aliases [] = {
117 		"gosh", "guile", "racket", NULL
118 	};
119 	parserDefinition* def = parserNew ("Scheme");
120 	def->kindTable      = SchemeKinds;
121 	def->kindCount  = ARRAY_SIZE (SchemeKinds);
122 	def->extensions = extensions;
123 	def->aliases    = aliases;
124 	def->parser     = findSchemeTags;
125 	return def;
126 }
127