-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
186 lines (170 loc) · 5.64 KB
/
parse.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
/* parse.c (acdc) - copyright Mike Arnautov 1990-2018.
* Licensed under GPL, version 3 or later (see the supplied LICENCE file).
*
* 07 Mar 16 MLA Bug: In-line automatic text names can get too long.
* 03 Mar 16 MLA Removed non-ANSI C support.
* 28 Feb 16 MLA BUG: Fixed buffer overrun in the inline text fudge.
* 04 Jan 15 MLA BUG: Hendle CR-terminated lines correctly.
* 02 Dec 13 MLA BUG: Take care with in-line texts in style 1!
* 09 Jan 11 MLA Added in-line text parsing.
* 14 Jul 09 MLA Fixed gcc --pedantic warnings.
* 15 Mar 08 MLA Version 12 changes.
* 19 Aug 04 MLA Added FREE_ARG.
* 03 Mar 03 MLA Use new-style get_token().
* 06 Nov 01 MLA Fiddle FULLDISP for Platt-style A-code.
* 17 Oct 01 MLA Improved compatibility with Platt's A-code.
* 03 Dec 00 MLA Allowed '#' as a comment delimiter.
* 09 Aug 99 MLA Allowed for \r in sources imported from Windows.
* 24 Jul 99 MLA Fixed complier warnings.
* 26 Dec 90 MLA Support for new CONSTANT etc syntax.
* 15 Sep 90 MLA Initial coding.
*
*/
#include <stdio.h>
#include <string.h>
#include "acdc.h"
#include "game.h"
#include "const.h"
#include "line.h"
#include "symbol.h"
#include "major.h"
#include "text.h"
#include "source.h"
char *get_token(char **cstring, char *delims)
{
char *cptr;
char *token = *cstring;
if (! *token)
return (NULL);
while ((cptr = strpbrk(token, delims)) == token)
(token)++;
if (cptr)
*cptr++ = 0;
*cstring = cptr;
return(token);
}
/*====================================================================*/
struct node *parse(int type)
{
int index = 0;
struct node *np;
char *cptr = line;
int minargs;
int direct_call = 0;
strcpy (raw_line, line);
recase (LOWERCASE, line);
tp [1] = NULL;
if ((tp[0] = get_token(&cptr, " ,\r\n")) == NULL)
gripe ("","Null directive???");
if (type != NONE)
{
if ((np = fndsymb(type, tp[0])) == NULL)
{
if (type != MINOR ||
(np = fndsymb(TESTSYMBOL, tp[0])) == NULL)
return (NULL);
direct_call = 1;
}
if (direct_call == 0 && np -> max_args == REST)
{
cptr = raw_line + (cptr - line);
cptr += strspn (cptr, " ,\r\n");
tp [1] = cptr;
cptr += strlen(cptr) - 1;
if (*cptr == '\n' || *cptr == '\r') *cptr = '\0';
if (! *tp[1])
gripe (tp[0], "Missing \"rest of line\" argument.");
return (np);
}
}
while (cptr && (tp[++index] = get_token(&cptr," ,\n\r")) != NULL)
{
if (style <= 1)
{
if (strcmp (tp[index], "object") == 0)
tp[index] = "objflag";
else if (strcmp (tp[index], "place") == 0)
tp[index] = "placeflag";
else if (strcmp (tp[index], "verb") == 0)
tp[index] = "verbflag";
else if (strcmp (tp[index], "variable") == 0)
tp[index] = "varflag";
else if (strcmp (tp[index], "fulldisp") == 0)
tp[index] = "fulldisplay";
}
if (index > ANY_NUMBER)
gripe ("","*Far* too many arguments!");
if (! *tp[index] || *tp[index] == '#' ||
(*tp[index] == '{' && style < 11))
{
tp[index] = NULL;
break;
}
if (*tp[index] == '"' && (style > 1 || index == 1))
{
struct node *np;
char autoname [32];
char tmpline [MAXLINE];
char *aptr;
int offset;
int tail;
sprintf (autoname, "inline_%d_", ++inline_count);
np = fndsymb (SYMBOL, autoname);
np -> text_type = 0;
np -> name_addr = next_addr;
offset = tp[index] - line;
memcpy (tmpline, line, offset);
strcpy (tmpline + offset, autoname);
aptr = tp[index] + 1;
while (
(*aptr == 'c' || *aptr == 'i' || *aptr == 'r' || *aptr == 'f') &&
*(aptr + 1) == ':')
{
if (*aptr == 'f')
{
if (np -> text_type & FRAGMENT_TEXT)
gripe("", "Repeated 'f:' in inline text definition.");
np -> text_type |= FRAGMENT_TEXT;
}
else
{
if ( np -> text_type & MORPHING_TEXT)
gripe ("", "Multiple inline text typifiers.");
if (*aptr == 'r')
np -> text_type |= RANDOM_TEXT;
else if (*aptr == 'c')
np -> text_type |= CYCLIC_TEXT;
else if (*aptr == 'i')
np -> text_type |= INCREMENTING_TEXT;
}
aptr += 2;
}
memcpy (line + 1, raw_line + (aptr - line), MAXLINE - offset);
*line = ' ';
line_status = BOL;
np -> text_type |= INLINE_TEXT;
tail = gettxt (0, &(np -> state_count), &(np -> text_type));
offset += strlen (autoname) + 1;
strcpy (tmpline + offset, line + tail + 1);
recase (LOWERCASE, tmpline + offset);
memcpy (line, tmpline, MAXLINE);
/* cptr += strlen(cptr) + 1; */
cptr = tp[index] + strlen(tp[index]) + 1;
line_status = EOL;
}
}
if (type == NONE)
return NULL;
if (direct_call == 0)
{
index--;
minargs = np -> min_args;
if (minargs >= FREE_ARG)
minargs -= FREE_ARG - 1;
if (index < minargs)
gripe (tp[0],"Not enough arguments.");
if (index > np -> max_args)
gripe (tp[0],"Too many arguments.");
}
return (np);
}