File: styleparser.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 (253 lines) | stat: -rw-r--r-- 5,818 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
%{
/*
 * Copyright (C) 1999-2007 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 <iostream>
#include <string>

#include "formatterfactory.h"
#include "colors.h"
#include "keys.h"
#include "parsestyles.h"
#include "fileutil.h"
#include "stylekey.h"
#include "parserexception.h"
#include "settings.h"

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

using namespace srchilite;

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

int line = 1 ;

extern int stylesc_lex() ;
extern FILE *stylesc_in ;
extern int stylesc_lex_destroy  (void);

static string bodyBgColor;

static void updateBgColor(const std::string *);

// to generate the formatter for each language element
static FormatterFactory *formatterFactory;

/// where we store possible errors
static string errorBuffer;

%}

%union {
  int tok ; /* command */
  const std::string * string ; /* string : id, ... */
  srchilite::StyleConstant flag ;
  srchilite::StyleConstants *styleconstants;
  srchilite::KeyList *keylist;
} ;

%token <flag> BOLD ITALICS UNDERLINE FIXED NOTFIXED NOREF
%token <string> KEY COLOR BG_COLOR STRINGDEF
%token <tok> BG_T BODY_BG_COLOR

%type <flag> value
%type <styleconstants> stylevalues values
%type <string> color bgcolor
%type <keylist> keylist

%destructor {
	if ($$)
	  delete $$;
} keylist KEY COLOR BG_COLOR STRINGDEF color bgcolor

%%

stylefile : { /* allow empty files */ }
    | statements
    ;

statements : statements statement
    | statement
    ;

statement : option
    | bodybgcolor
    ;

option : keylist color bgcolor
             {
                 //printSequence( $1 ) ;
                 //printMessage_noln( ": " ) ;
                 //printMessage_noln( ($2 ? *$2 : "") ) ;
                 //printMessage_noln( " bg: " ) ;
                 //printMessage_noln( ($3 ? *$3 : "") ) ;
             }
         stylevalues ';'
             {
               KeyType key;
               KeyList *keylist = $1;
               StyleConstantsPtr stylevalues = StyleConstantsPtr($5);
               for (KeyList::const_iterator it = keylist->begin(); it != keylist->end(); ++it) {
                key = *it;
                if (!formatterFactory->createFormatter(key, ($2 ? *$2 : ""), ($3 ? *$3 : ""), stylevalues)) {
                  errorBuffer = "already defined " + *it;
                  //yyerror(alreadydef);

                  delete keylist;
                  if ($2)
                   delete $2;
                  if ($3)
                   delete $3;

                  YYERROR;
                }
               }
               delete keylist;
               if ($2)
                delete $2;
               if ($3)
                delete $3;
             }
       ;

keylist : keylist ',' KEY
            {
              $1->push_back(*$3);
              $$ = $1;
              delete $3;
            }
        | KEY
            {
              $$ = new KeyList;
              $$->push_back(*$1);
              delete $1;
            }
  ;

color : { $$ = 0; }
      | COLOR { $$ = $1;}
      | STRINGDEF { $$ = $1; }
      ;

bgcolor : { $$ = 0; }
      | BG_T ':' COLOR { $$ = $3;}
      | BG_T ':' STRINGDEF { $$ = $3; }
      ;

bodybgcolor : BODY_BG_COLOR COLOR ';' { updateBgColor($2); }
      | BODY_BG_COLOR STRINGDEF ';' { updateBgColor($2); }
      ;

stylevalues : { $$ = 0 ; }
            | values { $$ = $1; }
            ;

values : values ',' value { $$ = $1; $$->push_back($3) ; }
       | value { $$ = new StyleConstants; $$->push_back($1); }
       ;

value : BOLD { $$ = ISBOLD ; }
      | ITALICS { $$ = ISITALIC ; }
      | UNDERLINE { $$ = ISUNDERLINE ; }
      | FIXED { $$ = ISFIXED ; }
      | NOTFIXED { $$ = ISNOTFIXED ; }
      | NOREF { $$ = ISNOREF ; }
      ;

%%

string current_file;

namespace srchilite {

void
parseStyles(const string &name, FormatterFactory *genFactory,
           string &bodyBgColor_) {
  parseStyles(Settings::retrieveDataDir(), name, genFactory, bodyBgColor_);
}

void
parseStyles(const string &path, const string &name, FormatterFactory *genFactory,
           string &bodyBgColor_)
{
  formatterFactory = genFactory;
  errorBuffer = "";
  int result = 1;
  line = 1;

  // opens the file for yylex
  stylesc_in = open_data_file_stream(path, name);

  if (contains_path(name))
    current_file = name;
  else
    current_file = (path.size() ? path + "/" : "") + name;

  bodyBgColor = "";

  result = yyparse() ;

  bodyBgColor_ = bodyBgColor;

  fclose(stylesc_in);

  // free scanner memory
  stylesc_lex_destroy();
  
  if (result != 0 || errorBuffer.size()) {
  	throw ParserException(errorBuffer, current_file, line);
  }
}

}

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

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

void parseStyleError(const std::string &error)
{
	yyerror(error);
}

void updateBgColor(const std::string *c)
{
  if (bodyBgColor != "")
    yyerror("bgcolor already defined");
  else
    bodyBgColor = *c;

  // we don't need it anymore
  delete c;
}