2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "stringclass.h"
38 definition::definition() : is_macro(1), is_simple(0)
43 definition::~definition()
49 declare_ptable(definition)
50 implement_ptable(definition)
52 PTABLE(definition) macro_table;
59 { "smallover", SMALLOVER },
80 { "uaccent", UACCENT },
97 { "sdefine", SDEFINE },
98 { "ndefine", NDEFINE },
99 { "tdefine", TDEFINE },
102 { "include", INCLUDE },
105 { "chartype", CHARTYPE },
107 { "vcenter", VCENTER },
109 { "opprime", PRIME },
110 { "grfont", GRFONT },
111 { "gbfont", GBFONT },
113 { "nosplit", NOSPLIT },
114 { "special", SPECIAL },
121 { "ALPHA", "\\(*A" },
124 { "DELTA", "\\(*D" },
125 { "EPSILON", "\\(*E" },
127 { "GAMMA", "\\(*G" },
129 { "KAPPA", "\\(*K" },
130 { "LAMBDA", "\\(*L" },
133 { "OMEGA", "\\(*W" },
134 { "OMICRON", "\\(*O" },
139 { "SIGMA", "\\(*S" },
141 { "THETA", "\\(*H" },
142 { "UPSILON", "\\(*U" },
145 { "Alpha", "\\(*A" },
148 { "Delta", "\\(*D" },
149 { "Epsilon", "\\(*E" },
151 { "Gamma", "\\(*G" },
153 { "Kappa", "\\(*K" },
154 { "Lambda", "\\(*L" },
157 { "Omega", "\\(*W" },
158 { "Omicron", "\\(*O" },
163 { "Sigma", "\\(*S" },
165 { "Theta", "\\(*H" },
166 { "Upsilon", "\\(*U" },
169 { "alpha", "\\(*a" },
172 { "delta", "\\(*d" },
173 { "epsilon", "\\(*e" },
175 { "gamma", "\\(*g" },
177 { "kappa", "\\(*k" },
178 { "lambda", "\\(*l" },
181 { "omega", "\\(*w" },
182 { "omicron", "\\(*o" },
187 { "sigma", "\\(*s" },
189 { "theta", "\\(*h" },
190 { "upsilon", "\\(*u" },
193 { "max", "{type \"operator\" roman \"max\"}" },
194 { "min", "{type \"operator\" roman \"min\"}" },
195 { "lim", "{type \"operator\" roman \"lim\"}" },
196 { "sin", "{type \"operator\" roman \"sin\"}" },
197 { "cos", "{type \"operator\" roman \"cos\"}" },
198 { "tan", "{type \"operator\" roman \"tan\"}" },
199 { "sinh", "{type \"operator\" roman \"sinh\"}" },
200 { "cosh", "{type \"operator\" roman \"cosh\"}" },
201 { "tanh", "{type \"operator\" roman \"tanh\"}" },
202 { "arc", "{type \"operator\" roman \"arc\"}" },
203 { "log", "{type \"operator\" roman \"log\"}" },
204 { "ln", "{type \"operator\" roman \"ln\"}" },
205 { "exp", "{type \"operator\" roman \"exp\"}" },
206 { "Re", "{type \"operator\" roman \"Re\"}" },
207 { "Im", "{type \"operator\" roman \"Im\"}" },
208 { "det", "{type \"operator\" roman \"det\"}" },
209 { "and", "{roman \"and\"}" },
210 { "if", "{roman \"if\"}" },
211 { "for", "{roman \"for\"}" },
212 { "sum", "{type \"operator\" vcenter size +5 \\(*S}" },
213 { "prod", "{type \"operator\" vcenter size +5 \\(*P}" },
214 { "int", "{type \"operator\" vcenter size +8 \\(is}" },
215 { "union", "{type \"operator\" vcenter size +5 \\(cu}" },
216 { "inter", "{type \"operator\" vcenter size +5 \\(ca}" },
217 { "times", "type \"binary\" \\(mu" },
218 { "ldots", "type \"inner\" { . . . }" },
220 { "partial", "\\(pd" },
221 { "nothing", "\"\"" },
222 { "half", "{1 smallover 2}" },
223 { "hat_def", "roman \"^\"" },
224 { "hat", "accent { hat_def }" },
225 { "dot_def", "back 15 \"\\v'-52M'.\\v'52M'\"" },
226 { "dot", "accent { dot_def }" },
227 { "dotdot_def", "back 25 \"\\v'-52M'..\\v'52M'\"" },
228 { "dotdot", "accent { dotdot_def }" },
229 { "tilde_def", "\"~\"" },
230 { "tilde", "accent { tilde_def }" },
231 { "utilde_def", "\"\\v'75M'~\\v'-75M'\"" },
232 { "utilde", "uaccent { utilde_def }" },
233 { "vec_def", "up 52 size -5 \\(->" },
234 { "vec", "accent { vec_def }" },
235 { "dyad_def", "up 52 size -5 {\\(<- back 60 \\(->}" },
236 { "dyad", "accent { dyad_def }" },
237 { "==", "type \"relation\" \\(==" },
238 { "!=", "type \"relation\" \\(!=" },
239 { "+-", "type \"binary\" \\(+-" },
240 { "->", "type \"relation\" \\(->" },
241 { "<-", "type \"relation\" \\(<-" },
242 { "<<", "{ < back 20 < }" },
243 { ">>", "{ > back 20 > }" },
244 { "...", "type \"inner\" vcenter { . . . }" },
246 { "approx", "type \"relation\" \"\\(~=\"" },
249 { "cdot", "type \"binary\" vcenter ." },
253 void init_table(const char *device)
256 for (i = 0; i < sizeof(token_table)/sizeof(token_table[0]); i++) {
257 definition *def = new definition;
259 def->tok = token_table[i].token;
260 macro_table.define(token_table[i].name, def);
262 for (i = 0; i < sizeof(def_table)/sizeof(def_table[0]); i++) {
263 definition *def = new definition;
265 def->contents = strsave(def_table[i].def);
267 macro_table.define(def_table[i].name, def);
269 definition *def = new definition;
271 def->contents = strsave("1");
272 macro_table.define(device, def);
280 virtual int get() = 0;
281 virtual int peek() = 0;
282 virtual int get_location(char **, int *);
284 friend int get_char();
285 friend int peek_char();
286 friend int get_location(char **, int *);
287 friend void init_lex(const char *str, const char *filename, int lineno);
290 class file_input : public input {
298 file_input(FILE *, const char *, input *);
302 int get_location(char **, int *);
306 class macro_input : public input {
310 macro_input(const char *, input *);
316 class top_input : public macro_input {
320 top_input(const char *, const char *, int, input *);
323 int get_location(char **, int *);
326 class argument_macro_input: public input {
333 argument_macro_input(const char *, int, char **, input *);
334 ~argument_macro_input();
339 input::input(input *x) : next(x)
347 int input::get_location(char **, int *)
352 file_input::file_input(FILE *f, const char *fn, input *p)
353 : input(p), lineno(0), ptr("")
356 filename = strsave(fn);
359 file_input::~file_input()
365 int file_input::read_line()
374 else if (invalid_input_char(c))
375 lex_error("invalid input character code %1", c);
382 if (line.length() == 0)
384 if (!(line.length() >= 3 && line[0] == '.' && line[1] == 'E'
385 && (line[2] == 'Q' || line[2] == 'N')
386 && (line.length() == 3 || line[3] == ' ' || line[3] == '\n'
387 || compatible_flag))) {
389 ptr = line.contents();
395 int file_input::get()
397 if (*ptr != '\0' || read_line())
398 return *ptr++ & 0377;
403 int file_input::peek()
405 if (*ptr != '\0' || read_line())
411 int file_input::get_location(char **fnp, int *lnp)
418 macro_input::macro_input(const char *str, input *x) : input(x)
420 p = s = strsave(str);
423 macro_input::~macro_input()
428 int macro_input::get()
430 if (p == 0 || *p == '\0')
436 int macro_input::peek()
438 if (p == 0 || *p == '\0')
444 top_input::top_input(const char *str, const char *fn, int ln, input *x)
445 : macro_input(str, x), lineno(ln)
447 filename = strsave(fn);
450 top_input::~top_input()
457 int c = macro_input::get();
463 int top_input::get_location(char **fnp, int *lnp)
470 // Character representing $1. Must be invalid input character.
473 argument_macro_input::argument_macro_input(const char *body, int ac,
475 : input(x), ap(0), argc(ac)
478 for (i = 0; i < argc; i++)
480 p = s = strsave(body);
482 for (i = 0; s[i] != '\0'; i++)
483 if (s[i] == '$' && s[i+1] >= '0' && s[i+1] <= '9') {
485 s[j++] = ARG1 + s[++i] - '1';
493 argument_macro_input::~argument_macro_input()
495 for (int i = 0; i < argc; i++)
500 int argument_macro_input::get()
509 while (*p >= ARG1 && *p <= ARG1 + 8) {
511 if (i < argc && argv[i] != 0 && argv[i][0] != '\0') {
521 int argument_macro_input::peek()
530 while (*p >= ARG1 && *p <= ARG1 + 8) {
532 if (i < argc && argv[i] != 0 && argv[i][0] != '\0') {
542 static input *current_input = 0;
544 /* we insert a newline between input from different levels */
548 if (current_input == 0)
551 int c = current_input->get();
555 input *tem = current_input;
556 current_input = current_input->next;
565 if (current_input == 0)
568 int c = current_input->peek();
576 int get_location(char **fnp, int *lnp)
578 for (input *p = current_input; p; p = p->next)
579 if (p->get_location(fnp, lnp))
585 const int NCONTEXT = 4;
586 string context_ring[NCONTEXT];
587 int context_index = 0;
591 for (int i = 0; i < NCONTEXT; i++)
592 context_ring[i] = "";
598 int i = context_index;
599 fputs(" context is\n\t", stderr);
601 int j = (i + 1) % NCONTEXT;
602 if (j == context_index) {
603 fputs(">>> ", stderr);
604 put_string(context_ring[i], stderr);
605 fputs(" <<<", stderr);
608 else if (context_ring[i].length() > 0) {
609 put_string(context_ring[i], stderr);
617 void add_context(const string &s)
619 context_ring[context_index] = s;
620 context_index = (context_index + 1) % NCONTEXT;
623 void add_context(char c)
625 context_ring[context_index] = c;
626 context_index = (context_index + 1) % NCONTEXT;
629 void add_quoted_context(const string &s)
631 string &r = context_ring[context_index];
633 for (int i = 0; i < s.length(); i++)
639 context_index = (context_index + 1) % NCONTEXT;
642 void init_lex(const char *str, const char *filename, int lineno)
644 while (current_input != 0) {
645 input *tem = current_input;
646 current_input = current_input->next;
649 current_input = new top_input(str, filename, lineno, 0);
654 void get_delimited_text()
658 int got_location = get_location(&filename, &lineno);
659 int start = get_char();
660 while (start == ' ' || start == '\t' || start == '\n')
662 token_buffer.clear();
665 error_with_file_and_line(filename, lineno,
666 "end of input while defining macro");
668 error("end of input while defining macro");
675 error_with_file_and_line(filename, lineno,
676 "end of input while defining macro");
678 error("end of input while defining macro");
679 add_context(start + token_buffer);
684 token_buffer += char(c);
686 add_context(start + token_buffer + start);
689 void interpolate_macro_with_args(const char *body)
694 for (i = 0; i < 9; i++)
699 token_buffer.clear();
703 lex_error("end of input while scanning macro arguments");
706 if (level == 0 && (c == ',' || c == ')')) {
707 if (token_buffer.length() > 0) {
708 token_buffer += '\0';
709 argv[argc] = strsave(token_buffer.contents());
711 // for `foo()', argc = 0
712 if (argc > 0 || c != ')' || i > 0)
716 token_buffer += char(c);
722 } while (c != ')' && c != EOF);
723 current_input = new argument_macro_input(body, argc, argv, current_input);
726 /* If lookup flag is non-zero the token will be looked up to see
727 if it is macro. If it's 1, it will looked up to see if it's a token.
730 int get_token(int lookup_flag = 0)
734 while (c == ' ' || c == '\n')
739 add_context("end of input");
745 token_buffer.clear();
749 lex_error("missing \"");
752 else if (c == '\n') {
753 lex_error("newline before end of quoted text");
759 token_buffer[token_buffer.length() - 1] = '"';
764 quoted = quoted ? 0 : c == '\\';
768 add_quoted_context(token_buffer);
781 token_buffer.clear();
789 if (!quoted && lookup_flag != 0 && c == '(') {
790 token_buffer += '\0';
791 definition *def = macro_table.lookup(token_buffer.contents());
792 if (def && def->is_macro && !def->is_simple) {
793 (void)get_char(); // skip initial '('
794 interpolate_macro_with_args(def->contents);
798 token_buffer.set_length(token_buffer.length() - 1);
804 lex_error("`\\' ignored at end of equation");
808 lex_error("`\\' ignored because followed by newline");
812 lex_error("`\\' ignored because followed by tab");
821 token_buffer += '\\';
845 token_buffer += char(c);
850 if (break_flag || token_buffer.length() == 0)
852 if (lookup_flag != 0) {
853 token_buffer += '\0';
854 definition *def = macro_table.lookup(token_buffer.contents());
855 token_buffer.set_length(token_buffer.length() - 1);
858 current_input = new macro_input(def->contents, current_input);
861 else if (lookup_flag == 1) {
862 add_context(token_buffer);
867 add_context(token_buffer);
876 int t = get_token(2);
877 if (t != TEXT && t != QUOTED_TEXT) {
878 lex_error("bad filename for include");
881 token_buffer += '\0';
882 const char *filename = token_buffer.contents();
884 FILE *fp = fopen(filename, "r");
886 lex_error("can't open included file `%1'", filename);
889 current_input = new file_input(fp, filename, current_input);
892 void ignore_definition()
896 lex_error("bad definition");
899 get_delimited_text();
902 void do_definition(int is_simple)
906 lex_error("bad definition");
909 token_buffer += '\0';
910 const char *name = token_buffer.contents();
911 definition *def = macro_table.lookup(name);
913 def = new definition;
914 macro_table.define(name, def);
916 else if (def->is_macro) {
917 a_delete def->contents;
919 get_delimited_text();
920 token_buffer += '\0';
922 def->contents = strsave(token_buffer.contents());
923 def->is_simple = is_simple;
930 lex_error("bad undef command");
933 token_buffer += '\0';
934 macro_table.define(token_buffer.contents(), 0);
939 int t = get_token(2);
940 if (t != TEXT && t != QUOTED_TEXT) {
941 lex_error("bad argument to gsize command");
944 token_buffer += '\0';
945 if (!set_gsize(token_buffer.contents()))
946 lex_error("invalid size `%1'", token_buffer.contents());
951 int t = get_token(2);
952 if (t != TEXT && t != QUOTED_TEXT) {
953 lex_error("bad argument to gfont command");
956 token_buffer += '\0';
957 set_gfont(token_buffer.contents());
962 int t = get_token(2);
963 if (t != TEXT && t != QUOTED_TEXT) {
964 lex_error("bad argument to grfont command");
967 token_buffer += '\0';
968 set_grfont(token_buffer.contents());
973 int t = get_token(2);
974 if (t != TEXT && t != QUOTED_TEXT) {
975 lex_error("bad argument to gbfont command");
978 token_buffer += '\0';
979 set_gbfont(token_buffer.contents());
984 int t = get_token(2);
985 if (t != TEXT && t != QUOTED_TEXT) {
986 lex_error("bad argument to space command");
989 token_buffer += '\0';
991 long n = strtol(token_buffer.contents(), &ptr, 10);
992 if (n == 0 && ptr == token_buffer.contents())
993 lex_error("bad argument `%1' to space command", token_buffer.contents());
1000 int t = get_token();
1002 lex_error("bad ifdef");
1005 token_buffer += '\0';
1006 definition *def = macro_table.lookup(token_buffer.contents());
1007 int result = def && def->is_macro && !def->is_simple;
1008 get_delimited_text();
1010 token_buffer += '\0';
1011 current_input = new macro_input(token_buffer.contents(), current_input);
1018 while (c == ' ' || c == '\n')
1021 if (c == EOF || (d = get_char()) == EOF)
1022 lex_error("end of file while reading argument to `delim'");
1024 if (c == 'o' && d == 'f' && peek_char() == 'f') {
1026 start_delim = end_delim = '\0';
1037 int t = get_token(2);
1038 if (t != TEXT && t != QUOTED_TEXT) {
1039 lex_error("bad chartype");
1042 token_buffer += '\0';
1043 string type = token_buffer;
1045 if (t != TEXT && t != QUOTED_TEXT) {
1046 lex_error("bad chartype");
1049 token_buffer += '\0';
1050 set_char_type(type.contents(), strsave(token_buffer.contents()));
1055 int t = get_token(2);
1056 if (t != TEXT && t != QUOTED_TEXT) {
1057 lex_error("bad set");
1060 token_buffer += '\0';
1061 string param = token_buffer;
1063 if (t != TEXT && t != QUOTED_TEXT) {
1064 lex_error("bad set");
1067 token_buffer += '\0';
1069 if (sscanf(&token_buffer[0], "%d", &n) != 1) {
1070 lex_error("bad number `%1'", token_buffer.contents());
1073 set_param(param.contents(), n);
1079 int tk = get_token(1);
1094 ignore_definition();
1100 ignore_definition();
1134 token_buffer += '\0';
1135 yylval.str = strsave(token_buffer.contents());
1143 void lex_error(const char *message,
1150 if (!get_location(&filename, &lineno))
1151 error(message, arg1, arg2, arg3);
1153 error_with_file_and_line(filename, lineno, message, arg1, arg2, arg3);
1156 void yyerror(const char *s)
1160 if (!get_location(&filename, &lineno))
1163 error_with_file_and_line(filename, lineno, s);