-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar-dump.cpp
159 lines (132 loc) · 3.74 KB
/
grammar-dump.cpp
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
/* PET
* Platform for Experimentation with efficient HPSG processing Techniques
* (C) 1999 - 2002 Ulrich Callmeier [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* support functions for dumping/undumping of the grammar */
#include "grammar-dump.h"
using namespace std;
void
dump_header(dumper *f, const char *desc)
{
f->dump_int(DUMP_MAGIC);
f->dump_int(DUMP_VERSION);
f->dump_string(desc);
}
char *
undump_header(dumper *f, int &version)
{
int magic;
char *desc;
magic = f->undump_int();
if(magic != DUMP_MAGIC)
throw tError("invalid grammar file");
version = f->undump_int();
if(version != DUMP_VERSION)
{
// From version 16 on we try to be backwards compatible,
// but print a warning anyway.
if(DUMP_VERSION >= 16 && version >= 15)
{
fprintf(stderr, "warning: loading grammar with backwards "
"compatible format (version %d, current %d)\n",
version, DUMP_VERSION);
}
else
throw tError("grammar file has incompatible version");
}
desc = f->undump_string();
return desc;
}
dump_toc::dump_toc(dumper *dump)
: _dump(dump)
{
sectiontype s;
long int offset;
do {
s = sectiontype(_dump->undump_int());
if(s != SEC_NOSECTION)
{
offset = _dump->undump_int();
_toc[s] = offset;
}
} while (s != SEC_NOSECTION);
}
bool
dump_toc::goto_section(sectiontype s)
{
if(_toc.find(s) != _toc.end())
{
try
{
_dump->seek(_toc[s]);
}
catch (tError &e)
{
throw tError("grammar file is corrupted");
}
sectiontype actual = sectiontype(_dump->undump_int());
if(actual != s)
throw tError("grammar file is corrupted");
return true;
}
return false;
}
dump_toc_maker::dump_toc_maker(dumper *dump)
: _dump(dump), _open(true)
{
}
void
dump_toc_maker::add_section(sectiontype s)
{
if(!_open)
throw tError("TOC already closed");
_dump->dump_int(s);
_where[s] = _dump->dump_int_variable();
}
void
dump_toc_maker::close()
{
if(!_open)
throw tError("TOC already closed");
_dump->dump_int(SEC_NOSECTION);
_open = false;
}
void
dump_toc_maker::start_section(sectiontype s)
{
if(_open)
throw tError("TOC still open");
if(_where.find(s) == _where.end())
throw tError("Trying to start a section that is not in the TOC");
_toc[s] = _dump->tell();
_dump -> dump_int(s);
}
void
dump_toc_maker::dump()
{
if(_open)
throw tError("TOC still open");
for(map<sectiontype, long int>::iterator iter = _where.begin();
iter != _where.end(); ++iter)
{
sectiontype s = iter->first;
long int pos = iter->second;
if(_toc.find(s) == _toc.end())
throw tError("Undefined section in TOC");
_dump->set_int_variable(pos, _toc[s]);
}
}