File: langdefparser.yy

package info (click to toggle)
source-highlight 3.1.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,332 kB
  • ctags: 5,233
  • sloc: sh: 11,270; cpp: 10,206; ansic: 9,515; makefile: 1,865; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ml: 38; ada: 36; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 6; sed: 4
file content (415 lines) | stat: -rw-r--r-- 10,827 bytes parent folder | download | duplicates (6)
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
%{
/*
 * Copyright (C) 1999-2009 Lorenzo Bettini <http://www.lorenzobettini.it>
 *
 * 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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>

#include "stringdef.h"
#include "statelangelem.h"
#include "statestartlangelem.h"
#include "stringlistlangelem.h"
#include "delimitedlangelem.h"
#include "langelems.h"
#include "langdefparserfun.h"
#include "langdefscanner.h"
#include "vardefinitions.h"
#include "namedsubexpslangelem.h"
#include "parserexception.h"
#include "ioexception.h"

using std::cerr;
using std::string;

using namespace srchilite;

extern int langdef_lex() ;
extern FILE *langdef_in;
extern ParseStructPtr parsestruct;

static void yyerror( const char *s ) ;
static void yyerror( const string &s ) ;

/// the buffer for storing errors
static string errorBuffer;

/// this is where the parsed elements are store
LangElems *current_lang_elems = 0;

VarDefinitions *vardefinitions = 0;

/// used to record that the error is due to an included file not found
static bool includedFileNotFound = false;

#define UPDATE_REDEF(el, r) if (r == 1) { el->setRedef(); } else if (r == 2) { el->setSubst(); }
#define ADD_ELEMENT(elems, elem) \
  if (elem) { \
    if (elem->isRedef()) \
      elems->redef(elem); \
    else if (elem->isSubst()) \
      elems->subst(elem); \
    else \
      elems->add(elem); \
  }

struct Key : public ParserInfo
{
  const string *key;

  ~Key() { 
  	/* the string belongs to the string table so it is
  	   deallocated automatically by clearing the scanner */ 
  }
};

// this is a trick since ElementNames is a typedef and cannot
// be used in the union below
struct ElementNamesList : ElementNames {
};

%}

%union {
  int tok ; /* command */
  bool booloption ;
  const std::string * string ; /* string : id, ... */
  class srchilite::StringDef *stringdef;
  class StringDefs *stringdefs;
  class LangElem *langelem;
  class StateLangElem *statelangelem;
  class StateStartLangElem *statestartlangelem;
  class LangElems *langelems;
  class NamedSubExpsLangElem *namedsubexpslangelem;
  struct Key *key;
  struct ElementNamesList *keys;
  int flag ;
  unsigned int level;
};

%token <tok> BEGIN_T END_T ENVIRONMENT_T STATE_T MULTILINE_T DELIM_T START_T 
%token <tok> ESCAPE_T NESTED_T EXIT_ALL EXIT_T VARDEF_T REDEF_T SUBST_T NONSENSITIVE_T 
%token <tok> WRONG_BACKREFERENCE
%token <level> LEVEL
%token <string> KEY STRINGDEF REGEXPNOPREPROC VARUSE BACKREFVAR WRONG_INCLUDE_FILE
%token <stringdef> REGEXPDEF

%type <stringdef> stringdef escapedef stringdefwreferences
%type <stringdefs> stringdefs
%type <langelem> elemdef
%type <langelems> elemdefs
%type <statestartlangelem> complexelem
%type <booloption> multiline startnewenv nested nonsensitive
%type <tok> exitlevel redefsubst
%type <level> level
%type <key> key;
%type <keys> keys;

%destructor {
	//std::cout << "freeing discarded symbol" << std::endl;
	delete $$;
} stringdef stringdefs elemdef elemdefs key keys

%%

allelements :
        {
          /* no definitions (i.e., empty a .lang file with no definition) */
          /* such as, default.lang */
          /* synthetize a normal elem that catches everything */
          current_lang_elems = new LangElems;
          StringDefs *defs = new StringDefs;
          defs->push_back (new StringDef("(?:.+)"));
          current_lang_elems->add(new StringListLangElem("normal", defs, false));
        }
        | elemdefs { current_lang_elems = $1; }
;

elemdefs : elemdefs elemdef {
              $$ = $1;
              ADD_ELEMENT($$, $2);
           }
          | elemdef {
              $$ = new LangElems;
              ADD_ELEMENT($$, $1);
          }
         ;

elemdef : redefsubst complexelem exitlevel
          {
            $$ = $2;
            if ($3 < 0)
              $2->setExitAll();
            if ($3 > 0)
              $2->setExit($3);
            UPDATE_REDEF($2, $1);
          }
        | redefsubst startnewenv complexelem BEGIN_T elemdefs END_T
          {
            StateLangElem *statelangelem = new StateLangElem($3->getName(), $3, $5, !$2);
            $$ = statelangelem;
            $3->setStateLangElem(statelangelem);
            
            UPDATE_REDEF($$, $1);
          }
        | VARDEF_T KEY '=' stringdefs {
            vardefinitions->add(*$2, $4);
            $$ = 0;
          }
        | WRONG_INCLUDE_FILE {
            // this token is used by the scanner to signal an error
            // in opening an include file
            includedFileNotFound = true;
            yyerror("cannot open include file " + *$1);
            YYERROR;
            
            $$ = 0;
          }
        ;

