2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2005, 2007,
4 Free Software Foundation, Inc.
5 Written by James Clark (jjc@jclark.com)
7 This file is part of groff.
9 groff is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License along
20 with groff; see the file COPYING. If not, write to the Free Software
21 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include "stringclass.h"
27 #include "file_case.h"
30 // declarations to avoid friend name injection problems
33 int get_location(char **, int *);
46 definition::definition() : is_macro(1), is_simple(0)
51 definition::~definition()
57 declare_ptable(definition
)
58 implement_ptable(definition
)
60 PTABLE(definition
) macro_table
;
67 { "smallover", SMALLOVER
},
88 { "uaccent", UACCENT
},
100 { "lineup", LINEUP
},
104 { "define", DEFINE
},
105 { "sdefine", SDEFINE
},
106 { "ndefine", NDEFINE
},
107 { "tdefine", TDEFINE
},
110 { "include", INCLUDE
},
113 { "chartype", CHARTYPE
},
115 { "vcenter", VCENTER
},
117 { "opprime", PRIME
},
118 { "grfont", GRFONT
},
119 { "gbfont", GBFONT
},
121 { "nosplit", NOSPLIT
},
122 { "special", SPECIAL
},
130 static struct builtin_def common_defs
[] = {
131 { "ALPHA", "\\(*A" },
134 { "DELTA", "\\(*D" },
135 { "EPSILON", "\\(*E" },
137 { "GAMMA", "\\(*G" },
139 { "KAPPA", "\\(*K" },
140 { "LAMBDA", "\\(*L" },
143 { "OMEGA", "\\(*W" },
144 { "OMICRON", "\\(*O" },
149 { "SIGMA", "\\(*S" },
151 { "THETA", "\\(*H" },
152 { "UPSILON", "\\(*U" },
155 { "Alpha", "\\(*A" },
158 { "Delta", "\\(*D" },
159 { "Epsilon", "\\(*E" },
161 { "Gamma", "\\(*G" },
163 { "Kappa", "\\(*K" },
164 { "Lambda", "\\(*L" },
167 { "Omega", "\\(*W" },
168 { "Omicron", "\\(*O" },
173 { "Sigma", "\\(*S" },
175 { "Theta", "\\(*H" },
176 { "Upsilon", "\\(*U" },
179 { "alpha", "\\(*a" },
182 { "delta", "\\(*d" },
183 { "epsilon", "\\(*e" },
185 { "gamma", "\\(*g" },
187 { "kappa", "\\(*k" },
188 { "lambda", "\\(*l" },
191 { "omega", "\\(*w" },
192 { "omicron", "\\(*o" },
197 { "sigma", "\\(*s" },
199 { "theta", "\\(*h" },
200 { "upsilon", "\\(*u" },
203 { "max", "{type \"operator\" roman \"max\"}" },
204 { "min", "{type \"operator\" roman \"min\"}" },
205 { "lim", "{type \"operator\" roman \"lim\"}" },
206 { "sin", "{type \"operator\" roman \"sin\"}" },
207 { "cos", "{type \"operator\" roman \"cos\"}" },
208 { "tan", "{type \"operator\" roman \"tan\"}" },
209 { "sinh", "{type \"operator\" roman \"sinh\"}" },
210 { "cosh", "{type \"operator\" roman \"cosh\"}" },
211 { "tanh", "{type \"operator\" roman \"tanh\"}" },
212 { "arc", "{type \"operator\" roman \"arc\"}" },
213 { "log", "{type \"operator\" roman \"log\"}" },
214 { "ln", "{type \"operator\" roman \"ln\"}" },
215 { "exp", "{type \"operator\" roman \"exp\"}" },
216 { "Re", "{type \"operator\" roman \"Re\"}" },
217 { "Im", "{type \"operator\" roman \"Im\"}" },
218 { "det", "{type \"operator\" roman \"det\"}" },
219 { "and", "{roman \"and\"}" },
220 { "if", "{roman \"if\"}" },
221 { "for", "{roman \"for\"}" },
222 { "times", "type \"binary\" \\(mu" },
223 { "ldots", "type \"inner\" { . . . }" },
225 { "partial", "\\(pd" },
226 { "nothing", "\"\"" },
227 { "half", "{1 smallover 2}" },
228 { "hat_def", "roman \"^\"" },
229 { "hat", "accent { hat_def }" },
230 { "tilde_def", "\"~\"" },
231 { "tilde", "accent { tilde_def }" },
232 { "==", "type \"relation\" \\(==" },
233 { "!=", "type \"relation\" \\(!=" },
234 { "+-", "type \"binary\" \\(+-" },
235 { "->", "type \"relation\" \\(->" },
236 { "<-", "type \"relation\" \\(<-" },
237 { "<<", "type \"relation\" \\(<<" },
238 { ">>", "type \"relation\" \\(>>" },
240 { "approx", "type \"relation\" \"\\(~=\"" },
243 { "cdot", "type \"binary\" \\(md" },
244 { "cdots", "type \"inner\" { \\(md \\(md \\(md }" },
248 /* composite definitions that require troff size and motion operators */
249 static struct builtin_def troff_defs
[] = {
250 { "sum", "{type \"operator\" vcenter size +5 \\(*S}" },
251 { "prod", "{type \"operator\" vcenter size +5 \\(*P}" },
252 { "int", "{type \"operator\" vcenter size +8 \\(is}" },
253 { "union", "{type \"operator\" vcenter size +5 \\(cu}" },
254 { "inter", "{type \"operator\" vcenter size +5 \\(ca}" },
255 { "dot_def", "up 52 back 15 \".\"" },
256 { "dot", "accent { dot_def }" },
257 { "dotdot_def", "up 52 back 25 \"..\"" },
258 { "dotdot", "accent { dotdot_def }" },
259 { "utilde_def", "down 75 \"~\"" },
260 { "utilde", "uaccent { utilde_def }" },
261 { "vec_def", "up 52 size -5 \\(->" },
262 { "vec", "accent { vec_def }" },
263 { "dyad_def", "up 52 size -5 { \\(<> }" },
264 { "dyad", "accent { dyad_def }" },
265 { "...", "type \"inner\" vcenter { . . . }" },
268 /* equivalent definitions for MathML mode */
269 static struct builtin_def mathml_defs
[] = {
270 { "sum", "{type \"operator\" size big \\(*S}" },
271 { "prod", "{type \"operator\" size big \\(*P}" },
272 { "int", "{type \"operator\" size big \\(is}" },
273 { "union", "{type \"operator\" size big \\(cu}" },
274 { "inter", "{type \"operator\" size big \\(ca}" },
275 { "dot", "accent { \".\" }" },
276 { "dotdot", "accent { \"..\" }" },
277 { "utilde", "uaccent { \"~\" }" },
278 { "vec", "accent { \\(-> }" },
279 { "dyad", "accent { \\(<> }" },
280 { "...", "type \"inner\" { . . . }" },
283 void init_table(const char *device
)
286 for (i
= 0; i
< sizeof(token_table
)/sizeof(token_table
[0]); i
++) {
287 definition
*def
= new definition
[1];
289 def
->tok
= token_table
[i
].token
;
290 macro_table
.define(token_table
[i
].name
, def
);
292 for (i
= 0; i
< sizeof(common_defs
)/sizeof(common_defs
[0]); i
++) {
293 definition
*def
= new definition
[1];
295 def
->contents
= strsave(common_defs
[i
].def
);
297 macro_table
.define(common_defs
[i
].name
, def
);
299 if (output_format
== troff
) {
300 for (i
= 0; i
< sizeof(troff_defs
)/sizeof(troff_defs
[0]); i
++) {
301 definition
*def
= new definition
[1];
303 def
->contents
= strsave(troff_defs
[i
].def
);
305 macro_table
.define(troff_defs
[i
].name
, def
);
308 else if (output_format
== mathml
) {
309 for (i
= 0; i
< sizeof(mathml_defs
)/sizeof(mathml_defs
[0]); i
++) {
310 definition
*def
= new definition
[1];
312 def
->contents
= strsave(mathml_defs
[i
].def
);
314 macro_table
.define(mathml_defs
[i
].name
, def
);
317 definition
*def
= new definition
[1];
319 def
->contents
= strsave("1");
320 macro_table
.define(device
, def
);
328 virtual int get() = 0;
329 virtual int peek() = 0;
330 virtual int get_location(char **, int *);
332 friend int get_char();
333 friend int peek_char();
334 friend int get_location(char **, int *);
335 friend void init_lex(const char *str
, const char *filename
, int lineno
);
338 class file_input
: public input
{
346 file_input(file_case
*, const char *, input
*);
350 int get_location(char **, int *);
354 class macro_input
: public input
{
358 macro_input(const char *, input
*);
364 class top_input
: public macro_input
{
368 top_input(const char *, const char *, int, input
*);
371 int get_location(char **, int *);
374 class argument_macro_input
: public input
{
381 argument_macro_input(const char *, int, char **, input
*);
382 ~argument_macro_input();
387 input::input(input
*x
) : next(x
)
395 int input::get_location(char **, int *)
400 file_input::file_input(file_case
*fcp
, const char *fn
, input
*p
)
401 : input(p
), _fcp(fcp
), lineno(0), ptr("")
403 filename
= strsave(fn
);
406 file_input::~file_input()
412 int file_input::read_line()
418 int c
= _fcp
->get_c();
421 else if (invalid_input_char(c
))
422 lex_error("invalid input character code %1", c
);
429 if (line
.length() == 0)
431 if (!(line
.length() >= 3 && line
[0] == '.' && line
[1] == 'E'
432 && (line
[2] == 'Q' || line
[2] == 'N')
433 && (line
.length() == 3 || line
[3] == ' ' || line
[3] == '\n'
434 || compatible_flag
))) {
436 ptr
= line
.contents();
442 int file_input::get()
444 if (*ptr
!= '\0' || read_line())
445 return *ptr
++ & 0377;
450 int file_input::peek()
452 if (*ptr
!= '\0' || read_line())
458 int file_input::get_location(char **fnp
, int *lnp
)
465 macro_input::macro_input(const char *str
, input
*x
) : input(x
)
467 p
= s
= strsave(str
);
470 macro_input::~macro_input()
475 int macro_input::get()
477 if (p
== 0 || *p
== '\0')
483 int macro_input::peek()
485 if (p
== 0 || *p
== '\0')
491 top_input::top_input(const char *str
, const char *fn
, int ln
, input
*x
)
492 : macro_input(str
, x
), lineno(ln
)
494 filename
= strsave(fn
);
497 top_input::~top_input()
504 int c
= macro_input::get();
510 int top_input::get_location(char **fnp
, int *lnp
)
517 // Character representing $1. Must be invalid input character.
520 argument_macro_input::argument_macro_input(const char *body
, int ac
,
522 : input(x
), ap(0), argc(ac
)
525 for (i
= 0; i
< argc
; i
++)
527 p
= s
= strsave(body
);
529 for (i
= 0; s
[i
] != '\0'; i
++)
530 if (s
[i
] == '$' && s
[i
+1] >= '0' && s
[i
+1] <= '9') {
532 s
[j
++] = ARG1
+ s
[++i
] - '1';
540 argument_macro_input::~argument_macro_input()
542 for (int i
= 0; i
< argc
; i
++)
547 int argument_macro_input::get()
556 while (*p
>= ARG1
&& *p
<= ARG1
+ 8) {
558 if (i
< argc
&& argv
[i
] != 0 && argv
[i
][0] != '\0') {
568 int argument_macro_input::peek()
577 while (*p
>= ARG1
&& *p
<= ARG1
+ 8) {
579 if (i
< argc
&& argv
[i
] != 0 && argv
[i
][0] != '\0') {
589 static input
*current_input
= 0;
591 /* we insert a newline between input from different levels */
595 if (current_input
== 0)
598 int c
= current_input
->get();
602 input
*tem
= current_input
;
603 current_input
= current_input
->next
;
612 if (current_input
== 0)
615 int c
= current_input
->peek();
623 int get_location(char **fnp
, int *lnp
)
625 for (input
*p
= current_input
; p
; p
= p
->next
)
626 if (p
->get_location(fnp
, lnp
))
632 const int NCONTEXT
= 4;
633 string context_ring
[NCONTEXT
];
634 int context_index
= 0;
638 for (int i
= 0; i
< NCONTEXT
; i
++)
639 context_ring
[i
] = "";
645 int i
= context_index
;
646 fputs(" context is\n\t", stderr
);
648 int j
= (i
+ 1) % NCONTEXT
;
649 if (j
== context_index
) {
650 fputs(">>> ", stderr
);
651 put_string(context_ring
[i
], stderr
);
652 fputs(" <<<", stderr
);
655 else if (context_ring
[i
].length() > 0) {
656 put_string(context_ring
[i
], stderr
);
664 void add_context(const string
&s
)
666 context_ring
[context_index
] = s
;
667 context_index
= (context_index
+ 1) % NCONTEXT
;
670 void add_context(char c
)
672 context_ring
[context_index
] = c
;
673 context_index
= (context_index
+ 1) % NCONTEXT
;
676 void add_quoted_context(const string
&s
)
678 string
&r
= context_ring
[context_index
];
680 for (int i
= 0; i
< s
.length(); i
++)
686 context_index
= (context_index
+ 1) % NCONTEXT
;
689 void init_lex(const char *str
, const char *filename
, int lineno
)
691 while (current_input
!= 0) {
692 input
*tem
= current_input
;
693 current_input
= current_input
->next
;
696 current_input
= new top_input(str
, filename
, lineno
, 0);
701 void get_delimited_text()
705 int got_location
= get_location(&filename
, &lineno
);
706 int start
= get_char();
707 while (start
== ' ' || start
== '\t' || start
== '\n')
709 token_buffer
.clear();
712 error_with_file_and_line(filename
, lineno
,
713 "end of input while defining macro");
715 error("end of input while defining macro");
722 error_with_file_and_line(filename
, lineno
,
723 "end of input while defining macro");
725 error("end of input while defining macro");
726 add_context(start
+ token_buffer
);
731 token_buffer
+= char(c
);
733 add_context(start
+ token_buffer
+ start
);
736 void interpolate_macro_with_args(const char *body
)
741 for (i
= 0; i
< 9; i
++)
746 token_buffer
.clear();
750 lex_error("end of input while scanning macro arguments");
753 if (level
== 0 && (c
== ',' || c
== ')')) {
754 if (token_buffer
.length() > 0) {
755 token_buffer
+= '\0';
756 argv
[argc
] = strsave(token_buffer
.contents());
758 // for `foo()', argc = 0
759 if (argc
> 0 || c
!= ')' || i
> 0)
763 token_buffer
+= char(c
);
769 } while (c
!= ')' && c
!= EOF
);
770 current_input
= new argument_macro_input(body
, argc
, argv
, current_input
);
773 /* If lookup flag is non-zero the token will be looked up to see
774 if it is macro. If it's 1, it will looked up to see if it's a token.
777 int get_token(int lookup_flag
= 0)
781 while (c
== ' ' || c
== '\n')
786 add_context("end of input");
792 token_buffer
.clear();
796 lex_error("missing \"");
799 else if (c
== '\n') {
800 lex_error("newline before end of quoted text");
806 token_buffer
[token_buffer
.length() - 1] = '"';
811 quoted
= quoted
? 0 : c
== '\\';
815 add_quoted_context(token_buffer
);
828 token_buffer
.clear();
836 if (!quoted
&& lookup_flag
!= 0 && c
== '(') {
837 token_buffer
+= '\0';
838 definition
*def
= macro_table
.lookup(token_buffer
.contents());
839 if (def
&& def
->is_macro
&& !def
->is_simple
) {
840 (void)get_char(); // skip initial '('
841 interpolate_macro_with_args(def
->contents
);
845 token_buffer
.set_length(token_buffer
.length() - 1);
851 lex_error("`\\' ignored at end of equation");
855 lex_error("`\\' ignored because followed by newline");
859 lex_error("`\\' ignored because followed by tab");
868 token_buffer
+= '\\';
892 token_buffer
+= char(c
);
897 if (break_flag
|| token_buffer
.length() == 0)
899 if (lookup_flag
!= 0) {
900 token_buffer
+= '\0';
901 definition
*def
= macro_table
.lookup(token_buffer
.contents());
902 token_buffer
.set_length(token_buffer
.length() - 1);
905 current_input
= new macro_input(def
->contents
, current_input
);
908 else if (lookup_flag
== 1) {
909 add_context(token_buffer
);
914 add_context(token_buffer
);
923 int t
= get_token(2);
924 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
925 lex_error("bad filename for include");
928 token_buffer
+= '\0';
929 const char *filename
= token_buffer
.contents();
930 file_case
*fcp
= file_case::muxer(filename
);
932 lex_error("can't open included file `%1'", filename
);
935 current_input
= new file_input(fcp
, filename
, current_input
);
938 void ignore_definition()
942 lex_error("bad definition");
945 get_delimited_text();
948 void do_definition(int is_simple
)
952 lex_error("bad definition");
955 token_buffer
+= '\0';
956 const char *name
= token_buffer
.contents();
957 definition
*def
= macro_table
.lookup(name
);
959 def
= new definition
[1];
960 macro_table
.define(name
, def
);
962 else if (def
->is_macro
) {
963 a_delete def
->contents
;
965 get_delimited_text();
966 token_buffer
+= '\0';
968 def
->contents
= strsave(token_buffer
.contents());
969 def
->is_simple
= is_simple
;
976 lex_error("bad undef command");
979 token_buffer
+= '\0';
980 macro_table
.define(token_buffer
.contents(), 0);
985 int t
= get_token(2);
986 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
987 lex_error("bad argument to gsize command");
990 token_buffer
+= '\0';
991 if (!set_gsize(token_buffer
.contents()))
992 lex_error("invalid size `%1'", token_buffer
.contents());
997 int t
= get_token(2);
998 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
999 lex_error("bad argument to gfont command");
1002 token_buffer
+= '\0';
1003 set_gfont(token_buffer
.contents());
1008 int t
= get_token(2);
1009 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
1010 lex_error("bad argument to grfont command");
1013 token_buffer
+= '\0';
1014 set_grfont(token_buffer
.contents());
1019 int t
= get_token(2);
1020 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
1021 lex_error("bad argument to gbfont command");
1024 token_buffer
+= '\0';
1025 set_gbfont(token_buffer
.contents());
1030 int t
= get_token(2);
1031 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
1032 lex_error("bad argument to space command");
1035 token_buffer
+= '\0';
1037 long n
= strtol(token_buffer
.contents(), &ptr
, 10);
1038 if (n
== 0 && ptr
== token_buffer
.contents())
1039 lex_error("bad argument `%1' to space command", token_buffer
.contents());
1046 int t
= get_token();
1048 lex_error("bad ifdef");
1051 token_buffer
+= '\0';
1052 definition
*def
= macro_table
.lookup(token_buffer
.contents());
1053 int result
= def
&& def
->is_macro
&& !def
->is_simple
;
1054 get_delimited_text();
1056 token_buffer
+= '\0';
1057 current_input
= new macro_input(token_buffer
.contents(), current_input
);
1064 while (c
== ' ' || c
== '\n')
1067 if (c
== EOF
|| (d
= get_char()) == EOF
)
1068 lex_error("end of file while reading argument to `delim'");
1070 if (c
== 'o' && d
== 'f' && peek_char() == 'f') {
1072 start_delim
= end_delim
= '\0';
1083 int t
= get_token(2);
1084 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
1085 lex_error("bad chartype");
1088 token_buffer
+= '\0';
1089 string type
= token_buffer
;
1091 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
1092 lex_error("bad chartype");
1095 token_buffer
+= '\0';
1096 set_char_type(type
.contents(), strsave(token_buffer
.contents()));
1101 int t
= get_token(2);
1102 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
1103 lex_error("bad set");
1106 token_buffer
+= '\0';
1107 string param
= token_buffer
;
1109 if (t
!= TEXT
&& t
!= QUOTED_TEXT
) {
1110 lex_error("bad set");
1113 token_buffer
+= '\0';
1115 if (sscanf(&token_buffer
[0], "%d", &n
) != 1) {
1116 lex_error("bad number `%1'", token_buffer
.contents());
1119 set_param(param
.contents(), n
);
1125 int tk
= get_token(1);
1140 ignore_definition();
1146 ignore_definition();
1180 token_buffer
+= '\0';
1181 yylval
.str
= strsave(token_buffer
.contents());
1189 void lex_error(const char *message
,
1196 if (!get_location(&filename
, &lineno
))
1197 error(message
, arg1
, arg2
, arg3
);
1199 error_with_file_and_line(filename
, lineno
, message
, arg1
, arg2
, arg3
);
1202 void yyerror(const char *s
)
1206 if (!get_location(&filename
, &lineno
))
1209 error_with_file_and_line(filename
, lineno
, s
);