1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <libpspp/message.h>
27 #include <libpspp/assertion.h>
28 #include <language/command.h>
29 #include <libpspp/message.h>
30 #include <data/settings.h>
31 #include <libpspp/getl.h>
32 #include <libpspp/str.h>
33 #include <output/journal.h>
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
48 struct string line_buffer;
50 struct source_stream *ss;
52 int token; /* Current token. */
53 double tokval; /* T_POS_NUM, T_NEG_NUM: the token's value. */
55 char tokid [VAR_NAME_LEN + 1]; /* T_ID: the identifier. */
57 struct string tokstr; /* T_ID, T_STRING: token string value.
58 For T_ID, this is not truncated as is
61 char *prog; /* Pointer to next token in line_buffer. */
62 bool dot; /* True only if this line ends with a terminal dot. */
64 int put_token ; /* If nonzero, next token returned by lex_get().
65 Used only in exceptional circumstances. */
67 struct string put_tokstr;
72 static int parse_id (struct lexer *);
74 /* How a string represents its contents. */
77 CHARACTER_STRING, /* Characters. */
78 BINARY_STRING, /* Binary digits. */
79 OCTAL_STRING, /* Octal digits. */
80 HEX_STRING /* Hexadecimal digits. */
83 static int parse_string (struct lexer *, enum string_type);
86 static void dump_token (struct lexer *);
91 /* Initializes the lexer. */
93 lex_create (struct source_stream *ss)
95 struct lexer *lexer = xzalloc (sizeof (*lexer));
97 ds_init_empty (&lexer->tokstr);
98 ds_init_empty (&lexer->put_tokstr);
99 ds_init_empty (&lexer->line_buffer);
105 struct source_stream *
106 lex_get_source_stream (const struct lexer *lex)
112 lex_current_syntax_mode (const struct lexer *lex)
114 return source_stream_current_syntax_mode (lex->ss);
118 lex_current_error_mode (const struct lexer *lex)
120 return source_stream_current_error_mode (lex->ss);
125 lex_destroy (struct lexer *lexer)
129 ds_destroy (&lexer->put_tokstr);
130 ds_destroy (&lexer->tokstr);
131 ds_destroy (&lexer->line_buffer);
138 /* Common functions. */
140 /* Copies put_token, lexer->put_tokstr, put_tokval into token, tokstr,
141 tokval, respectively, and sets tokid appropriately. */
143 restore_token (struct lexer *lexer)
145 assert (lexer->put_token != 0);
146 lexer->token = lexer->put_token;
147 ds_assign_string (&lexer->tokstr, &lexer->put_tokstr);
148 str_copy_trunc (lexer->tokid, sizeof lexer->tokid, ds_cstr (&lexer->tokstr));
149 lexer->tokval = lexer->put_tokval;
150 lexer->put_token = 0;
153 /* Copies token, tokstr, lexer->tokval into lexer->put_token, put_tokstr,
154 put_lexer->tokval respectively. */
156 save_token (struct lexer *lexer)
158 lexer->put_token = lexer->token;
159 ds_assign_string (&lexer->put_tokstr, &lexer->tokstr);
160 lexer->put_tokval = lexer->tokval;
163 /* Parses a single token, setting appropriate global variables to
164 indicate the token's attributes. */
166 lex_get (struct lexer *lexer)
171 if (NULL == lexer->prog && ! lex_get_line (lexer) )
173 lexer->token = T_STOP;
177 /* If a token was pushed ahead, return it. */
178 if (lexer->put_token)
180 restore_token (lexer);
189 /* Skip whitespace. */
190 while (isspace ((unsigned char) *lexer->prog))
205 else if (!lex_get_line (lexer))
208 lexer->token = T_STOP;
215 if (lexer->put_token)
217 restore_token (lexer);
226 /* Actually parse the token. */
227 ds_clear (&lexer->tokstr);
229 switch (*lexer->prog)
232 case '0': case '1': case '2': case '3': case '4':
233 case '5': case '6': case '7': case '8': case '9':
237 /* `-' can introduce a negative number, or it can be a
238 token by itself. If it is not followed by a digit or a
239 decimal point, it is definitely not a number.
240 Otherwise, it might be either, but most of the time we
241 want it as a number. When the syntax calls for a `-'
242 token, lex_negative_to_dash() must be used to break
243 negative numbers into two tokens. */
244 if (*lexer->prog == '-')
246 ds_put_char (&lexer->tokstr, *lexer->prog++);
247 while (isspace ((unsigned char) *lexer->prog))
250 if (!isdigit ((unsigned char) *lexer->prog) && *lexer->prog != '.')
255 lexer->token = T_NEG_NUM;
258 lexer->token = T_POS_NUM;
260 /* Parse the number, copying it into tokstr. */
261 while (isdigit ((unsigned char) *lexer->prog))
262 ds_put_char (&lexer->tokstr, *lexer->prog++);
263 if (*lexer->prog == '.')
265 ds_put_char (&lexer->tokstr, *lexer->prog++);
266 while (isdigit ((unsigned char) *lexer->prog))
267 ds_put_char (&lexer->tokstr, *lexer->prog++);
269 if (*lexer->prog == 'e' || *lexer->prog == 'E')
271 ds_put_char (&lexer->tokstr, *lexer->prog++);
272 if (*lexer->prog == '+' || *lexer->prog == '-')
273 ds_put_char (&lexer->tokstr, *lexer->prog++);
274 while (isdigit ((unsigned char) *lexer->prog))
275 ds_put_char (&lexer->tokstr, *lexer->prog++);
278 /* Parse as floating point. */
279 lexer->tokval = strtod (ds_cstr (&lexer->tokstr), &tail);
282 msg (SE, _("%s does not form a valid number."),
283 ds_cstr (&lexer->tokstr));
286 ds_clear (&lexer->tokstr);
287 ds_put_char (&lexer->tokstr, '0');
294 lexer->token = parse_string (lexer, CHARACTER_STRING);
297 case '(': case ')': case ',': case '=': case '+': case '/':
299 lexer->token = *lexer->prog++;
303 if (*++lexer->prog == '*')
306 lexer->token = T_EXP;
313 if (*++lexer->prog == '=')
318 else if (*lexer->prog == '>')
328 if (*++lexer->prog == '=')
338 if (*++lexer->prog == '=')
344 lexer->token = T_NOT;
349 lexer->token = T_AND;
358 if (lexer->prog[1] == '\'' || lexer->prog[1] == '"')
359 lexer->token = parse_string (lexer, BINARY_STRING);
361 lexer->token = parse_id (lexer);
365 if (lexer->prog[1] == '\'' || lexer->prog[1] == '"')
366 lexer->token = parse_string (lexer, OCTAL_STRING);
368 lexer->token = parse_id (lexer);
372 if (lexer->prog[1] == '\'' || lexer->prog[1] == '"')
373 lexer->token = parse_string (lexer, HEX_STRING);
375 lexer->token = parse_id (lexer);
379 if (lex_is_id1 (*lexer->prog))
381 lexer->token = parse_id (lexer);
386 if (isgraph ((unsigned char) *lexer->prog))
387 msg (SE, _("Bad character in input: `%c'."), *lexer->prog++);
389 msg (SE, _("Bad character in input: `\\%o'."), *lexer->prog++);
401 /* Parses an identifier at the current position into tokid and
403 Returns the correct token type. */
405 parse_id (struct lexer *lexer)
407 struct substring rest_of_line
408 = ss_substr (ds_ss (&lexer->line_buffer),
409 ds_pointer_to_position (&lexer->line_buffer, lexer->prog),
411 struct substring id = ss_head (rest_of_line,
412 lex_id_get_length (rest_of_line));
413 lexer->prog += ss_length (id);
415 ds_assign_substring (&lexer->tokstr, id);
416 str_copy_trunc (lexer->tokid, sizeof lexer->tokid, ds_cstr (&lexer->tokstr));
417 return lex_id_to_token (id);
420 /* Reports an error to the effect that subcommand SBC may only be
423 lex_sbc_only_once (const char *sbc)
425 msg (SE, _("Subcommand %s may only be specified once."), sbc);
428 /* Reports an error to the effect that subcommand SBC is
431 lex_sbc_missing (struct lexer *lexer, const char *sbc)
433 lex_error (lexer, _("missing required subcommand %s"), sbc);
436 /* Prints a syntax error message containing the current token and
437 given message MESSAGE (if non-null). */
439 lex_error (struct lexer *lexer, const char *message, ...)
444 token_rep = lex_token_representation (lexer);
445 if (lexer->token == T_STOP)
446 strcpy (where, "end of file");
447 else if (lexer->token == '.')
448 strcpy (where, "end of command");
450 snprintf (where, sizeof where, "`%s'", token_rep);
458 va_start (args, message);
459 vsnprintf (buf, 1024, message, args);
462 msg (SE, _("Syntax error %s at %s."), buf, where);
465 msg (SE, _("Syntax error at %s."), where);
468 /* Checks that we're at end of command.
469 If so, returns a successful command completion code.
470 If not, flags a syntax error and returns an error command
473 lex_end_of_command (struct lexer *lexer)
475 if (lexer->token != '.')
477 lex_error (lexer, _("expecting end of command"));
484 /* Token testing functions. */
486 /* Returns true if the current token is a number. */
488 lex_is_number (struct lexer *lexer)
490 return lexer->token == T_POS_NUM || lexer->token == T_NEG_NUM;
494 /* Returns true if the current token is a string. */
496 lex_is_string (struct lexer *lexer)
498 return lexer->token == T_STRING;
502 /* Returns the value of the current token, which must be a
503 floating point number. */
505 lex_number (struct lexer *lexer)
507 assert (lex_is_number (lexer));
508 return lexer->tokval;
511 /* Returns true iff the current token is an integer. */
513 lex_is_integer (struct lexer *lexer)
515 return (lex_is_number (lexer)
516 && lexer->tokval > LONG_MIN
517 && lexer->tokval <= LONG_MAX
518 && floor (lexer->tokval) == lexer->tokval);
521 /* Returns the value of the current token, which must be an
524 lex_integer (struct lexer *lexer)
526 assert (lex_is_integer (lexer));
527 return lexer->tokval;
530 /* Token matching functions. */
532 /* If TOK is the current token, skips it and returns true
533 Otherwise, returns false. */
535 lex_match (struct lexer *lexer, int t)
537 if (lexer->token == t)
546 /* If the current token is the identifier S, skips it and returns
547 true. The identifier may be abbreviated to its first three
549 Otherwise, returns false. */
551 lex_match_id (struct lexer *lexer, const char *s)
553 return lex_match_id_n (lexer, s, 3);
556 /* If the current token is the identifier S, skips it and returns
557 true. The identifier may be abbreviated to its first N
559 Otherwise, returns false. */
561 lex_match_id_n (struct lexer *lexer, const char *s, size_t n)
563 if (lexer->token == T_ID
564 && lex_id_match_n (ss_cstr (s), ss_cstr (lexer->tokid), n))
573 /* If the current token is integer N, skips it and returns true.
574 Otherwise, returns false. */
576 lex_match_int (struct lexer *lexer, int x)
578 if (lex_is_integer (lexer) && lex_integer (lexer) == x)
587 /* Forced matches. */
589 /* If this token is identifier S, fetches the next token and returns
591 Otherwise, reports an error and returns zero. */
593 lex_force_match_id (struct lexer *lexer, const char *s)
595 if (lex_match_id (lexer, s))
599 lex_error (lexer, _("expecting `%s'"), s);
604 /* If the current token is T, skips the token. Otherwise, reports an
605 error and returns from the current function with return value false. */
607 lex_force_match (struct lexer *lexer, int t)
609 if (lexer->token == t)
616 lex_error (lexer, _("expecting `%s'"), lex_token_name (t));
621 /* If this token is a string, does nothing and returns true.
622 Otherwise, reports an error and returns false. */
624 lex_force_string (struct lexer *lexer)
626 if (lexer->token == T_STRING)
630 lex_error (lexer, _("expecting string"));
635 /* If this token is an integer, does nothing and returns true.
636 Otherwise, reports an error and returns false. */
638 lex_force_int (struct lexer *lexer)
640 if (lex_is_integer (lexer))
644 lex_error (lexer, _("expecting integer"));
649 /* If this token is a number, does nothing and returns true.
650 Otherwise, reports an error and returns false. */
652 lex_force_num (struct lexer *lexer)
654 if (lex_is_number (lexer))
657 lex_error (lexer, _("expecting number"));
661 /* If this token is an identifier, does nothing and returns true.
662 Otherwise, reports an error and returns false. */
664 lex_force_id (struct lexer *lexer)
666 if (lexer->token == T_ID)
669 lex_error (lexer, _("expecting identifier"));
673 /* Weird token functions. */
675 /* Returns the first character of the next token, except that if the
676 next token is not an identifier, the character returned will not be
677 a character that can begin an identifier. Specifically, the
678 hexstring lead-in X' causes lookahead() to return '. Note that an
679 alphanumeric return value doesn't guarantee an ID token, it could
680 also be a reserved-word token. */
682 lex_look_ahead (struct lexer *lexer)
684 if (lexer->put_token)
685 return lexer->put_token;
689 if (NULL == lexer->prog && ! lex_get_line (lexer) )
694 while (isspace ((unsigned char) *lexer->prog))
701 else if (!lex_get_line (lexer))
704 if (lexer->put_token)
705 return lexer->put_token;
708 if ((toupper ((unsigned char) *lexer->prog) == 'X'
709 || toupper ((unsigned char) *lexer->prog) == 'B'
710 || toupper ((unsigned char) *lexer->prog) == 'O')
711 && (lexer->prog[1] == '\'' || lexer->prog[1] == '"'))
718 /* Makes the current token become the next token to be read; the
719 current token is set to T. */
721 lex_put_back (struct lexer *lexer, int t)
727 /* Makes the current token become the next token to be read; the
728 current token is set to the identifier ID. */
730 lex_put_back_id (struct lexer *lexer, const char *id)
732 assert (lex_id_to_token (ss_cstr (id)) == T_ID);
735 ds_assign_cstr (&lexer->tokstr, id);
736 str_copy_trunc (lexer->tokid, sizeof lexer->tokid, ds_cstr (&lexer->tokstr));
739 /* Weird line processing functions. */
741 /* Returns the entire contents of the current line. */
743 lex_entire_line (const struct lexer *lexer)
745 return ds_cstr (&lexer->line_buffer);
748 const struct string *
749 lex_entire_line_ds (const struct lexer *lexer)
751 return &lexer->line_buffer;
754 /* As lex_entire_line(), but only returns the part of the current line
755 that hasn't already been tokenized. */
757 lex_rest_of_line (const struct lexer *lexer)
762 /* Returns true if the current line ends in a terminal dot,
765 lex_end_dot (const struct lexer *lexer)
770 /* Causes the rest of the current input line to be ignored for
771 tokenization purposes. */
773 lex_discard_line (struct lexer *lexer)
775 ds_cstr (&lexer->line_buffer); /* Ensures ds_end points to something valid */
776 lexer->prog = ds_end (&lexer->line_buffer);
778 lexer->put_token = 0;
782 /* Discards the rest of the current command.
783 When we're reading commands from a file, we skip tokens until
784 a terminal dot or EOF.
785 When we're reading commands interactively from the user,
786 that's just discarding the current line, because presumably
787 the user doesn't want to finish typing a command that will be
790 lex_discard_rest_of_command (struct lexer *lexer)
792 if (!getl_is_interactive (lexer->ss))
794 while (lexer->token != T_STOP && lexer->token != '.')
798 lex_discard_line (lexer);
801 /* Weird line reading functions. */
803 /* Remove C-style comments in STRING, begun by slash-star and
804 terminated by star-slash or newline. */
806 strip_comments (struct string *string)
814 for (cp = ds_cstr (string); *cp; )
816 /* If we're not in a comment, check for quote marks. */
821 else if (*cp == '\'' || *cp == '"')
825 /* If we're not inside a quotation, check for comment. */
828 if (cp[0] == '/' && cp[1] == '*')
835 else if (in_comment && cp[0] == '*' && cp[1] == '/')
844 /* Check commenting. */
851 /* Prepares LINE, which is subject to the given SYNTAX rules, for
852 tokenization by stripping comments and determining whether it
853 is the beginning or end of a command and storing into
854 *LINE_STARTS_COMMAND and *LINE_ENDS_COMMAND appropriately. */
856 lex_preprocess_line (struct string *line,
857 enum syntax_mode syntax,
858 bool *line_starts_command,
859 bool *line_ends_command)
861 strip_comments (line);
862 ds_rtrim (line, ss_cstr (CC_SPACES));
863 *line_ends_command = (ds_chomp (line, settings_get_endcmd ())
864 || (ds_is_empty (line) && settings_get_nulline ()));
865 *line_starts_command = false;
866 if (syntax == GETL_BATCH)
868 int first = ds_first (line);
869 *line_starts_command = !isspace (first);
870 if (first == '+' || first == '-')
871 *ds_data (line) = ' ';
875 /* Reads a line, without performing any preprocessing.
876 Sets *SYNTAX, if SYNTAX is non-null, to the line's syntax
879 lex_get_line_raw (struct lexer *lexer)
881 bool ok = getl_read_line (lexer->ss, &lexer->line_buffer);
882 enum syntax_mode mode = lex_current_syntax_mode (lexer);
883 journal_write (mode == GETL_BATCH, ds_cstr (&lexer->line_buffer));
888 /* Reads a line for use by the tokenizer, and preprocesses it by
889 removing comments, stripping trailing whitespace and the
890 terminal dot, and removing leading indentors. */
892 lex_get_line (struct lexer *lexer)
894 bool line_starts_command;
896 if (!lex_get_line_raw (lexer))
902 lex_preprocess_line (&lexer->line_buffer,
903 lex_current_syntax_mode (lexer),
904 &line_starts_command, &lexer->dot);
906 if (line_starts_command)
907 lexer->put_token = '.';
909 lexer->prog = ds_cstr (&lexer->line_buffer);
915 /* Returns the name of a token. */
917 lex_token_name (int token)
919 if (lex_is_keyword (token))
920 return lex_id_name (token);
921 else if (token < 256)
923 static char t[256][2];
933 /* Returns an ASCII representation of the current token as a
934 malloc()'d string. */
936 lex_token_representation (struct lexer *lexer)
940 switch (lexer->token)
945 return ds_xstrdup (&lexer->tokstr);
953 for (sp = ds_cstr (&lexer->tokstr); sp < ds_end (&lexer->tokstr); sp++)
954 if (!isprint ((unsigned char) *sp))
960 token_rep = xmalloc (2 + ds_length (&lexer->tokstr) * 2 + 1 + 1);
968 for (sp = ds_cstr (&lexer->tokstr); *sp; )
972 *dp++ = (unsigned char) *sp++;
975 for (sp = ds_cstr (&lexer->tokstr); sp < ds_end (&lexer->tokstr); sp++)
977 *dp++ = (((unsigned char) *sp) >> 4)["0123456789ABCDEF"];
978 *dp++ = (((unsigned char) *sp) & 15)["0123456789ABCDEF"];
988 token_rep = xmalloc (1);
993 return xstrdup ("**");
996 return xstrdup (lex_token_name (lexer->token));
1002 /* Really weird functions. */
1004 /* Most of the time, a `-' is a lead-in to a negative number. But
1005 sometimes it's actually part of the syntax. If a dash can be part
1006 of syntax then this function is called to rip it off of a
1009 lex_negative_to_dash (struct lexer *lexer)
1011 if (lexer->token == T_NEG_NUM)
1013 lexer->token = T_POS_NUM;
1014 lexer->tokval = -lexer->tokval;
1015 ds_assign_substring (&lexer->tokstr, ds_substr (&lexer->tokstr, 1, SIZE_MAX));
1021 /* Skip a COMMENT command. */
1023 lex_skip_comment (struct lexer *lexer)
1027 if (!lex_get_line (lexer))
1029 lexer->put_token = T_STOP;
1034 if (lexer->put_token == '.')
1037 ds_cstr (&lexer->line_buffer); /* Ensures ds_end will point to a valid char */
1038 lexer->prog = ds_end (&lexer->line_buffer);
1044 /* Private functions. */
1046 /* When invoked, tokstr contains a string of binary, octal, or
1047 hex digits, according to TYPE. The string is converted to
1048 characters having the specified values. */
1050 convert_numeric_string_to_char_string (struct lexer *lexer,
1051 enum string_type type)
1053 const char *base_name;
1063 base_name = _("binary");
1068 base_name = _("octal");
1073 base_name = _("hex");
1081 byte_cnt = ds_length (&lexer->tokstr) / chars_per_byte;
1082 if (ds_length (&lexer->tokstr) % chars_per_byte)
1083 msg (SE, _("String of %s digits has %zu characters, which is not a "
1085 base_name, ds_length (&lexer->tokstr), chars_per_byte);
1087 p = ds_cstr (&lexer->tokstr);
1088 for (i = 0; i < byte_cnt; i++)
1094 for (j = 0; j < chars_per_byte; j++, p++)
1098 if (*p >= '0' && *p <= '9')
1102 static const char alpha[] = "abcdef";
1103 const char *q = strchr (alpha, tolower ((unsigned char) *p));
1112 msg (SE, _("`%c' is not a valid %s digit."), *p, base_name);
1114 value = value * base + v;
1117 ds_cstr (&lexer->tokstr)[i] = (unsigned char) value;
1120 ds_truncate (&lexer->tokstr, byte_cnt);
1123 /* Parses a string from the input buffer into tokstr. The input
1124 buffer pointer lexer->prog must point to the initial single or double
1125 quote. TYPE indicates the type of string to be parsed.
1126 Returns token type. */
1128 parse_string (struct lexer *lexer, enum string_type type)
1130 if (type != CHARACTER_STRING)
1133 /* Accumulate the entire string, joining sections indicated by +
1137 /* Single or double quote. */
1138 int c = *lexer->prog++;
1140 /* Accumulate section. */
1143 /* Check end of line. */
1144 if (*lexer->prog == '\0')
1146 msg (SE, _("Unterminated string constant."));
1150 /* Double quote characters to embed them in strings. */
1151 if (*lexer->prog == c)
1153 if (lexer->prog[1] == c)
1159 ds_put_char (&lexer->tokstr, *lexer->prog++);
1163 /* Skip whitespace after final quote mark. */
1164 if (lexer->prog == NULL)
1168 while (isspace ((unsigned char) *lexer->prog))
1176 if (!lex_get_line (lexer))
1180 /* Skip plus sign. */
1181 if (*lexer->prog != '+')
1185 /* Skip whitespace after plus sign. */
1186 if (lexer->prog == NULL)
1190 while (isspace ((unsigned char) *lexer->prog))
1198 if (!lex_get_line (lexer))
1200 msg (SE, _("Unexpected end of file in string concatenation."));
1205 /* Ensure that a valid string follows. */
1206 if (*lexer->prog != '\'' && *lexer->prog != '"')
1208 msg (SE, _("String expected following `+'."));
1213 /* We come here when we've finished concatenating all the string sections
1214 into one large string. */
1216 if (type != CHARACTER_STRING)
1217 convert_numeric_string_to_char_string (lexer, type);
1219 if (ds_length (&lexer->tokstr) > 255)
1221 msg (SE, _("String exceeds 255 characters in length (%zu characters)."),
1222 ds_length (&lexer->tokstr));
1223 ds_truncate (&lexer->tokstr, 255);
1230 /* Reads one token from the lexer and writes a textual representation
1231 on stdout for debugging purposes. */
1233 dump_token (struct lexer *lexer)
1239 curln = getl_source_location (lexer->ss);
1240 curfn = getl_source_name (lexer->ss);
1242 fprintf (stderr, "%s:%d\t", curfn, curln);
1245 switch (lexer->token)
1248 fprintf (stderr, "ID\t%s\n", lexer->tokid);
1253 fprintf (stderr, "NUM\t%f\n", lexer->tokval);
1257 fprintf (stderr, "STRING\t\"%s\"\n", ds_cstr (&lexer->tokstr));
1261 fprintf (stderr, "STOP\n");
1265 fprintf (stderr, "MISC\tEXP\"");
1269 fprintf (stderr, "MISC\tEOF\n");
1273 if (lex_is_keyword (lexer->token))
1274 fprintf (stderr, "KEYWORD\t%s\n", lex_token_name (lexer->token));
1276 fprintf (stderr, "PUNCT\t%c\n", lexer->token);
1280 #endif /* DUMP_TOKENS */
1283 /* Token Accessor Functions */
1286 lex_token (const struct lexer *lexer)
1288 return lexer->token;
1292 lex_tokval (const struct lexer *lexer)
1294 return lexer->tokval;
1298 lex_tokid (const struct lexer *lexer)
1300 return lexer->tokid;
1303 const struct string *
1304 lex_tokstr (const struct lexer *lexer)
1306 return &lexer->tokstr;
1309 /* If the lexer is positioned at the (pseudo)identifier S, which
1310 may contain a hyphen ('-'), skips it and returns true. Each
1311 half of the identifier may be abbreviated to its first three
1313 Otherwise, returns false. */
1315 lex_match_hyphenated_word (struct lexer *lexer, const char *s)
1317 const char *hyphen = strchr (s, '-');
1319 return lex_match_id (lexer, s);
1320 else if (lexer->token != T_ID
1321 || !lex_id_match (ss_buffer (s, hyphen - s), ss_cstr (lexer->tokid))
1322 || lex_look_ahead (lexer) != '-')
1327 lex_force_match (lexer, '-');
1328 lex_force_match_id (lexer, hyphen + 1);