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
|
%{
/*
* 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 <string.h>
#include <iostream>
#include <string>
#include "parsestruct.h"
#include "outlangdefscanner.h"
#include "outlangdefparserfun.h"
#include "textstyles.h"
#include "parserexception.h"
#include "ioexception.h"
using std::cerr;
using std::string;
using namespace srchilite;
static void yyerror( const char *s ) ;
static void yyerror( const string &s ) ;
/// the buffer for storing errors
static string errorBuffer;
TextStylesPtr textstyles;
string start_doc, end_doc;
ColorMapPtr colorMap;
CharTranslatorPtr charTranslator;
const char *reference_vars[] = {"linenum", "infilename", "infile", "outfile", 0};
const char *anchor_vars[] = {"linenum", "infilename", "infile", 0};
/// used to record that the error is due to an included file not found
static bool includedFileNotFound = false;
%}
%union {
int tok ; /* command */
bool booloption ;
const std::string * string ; /* string : id, ... */
int flag ;
};
%token <tok> BEGIN_T END_T DOC_TEMPLATE_T NODOC_TEMPLATE_T STYLE_TEMPLATE_T STYLE_SEPARATOR_T
%token <tok> BOLD_T ITALICS_T UNDERLINE_T COLOR_T BG_COLOR_T FIXED_T NOTFIXED_T
%token <tok> COLORMAP_T DEFAULT_T ONESTYLE_T TRANSLATIONS_T EXTENSION_T ANCHOR_T
%token <tok> REFERENCE_T INLINE_REFERENCE_T POSTLINE_REFERENCE_T POSTDOC_REFERENCE_T
%token <string> KEY STRINGDEF REGEXDEF LINE_PREFIX_T LINENUM_T WRONG_INCLUDE_FILE
%%
outputlang : outputlangdefs
;
outputlangdefs : outputlangdefs outputlangdef
| outputlangdef
;
outputlangdef : DOC_TEMPLATE_T STRINGDEF STRINGDEF END_T
{
textstyles->docTemplate = DocTemplate(*$2, *$3);
delete $2;
delete $3;
}
|
NODOC_TEMPLATE_T STRINGDEF STRINGDEF END_T
{
textstyles->noDocTemplate = DocTemplate(*$2, *$3);
delete $2;
delete $3;
}
|
STYLE_TEMPLATE_T STRINGDEF
{
textstyles->starting_template = *$2;
delete $2;
}
|
STYLE_SEPARATOR_T STRINGDEF
{
textstyles->style_separator = *$2;
delete $2;
}
|
BOLD_T STRINGDEF
{
textstyles->bold = *$2;
delete $2;
}
|
ITALICS_T STRINGDEF
{
textstyles->italics = *$2;
delete $2;
}
|
UNDERLINE_T STRINGDEF
{
textstyles->underline = *$2;
delete $2;
}
|
FIXED_T STRINGDEF
{
textstyles->fixed = *$2;
delete $2;
}
|
NOTFIXED_T STRINGDEF
{
textstyles->notfixed = *$2;
delete $2;
}
|
COLOR_T STRINGDEF
{
textstyles->color = *$2;
delete $2;
}
|
BG_COLOR_T STRINGDEF
{
textstyles->bg_color = *$2;
delete $2;
}
|
ONESTYLE_T STRINGDEF
{
textstyles->onestyle = *$2;
delete $2;
}
|
EXTENSION_T STRINGDEF
{
textstyles->file_extension = *$2;
delete $2;
}
|
ANCHOR_T STRINGDEF
{
textstyles->refstyle.anchor = TextStyle(*$2, anchor_vars);
delete $2;
}
|
LINE_PREFIX_T STRINGDEF
{
textstyles->line_prefix = *$2;
delete $2;
}
|
LINENUM_T STRINGDEF
{
textstyles->linenum = *$2;
delete $2;
}
|
REFERENCE_T STRINGDEF
{
if (textstyles->refstyle.inline_reference.empty())
textstyles->refstyle.inline_reference = TextStyle(*$2, reference_vars);
if (textstyles->refstyle.postline_reference.empty())
textstyles->refstyle.postline_reference = TextStyle(*$2, reference_vars);
if (textstyles->refstyle.postdoc_reference.empty())
textstyles->refstyle.postdoc_reference = TextStyle(*$2, reference_vars);
delete $2;
}
|
INLINE_REFERENCE_T STRINGDEF
{
textstyles->refstyle.inline_reference = TextStyle(*$2, reference_vars);
delete $2;
}
|
POSTLINE_REFERENCE_T STRINGDEF
{
textstyles->refstyle.postline_reference = TextStyle(*$2, reference_vars);
delete $2;
}
|
POSTDOC_REFERENCE_T STRINGDEF
{
textstyles->refstyle.postdoc_reference = TextStyle(*$2, reference_vars);
delete $2;
}
|
colormap
{
textstyles->colorMap = colorMap;
}
|
chartranslator
{
textstyles->charTranslator = charTranslator;
}
| 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);
delete $1;
YYERROR;
}
;
colormap : COLORMAP_T colormapentries END_T
;
colormapentries : colormapentries colormapentry
|
colormapentry
;
colormapentry : STRINGDEF STRINGDEF
{
(*colorMap)[*$1] = *$2;
delete $1;
delete $2;
}
|
DEFAULT_T STRINGDEF
{
colorMap->setDefault(*$2);
delete $2;
}
;
chartranslator : TRANSLATIONS_T translations END_T
;
translations : translations translation
|
translation
;
translation : REGEXDEF REGEXDEF
{
charTranslator->set_translation(*$1, *$2);
delete $1;
delete $2;
}
%%
extern int outlangdef_lex_destroy (void);
void
yyerror( const char *s )
{
errorBuffer = s;
}
void
yyerror( const string &s )
{
yyerror(s.c_str());
}
namespace srchilite {
TextStylesPtr
parse_outlang_def()
{
return parse_outlang_def("", "stdin");
}
TextStylesPtr
parse_outlang_def(const char *path, const char *name)
{
includedFileNotFound = false;
outlang_parsestruct = ParseStructPtr(new ParseStruct(path, name));
textstyles = TextStylesPtr(new TextStyles);
colorMap = ColorMapPtr(new ColorMap);
charTranslator = CharTranslatorPtr(new CharTranslator);
errorBuffer = "";
int result = 1;
bool fileNotFound = false;
try {
if (strcmp(name, "stdin") != 0)
open_outlang_file_to_scan(path, name);
} catch (IOException &e) {
errorBuffer = e.message;
fileNotFound = true;
}
if (!fileNotFound)
result = outlangdef_parse();
if (result != 0 && ! fileNotFound) {
// make sure the input file is closed
close_outlangdefinputfile();
// close it before clearing the scanner
}
// release scanner memory
clear_outlangdefscanner ();
if (result != 0 || errorBuffer.size()) {
if (fileNotFound || includedFileNotFound) {
throw ParserException(errorBuffer);
} else {
ParserException e(errorBuffer, outlang_parsestruct.get());
throw e;
}
}
return textstyles;
}
}
|