* src/libs/libgroff/glyphuni.cpp (glyph_to_unicode_list),
[s-roff.git] / src / preproc / pic / pic.y
blob0bd85c40e87eb545200923fbaaa02f80469d8ab0
1 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004
2 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "pic.h"
22 #include "ptable.h"
23 #include "object.h"
25 extern int delim_flag;
26 extern void copy_rest_thru(const char *, const char *);
27 extern void copy_file_thru(const char *, const char *, const char *);
28 extern void push_body(const char *);
29 extern void do_for(char *var, double from, double to,
30 int by_is_multiplicative, double by, char *body);
31 extern void do_lookahead();
33 /* Maximum number of characters produced by printf("%g") */
34 #define GDIGITS 14
36 int yylex();
37 void yyerror(const char *);
39 void reset(const char *nm);
40 void reset_all();
42 place *lookup_label(const char *);
43 void define_label(const char *label, const place *pl);
45 direction current_direction;
46 position current_position;
48 implement_ptable(place)
50 PTABLE(place) top_table;
52 PTABLE(place) *current_table = &top_table;
53 saved_state *current_saved_state = 0;
55 object_list olist;
57 const char *ordinal_postfix(int n);
58 const char *object_type_name(object_type type);
59 char *format_number(const char *form, double n);
60 char *do_sprintf(const char *form, const double *v, int nv);
65 %union {
66 char *str;
67 int n;
68 double x;
69 struct { double x, y; } pair;
70 struct { double x; char *body; } if_data;
71 struct { char *str; const char *filename; int lineno; } lstr;
72 struct { double *v; int nv; int maxv; } dv;
73 struct { double val; int is_multiplicative; } by;
74 place pl;
75 object *obj;
76 corner crn;
77 path *pth;
78 object_spec *spec;
79 saved_state *pstate;
80 graphics_state state;
81 object_type obtype;
84 %token <str> LABEL
85 %token <str> VARIABLE
86 %token <x> NUMBER
87 %token <lstr> TEXT
88 %token <lstr> COMMAND_LINE
89 %token <str> DELIMITED
90 %token <n> ORDINAL
91 %token TH
92 %token LEFT_ARROW_HEAD
93 %token RIGHT_ARROW_HEAD
94 %token DOUBLE_ARROW_HEAD
95 %token LAST
96 %token UP
97 %token DOWN
98 %token LEFT
99 %token RIGHT
100 %token BOX
101 %token CIRCLE
102 %token ELLIPSE
103 %token ARC
104 %token LINE
105 %token ARROW
106 %token MOVE
107 %token SPLINE
108 %token HEIGHT
109 %token RADIUS
110 %token FIGNAME
111 %token WIDTH
112 %token DIAMETER
113 %token UP
114 %token DOWN
115 %token RIGHT
116 %token LEFT
117 %token FROM
118 %token TO
119 %token AT
120 %token WITH
121 %token BY
122 %token THEN
123 %token SOLID
124 %token DOTTED
125 %token DASHED
126 %token CHOP
127 %token SAME
128 %token INVISIBLE
129 %token LJUST
130 %token RJUST
131 %token ABOVE
132 %token BELOW
133 %token OF
134 %token THE
135 %token WAY
136 %token BETWEEN
137 %token AND
138 %token HERE
139 %token DOT_N
140 %token DOT_E
141 %token DOT_W
142 %token DOT_S
143 %token DOT_NE
144 %token DOT_SE
145 %token DOT_NW
146 %token DOT_SW
147 %token DOT_C
148 %token DOT_START
149 %token DOT_END
150 %token DOT_X
151 %token DOT_Y
152 %token DOT_HT
153 %token DOT_WID
154 %token DOT_RAD
155 %token SIN
156 %token COS
157 %token ATAN2
158 %token LOG
159 %token EXP
160 %token SQRT
161 %token K_MAX
162 %token K_MIN
163 %token INT
164 %token RAND
165 %token SRAND
166 %token COPY
167 %token THRU
168 %token TOP
169 %token BOTTOM
170 %token UPPER
171 %token LOWER
172 %token SH
173 %token PRINT
174 %token CW
175 %token CCW
176 %token FOR
177 %token DO
178 %token IF
179 %token ELSE
180 %token ANDAND
181 %token OROR
182 %token NOTEQUAL
183 %token EQUALEQUAL
184 %token LESSEQUAL
185 %token GREATEREQUAL
186 %token LEFT_CORNER
187 %token RIGHT_CORNER
188 %token NORTH
189 %token SOUTH
190 %token EAST
191 %token WEST
192 %token CENTER
193 %token END
194 %token START
195 %token RESET
196 %token UNTIL
197 %token PLOT
198 %token THICKNESS
199 %token FILL
200 %token COLORED
201 %token OUTLINED
202 %token SHADED
203 %token ALIGNED
204 %token SPRINTF
205 %token COMMAND
207 %token DEFINE
208 %token UNDEF
210 %left '.'
212 /* this ensures that plot 17 "%g" parses as (plot 17 "%g") */
213 %left PLOT
214 %left TEXT SPRINTF
216 /* give text adjustments higher precedence than TEXT, so that
217 box "foo" above ljust == box ("foo" above ljust)
220 %left LJUST RJUST ABOVE BELOW
222 %left LEFT RIGHT
223 /* Give attributes that take an optional expression a higher
224 precedence than left and right, so that eg `line chop left'
225 parses properly. */
226 %left CHOP SOLID DASHED DOTTED UP DOWN FILL COLORED OUTLINED
227 %left LABEL
229 %left VARIABLE NUMBER '(' SIN COS ATAN2 LOG EXP SQRT K_MAX K_MIN INT RAND SRAND LAST
230 %left ORDINAL HERE '`'
232 %left BOX CIRCLE ELLIPSE ARC LINE ARROW SPLINE '['
234 /* these need to be lower than '-' */
235 %left HEIGHT RADIUS WIDTH DIAMETER FROM TO AT THICKNESS
237 /* these must have higher precedence than CHOP so that `label %prec CHOP'
238 works */
239 %left DOT_N DOT_E DOT_W DOT_S DOT_NE DOT_SE DOT_NW DOT_SW DOT_C
240 %left DOT_START DOT_END TOP BOTTOM LEFT_CORNER RIGHT_CORNER
241 %left UPPER LOWER NORTH SOUTH EAST WEST CENTER START END
243 %left ','
244 %left OROR
245 %left ANDAND
246 %left EQUALEQUAL NOTEQUAL
247 %left '<' '>' LESSEQUAL GREATEREQUAL
249 %left BETWEEN OF
250 %left AND
252 %left '+' '-'
253 %left '*' '/' '%'
254 %right '!'
255 %right '^'
257 %type <x> expr any_expr text_expr
258 %type <by> optional_by
259 %type <pair> expr_pair position_not_place
260 %type <if_data> simple_if
261 %type <obj> nth_primitive
262 %type <crn> corner
263 %type <pth> path label_path relative_path
264 %type <pl> place label element element_list middle_element_list
265 %type <spec> object_spec
266 %type <pair> position
267 %type <obtype> object_type
268 %type <n> optional_ordinal_last ordinal
269 %type <str> macro_name until
270 %type <dv> sprintf_args
271 %type <lstr> text print_args print_arg
275 top:
276 optional_separator
277 | element_list
279 if (olist.head)
280 print_picture(olist.head);
285 element_list:
286 optional_separator middle_element_list optional_separator
287 { $$ = $2; }
290 middle_element_list:
291 element
292 { $$ = $1; }
293 | middle_element_list separator element
294 { $$ = $1; }
297 optional_separator:
298 /* empty */
299 | separator
302 separator:
304 | separator ';'
307 placeless_element:
308 FIGNAME '=' macro_name
310 a_delete graphname;
311 graphname = new char[strlen($3) + 1];
312 strcpy(graphname, $3);
313 a_delete $3;
316 VARIABLE '=' any_expr
318 define_variable($1, $3);
319 a_delete $1;
321 | VARIABLE ':' '=' any_expr
323 place *p = lookup_label($1);
324 if (!p) {
325 lex_error("variable `%1' not defined", $1);
326 YYABORT;
328 p->obj = 0;
329 p->x = $4;
330 p->y = 0.0;
331 a_delete $1;
333 | UP
334 { current_direction = UP_DIRECTION; }
335 | DOWN
336 { current_direction = DOWN_DIRECTION; }
337 | LEFT
338 { current_direction = LEFT_DIRECTION; }
339 | RIGHT
340 { current_direction = RIGHT_DIRECTION; }
341 | COMMAND_LINE
343 olist.append(make_command_object($1.str, $1.filename,
344 $1.lineno));
346 | COMMAND print_args
348 olist.append(make_command_object($2.str, $2.filename,
349 $2.lineno));
351 | PRINT print_args
353 fprintf(stderr, "%s\n", $2.str);
354 a_delete $2.str;
355 fflush(stderr);
357 | SH
358 { delim_flag = 1; }
359 DELIMITED
361 delim_flag = 0;
362 if (safer_flag)
363 lex_error("unsafe to run command `%1'", $3);
364 else
365 system($3);
366 a_delete $3;
368 | COPY TEXT
370 if (yychar < 0)
371 do_lookahead();
372 do_copy($2.str);
373 // do not delete the filename
375 | COPY TEXT THRU
376 { delim_flag = 2; }
377 DELIMITED
378 { delim_flag = 0; }
379 until
381 if (yychar < 0)
382 do_lookahead();
383 copy_file_thru($2.str, $5, $7);
384 // do not delete the filename
385 a_delete $5;
386 a_delete $7;
388 | COPY THRU
389 { delim_flag = 2; }
390 DELIMITED
391 { delim_flag = 0; }
392 until
394 if (yychar < 0)
395 do_lookahead();
396 copy_rest_thru($4, $6);
397 a_delete $4;
398 a_delete $6;
400 | FOR VARIABLE '=' expr TO expr optional_by DO
401 { delim_flag = 1; }
402 DELIMITED
404 delim_flag = 0;
405 if (yychar < 0)
406 do_lookahead();
407 do_for($2, $4, $6, $7.is_multiplicative, $7.val, $10);
409 | simple_if
411 if (yychar < 0)
412 do_lookahead();
413 if ($1.x != 0.0)
414 push_body($1.body);
415 a_delete $1.body;
417 | simple_if ELSE
418 { delim_flag = 1; }
419 DELIMITED
421 delim_flag = 0;
422 if (yychar < 0)
423 do_lookahead();
424 if ($1.x != 0.0)
425 push_body($1.body);
426 else
427 push_body($4);
428 a_delete $1.body;
429 a_delete $4;
431 | reset_variables
432 | RESET
433 { define_variable("scale", 1.0); }
436 macro_name:
437 VARIABLE
438 | LABEL
441 reset_variables:
442 RESET VARIABLE
444 reset($2);
445 a_delete $2;
447 | reset_variables VARIABLE
449 reset($2);
450 a_delete $2;
452 | reset_variables ',' VARIABLE
454 reset($3);
455 a_delete $3;
459 print_args:
460 print_arg
461 { $$ = $1; }
462 | print_args print_arg
464 $$.str = new char[strlen($1.str) + strlen($2.str) + 1];
465 strcpy($$.str, $1.str);
466 strcat($$.str, $2.str);
467 a_delete $1.str;
468 a_delete $2.str;
469 if ($1.filename) {
470 $$.filename = $1.filename;
471 $$.lineno = $1.lineno;
473 else if ($2.filename) {
474 $$.filename = $2.filename;
475 $$.lineno = $2.lineno;
480 print_arg:
481 expr %prec ','
483 $$.str = new char[GDIGITS + 1];
484 sprintf($$.str, "%g", $1);
485 $$.filename = 0;
486 $$.lineno = 0;
488 | text
489 { $$ = $1; }
490 | position %prec ','
492 $$.str = new char[GDIGITS + 2 + GDIGITS + 1];
493 sprintf($$.str, "%g, %g", $1.x, $1.y);
494 $$.filename = 0;
495 $$.lineno = 0;
499 simple_if:
500 IF any_expr THEN
501 { delim_flag = 1; }
502 DELIMITED
504 delim_flag = 0;
505 $$.x = $2;
506 $$.body = $5;
510 until:
511 /* empty */
512 { $$ = 0; }
513 | UNTIL TEXT
514 { $$ = $2.str; }
517 any_expr:
518 expr
519 { $$ = $1; }
520 | text_expr
521 { $$ = $1; }
524 text_expr:
525 text EQUALEQUAL text
527 $$ = strcmp($1.str, $3.str) == 0;
528 a_delete $1.str;
529 a_delete $3.str;
531 | text NOTEQUAL text
533 $$ = strcmp($1.str, $3.str) != 0;
534 a_delete $1.str;
535 a_delete $3.str;
537 | text_expr ANDAND text_expr
538 { $$ = ($1 != 0.0 && $3 != 0.0); }
539 | text_expr ANDAND expr
540 { $$ = ($1 != 0.0 && $3 != 0.0); }
541 | expr ANDAND text_expr
542 { $$ = ($1 != 0.0 && $3 != 0.0); }
543 | text_expr OROR text_expr
544 { $$ = ($1 != 0.0 || $3 != 0.0); }
545 | text_expr OROR expr
546 { $$ = ($1 != 0.0 || $3 != 0.0); }
547 | expr OROR text_expr
548 { $$ = ($1 != 0.0 || $3 != 0.0); }
549 | '!' text_expr
550 { $$ = ($2 == 0.0); }
554 optional_by:
555 /* empty */
557 $$.val = 1.0;
558 $$.is_multiplicative = 0;
560 | BY expr
562 $$.val = $2;
563 $$.is_multiplicative = 0;
565 | BY '*' expr
567 $$.val = $3;
568 $$.is_multiplicative = 1;
572 element:
573 object_spec
575 $$.obj = $1->make_object(&current_position,
576 &current_direction);
577 if ($$.obj == 0)
578 YYABORT;
579 delete $1;
580 if ($$.obj)
581 olist.append($$.obj);
582 else {
583 $$.x = current_position.x;
584 $$.y = current_position.y;
587 | LABEL ':' optional_separator element
589 $$ = $4;
590 define_label($1, & $$);
591 a_delete $1;
593 | LABEL ':' optional_separator position_not_place
595 $$.obj = 0;
596 $$.x = $4.x;
597 $$.y = $4.y;
598 define_label($1, & $$);
599 a_delete $1;
601 | LABEL ':' optional_separator place
603 $$ = $4;
604 define_label($1, & $$);
605 a_delete $1;
607 | '{'
609 $<state>$.x = current_position.x;
610 $<state>$.y = current_position.y;
611 $<state>$.dir = current_direction;
613 element_list '}'
615 current_position.x = $<state>2.x;
616 current_position.y = $<state>2.y;
617 current_direction = $<state>2.dir;
619 optional_element
621 $$ = $3;
623 | placeless_element
625 $$.obj = 0;
626 $$.x = current_position.x;
627 $$.y = current_position.y;
631 optional_element:
632 /* empty */
634 | element
638 object_spec:
640 { $$ = new object_spec(BOX_OBJECT); }
641 | CIRCLE
642 { $$ = new object_spec(CIRCLE_OBJECT); }
643 | ELLIPSE
644 { $$ = new object_spec(ELLIPSE_OBJECT); }
645 | ARC
647 $$ = new object_spec(ARC_OBJECT);
648 $$->dir = current_direction;
650 | LINE
652 $$ = new object_spec(LINE_OBJECT);
653 lookup_variable("lineht", & $$->segment_height);
654 lookup_variable("linewid", & $$->segment_width);
655 $$->dir = current_direction;
657 | ARROW
659 $$ = new object_spec(ARROW_OBJECT);
660 lookup_variable("lineht", & $$->segment_height);
661 lookup_variable("linewid", & $$->segment_width);
662 $$->dir = current_direction;
664 | MOVE
666 $$ = new object_spec(MOVE_OBJECT);
667 lookup_variable("moveht", & $$->segment_height);
668 lookup_variable("movewid", & $$->segment_width);
669 $$->dir = current_direction;
671 | SPLINE
673 $$ = new object_spec(SPLINE_OBJECT);
674 lookup_variable("lineht", & $$->segment_height);
675 lookup_variable("linewid", & $$->segment_width);
676 $$->dir = current_direction;
678 | text %prec TEXT
680 $$ = new object_spec(TEXT_OBJECT);
681 $$->text = new text_item($1.str, $1.filename, $1.lineno);
683 | PLOT expr
685 $$ = new object_spec(TEXT_OBJECT);
686 $$->text = new text_item(format_number(0, $2), 0, -1);
688 | PLOT expr text
690 $$ = new object_spec(TEXT_OBJECT);
691 $$->text = new text_item(format_number($3.str, $2),
692 $3.filename, $3.lineno);
693 a_delete $3.str;
695 | '['
697 saved_state *p = new saved_state;
698 $<pstate>$ = p;
699 p->x = current_position.x;
700 p->y = current_position.y;
701 p->dir = current_direction;
702 p->tbl = current_table;
703 p->prev = current_saved_state;
704 current_position.x = 0.0;
705 current_position.y = 0.0;
706 current_table = new PTABLE(place);
707 current_saved_state = p;
708 olist.append(make_mark_object());
710 element_list ']'
712 current_position.x = $<pstate>2->x;
713 current_position.y = $<pstate>2->y;
714 current_direction = $<pstate>2->dir;
715 $$ = new object_spec(BLOCK_OBJECT);
716 olist.wrap_up_block(& $$->oblist);
717 $$->tbl = current_table;
718 current_table = $<pstate>2->tbl;
719 current_saved_state = $<pstate>2->prev;
720 delete $<pstate>2;
722 | object_spec HEIGHT expr
724 $$ = $1;
725 $$->height = $3;
726 $$->flags |= HAS_HEIGHT;
728 | object_spec RADIUS expr
730 $$ = $1;
731 $$->radius = $3;
732 $$->flags |= HAS_RADIUS;
734 | object_spec WIDTH expr
736 $$ = $1;
737 $$->width = $3;
738 $$->flags |= HAS_WIDTH;
740 | object_spec DIAMETER expr
742 $$ = $1;
743 $$->radius = $3/2.0;
744 $$->flags |= HAS_RADIUS;
746 | object_spec expr %prec HEIGHT
748 $$ = $1;
749 $$->flags |= HAS_SEGMENT;
750 switch ($$->dir) {
751 case UP_DIRECTION:
752 $$->segment_pos.y += $2;
753 break;
754 case DOWN_DIRECTION:
755 $$->segment_pos.y -= $2;
756 break;
757 case RIGHT_DIRECTION:
758 $$->segment_pos.x += $2;
759 break;
760 case LEFT_DIRECTION:
761 $$->segment_pos.x -= $2;
762 break;
765 | object_spec UP
767 $$ = $1;
768 $$->dir = UP_DIRECTION;
769 $$->flags |= HAS_SEGMENT;
770 $$->segment_pos.y += $$->segment_height;
772 | object_spec UP expr
774 $$ = $1;
775 $$->dir = UP_DIRECTION;
776 $$->flags |= HAS_SEGMENT;
777 $$->segment_pos.y += $3;
779 | object_spec DOWN
781 $$ = $1;
782 $$->dir = DOWN_DIRECTION;
783 $$->flags |= HAS_SEGMENT;
784 $$->segment_pos.y -= $$->segment_height;
786 | object_spec DOWN expr
788 $$ = $1;
789 $$->dir = DOWN_DIRECTION;
790 $$->flags |= HAS_SEGMENT;
791 $$->segment_pos.y -= $3;
793 | object_spec RIGHT
795 $$ = $1;
796 $$->dir = RIGHT_DIRECTION;
797 $$->flags |= HAS_SEGMENT;
798 $$->segment_pos.x += $$->segment_width;
800 | object_spec RIGHT expr
802 $$ = $1;
803 $$->dir = RIGHT_DIRECTION;
804 $$->flags |= HAS_SEGMENT;
805 $$->segment_pos.x += $3;
807 | object_spec LEFT
809 $$ = $1;
810 $$->dir = LEFT_DIRECTION;
811 $$->flags |= HAS_SEGMENT;
812 $$->segment_pos.x -= $$->segment_width;
814 | object_spec LEFT expr
816 $$ = $1;
817 $$->dir = LEFT_DIRECTION;
818 $$->flags |= HAS_SEGMENT;
819 $$->segment_pos.x -= $3;
821 | object_spec FROM position
823 $$ = $1;
824 $$->flags |= HAS_FROM;
825 $$->from.x = $3.x;
826 $$->from.y = $3.y;
828 | object_spec TO position
830 $$ = $1;
831 if ($$->flags & HAS_SEGMENT)
832 $$->segment_list = new segment($$->segment_pos,
833 $$->segment_is_absolute,
834 $$->segment_list);
835 $$->flags |= HAS_SEGMENT;
836 $$->segment_pos.x = $3.x;
837 $$->segment_pos.y = $3.y;
838 $$->segment_is_absolute = 1;
839 $$->flags |= HAS_TO;
840 $$->to.x = $3.x;
841 $$->to.y = $3.y;
843 | object_spec AT position
845 $$ = $1;
846 $$->flags |= HAS_AT;
847 $$->at.x = $3.x;
848 $$->at.y = $3.y;
849 if ($$->type != ARC_OBJECT) {
850 $$->flags |= HAS_FROM;
851 $$->from.x = $3.x;
852 $$->from.y = $3.y;
855 | object_spec WITH path
857 $$ = $1;
858 $$->flags |= HAS_WITH;
859 $$->with = $3;
861 | object_spec WITH position %prec ','
863 $$ = $1;
864 $$->flags |= HAS_WITH;
865 position pos;
866 pos.x = $3.x;
867 pos.y = $3.y;
868 $$->with = new path(pos);
870 | object_spec BY expr_pair
872 $$ = $1;
873 $$->flags |= HAS_SEGMENT;
874 $$->segment_pos.x += $3.x;
875 $$->segment_pos.y += $3.y;
877 | object_spec THEN
879 $$ = $1;
880 if ($$->flags & HAS_SEGMENT) {
881 $$->segment_list = new segment($$->segment_pos,
882 $$->segment_is_absolute,
883 $$->segment_list);
884 $$->flags &= ~HAS_SEGMENT;
885 $$->segment_pos.x = $$->segment_pos.y = 0.0;
886 $$->segment_is_absolute = 0;
889 | object_spec SOLID
891 $$ = $1; // nothing
893 | object_spec DOTTED
895 $$ = $1;
896 $$->flags |= IS_DOTTED;
897 lookup_variable("dashwid", & $$->dash_width);
899 | object_spec DOTTED expr
901 $$ = $1;
902 $$->flags |= IS_DOTTED;
903 $$->dash_width = $3;
905 | object_spec DASHED
907 $$ = $1;
908 $$->flags |= IS_DASHED;
909 lookup_variable("dashwid", & $$->dash_width);
911 | object_spec DASHED expr
913 $$ = $1;
914 $$->flags |= IS_DASHED;
915 $$->dash_width = $3;
917 | object_spec FILL
919 $$ = $1;
920 $$->flags |= IS_DEFAULT_FILLED;
922 | object_spec FILL expr
924 $$ = $1;
925 $$->flags |= IS_FILLED;
926 $$->fill = $3;
928 | object_spec SHADED text
930 $$ = $1;
931 $$->flags |= (IS_SHADED | IS_FILLED);
932 $$->shaded = new char[strlen($3.str)+1];
933 strcpy($$->shaded, $3.str);
935 | object_spec COLORED text
937 $$ = $1;
938 $$->flags |= (IS_SHADED | IS_OUTLINED | IS_FILLED);
939 $$->shaded = new char[strlen($3.str)+1];
940 strcpy($$->shaded, $3.str);
941 $$->outlined = new char[strlen($3.str)+1];
942 strcpy($$->outlined, $3.str);
944 | object_spec OUTLINED text
946 $$ = $1;
947 $$->flags |= IS_OUTLINED;
948 $$->outlined = new char[strlen($3.str)+1];
949 strcpy($$->outlined, $3.str);
951 | object_spec CHOP
953 $$ = $1;
954 // line chop chop means line chop 0 chop 0
955 if ($$->flags & IS_DEFAULT_CHOPPED) {
956 $$->flags |= IS_CHOPPED;
957 $$->flags &= ~IS_DEFAULT_CHOPPED;
958 $$->start_chop = $$->end_chop = 0.0;
960 else if ($$->flags & IS_CHOPPED) {
961 $$->end_chop = 0.0;
963 else {
964 $$->flags |= IS_DEFAULT_CHOPPED;
967 | object_spec CHOP expr
969 $$ = $1;
970 if ($$->flags & IS_DEFAULT_CHOPPED) {
971 $$->flags |= IS_CHOPPED;
972 $$->flags &= ~IS_DEFAULT_CHOPPED;
973 $$->start_chop = 0.0;
974 $$->end_chop = $3;
976 else if ($$->flags & IS_CHOPPED) {
977 $$->end_chop = $3;
979 else {
980 $$->start_chop = $$->end_chop = $3;
981 $$->flags |= IS_CHOPPED;
984 | object_spec SAME
986 $$ = $1;
987 $$->flags |= IS_SAME;
989 | object_spec INVISIBLE
991 $$ = $1;
992 $$->flags |= IS_INVISIBLE;
994 | object_spec LEFT_ARROW_HEAD
996 $$ = $1;
997 $$->flags |= HAS_LEFT_ARROW_HEAD;
999 | object_spec RIGHT_ARROW_HEAD
1001 $$ = $1;
1002 $$->flags |= HAS_RIGHT_ARROW_HEAD;
1004 | object_spec DOUBLE_ARROW_HEAD
1006 $$ = $1;
1007 $$->flags |= (HAS_LEFT_ARROW_HEAD|HAS_RIGHT_ARROW_HEAD);
1009 | object_spec CW
1011 $$ = $1;
1012 $$->flags |= IS_CLOCKWISE;
1014 | object_spec CCW
1016 $$ = $1;
1017 $$->flags &= ~IS_CLOCKWISE;
1019 | object_spec text %prec TEXT
1021 $$ = $1;
1022 text_item **p;
1023 for (p = & $$->text; *p; p = &(*p)->next)
1025 *p = new text_item($2.str, $2.filename, $2.lineno);
1027 | object_spec LJUST
1029 $$ = $1;
1030 if ($$->text) {
1031 text_item *p;
1032 for (p = $$->text; p->next; p = p->next)
1034 p->adj.h = LEFT_ADJUST;
1037 | object_spec RJUST
1039 $$ = $1;
1040 if ($$->text) {
1041 text_item *p;
1042 for (p = $$->text; p->next; p = p->next)
1044 p->adj.h = RIGHT_ADJUST;
1047 | object_spec ABOVE
1049 $$ = $1;
1050 if ($$->text) {
1051 text_item *p;
1052 for (p = $$->text; p->next; p = p->next)
1054 p->adj.v = ABOVE_ADJUST;
1057 | object_spec BELOW
1059 $$ = $1;
1060 if ($$->text) {
1061 text_item *p;
1062 for (p = $$->text; p->next; p = p->next)
1064 p->adj.v = BELOW_ADJUST;
1067 | object_spec THICKNESS expr
1069 $$ = $1;
1070 $$->flags |= HAS_THICKNESS;
1071 $$->thickness = $3;
1073 | object_spec ALIGNED
1075 $$ = $1;
1076 $$->flags |= IS_ALIGNED;
1080 text:
1081 TEXT
1082 { $$ = $1; }
1083 | SPRINTF '(' TEXT sprintf_args ')'
1085 $$.filename = $3.filename;
1086 $$.lineno = $3.lineno;
1087 $$.str = do_sprintf($3.str, $4.v, $4.nv);
1088 a_delete $4.v;
1089 a_delete $3.str;
1093 sprintf_args:
1094 /* empty */
1096 $$.v = 0;
1097 $$.nv = 0;
1098 $$.maxv = 0;
1100 | sprintf_args ',' expr
1102 $$ = $1;
1103 if ($$.nv >= $$.maxv) {
1104 if ($$.nv == 0) {
1105 $$.v = new double[4];
1106 $$.maxv = 4;
1108 else {
1109 double *oldv = $$.v;
1110 $$.maxv *= 2;
1111 #if 0
1112 $$.v = new double[$$.maxv];
1113 memcpy($$.v, oldv, $$.nv*sizeof(double));
1114 #else
1115 // workaround for bug in Compaq C++ V6.5-033
1116 // for Compaq Tru64 UNIX V5.1A (Rev. 1885)
1117 double *foo = new double[$$.maxv];
1118 memcpy(foo, oldv, $$.nv*sizeof(double));
1119 $$.v = foo;
1120 #endif
1121 a_delete oldv;
1124 $$.v[$$.nv] = $3;
1125 $$.nv += 1;
1129 position:
1130 position_not_place
1131 { $$ = $1; }
1132 | place
1134 position pos = $1;
1135 $$.x = pos.x;
1136 $$.y = pos.y;
1140 position_not_place:
1141 expr_pair
1142 { $$ = $1; }
1143 | position '+' expr_pair
1145 $$.x = $1.x + $3.x;
1146 $$.y = $1.y + $3.y;
1148 | position '-' expr_pair
1150 $$.x = $1.x - $3.x;
1151 $$.y = $1.y - $3.y;
1153 | '(' position ',' position ')'
1155 $$.x = $2.x;
1156 $$.y = $4.y;
1158 | expr between position AND position
1160 $$.x = (1.0 - $1)*$3.x + $1*$5.x;
1161 $$.y = (1.0 - $1)*$3.y + $1*$5.y;
1163 | expr '<' position ',' position '>'
1165 $$.x = (1.0 - $1)*$3.x + $1*$5.x;
1166 $$.y = (1.0 - $1)*$3.y + $1*$5.y;
1170 between:
1171 BETWEEN
1172 | OF THE WAY BETWEEN
1175 expr_pair:
1176 expr ',' expr
1178 $$.x = $1;
1179 $$.y = $3;
1181 | '(' expr_pair ')'
1182 { $$ = $2; }
1185 place:
1186 /* line at A left == line (at A) left */
1187 label %prec CHOP
1188 { $$ = $1; }
1189 | label corner
1191 path pth($2);
1192 if (!pth.follow($1, & $$))
1193 YYABORT;
1195 | corner label
1197 path pth($1);
1198 if (!pth.follow($2, & $$))
1199 YYABORT;
1201 | corner OF label
1203 path pth($1);
1204 if (!pth.follow($3, & $$))
1205 YYABORT;
1207 | HERE
1209 $$.x = current_position.x;
1210 $$.y = current_position.y;
1211 $$.obj = 0;
1215 label:
1216 LABEL
1218 place *p = lookup_label($1);
1219 if (!p) {
1220 lex_error("there is no place `%1'", $1);
1221 YYABORT;
1223 $$ = *p;
1224 a_delete $1;
1226 | nth_primitive
1227 { $$.obj = $1; }
1228 | label '.' LABEL
1230 path pth($3);
1231 if (!pth.follow($1, & $$))
1232 YYABORT;
1236 ordinal:
1237 ORDINAL
1238 { $$ = $1; }
1239 | '`' any_expr TH
1241 // XXX Check for overflow (and non-integers?).
1242 $$ = (int)$2;
1246 optional_ordinal_last:
1247 LAST
1248 { $$ = 1; }
1249 | ordinal LAST
1250 { $$ = $1; }
1253 nth_primitive:
1254 ordinal object_type
1256 int count = 0;
1257 object *p;
1258 for (p = olist.head; p != 0; p = p->next)
1259 if (p->type() == $2 && ++count == $1) {
1260 $$ = p;
1261 break;
1263 if (p == 0) {
1264 lex_error("there is no %1%2 %3", $1, ordinal_postfix($1),
1265 object_type_name($2));
1266 YYABORT;
1269 | optional_ordinal_last object_type
1271 int count = 0;
1272 object *p;
1273 for (p = olist.tail; p != 0; p = p->prev)
1274 if (p->type() == $2 && ++count == $1) {
1275 $$ = p;
1276 break;
1278 if (p == 0) {
1279 lex_error("there is no %1%2 last %3", $1,
1280 ordinal_postfix($1), object_type_name($2));
1281 YYABORT;
1286 object_type:
1288 { $$ = BOX_OBJECT; }
1289 | CIRCLE
1290 { $$ = CIRCLE_OBJECT; }
1291 | ELLIPSE
1292 { $$ = ELLIPSE_OBJECT; }
1293 | ARC
1294 { $$ = ARC_OBJECT; }
1295 | LINE
1296 { $$ = LINE_OBJECT; }
1297 | ARROW
1298 { $$ = ARROW_OBJECT; }
1299 | SPLINE
1300 { $$ = SPLINE_OBJECT; }
1301 | '[' ']'
1302 { $$ = BLOCK_OBJECT; }
1303 | TEXT
1304 { $$ = TEXT_OBJECT; }
1307 label_path:
1308 '.' LABEL
1309 { $$ = new path($2); }
1310 | label_path '.' LABEL
1312 $$ = $1;
1313 $$->append($3);
1317 relative_path:
1318 corner %prec CHOP
1319 { $$ = new path($1); }
1320 /* give this a lower precedence than LEFT and RIGHT so that
1321 [A: box] with .A left == [A: box] with (.A left) */
1322 | label_path %prec TEXT
1323 { $$ = $1; }
1324 | label_path corner
1326 $$ = $1;
1327 $$->append($2);
1331 path:
1332 relative_path
1333 { $$ = $1; }
1334 | '(' relative_path ',' relative_path ')'
1336 $$ = $2;
1337 $$->set_ypath($4);
1339 /* The rest of these rules are a compatibility sop. */
1340 | ORDINAL LAST object_type relative_path
1342 lex_warning("`%1%2 last %3' in `with' argument ignored",
1343 $1, ordinal_postfix($1), object_type_name($3));
1344 $$ = $4;
1346 | LAST object_type relative_path
1348 lex_warning("`last %1' in `with' argument ignored",
1349 object_type_name($2));
1350 $$ = $3;
1352 | ORDINAL object_type relative_path
1354 lex_warning("`%1%2 %3' in `with' argument ignored",
1355 $1, ordinal_postfix($1), object_type_name($2));
1356 $$ = $3;
1358 | LABEL relative_path
1360 lex_warning("initial `%1' in `with' argument ignored", $1);
1361 a_delete $1;
1362 $$ = $2;
1366 corner:
1367 DOT_N
1368 { $$ = &object::north; }
1369 | DOT_E
1370 { $$ = &object::east; }
1371 | DOT_W
1372 { $$ = &object::west; }
1373 | DOT_S
1374 { $$ = &object::south; }
1375 | DOT_NE
1376 { $$ = &object::north_east; }
1377 | DOT_SE
1378 { $$ = &object:: south_east; }
1379 | DOT_NW
1380 { $$ = &object::north_west; }
1381 | DOT_SW
1382 { $$ = &object::south_west; }
1383 | DOT_C
1384 { $$ = &object::center; }
1385 | DOT_START
1386 { $$ = &object::start; }
1387 | DOT_END
1388 { $$ = &object::end; }
1389 | TOP
1390 { $$ = &object::north; }
1391 | BOTTOM
1392 { $$ = &object::south; }
1393 | LEFT
1394 { $$ = &object::west; }
1395 | RIGHT
1396 { $$ = &object::east; }
1397 | UPPER LEFT
1398 { $$ = &object::north_west; }
1399 | LOWER LEFT
1400 { $$ = &object::south_west; }
1401 | UPPER RIGHT
1402 { $$ = &object::north_east; }
1403 | LOWER RIGHT
1404 { $$ = &object::south_east; }
1405 | LEFT_CORNER
1406 { $$ = &object::west; }
1407 | RIGHT_CORNER
1408 { $$ = &object::east; }
1409 | UPPER LEFT_CORNER
1410 { $$ = &object::north_west; }
1411 | LOWER LEFT_CORNER
1412 { $$ = &object::south_west; }
1413 | UPPER RIGHT_CORNER
1414 { $$ = &object::north_east; }
1415 | LOWER RIGHT_CORNER
1416 { $$ = &object::south_east; }
1417 | NORTH
1418 { $$ = &object::north; }
1419 | SOUTH
1420 { $$ = &object::south; }
1421 | EAST
1422 { $$ = &object::east; }
1423 | WEST
1424 { $$ = &object::west; }
1425 | CENTER
1426 { $$ = &object::center; }
1427 | START
1428 { $$ = &object::start; }
1429 | END
1430 { $$ = &object::end; }
1433 expr:
1434 VARIABLE
1436 if (!lookup_variable($1, & $$)) {
1437 lex_error("there is no variable `%1'", $1);
1438 YYABORT;
1440 a_delete $1;
1442 | NUMBER
1443 { $$ = $1; }
1444 | place DOT_X
1446 if ($1.obj != 0)
1447 $$ = $1.obj->origin().x;
1448 else
1449 $$ = $1.x;
1451 | place DOT_Y
1453 if ($1.obj != 0)
1454 $$ = $1.obj->origin().y;
1455 else
1456 $$ = $1.y;
1458 | place DOT_HT
1460 if ($1.obj != 0)
1461 $$ = $1.obj->height();
1462 else
1463 $$ = 0.0;
1465 | place DOT_WID
1467 if ($1.obj != 0)
1468 $$ = $1.obj->width();
1469 else
1470 $$ = 0.0;
1472 | place DOT_RAD
1474 if ($1.obj != 0)
1475 $$ = $1.obj->radius();
1476 else
1477 $$ = 0.0;
1479 | expr '+' expr
1480 { $$ = $1 + $3; }
1481 | expr '-' expr
1482 { $$ = $1 - $3; }
1483 | expr '*' expr
1484 { $$ = $1 * $3; }
1485 | expr '/' expr
1487 if ($3 == 0.0) {
1488 lex_error("division by zero");
1489 YYABORT;
1491 $$ = $1/$3;
1493 | expr '%' expr
1495 if ($3 == 0.0) {
1496 lex_error("modulus by zero");
1497 YYABORT;
1499 $$ = fmod($1, $3);
1501 | expr '^' expr
1503 errno = 0;
1504 $$ = pow($1, $3);
1505 if (errno == EDOM) {
1506 lex_error("arguments to `^' operator out of domain");
1507 YYABORT;
1509 if (errno == ERANGE) {
1510 lex_error("result of `^' operator out of range");
1511 YYABORT;
1514 | '-' expr %prec '!'
1515 { $$ = -$2; }
1516 | '(' any_expr ')'
1517 { $$ = $2; }
1518 | SIN '(' any_expr ')'
1520 errno = 0;
1521 $$ = sin($3);
1522 if (errno == ERANGE) {
1523 lex_error("sin result out of range");
1524 YYABORT;
1527 | COS '(' any_expr ')'
1529 errno = 0;
1530 $$ = cos($3);
1531 if (errno == ERANGE) {
1532 lex_error("cos result out of range");
1533 YYABORT;
1536 | ATAN2 '(' any_expr ',' any_expr ')'
1538 errno = 0;
1539 $$ = atan2($3, $5);
1540 if (errno == EDOM) {
1541 lex_error("atan2 argument out of domain");
1542 YYABORT;
1544 if (errno == ERANGE) {
1545 lex_error("atan2 result out of range");
1546 YYABORT;
1549 | LOG '(' any_expr ')'
1551 errno = 0;
1552 $$ = log10($3);
1553 if (errno == ERANGE) {
1554 lex_error("log result out of range");
1555 YYABORT;
1558 | EXP '(' any_expr ')'
1560 errno = 0;
1561 $$ = pow(10.0, $3);
1562 if (errno == ERANGE) {
1563 lex_error("exp result out of range");
1564 YYABORT;
1567 | SQRT '(' any_expr ')'
1569 errno = 0;
1570 $$ = sqrt($3);
1571 if (errno == EDOM) {
1572 lex_error("sqrt argument out of domain");
1573 YYABORT;
1576 | K_MAX '(' any_expr ',' any_expr ')'
1577 { $$ = $3 > $5 ? $3 : $5; }
1578 | K_MIN '(' any_expr ',' any_expr ')'
1579 { $$ = $3 < $5 ? $3 : $5; }
1580 | INT '(' any_expr ')'
1581 { $$ = floor($3); }
1582 | RAND '(' any_expr ')'
1583 { $$ = 1.0 + floor(((rand()&0x7fff)/double(0x7fff))*$3); }
1584 | RAND '(' ')'
1586 /* return a random number in the range [0,1) */
1587 /* portable, but not very random */
1588 $$ = (rand() & 0x7fff) / double(0x8000);
1590 | SRAND '(' any_expr ')'
1592 $$ = 0;
1593 srand((unsigned int)$3);
1595 | expr '<' expr
1596 { $$ = ($1 < $3); }
1597 | expr LESSEQUAL expr
1598 { $$ = ($1 <= $3); }
1599 | expr '>' expr
1600 { $$ = ($1 > $3); }
1601 | expr GREATEREQUAL expr
1602 { $$ = ($1 >= $3); }
1603 | expr EQUALEQUAL expr
1604 { $$ = ($1 == $3); }
1605 | expr NOTEQUAL expr
1606 { $$ = ($1 != $3); }
1607 | expr ANDAND expr
1608 { $$ = ($1 != 0.0 && $3 != 0.0); }
1609 | expr OROR expr
1610 { $$ = ($1 != 0.0 || $3 != 0.0); }
1611 | '!' expr
1612 { $$ = ($2 == 0.0); }
1618 /* bison defines const to be empty unless __STDC__ is defined, which it
1619 isn't under cfront */
1621 #ifdef const
1622 #undef const
1623 #endif
1625 static struct {
1626 const char *name;
1627 double val;
1628 int scaled; // non-zero if val should be multiplied by scale
1629 } defaults_table[] = {
1630 { "arcrad", .25, 1 },
1631 { "arrowht", .1, 1 },
1632 { "arrowwid", .05, 1 },
1633 { "circlerad", .25, 1 },
1634 { "boxht", .5, 1 },
1635 { "boxwid", .75, 1 },
1636 { "boxrad", 0.0, 1 },
1637 { "dashwid", .05, 1 },
1638 { "ellipseht", .5, 1 },
1639 { "ellipsewid", .75, 1 },
1640 { "moveht", .5, 1 },
1641 { "movewid", .5, 1 },
1642 { "lineht", .5, 1 },
1643 { "linewid", .5, 1 },
1644 { "textht", 0.0, 1 },
1645 { "textwid", 0.0, 1 },
1646 { "scale", 1.0, 0 },
1647 { "linethick", -1.0, 0 }, // in points
1648 { "fillval", .5, 0 },
1649 { "arrowhead", 1.0, 0 },
1650 { "maxpswid", 8.5, 0 },
1651 { "maxpsht", 11.0, 0 },
1654 place *lookup_label(const char *label)
1656 saved_state *state = current_saved_state;
1657 PTABLE(place) *tbl = current_table;
1658 for (;;) {
1659 place *pl = tbl->lookup(label);
1660 if (pl)
1661 return pl;
1662 if (!state)
1663 return 0;
1664 tbl = state->tbl;
1665 state = state->prev;
1669 void define_label(const char *label, const place *pl)
1671 place *p = new place[1];
1672 *p = *pl;
1673 current_table->define(label, p);
1676 int lookup_variable(const char *name, double *val)
1678 place *pl = lookup_label(name);
1679 if (pl) {
1680 *val = pl->x;
1681 return 1;
1683 return 0;
1686 void define_variable(const char *name, double val)
1688 place *p = new place[1];
1689 p->obj = 0;
1690 p->x = val;
1691 p->y = 0.0;
1692 current_table->define(name, p);
1693 if (strcmp(name, "scale") == 0) {
1694 // When the scale changes, reset all scaled pre-defined variables to
1695 // their default values.
1696 for (unsigned int i = 0;
1697 i < sizeof(defaults_table)/sizeof(defaults_table[0]); i++)
1698 if (defaults_table[i].scaled)
1699 define_variable(defaults_table[i].name, val*defaults_table[i].val);
1703 // called once only (not once per parse)
1705 void parse_init()
1707 current_direction = RIGHT_DIRECTION;
1708 current_position.x = 0.0;
1709 current_position.y = 0.0;
1710 // This resets everything to its default value.
1711 reset_all();
1714 void reset(const char *nm)
1716 for (unsigned int i = 0;
1717 i < sizeof(defaults_table)/sizeof(defaults_table[0]); i++)
1718 if (strcmp(nm, defaults_table[i].name) == 0) {
1719 double val = defaults_table[i].val;
1720 if (defaults_table[i].scaled) {
1721 double scale;
1722 lookup_variable("scale", &scale);
1723 val *= scale;
1725 define_variable(defaults_table[i].name, val);
1726 return;
1728 lex_error("`%1' is not a predefined variable", nm);
1731 void reset_all()
1733 // We only have to explicitly reset the pre-defined variables that
1734 // aren't scaled because `scale' is not scaled, and changing the
1735 // value of `scale' will reset all the pre-defined variables that
1736 // are scaled.
1737 for (unsigned int i = 0;
1738 i < sizeof(defaults_table)/sizeof(defaults_table[0]); i++)
1739 if (!defaults_table[i].scaled)
1740 define_variable(defaults_table[i].name, defaults_table[i].val);
1743 // called after each parse
1745 void parse_cleanup()
1747 while (current_saved_state != 0) {
1748 delete current_table;
1749 current_table = current_saved_state->tbl;
1750 saved_state *tem = current_saved_state;
1751 current_saved_state = current_saved_state->prev;
1752 delete tem;
1754 assert(current_table == &top_table);
1755 PTABLE_ITERATOR(place) iter(current_table);
1756 const char *key;
1757 place *pl;
1758 while (iter.next(&key, &pl))
1759 if (pl->obj != 0) {
1760 position pos = pl->obj->origin();
1761 pl->obj = 0;
1762 pl->x = pos.x;
1763 pl->y = pos.y;
1765 while (olist.head != 0) {
1766 object *tem = olist.head;
1767 olist.head = olist.head->next;
1768 delete tem;
1770 olist.tail = 0;
1771 current_direction = RIGHT_DIRECTION;
1772 current_position.x = 0.0;
1773 current_position.y = 0.0;
1776 const char *ordinal_postfix(int n)
1778 if (n < 10 || n > 20)
1779 switch (n % 10) {
1780 case 1:
1781 return "st";
1782 case 2:
1783 return "nd";
1784 case 3:
1785 return "rd";
1787 return "th";
1790 const char *object_type_name(object_type type)
1792 switch (type) {
1793 case BOX_OBJECT:
1794 return "box";
1795 case CIRCLE_OBJECT:
1796 return "circle";
1797 case ELLIPSE_OBJECT:
1798 return "ellipse";
1799 case ARC_OBJECT:
1800 return "arc";
1801 case SPLINE_OBJECT:
1802 return "spline";
1803 case LINE_OBJECT:
1804 return "line";
1805 case ARROW_OBJECT:
1806 return "arrow";
1807 case MOVE_OBJECT:
1808 return "move";
1809 case TEXT_OBJECT:
1810 return "\"\"";
1811 case BLOCK_OBJECT:
1812 return "[]";
1813 case OTHER_OBJECT:
1814 case MARK_OBJECT:
1815 default:
1816 break;
1818 return "object";
1821 static char sprintf_buf[1024];
1823 char *format_number(const char *form, double n)
1825 if (form == 0)
1826 form = "%g";
1827 return do_sprintf(form, &n, 1);
1830 char *do_sprintf(const char *form, const double *v, int nv)
1832 string result;
1833 int i = 0;
1834 string one_format;
1835 while (*form) {
1836 if (*form == '%') {
1837 one_format += *form++;
1838 for (; *form != '\0' && strchr("#-+ 0123456789.", *form) != 0; form++)
1839 one_format += *form;
1840 if (*form == '\0' || strchr("eEfgG%", *form) == 0) {
1841 lex_error("bad sprintf format");
1842 result += one_format;
1843 result += form;
1844 break;
1846 if (*form == '%') {
1847 one_format += *form++;
1848 one_format += '\0';
1849 snprintf(sprintf_buf, sizeof(sprintf_buf),
1850 "%s", one_format.contents());
1852 else {
1853 if (i >= nv) {
1854 lex_error("too few arguments to snprintf");
1855 result += one_format;
1856 result += form;
1857 break;
1859 one_format += *form++;
1860 one_format += '\0';
1861 snprintf(sprintf_buf, sizeof(sprintf_buf),
1862 one_format.contents(), v[i++]);
1864 one_format.clear();
1865 result += sprintf_buf;
1867 else
1868 result += *form++;
1870 result += '\0';
1871 return strsave(result.contents());