-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton.c
147 lines (141 loc) · 3.05 KB
/
skeleton.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
/*
* skeleton.c - Include skeleton code in an output stream.
*
* Copyright (C) 2001 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "system.h"
#include "input.h"
#include "info.h"
#include "errors.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Find a particular skeleton string within "skels.c".
*/
extern const char * const TreeCCSkelFiles[];
static char *FindSkeletonString(const char *skeleton)
{
char **search = (char **)TreeCCSkelFiles;
while(*search != 0)
{
if(!strcmp(*search, skeleton))
{
return search[1];
}
search += 2;
}
return 0;
}
/*
* Read a line from a skeleton buffer..
*/
static int ReadSkeletonLine(char *buffer, int size, char **skel)
{
char *ptr = *skel;
if(*ptr == '\0')
{
return 0;
}
while(*ptr != '\0' && *ptr != '\n')
{
if(size > 2)
{
*buffer++ = *ptr;
--size;
}
++ptr;
}
if(*ptr == '\n')
{
*buffer++ = *ptr++;
}
*buffer = '\0';
*skel = ptr;
return 1;
}
void TreeCCIncludeSkeleton(TreeCCContext *context, TreeCCStream *stream,
const char *skeleton)
{
char *skelstr = FindSkeletonString(skeleton);
char buffer[BUFSIZ];
int posn, start;
if(!skelstr)
{
fprintf(stderr,
"treecc: internal error - could not find skeleton \"%s\"\n",
skeleton);
exit(1);
}
TreeCCStreamLine(stream, 1, skeleton);
while(ReadSkeletonLine(buffer, BUFSIZ, &skelstr))
{
#if HAVE_STRCHR
if(strchr(buffer, 'Y') != 0 || strchr(buffer, 'y') != 0)
#endif
{
/* The line probably contains "YYNODESTATE" or "yy" */
posn = 0;
start = 0;
while(buffer[posn] != '\0')
{
if(buffer[posn] == 'Y' &&
!strncmp(buffer + posn, "YYNODESTATE", 11))
{
buffer[posn] = '\0';
if(start < posn)
{
TreeCCStreamCode(stream, buffer + start);
}
TreeCCStreamCode(stream, context->state_type);
posn += 11;
start = posn;
}
else if(buffer[posn] == 'y' && buffer[posn + 1] == 'y')
{
buffer[posn] = '\0';
if(start < posn)
{
TreeCCStreamCode(stream, buffer + start);
}
TreeCCStreamCode(stream, context->yy_replacement);
posn += 2;
start = posn;
}
else
{
++posn;
}
}
if(start < posn)
{
TreeCCStreamCode(stream, buffer + start);
}
}
#if HAVE_STRCHR
else
{
/* Ordinary line */
TreeCCStreamCode(stream, buffer);
}
#endif
}
TreeCCStreamFixLine(stream);
}
#ifdef __cplusplus
};
#endif