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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/*
* Copyright 2015 Adrian Thurston <thurston@complang.org>
*/
/* This file is part of Ragel.
*
* Ragel 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.
*
* Ragel 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 Ragel; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "reducer.h"
#include "if.h"
#include <colm/colm.h>
#include <colm/tree.h>
#include <errno.h>
using std::endl;
using std::ifstream;
extern colm_sections rlparse_object;
void TopLevel::loadMachineName( string data )
{
/* Make/get the priority key. The name may have already been referenced
* and therefore exist. */
PriorDictEl *priorDictEl;
if ( pd->priorDict.insert( data, pd->fsmCtx->nextPriorKey, &priorDictEl ) )
pd->fsmCtx->nextPriorKey += 1;
pd->curDefPriorKey = priorDictEl->value;
/* Make/get the local error key. */
LocalErrDictEl *localErrDictEl;
if ( pd->localErrDict.insert( data, pd->nextLocalErrKey, &localErrDictEl ) )
pd->nextLocalErrKey += 1;
pd->curDefLocalErrKey = localErrDictEl->value;
}
void TopLevel::tryMachineDef( InputLoc &loc, std::string name,
MachineDef *machineDef, bool isInstance )
{
GraphDictEl *newEl = pd->graphDict.insert( name );
if ( newEl != 0 ) {
/* New element in the dict, all good. */
newEl->value = new VarDef( name, machineDef );
newEl->isInstance = isInstance;
newEl->loc = loc;
newEl->value->isExport = exportContext[exportContext.length()-1];
/* It it is an instance, put on the instance list. */
if ( isInstance )
pd->instanceList.append( newEl );
}
else {
// Recover by ignoring the duplicate.
pd->id->error(loc) << "fsm \"" << name << "\" previously defined" << endl;
}
}
long TopLevel::tryLongScan( const InputLoc &loc, const char *data )
{
/* Convert the priority number to a long. Check for overflow. */
long priorityNum;
errno = 0;
long aug = strtol( data, 0, 10 );
if ( errno == ERANGE && aug == LONG_MAX ) {
/* Priority number too large. Recover by setting the priority to 0. */
pd->id->error(loc) << "priority number " << data <<
" overflows" << endl;
priorityNum = 0;
}
else if ( errno == ERANGE && aug == LONG_MIN ) {
/* Priority number too large in the neg. Recover by using 0. */
pd->id->error(loc) << "priority number " << data <<
" underflows" << endl;
priorityNum = 0;
}
else {
/* No overflow or underflow. */
priorityNum = aug;
}
return priorityNum;
}
void TopLevel::loadIncludeData( IncludeRec *el, IncludePass &includePass, const string &fileName )
{
/* Count bytes. */
size_t len = 0;
for ( IncItem *ii = includePass.incItems.head; ii != 0; ii = ii->next )
len += ii->length;
/* Store bytes. */
el->data = new char[len+1];
len = 0;
if ( id->inLibRagel ) {
for ( IncItem *ii = includePass.incItems.head; ii != 0; ii = ii->next ) {
memcpy( el->data + len, id->input + ii->start, ii->length );
len += ii->length;
}
}
else {
for ( IncItem *ii = includePass.incItems.head; ii != 0; ii = ii->next ) {
std::ifstream f( fileName.c_str() );
f.seekg( ii->start, std::ios::beg );
f.read( el->data + len, ii->length );
size_t read = f.gcount();
if ( read != ii->length ) {
pd->id->error(ii->loc) << "unexpected length in read of included file: "
"possible change to file" << endp;
}
len += read;
}
}
el->data[len] = 0;
el->len = len;
}
void TopLevel::include( const InputLoc &incLoc, bool fileSpecified, string fileName, string machine )
{
/* Stash the current section name and pd. */
string sectionName = pd->sectionName;
ParseData *pd0 = pd;
IncludeRec *el = id->includeDict.find( FnMachine( fileName, machine ) );
if ( el == 0 ) {
el = new IncludeRec( fileName, machine );
const char **includeChecks = 0;
long found = 0;
/* First collect the locations of the text using an include pass. */
IncludePass includePass( id, machine );
if ( id->inLibRagel && !fileSpecified ) {
el->foundFileName = curFileName;
/* In LibRagel and no file was specified in the include statement.
* In this case we run the include pass on the input text supplied. */
includePass.reduceStr( fileName.c_str(), id->hostLang, id->input );
}
else {
const char *inclSectionName = machine.c_str();
/* Implement defaults for the input file and section name. */
if ( inclSectionName == 0 )
inclSectionName = sectionName.c_str();
if ( fileSpecified )
includeChecks = pd->id->makeIncludePathChecks( curFileName, fileName.c_str() );
else {
char *test = new char[strlen(curFileName)+1];
strcpy( test, curFileName );
includeChecks = new const char*[2];
includeChecks[0] = test;
includeChecks[1] = 0;
}
ifstream *inFile = pd->id->tryOpenInclude( includeChecks, found );
if ( inFile == 0 ) {
id->error(incLoc) << "include: failed to locate file" << endl;
const char **tried = includeChecks;
while ( *tried != 0 )
id->error(incLoc) << "include: attempted: \"" << *tried++ << '\"' << endl;
}
else {
delete inFile;
el->foundFileName = curFileName;
/* Don't include anything that's already been included. */
if ( !pd->duplicateInclude( includeChecks[found], inclSectionName ) ) {
pd->includeHistory.push_back( IncludeHistoryItem(
includeChecks[found], inclSectionName ) );
/* Either we are not in the lib, or a file was specifed, use the
* file-based include pass. */
includePass.reduceFile( includeChecks[found], id->hostLang );
}
}
}
if ( includePass.incItems.length() == 0 ) {
pd->id->error(incLoc) << "could not find machine " << machine <<
" in " << fileName << endp;
}
else {
/* Load the data into include el. Save in the dict. */
loadIncludeData( el, includePass, includeChecks[found] );
id->includeDict.insert( el );
includePass.incItems.empty();
}
}
const char *targetMachine0 = targetMachine;
const char *searchMachine0 = searchMachine;
includeDepth += 1;
pd = 0;
targetMachine = sectionName.c_str();
searchMachine = machine.c_str();
reduceStr( el->foundFileName.c_str(), el->data );
pd = pd0;
includeDepth -= 1;
targetMachine = targetMachine0;
searchMachine = searchMachine0;
}
void TopLevel::loadImport( std::string fileName )
{
const char *argv[5];
argv[0] = "rlparse";
argv[1] = "import-file";
argv[2] = fileName.c_str();
argv[3] = id->hostLang->rlhcArg;
argv[4] = 0;
colm_program *program = colm_new_program( &rlparse_object );
colm_set_debug( program, 0 );
colm_run_program( program, 4, argv );
/* Extract the parse tree. */
start Start = RagelTree( program );
str Error = RagelError( program );
_repeat_import ImportList = RagelImport( program );
if ( Start == 0 ) {
pd->id->error(Error.loc()) << fileName << ": parse error: " << Error.text() << std::endl;
return;
}
while ( !ImportList.end() ) {
import Import = ImportList.value();
InputLoc loc = Import.loc();
string name = Import.Name().text();
loadMachineName( name );
Literal *literal = 0;
switch ( Import.Val().prodName() ) {
case import_val::String: {
string s = Import.Val().string().text();
Token tok;
tok.set( s.c_str(), s.size(), loc );
literal = new Literal( loc, false, tok.data, tok.length, Literal::LitString );
break;
}
case import_val::Number: {
string s = Import.Val().number().text();
Token tok;
tok.set( s.c_str(), s.size(), loc );
literal = new Literal( loc, false, tok.data, tok.length, Literal::Number );
break;
}
}
MachineDef *machineDef = new MachineDef(
new Join(
new Expression(
new Term(
new FactorWithAug(
new FactorWithRep(
new FactorWithNeg( new Factor( literal ) )
)
)
)
)
)
);
/* Generic creation of machine for instantiation and assignment. */
tryMachineDef( loc, name, machineDef, false );
machineDef->join->loc = loc;
ImportList = ImportList.next();
}
id->streamFileNames.append( colm_extract_fns( program ) );
colm_delete_program( program );
}
void TopLevel::reduceFile( const char *inputFileName )
{
const char *argv[5];
argv[0] = "rlparse";
argv[1] = "toplevel-reduce-file";
argv[2] = inputFileName;
argv[3] = id->hostLang->rlhcArg;
argv[4] = 0;
const char *prevCurFileName = curFileName;
curFileName = inputFileName;
colm_program *program = colm_new_program( &rlparse_object );
colm_set_debug( program, 0 );
colm_set_reduce_ctx( program, this );
colm_run_program( program, 4, argv );
id->streamFileNames.append( colm_extract_fns( program ) );
str Error = RagelError( program );
if ( Error != 0 )
id->error(Error.loc()) << Error.text() << std::endl;
colm_delete_program( program );
curFileName = prevCurFileName;
}
void TopLevel::reduceStr( const char *inputFileName, const char *input )
{
const char *argv[6];
argv[0] = "rlparse";
argv[1] = "toplevel-reduce-str";
argv[2] = inputFileName;
argv[3] = id->hostLang->rlhcArg;
argv[4] = input;
argv[5] = 0;
const char *prevCurFileName = curFileName;
curFileName = inputFileName;
colm_program *program = colm_new_program( &rlparse_object );
colm_set_debug( program, 0 );
colm_set_reduce_ctx( program, this );
colm_run_program( program, 5, argv );
id->streamFileNames.append( colm_extract_fns( program ) );
str Error = RagelError( program );
if ( Error != 0 )
id->error(Error.loc()) << "trs: " << Error.text() << std::endl;
colm_delete_program( program );
curFileName = prevCurFileName;
}
void SectionPass::reduceFile( const char *inputFileName )
{
const char *argv[5];
argv[0] = "rlparse";
argv[1] = "section-reduce-file";
argv[2] = inputFileName;
argv[3] = id->hostLang->rlhcArg;
argv[4] = 0;
colm_program *program = colm_new_program( &rlparse_object );
colm_set_debug( program, 0 );
colm_set_reduce_ctx( program, this );
colm_run_program( program, 4, argv );
id->streamFileNames.append( colm_extract_fns( program ) );
str Error = RagelError( program );
if ( Error != 0 )
id->error(Error.loc()) << Error.text() << std::endl;
colm_delete_program( program );
}
void SectionPass::reduceStr( const char *inputFileName, const char *input )
{
const char *argv[6];
argv[0] = "rlparse";
argv[1] = "section-reduce-str";
argv[2] = inputFileName;
argv[3] = id->hostLang->rlhcArg;
argv[4] = input;
argv[5] = 0;
colm_program *program = colm_new_program( &rlparse_object );
colm_set_debug( program, 0 );
colm_set_reduce_ctx( program, this );
colm_run_program( program, 5, argv );
id->streamFileNames.append( colm_extract_fns( program ) );
str Error = RagelError( program );
if ( Error != 0 )
id->error(Error.loc()) << "srs: " << Error.text() << std::endl;
colm_delete_program( program );
}
void IncludePass::reduceFile( const char *inputFileName, const HostLang *hostLang )
{
const char *argv[5];
argv[0] = "rlparse";
argv[1] = "include-reduce-file";
argv[2] = inputFileName;
argv[3] = hostLang->rlhcArg;
argv[4] = 0;
colm_program *program = colm_new_program( &rlparse_object );
colm_set_debug( program, 0 );
colm_set_reduce_ctx( program, this );
colm_run_program( program, 4, argv );
id->streamFileNames.append( colm_extract_fns( program ) );
str Error = RagelError( program );
if ( Error != 0 )
id->error(Error.loc()) << Error.text() << std::endl;
colm_delete_program( program );
}
void IncludePass::reduceStr( const char *inputFileName, const HostLang *hostLang, const char *input )
{
const char *argv[6];
argv[0] = "rlparse";
argv[1] = "include-reduce-str";
argv[2] = inputFileName;
argv[3] = hostLang->rlhcArg;
argv[4] = input;
argv[5] = 0;
colm_program *program = colm_new_program( &rlparse_object );
colm_set_debug( program, 0 );
colm_set_reduce_ctx( program, this );
colm_run_program( program, 5, argv );
id->streamFileNames.append( colm_extract_fns( program ) );
str Error = RagelError( program );
if ( Error != 0 )
id->error(Error.loc()) << Error.text() << std::endl;
colm_delete_program( program );
}
|