* src/builtin.c (includes): Adjust to gnulib changes.
[m4.git] / src / input.c
blob6296281bf631851d31004c4a901d54f27aa6eca7
1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA
22 /* Handling of different input sources, and lexical analysis. */
24 #include "m4.h"
26 /* Unread input can be either files, that should be read (eg. included
27 files), strings, which should be rescanned (eg. macro expansion text),
28 or quoted macro definitions (as returned by the builtin "defn").
29 Unread input are organised in a stack, implemented with an obstack.
30 Each input source is described by a "struct input_block". The obstack
31 is "current_input". The top of the input stack is "isp".
33 The macro "m4wrap" places the text to be saved on another input
34 stack, on the obstack "wrapup_stack", whose top is "wsp". When EOF
35 is seen on normal input (eg, when "current_input" is empty), input is
36 switched over to "wrapup_stack", and the original "current_input" is
37 freed. A new stack is allocated for "wrapup_stack", which will
38 accept any text produced by calls to "m4wrap" from within the
39 wrapped text. This process of shuffling "wrapup_stack" to
40 "current_input" can continue indefinitely, even generating infinite
41 loops (e.g. "define(`f',`m4wrap(`f')')f"), without memory leaks.
43 Pushing new input on the input stack is done by push_file (),
44 push_string (), push_wrapup () (for wrapup text), and push_macro ()
45 (for macro definitions). Because macro expansion needs direct access
46 to the current input obstack (for optimisation), push_string () are
47 split in two functions, push_string_init (), which returns a pointer
48 to the current input stack, and push_string_finish (), which return a
49 pointer to the final text. The input_block *next is used to manage
50 the coordination between the different push routines.
52 The current file and line number are stored in two global
53 variables, for use by the error handling functions in m4.c. Macro
54 expansion wants to report the line where a macro name was detected,
55 rather than where it finished collecting arguments. This also
56 applies to text resulting from macro expansions. So each input
57 block maintains its own notion of the current file and line, and
58 swapping between input blocks updates the global variables
59 accordingly. */
61 #ifdef ENABLE_CHANGEWORD
62 #include "regex.h"
63 #endif
65 enum input_type
67 INPUT_STRING, /* String resulting from macro expansion. */
68 INPUT_FILE, /* File from command line or include. */
69 INPUT_MACRO /* Builtin resulting from defn. */
72 typedef enum input_type input_type;
74 struct input_block
76 struct input_block *prev; /* previous input_block on the input stack */
77 input_type type; /* see enum values */
78 const char *file; /* file where this input is from */
79 int line; /* line where this input is from */
80 union
82 struct
84 char *string; /* remaining string value */
86 u_s; /* INPUT_STRING */
87 struct
89 FILE *fp; /* input file handle */
90 bool end : 1; /* true if peek has seen EOF */
91 bool close : 1; /* true if we should close file on pop */
92 bool advance_line : 1; /* track previous start_of_input_line */
94 u_f; /* INPUT_FILE */
95 builtin_func *func; /* pointer to macro's function */
100 typedef struct input_block input_block;
103 /* Current input file name. */
104 const char *current_file;
106 /* Current input line number. */
107 int current_line;
109 /* Obstack for storing individual tokens. */
110 static struct obstack token_stack;
112 /* Obstack for storing file names. */
113 static struct obstack file_names;
115 /* Wrapup input stack. */
116 static struct obstack *wrapup_stack;
118 /* Current stack, from input or wrapup. */
119 static struct obstack *current_input;
121 /* Bottom of token_stack, for obstack_free. */
122 static void *token_bottom;
124 /* Pointer to top of current_input. */
125 static input_block *isp;
127 /* Pointer to top of wrapup_stack. */
128 static input_block *wsp;
130 /* Aux. for handling split push_string (). */
131 static input_block *next;
133 /* Flag for next_char () to increment current_line. */
134 static bool start_of_input_line;
136 /* Flag for next_char () to recognize change in input block. */
137 static bool input_change;
139 #define CHAR_EOF 256 /* character return on EOF */
140 #define CHAR_MACRO 257 /* character return for MACRO token */
142 /* Quote chars. */
143 STRING rquote;
144 STRING lquote;
146 /* Comment chars. */
147 STRING bcomm;
148 STRING ecomm;
150 #ifdef ENABLE_CHANGEWORD
152 # define DEFAULT_WORD_REGEXP "[_a-zA-Z][_a-zA-Z0-9]*"
154 static char *word_start;
155 static struct re_pattern_buffer word_regexp;
156 static int default_word_regexp;
157 static struct re_registers regs;
159 #else /* ! ENABLE_CHANGEWORD */
160 # define default_word_regexp 1
161 #endif /* ! ENABLE_CHANGEWORD */
163 #ifdef DEBUG_INPUT
164 static const char *token_type_string (token_type);
165 #endif
168 /*-------------------------------------------------------------------.
169 | push_file () pushes an input file on the input stack, saving the |
170 | current file name and line number. If next is non-NULL, this push |
171 | invalidates a call to push_string_init (), whose storage is |
172 | consequently released. If CLOSE, then close FP after EOF is |
173 | detected. |
174 `-------------------------------------------------------------------*/
176 void
177 push_file (FILE *fp, const char *title, bool close)
179 input_block *i;
181 if (next != NULL)
183 obstack_free (current_input, next);
184 next = NULL;
187 if (debug_level & DEBUG_TRACE_INPUT)
188 DEBUG_MESSAGE1 ("input read from %s", title);
190 i = (input_block *) obstack_alloc (current_input,
191 sizeof (struct input_block));
192 i->type = INPUT_FILE;
193 i->file = (char *) obstack_copy0 (&file_names, title, strlen (title));
194 i->line = 1;
195 input_change = true;
197 i->u.u_f.fp = fp;
198 i->u.u_f.end = false;
199 i->u.u_f.close = close;
200 i->u.u_f.advance_line = start_of_input_line;
201 output_current_line = -1;
203 i->prev = isp;
204 isp = i;
207 /*---------------------------------------------------------------.
208 | push_macro () pushes a builtin macro's definition on the input |
209 | stack. If next is non-NULL, this push invalidates a call to |
210 | push_string_init (), whose storage is consequently released. |
211 `---------------------------------------------------------------*/
213 void
214 push_macro (builtin_func *func)
216 input_block *i;
218 if (next != NULL)
220 obstack_free (current_input, next);
221 next = NULL;
224 i = (input_block *) obstack_alloc (current_input,
225 sizeof (struct input_block));
226 i->type = INPUT_MACRO;
227 i->file = current_file;
228 i->line = current_line;
229 input_change = true;
231 i->u.func = func;
232 i->prev = isp;
233 isp = i;
236 /*------------------------------------------------------------------.
237 | First half of push_string (). The pointer next points to the new |
238 | input_block. |
239 `------------------------------------------------------------------*/
241 struct obstack *
242 push_string_init (void)
244 if (next != NULL)
246 M4ERROR ((warning_status, 0,
247 "INTERNAL ERROR: recursive push_string!"));
248 abort ();
251 next = (input_block *) obstack_alloc (current_input,
252 sizeof (struct input_block));
253 next->type = INPUT_STRING;
254 next->file = current_file;
255 next->line = current_line;
257 return current_input;
260 /*------------------------------------------------------------------------.
261 | Last half of push_string (). If next is now NULL, a call to push_file |
262 | () has invalidated the previous call to push_string_init (), so we just |
263 | give up. If the new object is void, we do not push it. The function |
264 | push_string_finish () returns a pointer to the finished object. This |
265 | pointer is only for temporary use, since reading the next token might |
266 | release the memory used for the object. |
267 `------------------------------------------------------------------------*/
269 const char *
270 push_string_finish (void)
272 const char *ret = NULL;
274 if (next == NULL)
275 return NULL;
277 if (obstack_object_size (current_input) > 0)
279 obstack_1grow (current_input, '\0');
280 next->u.u_s.string = (char *) obstack_finish (current_input);
281 next->prev = isp;
282 isp = next;
283 ret = isp->u.u_s.string; /* for immediate use only */
284 input_change = true;
286 else
287 obstack_free (current_input, next); /* people might leave garbage on it. */
288 next = NULL;
289 return ret;
292 /*------------------------------------------------------------------.
293 | The function push_wrapup () pushes a string on the wrapup stack. |
294 | When the normal input stack gets empty, the wrapup stack will |
295 | become the input stack, and push_string () and push_file () will |
296 | operate on wrapup_stack. Push_wrapup should be done as |
297 | push_string (), but this will suffice, as long as arguments to |
298 | m4_m4wrap () are moderate in size. |
299 `------------------------------------------------------------------*/
301 void
302 push_wrapup (const char *s)
304 input_block *i;
305 i = (input_block *) obstack_alloc (wrapup_stack,
306 sizeof (struct input_block));
307 i->prev = wsp;
308 i->type = INPUT_STRING;
309 i->file = current_file;
310 i->line = current_line;
311 i->u.u_s.string = (char *) obstack_copy0 (wrapup_stack, s, strlen (s));
312 wsp = i;
316 /*-------------------------------------------------------------------------.
317 | The function pop_input () pops one level of input sources. If the |
318 | popped input_block is a file, current_file and current_line are reset to |
319 | the saved values before the memory for the input_block are released. |
320 `-------------------------------------------------------------------------*/
322 static void
323 pop_input (void)
325 input_block *tmp = isp->prev;
327 switch (isp->type)
329 case INPUT_STRING:
330 case INPUT_MACRO:
331 break;
333 case INPUT_FILE:
334 if (debug_level & DEBUG_TRACE_INPUT)
336 if (tmp)
337 DEBUG_MESSAGE2 ("input reverted to %s, line %d",
338 tmp->file, tmp->line);
339 else
340 DEBUG_MESSAGE ("input exhausted");
343 if (ferror (isp->u.u_f.fp))
345 M4ERROR ((warning_status, 0, "read error"));
346 if (isp->u.u_f.close)
347 fclose (isp->u.u_f.fp);
348 retcode = EXIT_FAILURE;
350 else if (isp->u.u_f.close && fclose (isp->u.u_f.fp) == EOF)
352 M4ERROR ((warning_status, errno, "error reading file"));
353 retcode = EXIT_FAILURE;
355 start_of_input_line = isp->u.u_f.advance_line;
356 output_current_line = -1;
357 break;
359 default:
360 M4ERROR ((warning_status, 0,
361 "INTERNAL ERROR: input stack botch in pop_input ()"));
362 abort ();
364 obstack_free (current_input, isp);
365 next = NULL; /* might be set in push_string_init () */
367 isp = tmp;
368 input_change = true;
371 /*------------------------------------------------------------------------.
372 | To switch input over to the wrapup stack, main () calls pop_wrapup (). |
373 | Since wrapup text can install new wrapup text, pop_wrapup () returns |
374 | false when there is no wrapup text on the stack, and true otherwise. |
375 `------------------------------------------------------------------------*/
377 bool
378 pop_wrapup (void)
380 next = NULL;
381 obstack_free (current_input, NULL);
382 free (current_input);
384 if (wsp == NULL)
386 /* End of the program. Free all memory even though we are about
387 to exit, since it makes leak detection easier. */
388 obstack_free (&token_stack, NULL);
389 obstack_free (&file_names, NULL);
390 obstack_free (wrapup_stack, NULL);
391 free (wrapup_stack);
392 return false;
395 current_input = wrapup_stack;
396 wrapup_stack = (struct obstack *) xmalloc (sizeof (struct obstack));
397 obstack_init (wrapup_stack);
399 isp = wsp;
400 wsp = NULL;
401 input_change = true;
403 return true;
406 /*-------------------------------------------------------------------.
407 | When a MACRO token is seen, next_token () uses init_macro_token () |
408 | to retrieve the value of the function pointer. |
409 `-------------------------------------------------------------------*/
411 static void
412 init_macro_token (token_data *td)
414 if (isp->type != INPUT_MACRO)
416 M4ERROR ((warning_status, 0,
417 "INTERNAL ERROR: bad call to init_macro_token ()"));
418 abort ();
421 TOKEN_DATA_TYPE (td) = TOKEN_FUNC;
422 TOKEN_DATA_FUNC (td) = isp->u.func;
426 /*------------------------------------------------------------------------.
427 | Low level input is done a character at a time. The function peek_input |
428 | () is used to look at the next character in the input stream. At any |
429 | given time, it reads from the input_block on the top of the current |
430 | input stack. |
431 `------------------------------------------------------------------------*/
433 static int
434 peek_input (void)
436 int ch;
437 input_block *block = isp;
439 while (1)
441 if (block == NULL)
442 return CHAR_EOF;
444 switch (block->type)
446 case INPUT_STRING:
447 ch = to_uchar (block->u.u_s.string[0]);
448 if (ch != '\0')
449 return ch;
450 break;
452 case INPUT_FILE:
453 ch = getc (block->u.u_f.fp);
454 if (ch != EOF)
456 ungetc (ch, block->u.u_f.fp);
457 return ch;
459 block->u.u_f.end = true;
460 break;
462 case INPUT_MACRO:
463 return CHAR_MACRO;
465 default:
466 M4ERROR ((warning_status, 0,
467 "INTERNAL ERROR: input stack botch in peek_input ()"));
468 abort ();
470 block = block->prev;
474 /*-------------------------------------------------------------------------.
475 | The function next_char () is used to read and advance the input to the |
476 | next character. It also manages line numbers for error messages, so |
477 | they do not get wrong, due to lookahead. The token consisting of a |
478 | newline alone is taken as belonging to the line it ends, and the current |
479 | line number is not incremented until the next character is read. |
480 | 99.9% of all calls will read from a string, so factor that out into a |
481 | macro for speed. |
482 `-------------------------------------------------------------------------*/
484 #define next_char() \
485 (isp && isp->type == INPUT_STRING && isp->u.u_s.string[0] \
486 && !input_change \
487 ? to_uchar (*isp->u.u_s.string++) \
488 : next_char_1 ())
490 static int
491 next_char_1 (void)
493 int ch;
495 while (1)
497 if (isp == NULL)
499 current_file = "";
500 current_line = 0;
501 return CHAR_EOF;
504 if (input_change)
506 current_file = isp->file;
507 current_line = isp->line;
508 input_change = false;
511 switch (isp->type)
513 case INPUT_STRING:
514 ch = to_uchar (*isp->u.u_s.string++);
515 if (ch != '\0')
516 return ch;
517 break;
519 case INPUT_FILE:
520 if (start_of_input_line)
522 start_of_input_line = false;
523 current_line = ++isp->line;
526 /* If stdin is a terminal, calling getc after peek_input
527 already called it would make the user have to hit ^D
528 twice to quit. */
529 ch = isp->u.u_f.end ? EOF : getc (isp->u.u_f.fp);
530 if (ch != EOF)
532 if (ch == '\n')
533 start_of_input_line = true;
534 return ch;
536 break;
538 case INPUT_MACRO:
539 pop_input (); /* INPUT_MACRO input sources has only one
540 token */
541 return CHAR_MACRO;
543 default:
544 M4ERROR ((warning_status, 0,
545 "INTERNAL ERROR: input stack botch in next_char ()"));
546 abort ();
549 /* End of input source --- pop one level. */
550 pop_input ();
554 /*------------------------------------------------------------------------.
555 | skip_line () simply discards all immediately following characters, upto |
556 | the first newline. It is only used from m4_dnl (). |
557 `------------------------------------------------------------------------*/
559 void
560 skip_line (void)
562 int ch;
563 const char *file = current_file;
564 int line = current_line;
566 while ((ch = next_char ()) != CHAR_EOF && ch != '\n')
568 if (ch == CHAR_EOF)
569 /* current_file changed to "" if we see CHAR_EOF, use the
570 previous value we stored earlier. */
571 M4ERROR_AT_LINE ((warning_status, 0, file, line,
572 "Warning: end of file treated as newline"));
573 /* On the rare occasion that dnl crosses include file boundaries
574 (either the input file did not end in a newline, or changeword
575 was used), calling next_char can update current_file and
576 current_line, and that update will be undone as we return to
577 expand_macro. This informs next_char to fix things again. */
578 if (file != current_file || line != current_line)
579 input_change = true;
583 /*------------------------------------------------------------------.
584 | This function is for matching a string against a prefix of the |
585 | input stream. If the string matches the input and consume is |
586 | true, the input is discarded; otherwise any characters read are |
587 | pushed back again. The function is used only when multicharacter |
588 | quotes or comment delimiters are used. |
589 `------------------------------------------------------------------*/
591 static bool
592 match_input (const char *s, bool consume)
594 int n; /* number of characters matched */
595 int ch; /* input character */
596 const char *t;
597 bool result = false;
599 ch = peek_input ();
600 if (ch != to_uchar (*s))
601 return false; /* fail */
603 if (s[1] == '\0')
605 if (consume)
606 (void) next_char ();
607 return true; /* short match */
610 (void) next_char ();
611 for (n = 1, t = s++; (ch = peek_input ()) == to_uchar (*s++); )
613 (void) next_char ();
614 n++;
615 if (*s == '\0') /* long match */
617 if (consume)
618 return true;
619 result = true;
620 break;
624 /* Failed or shouldn't consume, push back input. */
626 struct obstack *h = push_string_init ();
628 /* `obstack_grow' may be macro evaluating its arg 1 several times. */
629 obstack_grow (h, t, n);
631 push_string_finish ();
632 return result;
635 /*--------------------------------------------------------------------.
636 | The macro MATCH() is used to match a string S against the input. |
637 | The first character is handled inline, for speed. Hopefully, this |
638 | will not hurt efficiency too much when single character quotes and |
639 | comment delimiters are used. If CONSUME, then CH is the result of |
640 | next_char, and a successful match will discard the matched string. |
641 | Otherwise, CH is the result of peek_char, and the input stream is |
642 | effectively unchanged. |
643 `--------------------------------------------------------------------*/
645 #define MATCH(ch, s, consume) \
646 (to_uchar ((s)[0]) == (ch) \
647 && (ch) != '\0' \
648 && ((s)[1] == '\0' || (match_input ((s) + (consume), consume))))
651 /*----------------------------------------------------------.
652 | Inititialise input stacks, and quote/comment characters. |
653 `----------------------------------------------------------*/
655 void
656 input_init (void)
658 current_file = "";
659 current_line = 0;
661 current_input = (struct obstack *) xmalloc (sizeof (struct obstack));
662 obstack_init (current_input);
663 wrapup_stack = (struct obstack *) xmalloc (sizeof (struct obstack));
664 obstack_init (wrapup_stack);
666 obstack_init (&file_names);
668 /* Allocate an object in the current chunk, so that obstack_free
669 will always work even if the first token parsed spills to a new
670 chunk. */
671 obstack_init (&token_stack);
672 obstack_alloc (&token_stack, 1);
673 token_bottom = obstack_base (&token_stack);
675 isp = NULL;
676 wsp = NULL;
677 next = NULL;
679 start_of_input_line = false;
681 lquote.string = xstrdup (DEF_LQUOTE);
682 lquote.length = strlen (lquote.string);
683 rquote.string = xstrdup (DEF_RQUOTE);
684 rquote.length = strlen (rquote.string);
685 bcomm.string = xstrdup (DEF_BCOMM);
686 bcomm.length = strlen (bcomm.string);
687 ecomm.string = xstrdup (DEF_ECOMM);
688 ecomm.length = strlen (ecomm.string);
690 #ifdef ENABLE_CHANGEWORD
691 set_word_regexp (user_word_regexp);
692 #endif
696 /*------------------------------------------------------------------.
697 | Functions for setting quotes and comment delimiters. Used by |
698 | m4_changecom () and m4_changequote (). Pass NULL if the argument |
699 | was not present, to distinguish from an explicit empty string. |
700 `------------------------------------------------------------------*/
702 void
703 set_quotes (const char *lq, const char *rq)
705 free (lquote.string);
706 free (rquote.string);
708 /* POSIX states that with 0 arguments, the default quotes are used.
709 POSIX XCU ERN 112 states that behavior is implementation-defined
710 if there was only one argument, or if there is an empty string in
711 either position when there are two arguments. We allow an empty
712 left quote to disable quoting, but a non-empty left quote will
713 always create a non-empty right quote. See the texinfo for what
714 some other implementations do. */
715 if (!lq)
717 lq = DEF_LQUOTE;
718 rq = DEF_RQUOTE;
720 else if (!rq || (*lq && !*rq))
721 rq = DEF_RQUOTE;
723 lquote.string = xstrdup (lq);
724 lquote.length = strlen (lquote.string);
725 rquote.string = xstrdup (rq);
726 rquote.length = strlen (rquote.string);
729 void
730 set_comment (const char *bc, const char *ec)
732 free (bcomm.string);
733 free (ecomm.string);
735 /* POSIX requires no arguments to disable comments. It requires
736 empty arguments to be used as-is, but this is counter to
737 traditional behavior, because a non-null begin and null end makes
738 it impossible to end a comment. An aardvark has been filed:
739 http://www.opengroup.org/austin/mailarchives/ag-review/msg02168.html
740 This implementation assumes the aardvark will be approved. See
741 the texinfo for what some other implementations do. */
742 if (!bc)
743 bc = ec = "";
744 else if (!ec || (*bc && !*ec))
745 ec = DEF_ECOMM;
747 bcomm.string = xstrdup (bc);
748 bcomm.length = strlen (bcomm.string);
749 ecomm.string = xstrdup (ec);
750 ecomm.length = strlen (ecomm.string);
753 #ifdef ENABLE_CHANGEWORD
755 static void
756 init_pattern_buffer (struct re_pattern_buffer *buf)
758 buf->translate = NULL;
759 buf->fastmap = NULL;
760 buf->buffer = NULL;
761 buf->allocated = 0;
764 void
765 set_word_regexp (const char *regexp)
767 int i;
768 char test[2];
769 const char *msg;
770 struct re_pattern_buffer new_word_regexp;
772 if (!*regexp || !strcmp (regexp, DEFAULT_WORD_REGEXP))
774 default_word_regexp = true;
775 return;
778 /* Dry run to see whether the new expression is compilable. */
779 init_pattern_buffer (&new_word_regexp);
780 msg = re_compile_pattern (regexp, strlen (regexp), &new_word_regexp);
781 regfree (&new_word_regexp);
783 if (msg != NULL)
785 M4ERROR ((warning_status, 0,
786 "bad regular expression `%s': %s", regexp, msg));
787 return;
790 /* If compilation worked, retry using the word_regexp struct.
791 Can't rely on struct assigns working, so redo the compilation. */
792 regfree (&word_regexp);
793 msg = re_compile_pattern (regexp, strlen (regexp), &word_regexp);
794 re_set_registers (&word_regexp, &regs, regs.num_regs, regs.start, regs.end);
796 if (msg != NULL)
798 M4ERROR ((EXIT_FAILURE, 0,
799 "INTERNAL ERROR: expression recompilation `%s': %s",
800 regexp, msg));
803 default_word_regexp = false;
805 if (word_start == NULL)
806 word_start = (char *) xmalloc (256);
808 word_start[0] = '\0';
809 test[1] = '\0';
810 for (i = 1; i < 256; i++)
812 test[0] = i;
813 word_start[i] = re_search (&word_regexp, test, 1, 0, 0, NULL) >= 0;
817 #endif /* ENABLE_CHANGEWORD */
820 /*-------------------------------------------------------------------------.
821 | Parse and return a single token from the input stream. A token can |
822 | either be TOKEN_EOF, if the input_stack is empty; it can be TOKEN_STRING |
823 | for a quoted string; TOKEN_WORD for something that is a potential macro |
824 | name; and TOKEN_SIMPLE for any single character that is not a part of |
825 | any of the previous types. |
827 | Next_token () return the token type, and passes back a pointer to the |
828 | token data through TD. The token text is collected on the obstack |
829 | token_stack, which never contains more than one token text at a time. |
830 | The storage pointed to by the fields in TD is therefore subject to |
831 | change the next time next_token () is called. |
832 `-------------------------------------------------------------------------*/
834 token_type
835 next_token (token_data *td)
837 int ch;
838 int quote_level;
839 token_type type;
840 #ifdef ENABLE_CHANGEWORD
841 int startpos;
842 char *orig_text = NULL;
843 #endif
844 const char *file;
845 int line;
847 obstack_free (&token_stack, token_bottom);
849 /* Can't consume character until after CHAR_MACRO is handled. */
850 ch = peek_input ();
851 if (ch == CHAR_EOF)
853 #ifdef DEBUG_INPUT
854 fprintf (stderr, "next_token -> EOF\n");
855 #endif
856 next_char ();
857 return TOKEN_EOF;
859 if (ch == CHAR_MACRO)
861 init_macro_token (td);
862 next_char ();
863 #ifdef DEBUG_INPUT
864 fprintf (stderr, "next_token -> MACDEF (%s)\n",
865 find_builtin_by_addr (TOKEN_DATA_FUNC (td))->name);
866 #endif
867 return TOKEN_MACDEF;
870 next_char (); /* Consume character we already peeked at. */
871 file = current_file;
872 line = current_line;
873 if (MATCH (ch, bcomm.string, true))
875 obstack_grow (&token_stack, bcomm.string, bcomm.length);
876 while ((ch = next_char ()) != CHAR_EOF
877 && !MATCH (ch, ecomm.string, true))
878 obstack_1grow (&token_stack, ch);
879 if (ch != CHAR_EOF)
880 obstack_grow (&token_stack, ecomm.string, ecomm.length);
881 else
882 /* current_file changed to "" if we see CHAR_EOF, use the
883 previous value we stored earlier. */
884 M4ERROR_AT_LINE ((EXIT_FAILURE, 0, file, line,
885 "ERROR: end of file in comment"));
887 type = TOKEN_STRING;
889 else if (default_word_regexp && (isalpha (ch) || ch == '_'))
891 obstack_1grow (&token_stack, ch);
892 while ((ch = peek_input ()) != CHAR_EOF && (isalnum (ch) || ch == '_'))
894 obstack_1grow (&token_stack, ch);
895 (void) next_char ();
897 type = TOKEN_WORD;
900 #ifdef ENABLE_CHANGEWORD
902 else if (!default_word_regexp && word_start[ch])
904 obstack_1grow (&token_stack, ch);
905 while (1)
907 ch = peek_input ();
908 if (ch == CHAR_EOF)
909 break;
910 obstack_1grow (&token_stack, ch);
911 startpos = re_search (&word_regexp,
912 (char *) obstack_base (&token_stack),
913 obstack_object_size (&token_stack), 0, 0,
914 &regs);
915 if (startpos != 0 ||
916 regs.end [0] != obstack_object_size (&token_stack))
918 *(((char *) obstack_base (&token_stack)
919 + obstack_object_size (&token_stack)) - 1) = '\0';
920 break;
922 next_char ();
925 obstack_1grow (&token_stack, '\0');
926 orig_text = (char *) obstack_finish (&token_stack);
928 if (regs.start[1] != -1)
929 obstack_grow (&token_stack,orig_text + regs.start[1],
930 regs.end[1] - regs.start[1]);
931 else
932 obstack_grow (&token_stack, orig_text,regs.end[0]);
934 type = TOKEN_WORD;
937 #endif /* ENABLE_CHANGEWORD */
939 else if (!MATCH (ch, lquote.string, true))
941 switch (ch)
943 case '(':
944 type = TOKEN_OPEN;
945 break;
946 case ',':
947 type = TOKEN_COMMA;
948 break;
949 case ')':
950 type = TOKEN_CLOSE;
951 break;
952 default:
953 type = TOKEN_SIMPLE;
954 break;
956 obstack_1grow (&token_stack, ch);
958 else
960 quote_level = 1;
961 while (1)
963 ch = next_char ();
964 if (ch == CHAR_EOF)
965 /* current_file changed to "" if we see CHAR_EOF, use
966 the previous value we stored earlier. */
967 M4ERROR_AT_LINE ((EXIT_FAILURE, 0, file, line,
968 "ERROR: end of file in string"));
970 if (MATCH (ch, rquote.string, true))
972 if (--quote_level == 0)
973 break;
974 obstack_grow (&token_stack, rquote.string, rquote.length);
976 else if (MATCH (ch, lquote.string, true))
978 quote_level++;
979 obstack_grow (&token_stack, lquote.string, lquote.length);
981 else
982 obstack_1grow (&token_stack, ch);
984 type = TOKEN_STRING;
987 obstack_1grow (&token_stack, '\0');
989 TOKEN_DATA_TYPE (td) = TOKEN_TEXT;
990 TOKEN_DATA_TEXT (td) = (char *) obstack_finish (&token_stack);
991 #ifdef ENABLE_CHANGEWORD
992 if (orig_text == NULL)
993 orig_text = TOKEN_DATA_TEXT (td);
994 TOKEN_DATA_ORIG_TEXT (td) = orig_text;
995 #endif
996 #ifdef DEBUG_INPUT
997 fprintf (stderr, "next_token -> %s (%s)\n",
998 token_type_string (type), TOKEN_DATA_TEXT (td));
999 #endif
1000 return type;
1003 /*-----------------------------------------------.
1004 | Peek at the next token from the input stream. |
1005 `-----------------------------------------------*/
1007 token_type
1008 peek_token (void)
1010 token_type result;
1011 int ch = peek_input ();
1013 if (ch == CHAR_EOF)
1015 result = TOKEN_EOF;
1017 else if (ch == CHAR_MACRO)
1019 result = TOKEN_MACDEF;
1021 else if (MATCH (ch, bcomm.string, false))
1023 result = TOKEN_STRING;
1025 else if ((default_word_regexp && (isalpha (ch) || ch == '_'))
1026 #ifdef ENABLE_CHANGEWORD
1027 || (! default_word_regexp && word_start[ch])
1028 #endif /* ENABLE_CHANGEWORD */
1031 result = TOKEN_WORD;
1033 else if (MATCH (ch, lquote.string, false))
1035 result = TOKEN_STRING;
1037 else
1038 switch (ch)
1040 case '(':
1041 result = TOKEN_OPEN;
1042 break;
1043 case ',':
1044 result = TOKEN_COMMA;
1045 break;
1046 case ')':
1047 result = TOKEN_CLOSE;
1048 break;
1049 default:
1050 result = TOKEN_SIMPLE;
1053 #ifdef DEBUG_INPUT
1054 fprintf (stderr, "peek_token -> %s\n", token_type_string (result));
1055 #endif /* DEBUG_INPUT */
1056 return result;
1060 #ifdef DEBUG_INPUT
1062 static const char *
1063 token_type_string (token_type t)
1065 switch (t)
1066 { /* TOKSW */
1067 case TOKEN_EOF:
1068 return "EOF";
1069 case TOKEN_STRING:
1070 return "STRING";
1071 case TOKEN_WORD:
1072 return "WORD";
1073 case TOKEN_OPEN:
1074 return "OPEN";
1075 case TOKEN_COMMA:
1076 return "COMMA";
1077 case TOKEN_CLOSE:
1078 return "CLOSE";
1079 case TOKEN_SIMPLE:
1080 return "SIMPLE";
1081 case TOKEN_MACDEF:
1082 return "MACDEF";
1083 default:
1084 abort ();
1088 static void
1089 print_token (const char *s, token_type t, token_data *td)
1091 fprintf (stderr, "%s: ", s);
1092 switch (t)
1093 { /* TOKSW */
1094 case TOKEN_OPEN:
1095 case TOKEN_COMMA:
1096 case TOKEN_CLOSE:
1097 case TOKEN_SIMPLE:
1098 fprintf (stderr, "char:");
1099 break;
1101 case TOKEN_WORD:
1102 fprintf (stderr, "word:");
1103 break;
1105 case TOKEN_STRING:
1106 fprintf (stderr, "string:");
1107 break;
1109 case TOKEN_MACDEF:
1110 fprintf (stderr, "macro: %p\n", TOKEN_DATA_FUNC (td));
1111 break;
1113 case TOKEN_EOF:
1114 fprintf (stderr, "eof\n");
1115 break;
1117 fprintf (stderr, "\t\"%s\"\n", TOKEN_DATA_TEXT (td));
1120 static void M4_GNUC_UNUSED
1121 lex_debug (void)
1123 token_type t;
1124 token_data td;
1126 while ((t = next_token (&td)) != TOKEN_EOF)
1127 print_token ("lex", t, &td);
1129 #endif