-
Notifications
You must be signed in to change notification settings - Fork 0
/
sml.c
212 lines (194 loc) · 4.92 KB
/
sml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* $Id: sml.c 536 2007-06-02 06:09:00Z elliotth $
*
* Copyright (c) 2002, Venkatesh Prasad Ranganath and Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions for generating tags for SML language files.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <string.h>
#include "entry.h"
#include "parse.h"
#include "read.h"
#include "vstring.h"
/*
* DATA DECLARATIONS
*/
typedef enum {
K_AND = -2,
K_NONE = -1,
K_EXCEPTION,
K_FUNCTION,
K_FUNCTOR,
K_SIGNATURE,
K_STRUCTURE,
K_TYPE,
K_VAL
} smlKind;
/*
* DATA DEFINITIONS
*/
static kindOption SmlKinds[] = {
{ TRUE, 'e', "exception", "exception declarations" },
{ TRUE, 'f', "function", "function definitions" },
{ TRUE, 'c', "functor", "functor definitions" },
{ TRUE, 's', "signature", "signature declarations" },
{ TRUE, 'r', "structure", "structure declarations" },
{ TRUE, 't', "type", "type definitions" },
{ TRUE, 'v', "value", "value bindings" }
};
static struct {
const char *keyword;
smlKind kind;
} SmlKeywordTypes [] = {
{ "abstype", K_TYPE },
{ "and", K_AND },
{ "datatype", K_TYPE },
{ "exception", K_EXCEPTION },
{ "functor", K_FUNCTOR },
{ "fun", K_FUNCTION },
{ "signature", K_SIGNATURE },
{ "structure", K_STRUCTURE },
{ "type", K_TYPE },
{ "val", K_VAL }
};
static unsigned int CommentLevel = 0;
/*
* FUNCTION DEFINITIONS
*/
static void makeSmlTag (smlKind type, vString *name)
{
tagEntryInfo tag;
initTagEntry (&tag, vStringValue (name));
tag.kindName = SmlKinds [type].name;
tag.kind = SmlKinds [type].letter;
makeTagEntry (&tag);
}
static const unsigned char *skipSpace (const unsigned char *cp)
{
while (isspace ((int) *cp))
++cp;
return cp;
}
static boolean isIdentifier (int c)
{
boolean result = FALSE;
/* Consider '_' as an delimiter to aid user in tracking it's usage. */
const char *const alternateIdentifiers = "!%&$#+-<>=/?@\\~'^|*_";
if (isalnum (c))
result = TRUE;
else if (c != '\0' && strchr (alternateIdentifiers, c) != NULL)
result = TRUE;
return result;
}
static const unsigned char *parseIdentifier (
const unsigned char *cp, vString *const identifier)
{
boolean stringLit = FALSE;
vStringClear (identifier);
while (*cp != '\0' && (!isIdentifier ((int) *cp) || stringLit))
{
int oneback = *cp;
cp++;
if (oneback == '(' && *cp == '*' && stringLit == FALSE)
{
CommentLevel++;
return ++cp;
}
if (*cp == '"' && oneback != '\\')
{
stringLit = TRUE;
continue;
}
if (stringLit && *cp == '"' && oneback != '\\')
stringLit = FALSE;
}
if (strcmp ((const char *) cp, "") == 0 || cp == NULL)
return cp;
while (isIdentifier ((int) *cp))
{
vStringPut (identifier, (int) *cp);
cp++;
}
vStringTerminate (identifier);
return cp;
}
static smlKind findNextIdentifier (const unsigned char **cp)
{
smlKind result = K_NONE;
vString *const identifier = vStringNew ();
unsigned int count = sizeof (SmlKeywordTypes) / sizeof (SmlKeywordTypes [0]);
unsigned int i;
*cp = parseIdentifier (*cp, identifier);
for (i = 0 ; i < count && result == K_NONE ; ++i)
{
const char *id = vStringValue (identifier);
if (strcmp (id, SmlKeywordTypes [i].keyword) == 0)
result = SmlKeywordTypes [i].kind;
}
vStringDelete (identifier);
return result;
}
static void findSmlTags (void)
{
vString *const identifier = vStringNew ();
const unsigned char *line;
smlKind lastTag = K_NONE;
while ((line = fileReadLine ()) != NULL)
{
const unsigned char *cp = skipSpace (line);
do
{
smlKind foundTag;
if (CommentLevel != 0)
{
cp = (const unsigned char *) strstr ((const char *) cp, "*)");
if (cp == NULL)
continue;
else
{
--CommentLevel;
cp += 2;
}
}
foundTag = findNextIdentifier (&cp);
if (foundTag != K_NONE)
{
cp = skipSpace (cp);
cp = parseIdentifier (cp, identifier);
if (foundTag == K_AND)
makeSmlTag (lastTag, identifier);
else
{
makeSmlTag (foundTag, identifier);
lastTag = foundTag;
}
}
if (strstr ((const char *) cp, "(*") != NULL)
{
cp += 2;
cp = (const unsigned char *) strstr ((const char *) cp, "*)");
if (cp == NULL)
++CommentLevel;
}
} while (cp != NULL && strcmp ((const char *) cp, "") != 0);
}
vStringDelete (identifier);
}
extern parserDefinition *SmlParser (void)
{
static const char *const extensions[] = { "sml", "sig", NULL };
parserDefinition *def = parserNew ("SML");
def->kinds = SmlKinds;
def->kindCount = KIND_COUNT (SmlKinds);
def->extensions = extensions;
def->parser = findSmlTags;
return def;
}
/* vi:set tabstop=4 shiftwidth=4: */