complexelem : key DELIM_T stringdef stringdefwreferences escapedef multiline nested
                {
                  $$ = new DelimitedLangElem(*($1->key), $3, $4, $5, $6, $7);
                  $$->setParserInfo($1);
                  
                  delete $1;
                }
          | key START_T stringdef {
              $$ = new DelimitedLangElem(*($1->key), $3, 0, 0, false, false);
              $$->setParserInfo($1);
              delete $1;
            }
          | key '=' stringdefs nonsensitive {
              $$ = new StringListLangElem(*($1->key), $3, $4);
              $$->setParserInfo($1);
              
              delete $1;
            }
          | '(' keys ')' '=' REGEXPNOPREPROC {
          		$$ = new NamedSubExpsLangElem($2, new StringDef(*$5));
                        $$->setParserInfo(parsestruct->file_name, @1.first_line);
            } 
        ;

key: KEY {
    $$ = new Key;
    $$->key = $1;
    $$->setParserInfo(parsestruct->file_name, @1.first_line);
  }
;

keys: keys ',' KEY 
    {
        $$ = $1;
        $$->push_back(*$3);
    }
    | KEY
    {
    	$$ = new ElementNamesList;
    	$$->push_back(*$1);
    }
;

escapedef : ESCAPE_T stringdef { $$ = $2; }
        | { $$ = 0; }
        ;

exitlevel: EXIT_ALL { $$ = -1; }
        | EXIT_T level { $$ = $2; }
        | { $$ = 0; }
        ;

level: LEVEL { $$ = $1; }
        | { $$ = 1; }
        ;

startnewenv : ENVIRONMENT_T { $$ = true; }
        | STATE_T { $$ = false; }
          ;

multiline : MULTILINE_T { $$ = true; }
            | { $$ = false; }
            ;

redefsubst : REDEF_T { $$ = 1; }
           | SUBST_T { $$ = 2; }
            | { $$ = 0; }
            ;

nested : NESTED_T { $$ = true; }
            | { $$ = false; }
            ;

nonsensitive : NONSENSITIVE_T { $$ = true; }
            | { $$ = false; }
            ;

stringdefs : stringdefs ',' stringdef { $$ = $1; $$->push_back($3); }
            | stringdef {
                $$ = new StringDefs;
                $$->push_back($1); }
           ;

stringdefwreferences : REGEXPDEF {
              $$ = $1;
            }
          | STRINGDEF {
              $$ = new StringDef(*$1, true);
            }
          | REGEXPNOPREPROC {
              $$ = new StringDef(*$1);
            }
          | VARUSE {
              if (! vardefinitions->contains(*$1)) {
                yyerror("undefined variable " + *$1);
                YYERROR;
              }
              $$ = new StringDef(vardefinitions->getVar(*$1));
            }
          | BACKREFVAR {
              $$ = new StringDef(*$1);
              $$->setBackRef(true);
            }
          | stringdefwreferences '+' stringdefwreferences {
              $$ = StringDef::concat($1, $3);
              delete $1;
              delete $3;
            }
          | WRONG_BACKREFERENCE {
            // this token is used by the scanner to signal an error
            // in scanning a string with backreference
            yyerror("backreferences are allowed only inside ` `");
            YYERROR;
            
            $$ = 0;
          }
          ;

stringdef : REGEXPDEF {
              $$ = $1;
            }
          | STRINGDEF {
              $$ = new StringDef(*$1, true);
            }
          | REGEXPNOPREPROC {
              $$ = new StringDef(*$1);
            }
          | VARUSE {
              if (! vardefinitions->contains(*$1)) {
                yyerror("undefined variable " + *$1);
                YYERROR;
              }
              $$ = new StringDef(vardefinitions->getVar(*$1));
            }
          | stringdef '+' stringdef {
              $$ = StringDef::concat($1, $3);
              delete $1;
              delete $3;
            }
          | WRONG_BACKREFERENCE {
            // this token is used by the scanner to signal an error
            // in scanning a string with backreference
            yyerror("backreferences are allowed only inside ` `");
            YYERROR;
            
            $$ = 0;
          }
          ;

%%

extern int langdef_lex_destroy (void);

void
yyerror( const char *s )
{
  errorBuffer = s;
}

void
yyerror( const string &s )
{
  yyerror(s.c_str());
}

namespace srchilite {

LangElems *
parse_lang_def()
{
  return parse_lang_def("", "stdin");
}

LangElems *
parse_lang_def(const char *path, const char *name)
{
  current_lang_elems = 0;
  includedFileNotFound = false;
  vardefinitions = new VarDefinitions;
  parsestruct = ParseStructPtr(new ParseStruct(path, name));
  
  errorBuffer = "";
  int result = 1;
  bool fileNotFound = false;
  
  try {
  	if (strcmp(name, "stdin") != 0)
      open_file_to_scan(path, name);
  } catch (IOException &e) {
    errorBuffer = e.message;
    fileNotFound = true;
  }

  if (!fileNotFound)
     result = langdef_parse();

  delete vardefinitions;
  
  vardefinitions = 0;

  if (result != 0 && ! fileNotFound) {
  	  // make sure the input file is closed
	  close_langdefinputfile();
	  // close it before clearing the scanner
  }
  
  // release scanner memory
  clear_langdefscanner ();

  if (result != 0 || errorBuffer.size()) {
  	if (fileNotFound || includedFileNotFound) {
	  if (current_lang_elems) delete current_lang_elems;
	  throw ParserException(errorBuffer);
  	} else {
	  ParserException e(errorBuffer, parsestruct.get());
	  if (current_lang_elems) delete current_lang_elems;
	  throw e;
	}
  }

  return current_lang_elems;
}

}