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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
|
%{ /* -*- c -*- */
/*
* Grammar for parsing the style sheets
*
* Copyright (c) 1988-1993 Miguel Santana
* Copyright (c) 1995-1999 Akim Demaille, Miguel Santana
*
*/
/*
* This file is part of a2ps
*
* 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, 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; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* $Id: parsessh.y,v 1.1.1.1.2.2 2007/12/29 01:58:35 mhatta Exp $
*/
#include "a2ps.h"
#include "jobs.h"
#include "ffaces.h"
#include "ssheet.h"
#include "message.h"
#include "routines.h"
#include "yy2ssh.h"
#include "regex.h"
#define YYDEBUG 1
#define YYERROR_VERBOSE 1
#define YYPRINT(file, type, value) yyprint (file, type, value)
/* We need to use the same `const' as bison, to avoid the following
prototypes to diverge from the function declarations */
#undef const
#ifndef __cplusplus
# ifndef __STDC__
# define const
# endif
#endif
/* Comes from the parser */
extern int sshlineno;
/* Comes from the caller */
extern FILE * sshin;
extern struct a2ps_job * job;
extern const char * sshfilename;
/* Local prototypes */
void yyerror PARAMS ((const char *msg));
static void yyprint ();
/* Initilizes the obstacks */
void sshlex_initialize PARAMS ((void));
/* Comes from main.c */
extern int highlight_level;
int yylex PARAMS ((void));
struct style_sheet * parse_style_sheet PARAMS ((const char * filename));
/* Defines the style sheet being loaded */
static struct style_sheet * parsed_style_sheet = NULL;
%}
%union
{
int integer;
uchar * string;
struct pattern * pattern;
struct style_sheet * sheet;
struct rule * rule;
struct sequence * sequence;
struct darray * array;
struct words * words;
struct faced_string * faced_string;
enum face_e face; /* Face */
enum fflag_e fflags; /* Flags for faces */
struct fface_s fface; /* Flagged face */
enum case_sensitiveness sensitiveness;
}
%token tSTYLE tIS tEND tKEYWORDS tARE tIN tOPERATORS tSEQUENCES
%token tFIRST tSECOND tALPHABET tALPHABETS tDOCUMENTATION tEXCEPTIONS
%token tCASE tCSTRING tCCHAR tOPTIONAL tCLOSERS
%token tWRITTEN tBY tVERSION tREQUIRES tA2PS tANCESTORS
%token <face> tFACE
%token <fflags> tFFLAGS
%token <string> tSTRING tLATEXSYMBOL
%token <pattern> tREGEX
%token <sensitiveness> tSENSITIVENESS
%token <integer> tBACK_REF
%type <pattern> regex
%type <fface> fface fface_sxp
%type <string> authors documentation version long_string requirement
%type <faced_string> a_rhs
%type <rule> rule keyword_regex operator_regex
%type <sheet> style_sheet definition_list
%type <words> keywords_def keywords_rule_list
%type <words> operators_def operators_rule_list
%type <array> sequence_list sequence_def
%type <words> exception_def_opt
%type <array> ancestors_def ancestors_list
%type <words> closers_opt
%type <sensitiveness> case_def
%type <array> rhs rhs_list
%type <sequence> sequence
%%
/************************************************************************/
/* Top most */
/************************************************************************/
file :
style_sheet
{
parsed_style_sheet = $1;
}
;
style_sheet :
tSTYLE tSTRING tIS definition_list tEND tSTYLE
{
$4->name = $2;
$4->key = "<No key yet>";
$$ = $4;
}
;
definition_list :
/* empty */ {
$$ = new_style_sheet ((const uchar *) "<no name>");
}
| definition_list tOPTIONAL tKEYWORDS keywords_def tKEYWORDS
{
if (highlight_level == 2) {
words_set_no_face ($4, Plain_fface);
words_merge_rules_unique ($1->keywords, $4);
} else
words_free ($4);
$$ = $1;
}
| definition_list tKEYWORDS keywords_def tKEYWORDS
{
words_set_no_face ($3, Plain_fface);
words_merge_rules_unique ($1->keywords, $3);
$$ = $1;
}
| definition_list tOPTIONAL tOPERATORS operators_def tOPERATORS
{
if (highlight_level == 2) {
words_set_no_face ($4, Plain_fface);
words_merge_rules_unique ($1->operators, $4);
} else
words_free ($4);
$$ = $1;
}
| definition_list tOPERATORS operators_def tOPERATORS
{
words_set_no_face ($3, Plain_fface);
words_merge_rules_unique ($1->operators, $3);
$$ = $1;
}
| definition_list tOPTIONAL sequence_def {
if (highlight_level == 2) {
da_concat ($1->sequences, $3);
da_erase ($3);
} else
da_free ($3, (da_map_func_t) free_sequence);
$$ = $1;
}
| definition_list sequence_def {
da_concat ($1->sequences, $2);
da_erase ($2);
$$ = $1;
}
| definition_list ancestors_def {
da_concat ($1->ancestors, $2);
da_erase ($2);
$$ = $1;
}
| definition_list tALPHABETS tARE tSTRING {
string_to_array ($1->alpha1, $4);
string_to_array ($1->alpha2, $4);
/* This is the syntax table used by regex */
free ($4);
$4 = NULL;
$$ = $1;
}
| definition_list tFIRST tALPHABET tIS tSTRING {
string_to_array ($1->alpha1, $5);
/* This is the syntax table used by regex */
free ($5);
$5 = NULL;
$$ = $1;
}
| definition_list tSECOND tALPHABET tIS tSTRING {
string_to_array ($1->alpha2, $5);
/* This is the syntax table used by regex */
free ($5);
$5 = NULL;
$$ = $1;
}
| definition_list case_def {
$1->sensitiveness = $2;
}
| definition_list documentation {
$1->documentation = $2;
}
| definition_list authors {
$1->author = $2;
}
| definition_list version {
style_sheet_set_version ($1, (const char *) $2);
}
| definition_list requirement {
/* Make sure now that we won't encounter new tokens.
* This avoids nasty error messages, or worse:
* unexpected behavior at run time */
if (!style_sheet_set_requirement ($1, (const char *) $2))
error (1, 0,
_("cannot process `%s' which requires a2ps version %s"),
sshfilename, $2);
}
;
/************************************************************************/
/* Dealing with the inessential informations */
/************************************************************************/
requirement :
tREQUIRES tA2PS tSTRING { $$ = $3 ; }
| tREQUIRES tA2PS tVERSION tSTRING { $$ = $4 ; } ;
documentation :
tDOCUMENTATION tIS long_string tEND tDOCUMENTATION { $$ = $3 ; };
long_string: tSTRING { $$ = $1; }
| long_string tSTRING
{
size_t len1;
size_t len2;
len1 = ustrlen ($1);
$1[len1] = '\n';
len2 = ustrlen ($2);
$$ = XMALLOC (uchar, len1 + len2 + 2);
ustpcpy (ustpncpy ($$, $1, len1 + 1), $2);
free ($1);
free ($2);
}
;
authors : tWRITTEN tBY tSTRING { $$ = $3 ; };
version :
tVERSION tIS tSTRING { $$ = $3 ; }
| tVERSION tSTRING { $$ = $2 ; };
/************************************************************************/
/* Dealing with the ancestors of a style sheet */
/************************************************************************/
ancestors_def : tANCESTORS tARE ancestors_list tEND tANCESTORS
{
/* The list of keys of style sheets from which it inherits */
$$ = $3;
}
;
ancestors_list: tSTRING
{
/* Create a list of ancestors, and drop the new one in */
$$ = da_new ("Ancestors tmp", 2,
da_linear, 2,
(da_print_func_t) da_str_print, NULL);
da_append ($$, $1);
}
| ancestors_list ',' tSTRING
{
da_append ($1, $3);
$$ = $1;
}
;
case_def : tCASE tSENSITIVENESS { $$ = $2 ; } ;
/************************************************************************/
/* Rhs */
/* (Lists of (strings/regexp back references, face) */
/************************************************************************/
/* Regex split upon several lines */
regex:
tREGEX
{
$$ = $1;
}
| regex tREGEX
{
/* Concatenate $2 to $1 makes $$ */
$$ = $1;
$$->pattern = XREALLOC ($$->pattern, char, $1->len + $2->len + 1);
strncpy ($$->pattern + $$->len, $2->pattern, $2->len);
$$->len += $2->len;
free ($2->pattern);
}
;
rhs:
a_rhs
{
$$ = rhs_new ();
rhs_add ($$, $1);
}
;
a_rhs:
tSTRING fface
{
$$ = faced_string_new ($1, 0, $2);
}
| tSTRING
{
$$ = faced_string_new ($1, 0, No_fface);
}
| fface
{
$$ = faced_string_new (NULL, 0, $1);
}
| tBACK_REF fface
{
$$ = faced_string_new (UNULL, $1, $2);
}
| tBACK_REF
{
$$ = faced_string_new (UNULL, $1, No_fface);
}
| tLATEXSYMBOL
{
$$ = faced_string_new ($1, 0, Symbol_fface);
}
;
rhs_list:
a_rhs
{
$$ = rhs_new ();
rhs_add ($$, $1);
}
|
rhs_list ',' a_rhs
{
rhs_add ($1, $3);
$$ = $1;
}
;
/*
* The flagged faces (One (true face or Invisible) plus flags)
*/
fface:
tFACE
{
fface_set_face ($$, $1);
fface_reset_flags ($$);
}
| tFFLAGS
{
fface_reset_face ($$);
fface_set_flags ($$, $1);
/* If there is no face, then set Invisible */
fface_add_flags ($$, ff_Invisible);
}
| '(' fface_sxp ')'
{
$$ = $2;
/* If there is no face, then set Invisible */
if (fface_get_face ($$) == No_face)
fface_add_flags ($$, ff_Invisible);
}
;
fface_sxp:
tFACE
{
fface_set_face($$, $1);
fface_reset_flags($$);
}
| tFFLAGS
{
fface_reset_face($$);
fface_set_flags($$, $1);
}
| fface_sxp '+' tFACE
{
/* FIXME: Overloading of the face should be forbidden */
$$ = $1;
fface_set_face($$, $3);
}
| fface_sxp '+' tFFLAGS
{
$$ = $1;
fface_add_flags($$, $3);
}
;
/************************************************************************/
/* Symbol atoms */
/************************************************************************/
/* A rule is the group of 1. string to match, 2. the rhs
*
* In the special shortcut where no face is given, see where
* rule_list appears for resolving */
rule: tSTRING rhs
{
$$ = rule_new ($1, NULL, $2,
sshfilename, sshlineno);
}
| tSTRING
{
$$ = rule_new ($1, NULL,
rhs_new_single (UNULL, 0, No_fface),
sshfilename, sshlineno);
}
| '(' tSTRING rhs_list ')'
{
$$ = rule_new ($2, NULL, $3,
sshfilename, sshlineno);
}
;
/************************************************************************/
/* Keywords lists */
/* We make the difference because the regex must be compiled with a */
/* difference (which is \\b\\(%s\\)\\b) between keywords and operators */
/************************************************************************/
keywords_def : tIN fface tARE keywords_rule_list tEND {
words_set_no_face ($4, $2);
$$ = $4;
}
| tARE keywords_rule_list tEND {
/* First of all, the No_face must be turned into Plain */
$$ = $2;
}
;
keywords_rule_list:
rule
{
$$ = words_new ("Keywords: Strings", "Keywords: Regexps", 100, 100);
words_add_string ($$, $1);
}
| keyword_regex
{
$$ = words_new ("Keywords: Strings", "Keywords: Regexps", 100, 100);
words_add_regex ($$, $1);
}
| keywords_rule_list ',' rule
{
words_add_string ($1, $3);
$$ = $1;
}
| keywords_rule_list ',' keyword_regex
{
words_add_regex ($1, $3);
$$ = $1;
}
;
/* A rule is the group of 1. string to match, 2. string to
* print, 3. face of the string to print. In the special
* shortcut where no face is given, see where rule_list appears
* for resolving */
keyword_regex:
regex rhs
{
$$ = keyword_rule_new (UNULL, $1, $2,
sshfilename, sshlineno);
}
| regex
{
$$ = keyword_rule_new (UNULL, $1,
rhs_new_single (UNULL, 0,
No_fface),
sshfilename, sshlineno);
}
| '(' regex rhs_list ')'
{
$$ = keyword_rule_new (UNULL, $2, $3,
sshfilename, sshlineno);
}
;
/************************************************************************/
/* Operators lists */
/************************************************************************/
operators_def: tIN fface tARE operators_rule_list tEND {
words_set_no_face ($4, $2);
$$ = $4;
}
| tARE operators_rule_list tEND {
/* First of all, the No_face must be turned into Plain */
$$ = $2;
}
;
operators_rule_list:
rule
{
$$ = words_new ("Operators: Strings", "Operators: Regexps",
100, 100);
words_add_string ($$, $1);
}
| operator_regex
{
$$ = words_new ("Operators: Strings", "Operators: Regexps",
100, 100);
words_add_regex ($$, $1);
}
| operators_rule_list ',' rule
{
words_add_string ($1, $3);
$$ = $1;
}
| operators_rule_list ',' operator_regex
{
words_add_regex ($1, $3);
$$ = $1;
}
;
/* A rule is the group of 1. string to match, 2. string to
* print, 3. face of the string to print. In the special
* shortcut where no face is given, see where rule_list appears
* for resolving */
operator_regex:
regex rhs
{
$$ = rule_new (UNULL, $1, $2,
sshfilename, sshlineno);
}
| regex
{
$$ = rule_new (UNULL, $1,
rhs_new_single (UNULL, 0, No_fface),
sshfilename, sshlineno);
}
| '(' regex rhs_list ')'
{
$$ = rule_new (UNULL, $2, $3,
sshfilename, sshlineno);
}
;
/************************************************************************/
/* Dealing with the sequences */
/************************************************************************/
sequence_def : tSEQUENCES tARE sequence_list tEND tSEQUENCES { $$ = $3; }
;
sequence_list: sequence {
$$ = da_new ("Sequence tmp", 100,
da_linear, 100,
(da_print_func_t) sequence_self_print, NULL);
da_append ($$, $1);
}
| sequence_list ',' sequence {
da_append ($1, $3);
$$ = $1;
}
;
/*
* I can understand one will get sick reading this. It hurts, indeed,
* but is necessary because of shift/reduce conflicts if one uses
*
* sequence: operators_rule face closers_opt exception_def_opt
*
* The problem is that when bison/yacc reads this
*
* <word> <word> . <face1> <face2>
*
* it doesn't know whether <face1> should be in $1, or in $2.
* So we have to inline the rule at hand...
*/
sequence:
/* Expansion of each possibility for
* <operators_rule> <face> <closers_opt> <exception_def_opt>
*/
tSTRING tLATEXSYMBOL fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new ($1, NULL,
rhs_new_single ($2, 0, Symbol_fface),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $3, $4, $5);
}
| tSTRING tSTRING fface fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new ($1, NULL,
rhs_new_single ($2, 0, $3),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $4, $5, $6);
}
| tSTRING fface fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new ($1, NULL,
rhs_new_single (UNULL, 0, $2),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $3, $4, $5);
}
| tSTRING tSTRING fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new ($1, NULL,
rhs_new_single ($2, 0, $3),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $3, $4, $5);
}
| tSTRING fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new ($1, NULL,
rhs_new_single (UNULL, 0, $2),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $2, $3, $4);
}
| regex tSTRING fface fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new (UNULL, $1,
rhs_new_single ($2, 0, $3),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $4, $5, $6);
}
| regex fface fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new (UNULL, $1,
rhs_new_single (UNULL, 0, $2),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $3, $4, $5);
}
| regex tSTRING fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new (UNULL, $1,
rhs_new_single ($2, 0, $3),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $3, $4, $5);
}
| regex fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new (UNULL, $1,
rhs_new_single (UNULL, 0, No_fface),
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $2, $3, $4);
}
| '(' tSTRING rhs_list ')' fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new ($2, NULL, $3,
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $5, $6, $7);
}
| '(' regex rhs_list ')' fface closers_opt exception_def_opt
{
struct rule * open_rule;
open_rule = rule_new (UNULL, $2, $3,
sshfilename, sshlineno);
$$ = sequence_new (open_rule, $5, $6, $7);
}
/* End of the brute force expansion */
| tCSTRING
{
$$ = new_C_string_sequence ("\"");
}
| tCCHAR
{
$$ = new_C_string_sequence ("\'");
}
;
closers_opt:
/* Nothing */
{
/* This is a shortcut which means "up to the end of the line". */
$$ = words_new ("Closing: Strings", "Closing: Regexps", 2, 2);
words_add_string ($$, rule_new (xustrdup ("\n"), NULL,
rhs_new_single (NULL, 0,
No_fface),
sshfilename, sshlineno));
}
| rule
{
/* Only one */
$$ = words_new ("Closing: Strings", "Closing: Regexps", 2, 2);
words_add_string ($$, $1);
}
| operator_regex
{
/* Only one */
$$ = words_new ("Closing: Strings", "Closing: Regexps", 2, 2);
words_add_regex ($$, $1);
}
| tCLOSERS operators_def tCLOSERS
{
/* Several, comma separated, between () */
$$ = $2;
}
;
exception_def_opt:
/* Nothing */
{
$$ = words_new ("Exceptions: Strings", "Exceptions: Regexps", 1, 1);
}
| tEXCEPTIONS operators_def tEXCEPTIONS
{
$$ = $2;
};
%%
void
yyerror (const char *msg)
{
error_at_line (1, 0, sshfilename, sshlineno, "%s", msg);
}
/*
* FIXME: Cover the other relevant types
*/
static void
yyprint (FILE *file, int type, YYSTYPE value)
{
switch (type) {
case tBACK_REF:
fprintf (file, " \\%d", value.integer);
break;
case tFFLAGS:
putc (' ', file);
fflag_self_print (value.fflags, file);
break;
case tFACE:
fprintf (file, " %s", face_to_string (value.face));
break;
case tREGEX:
fprintf (file, " /%s/", value.pattern->pattern);
break;
case tSTRING:
fprintf (file, " \"%s\"", value.string);
break;
}
}
struct style_sheet *
parse_style_sheet (const char * filename)
{
int res;
sshfilename = filename;
sshlineno = 1;
sshin = xrfopen (sshfilename);
message (msg_file | msg_sheet | msg_parse,
(stderr, "Parsing file `%s'\n", sshfilename));
sshlex_initialize ();
if (msg_test (msg_parse))
yydebug = true;
res = yyparse (); /* FIXME: test the result of parsing */
if (msg_test (msg_sheet)) {
fprintf (stderr, "---------- Right after parsing of %s\n",
parsed_style_sheet->key);
style_sheet_self_print (parsed_style_sheet, stderr);
fprintf (stderr, "---------- End of after parsing of %s\n",
parsed_style_sheet->key);
}
fclose (sshin);
return parsed_style_sheet;
}
|