X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Flexer%2Flexer.c;h=a409a83ee957647a7b5d9b5f8195732ac8e0a61c;hb=17753a2ef0a1e33b5478a970c747a2a57454ee07;hp=a8a6cffc1745a532ec38e045cf28448fbae85bc1;hpb=c708736bdd0fea4b79f3ee4a10e00c3abb95d9e3;p=pspp-builds.git diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index a8a6cffc..a409a83e 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -1,90 +1,71 @@ -/* PSPP - computes sample statistics. - Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. - Written by Ben Pfaff . +/* PSPP - a program for statistical analysis. + Copyright (C) 1997-9, 2000, 2006, 2009, 2010 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include + #include "lexer.h" -#include -#include +#include +#include #include #include #include #include +#include #include -#include -#include #include -#include -#include -#include #include +#include +#include +#include #include +#include +#include -#include "size_max.h" +#include "xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) #define N_(msgid) msgid -/* -#define DUMP_TOKENS 1 -*/ - - -/* Global variables. */ - -extern const char *keywords[T_N_KEYWORDS + 1]; - - -/* Current token. */ -int token; +struct lexer +{ + struct string line_buffer; -/* T_POS_NUM, T_NEG_NUM: the token's value. */ -double tokval; + struct source_stream *ss; -/* T_ID: the identifier. */ -char tokid[LONG_NAME_LEN + 1]; + int token; /* Current token. */ + double tokval; /* T_POS_NUM, T_NEG_NUM: the token's value. */ -/* T_ID, T_STRING: token string value. - For T_ID, this is not truncated as is tokid. */ -struct string tokstr; - -/* Static variables. */ + struct string tokstr; /* T_ID, T_STRING: token string value. */ -/* Pointer to next token in getl_buf. */ -static char *prog; + char *prog; /* Pointer to next token in line_buffer. */ + bool dot; /* True only if this line ends with a terminal dot. */ -/* True only if this line ends with a terminal dot. */ -static bool dot; + int put_token ; /* If nonzero, next token returned by lex_get(). + Used only in exceptional circumstances. */ -/* True only if the last token returned was T_STOP. */ -static bool eof; + struct string put_tokstr; + double put_tokval; +}; -/* If nonzero, next token returned by lex_get(). - Used only in exceptional circumstances. */ -static int put_token; -static struct string put_tokstr; -static double put_tokval; -static int parse_id (void); +static int parse_id (struct lexer *); /* How a string represents its contents. */ -enum string_type +enum string_type { CHARACTER_STRING, /* Characters. */ BINARY_STRING, /* Binary digits. */ @@ -92,123 +73,136 @@ enum string_type HEX_STRING /* Hexadecimal digits. */ }; -static int parse_string (enum string_type); - -#if DUMP_TOKENS -static void dump_token (void); -#endif +static int parse_string (struct lexer *, enum string_type); /* Initialization. */ /* Initializes the lexer. */ -void -lex_init (void) +struct lexer * +lex_create (struct source_stream *ss) { - ds_init_empty (&tokstr); - ds_init_empty (&put_tokstr); - if (!lex_get_line ()) - eof = true; + struct lexer *lexer = xzalloc (sizeof (*lexer)); + + ds_init_empty (&lexer->tokstr); + ds_init_empty (&lexer->put_tokstr); + ds_init_empty (&lexer->line_buffer); + lexer->ss = ss; + + return lexer; } +struct source_stream * +lex_get_source_stream (const struct lexer *lex) +{ + return lex->ss; +} + +enum syntax_mode +lex_current_syntax_mode (const struct lexer *lex) +{ + return source_stream_current_syntax_mode (lex->ss); +} + +enum error_mode +lex_current_error_mode (const struct lexer *lex) +{ + return source_stream_current_error_mode (lex->ss); +} + + void -lex_done (void) +lex_destroy (struct lexer *lexer) { - ds_destroy (&put_tokstr); - ds_destroy (&tokstr); + if ( NULL != lexer ) + { + ds_destroy (&lexer->put_tokstr); + ds_destroy (&lexer->tokstr); + ds_destroy (&lexer->line_buffer); + + free (lexer); + } } /* Common functions. */ -/* Copies put_token, put_tokstr, put_tokval into token, tokstr, +/* Copies put_token, lexer->put_tokstr, put_tokval into token, tokstr, tokval, respectively, and sets tokid appropriately. */ static void -restore_token (void) +restore_token (struct lexer *lexer) { - assert (put_token != 0); - token = put_token; - ds_assign_string (&tokstr, &put_tokstr); - str_copy_trunc (tokid, sizeof tokid, ds_cstr (&tokstr)); - tokval = put_tokval; - put_token = 0; + assert (lexer->put_token != 0); + lexer->token = lexer->put_token; + ds_assign_string (&lexer->tokstr, &lexer->put_tokstr); + lexer->tokval = lexer->put_tokval; + lexer->put_token = 0; } -/* Copies token, tokstr, tokval into put_token, put_tokstr, - put_tokval respectively. */ +/* Copies token, tokstr, lexer->tokval into lexer->put_token, put_tokstr, + put_lexer->tokval respectively. */ static void -save_token (void) +save_token (struct lexer *lexer) { - put_token = token; - ds_assign_string (&put_tokstr, &tokstr); - put_tokval = tokval; + lexer->put_token = lexer->token; + ds_assign_string (&lexer->put_tokstr, &lexer->tokstr); + lexer->put_tokval = lexer->tokval; } /* Parses a single token, setting appropriate global variables to indicate the token's attributes. */ void -lex_get (void) +lex_get (struct lexer *lexer) { - /* If a token was pushed ahead, return it. */ - if (put_token) - { - restore_token (); -#if DUMP_TOKENS - dump_token (); -#endif - return; - } - /* Find a token. */ for (;;) { - /* Skip whitespace. */ - if (eof) + if (NULL == lexer->prog && ! lex_get_line (lexer) ) + { + lexer->token = T_STOP; + return; + } + + /* If a token was pushed ahead, return it. */ + if (lexer->put_token) { - token = T_STOP; + restore_token (lexer); return; } for (;;) - { - while (isspace ((unsigned char) *prog)) - prog++; - if (*prog) + { + /* Skip whitespace. */ + while (c_isspace ((unsigned char) *lexer->prog)) + lexer->prog++; + + if (*lexer->prog) break; - if (dot) + if (lexer->dot) { - dot = 0; - token = '.'; -#if DUMP_TOKENS - dump_token (); -#endif + lexer->dot = 0; + lexer->token = T_ENDCMD; return; } - else if (!lex_get_line ()) + else if (!lex_get_line (lexer)) { - eof = true; - token = T_STOP; -#if DUMP_TOKENS - dump_token (); -#endif + lexer->prog = NULL; + lexer->token = T_STOP; return; } - if (put_token) + if (lexer->put_token) { - restore_token (); -#if DUMP_TOKENS - dump_token (); -#endif + restore_token (lexer); return; } } /* Actually parse the token. */ - ds_clear (&tokstr); - - switch (*prog) + ds_clear (&lexer->tokstr); + + switch (*lexer->prog) { case '-': case '.': case '0': case '1': case '2': case '3': case '4': @@ -216,187 +210,217 @@ lex_get (void) { char *tail; - /* `-' can introduce a negative number, or it can be a - token by itself. If it is not followed by a digit or a - decimal point, it is definitely not a number. - Otherwise, it might be either, but most of the time we - want it as a number. When the syntax calls for a `-' - token, lex_negative_to_dash() must be used to break - negative numbers into two tokens. */ - if (*prog == '-') + /* `-' can introduce a negative number, or it can be a token by + itself. */ + if (*lexer->prog == '-') { - ds_put_char (&tokstr, *prog++); - while (isspace ((unsigned char) *prog)) - prog++; + ds_put_byte (&lexer->tokstr, *lexer->prog++); + while (c_isspace ((unsigned char) *lexer->prog)) + lexer->prog++; - if (!isdigit ((unsigned char) *prog) && *prog != '.') + if (!c_isdigit ((unsigned char) *lexer->prog) && *lexer->prog != '.') { - token = '-'; + lexer->token = T_DASH; break; } - token = T_NEG_NUM; + lexer->token = T_NEG_NUM; } - else - token = T_POS_NUM; - + else + lexer->token = T_POS_NUM; + /* Parse the number, copying it into tokstr. */ - while (isdigit ((unsigned char) *prog)) - ds_put_char (&tokstr, *prog++); - if (*prog == '.') + while (c_isdigit ((unsigned char) *lexer->prog)) + ds_put_byte (&lexer->tokstr, *lexer->prog++); + if (*lexer->prog == '.') { - ds_put_char (&tokstr, *prog++); - while (isdigit ((unsigned char) *prog)) - ds_put_char (&tokstr, *prog++); + ds_put_byte (&lexer->tokstr, *lexer->prog++); + while (c_isdigit ((unsigned char) *lexer->prog)) + ds_put_byte (&lexer->tokstr, *lexer->prog++); } - if (*prog == 'e' || *prog == 'E') + if (*lexer->prog == 'e' || *lexer->prog == 'E') { - ds_put_char (&tokstr, *prog++); - if (*prog == '+' || *prog == '-') - ds_put_char (&tokstr, *prog++); - while (isdigit ((unsigned char) *prog)) - ds_put_char (&tokstr, *prog++); + ds_put_byte (&lexer->tokstr, *lexer->prog++); + if (*lexer->prog == '+' || *lexer->prog == '-') + ds_put_byte (&lexer->tokstr, *lexer->prog++); + while (c_isdigit ((unsigned char) *lexer->prog)) + ds_put_byte (&lexer->tokstr, *lexer->prog++); } /* Parse as floating point. */ - tokval = strtod (ds_cstr (&tokstr), &tail); + lexer->tokval = c_strtod (ds_cstr (&lexer->tokstr), &tail); if (*tail) { msg (SE, _("%s does not form a valid number."), - ds_cstr (&tokstr)); - tokval = 0.0; + ds_cstr (&lexer->tokstr)); + lexer->tokval = 0.0; - ds_clear (&tokstr); - ds_put_char (&tokstr, '0'); + ds_clear (&lexer->tokstr); + ds_put_byte (&lexer->tokstr, '0'); } break; } case '\'': case '"': - token = parse_string (CHARACTER_STRING); + lexer->token = parse_string (lexer, CHARACTER_STRING); break; - case '(': case ')': case ',': case '=': case '+': case '/': - token = *prog++; - break; + case '+': + lexer->token = T_PLUS; + lexer->prog++; + break; + + case '/': + lexer->token = T_SLASH; + lexer->prog++; + break; + + case '=': + lexer->token = T_EQUALS; + lexer->prog++; + break; + + case '(': + lexer->token = T_LPAREN; + lexer->prog++; + break; + + case ')': + lexer->token = T_RPAREN; + lexer->prog++; + break; + + case '[': + lexer->token = T_LBRACK; + lexer->prog++; + break; + + case ']': + lexer->token = T_RBRACK; + lexer->prog++; + break; + + case ',': + lexer->token = T_COMMA; + lexer->prog++; + break; case '*': - if (*++prog == '*') + if (*++lexer->prog == '*') { - prog++; - token = T_EXP; + lexer->prog++; + lexer->token = T_EXP; } else - token = '*'; + lexer->token = T_ASTERISK; break; case '<': - if (*++prog == '=') + if (*++lexer->prog == '=') { - prog++; - token = T_LE; + lexer->prog++; + lexer->token = T_LE; } - else if (*prog == '>') + else if (*lexer->prog == '>') { - prog++; - token = T_NE; + lexer->prog++; + lexer->token = T_NE; } else - token = T_LT; + lexer->token = T_LT; break; case '>': - if (*++prog == '=') + if (*++lexer->prog == '=') { - prog++; - token = T_GE; + lexer->prog++; + lexer->token = T_GE; } else - token = T_GT; + lexer->token = T_GT; break; case '~': - if (*++prog == '=') + if (*++lexer->prog == '=') { - prog++; - token = T_NE; + lexer->prog++; + lexer->token = T_NE; } else - token = T_NOT; + lexer->token = T_NOT; break; case '&': - prog++; - token = T_AND; + lexer->prog++; + lexer->token = T_AND; break; case '|': - prog++; - token = T_OR; + lexer->prog++; + lexer->token = T_OR; break; case 'b': case 'B': - if (prog[1] == '\'' || prog[1] == '"') - token = parse_string (BINARY_STRING); + if (lexer->prog[1] == '\'' || lexer->prog[1] == '"') + lexer->token = parse_string (lexer, BINARY_STRING); else - token = parse_id (); + lexer->token = parse_id (lexer); break; - + case 'o': case 'O': - if (prog[1] == '\'' || prog[1] == '"') - token = parse_string (OCTAL_STRING); + if (lexer->prog[1] == '\'' || lexer->prog[1] == '"') + lexer->token = parse_string (lexer, OCTAL_STRING); else - token = parse_id (); + lexer->token = parse_id (lexer); break; - + case 'x': case 'X': - if (prog[1] == '\'' || prog[1] == '"') - token = parse_string (HEX_STRING); + if (lexer->prog[1] == '\'' || lexer->prog[1] == '"') + lexer->token = parse_string (lexer, HEX_STRING); else - token = parse_id (); + lexer->token = parse_id (lexer); break; - + default: - if (lex_is_id1 (*prog)) + if (lex_is_id1 (*lexer->prog)) { - token = parse_id (); - break; + lexer->token = parse_id (lexer); + break; } else { - if (isgraph ((unsigned char) *prog)) - msg (SE, _("Bad character in input: `%c'."), *prog++); - else - msg (SE, _("Bad character in input: `\\%o'."), *prog++); - continue; + unsigned char c = *lexer->prog++; + char *c_name = xasprintf (c_isgraph (c) ? "%c" : "\\%o", c); + msg (SE, _("Bad character in input: `%s'."), c_name); + free (c_name); + continue; } } break; } - -#if DUMP_TOKENS - dump_token (); -#endif } -/* Parses an identifier at the current position into tokid and - tokstr. +/* Parses an identifier at the current position into tokstr. Returns the correct token type. */ static int -parse_id (void) +parse_id (struct lexer *lexer) { - const char *start = prog; - prog = lex_skip_identifier (start); - - ds_put_substring (&tokstr, ss_buffer (start, prog - start)); - str_copy_trunc (tokid, sizeof tokid, ds_cstr (&tokstr)); - return lex_id_to_token (ds_cstr (&tokstr), ds_length (&tokstr)); + struct substring rest_of_line + = ss_substr (ds_ss (&lexer->line_buffer), + ds_pointer_to_position (&lexer->line_buffer, lexer->prog), + SIZE_MAX); + struct substring id = ss_head (rest_of_line, + lex_id_get_length (rest_of_line)); + lexer->prog += ss_length (id); + + ds_assign_substring (&lexer->tokstr, id); + return lex_id_to_token (id); } /* Reports an error to the effect that subcommand SBC may only be specified once. */ void -lex_sbc_only_once (const char *sbc) +lex_sbc_only_once (const char *sbc) { msg (SE, _("Subcommand %s may only be specified once."), sbc); } @@ -404,41 +428,44 @@ lex_sbc_only_once (const char *sbc) /* Reports an error to the effect that subcommand SBC is missing. */ void -lex_sbc_missing (const char *sbc) +lex_sbc_missing (struct lexer *lexer, const char *sbc) { - lex_error (_("missing required subcommand %s"), sbc); + lex_error (lexer, _("missing required subcommand %s"), sbc); } /* Prints a syntax error message containing the current token and given message MESSAGE (if non-null). */ void -lex_error (const char *message, ...) +lex_error (struct lexer *lexer, const char *message, ...) { - char *token_rep; - char where[128]; + struct string s; - token_rep = lex_token_representation (); - if (token == T_STOP) - strcpy (where, "end of file"); - else if (token == '.') - strcpy (where, "end of command"); + ds_init_empty (&s); + + if (lexer->token == T_STOP) + ds_put_cstr (&s, _("Syntax error at end of file")); + else if (lexer->token == T_ENDCMD) + ds_put_cstr (&s, _("Syntax error at end of command")); else - snprintf (where, sizeof where, "`%s'", token_rep); - free (token_rep); + { + char *token_rep = lex_token_representation (lexer); + ds_put_format (&s, _("Syntax error at `%s'"), token_rep); + free (token_rep); + } if (message) { - char buf[1024]; va_list args; - + + ds_put_cstr (&s, ": "); + va_start (args, message); - vsnprintf (buf, 1024, message, args); + ds_put_vformat (&s, message, args); va_end (args); - - msg (SE, _("Syntax error %s at %s."), buf, where); } - else - msg (SE, _("Syntax error at %s."), where); + + msg (SE, "%s.", ds_cstr (&s)); + ds_destroy (&s); } /* Checks that we're at end of command. @@ -446,11 +473,11 @@ lex_error (const char *message, ...) If not, flags a syntax error and returns an error command completion code. */ int -lex_end_of_command (void) +lex_end_of_command (struct lexer *lexer) { - if (token != '.') + if (lexer->token != T_ENDCMD) { - lex_error (_("expecting end of command")); + lex_error (lexer, _("expecting end of command")); return CMD_FAILURE; } else @@ -461,50 +488,58 @@ lex_end_of_command (void) /* Returns true if the current token is a number. */ bool -lex_is_number (void) +lex_is_number (struct lexer *lexer) { - return token == T_POS_NUM || token == T_NEG_NUM; + return lexer->token == T_POS_NUM || lexer->token == T_NEG_NUM; } + +/* Returns true if the current token is a string. */ +bool +lex_is_string (struct lexer *lexer) +{ + return lexer->token == T_STRING; +} + + /* Returns the value of the current token, which must be a floating point number. */ double -lex_number (void) +lex_number (struct lexer *lexer) { - assert (lex_is_number ()); - return tokval; + assert (lex_is_number (lexer)); + return lexer->tokval; } /* Returns true iff the current token is an integer. */ bool -lex_is_integer (void) +lex_is_integer (struct lexer *lexer) { - return (lex_is_number () - && tokval != NOT_LONG - && tokval >= LONG_MIN - && tokval <= LONG_MAX - && floor (tokval) == tokval); + return (lex_is_number (lexer) + && lexer->tokval > LONG_MIN + && lexer->tokval <= LONG_MAX + && floor (lexer->tokval) == lexer->tokval); } /* Returns the value of the current token, which must be an integer. */ long -lex_integer (void) +lex_integer (struct lexer *lexer) { - assert (lex_is_integer ()); - return tokval; + assert (lex_is_integer (lexer)); + return lexer->tokval; } - + /* Token matching functions. */ /* If TOK is the current token, skips it and returns true Otherwise, returns false. */ bool -lex_match (int t) +lex_match (struct lexer *lexer, enum token_type t) { - if (token == t) + if (lexer->token == t) { - lex_get (); + lex_get (lexer); return true; } else @@ -516,11 +551,22 @@ lex_match (int t) letters. Otherwise, returns false. */ bool -lex_match_id (const char *s) +lex_match_id (struct lexer *lexer, const char *s) +{ + return lex_match_id_n (lexer, s, 3); +} + +/* If the current token is the identifier S, skips it and returns + true. The identifier may be abbreviated to its first N + letters. + Otherwise, returns false. */ +bool +lex_match_id_n (struct lexer *lexer, const char *s, size_t n) { - if (token == T_ID && lex_id_match (s, tokid)) + if (lexer->token == T_ID + && lex_id_match_n (ss_cstr (s), lex_tokss (lexer), n)) { - lex_get (); + lex_get (lexer); return true; } else @@ -530,11 +576,11 @@ lex_match_id (const char *s) /* If the current token is integer N, skips it and returns true. Otherwise, returns false. */ bool -lex_match_int (int x) +lex_match_int (struct lexer *lexer, int x) { - if (lex_is_integer () && lex_integer () == x) + if (lex_is_integer (lexer) && lex_integer (lexer) == x) { - lex_get (); + lex_get (lexer); return true; } else @@ -547,16 +593,13 @@ lex_match_int (int x) nonzero. Otherwise, reports an error and returns zero. */ bool -lex_force_match_id (const char *s) +lex_force_match_id (struct lexer *lexer, const char *s) { - if (token == T_ID && lex_id_match (s, tokid)) - { - lex_get (); - return true; - } + if (lex_match_id (lexer, s)) + return true; else { - lex_error (_("expecting `%s'"), s); + lex_error (lexer, _("expecting `%s'"), s); return false; } } @@ -564,16 +607,16 @@ lex_force_match_id (const char *s) /* If the current token is T, skips the token. Otherwise, reports an error and returns from the current function with return value false. */ bool -lex_force_match (int t) +lex_force_match (struct lexer *lexer, enum token_type t) { - if (token == t) + if (lexer->token == t) { - lex_get (); + lex_get (lexer); return true; } else { - lex_error (_("expecting `%s'"), lex_token_name (t)); + lex_error (lexer, _("expecting `%s'"), lex_token_name (t)); return false; } } @@ -581,13 +624,13 @@ lex_force_match (int t) /* If this token is a string, does nothing and returns true. Otherwise, reports an error and returns false. */ bool -lex_force_string (void) +lex_force_string (struct lexer *lexer) { - if (token == T_STRING) + if (lex_is_string (lexer)) return true; else { - lex_error (_("expecting string")); + lex_error (lexer, _("expecting string")); return false; } } @@ -595,149 +638,193 @@ lex_force_string (void) /* If this token is an integer, does nothing and returns true. Otherwise, reports an error and returns false. */ bool -lex_force_int (void) +lex_force_int (struct lexer *lexer) { - if (lex_is_integer ()) + if (lex_is_integer (lexer)) return true; else { - lex_error (_("expecting integer")); + lex_error (lexer, _("expecting integer")); return false; } } - + /* If this token is a number, does nothing and returns true. Otherwise, reports an error and returns false. */ bool -lex_force_num (void) +lex_force_num (struct lexer *lexer) { - if (lex_is_number ()) + if (lex_is_number (lexer)) return true; - else - { - lex_error (_("expecting number")); - return false; - } + + lex_error (lexer, _("expecting number")); + return false; } - + /* If this token is an identifier, does nothing and returns true. Otherwise, reports an error and returns false. */ bool -lex_force_id (void) +lex_force_id (struct lexer *lexer) { - if (token == T_ID) + if (lexer->token == T_ID) return true; - else - { - lex_error (_("expecting identifier")); - return false; - } + + lex_error (lexer, _("expecting identifier")); + return false; } + /* Weird token functions. */ -/* Returns the first character of the next token, except that if the - next token is not an identifier, the character returned will not be - a character that can begin an identifier. Specifically, the - hexstring lead-in X' causes lookahead() to return '. Note that an - alphanumeric return value doesn't guarantee an ID token, it could - also be a reserved-word token. */ -int -lex_look_ahead (void) +/* Returns the likely type of the next token, or 0 if it's hard to tell. */ +enum token_type +lex_look_ahead (struct lexer *lexer) { - if (put_token) - return put_token; + if (lexer->put_token) + return lexer->put_token; for (;;) { - if (eof) + if (NULL == lexer->prog && ! lex_get_line (lexer) ) return 0; for (;;) { - while (isspace ((unsigned char) *prog)) - prog++; - if (*prog) + while (c_isspace ((unsigned char) *lexer->prog)) + lexer->prog++; + if (*lexer->prog) break; - if (dot) - return '.'; - else if (!lex_get_line ()) + if (lexer->dot) + return T_ENDCMD; + else if (!lex_get_line (lexer)) return 0; - if (put_token) - return put_token; + if (lexer->put_token) + return lexer->put_token; } - if ((toupper ((unsigned char) *prog) == 'X' - || toupper ((unsigned char) *prog) == 'B' - || toupper ((unsigned char) *prog) == 'O') - && (prog[1] == '\'' || prog[1] == '"')) - return '\''; + switch (toupper ((unsigned char) *lexer->prog)) + { + case 'X': case 'B': case 'O': + if (lexer->prog[1] == '\'' || lexer->prog[1] == '"') + return T_STRING; + /* Fall through */ + + case '-': + return T_DASH; + + case '.': + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + return T_POS_NUM; + + case '\'': case '"': + return T_STRING; + + case '+': + return T_PLUS; + + case '/': + return T_SLASH; + + case '=': + return T_EQUALS; + + case '(': + return T_LPAREN; + + case ')': + return T_RPAREN; + + case '[': + return T_LBRACK; + + case ']': + return T_RBRACK; + + case ',': + return T_COMMA; + + case '*': + return lexer->prog[1] == '*' ? T_EXP : T_ASTERISK; + + case '<': + return (lexer->prog[1] == '=' ? T_LE + : lexer->prog[1] == '>' ? T_NE + : T_LT); + + case '>': + return lexer->prog[1] == '=' ? T_GE : T_GT; + + case '~': + return lexer->prog[1] == '=' ? T_NE : T_NOT; + + case '&': + return T_AND; - return *prog; + case '|': + return T_OR; + + default: + if (lex_is_id1 (*lexer->prog)) + return T_ID; + return 0; + } } } /* Makes the current token become the next token to be read; the current token is set to T. */ void -lex_put_back (int t) -{ - save_token (); - token = t; -} - -/* Makes the current token become the next token to be read; the - current token is set to the identifier ID. */ -void -lex_put_back_id (const char *id) +lex_put_back (struct lexer *lexer, enum token_type t) { - assert (lex_id_to_token (id, strlen (id)) == T_ID); - save_token (); - token = T_ID; - ds_assign_cstr (&tokstr, id); - str_copy_trunc (tokid, sizeof tokid, ds_cstr (&tokstr)); + save_token (lexer); + lexer->token = t; } /* Weird line processing functions. */ /* Returns the entire contents of the current line. */ const char * -lex_entire_line (void) +lex_entire_line (const struct lexer *lexer) { - return ds_cstr (&getl_buf); + return ds_cstr (&lexer->line_buffer); +} + +const struct string * +lex_entire_line_ds (const struct lexer *lexer) +{ + return &lexer->line_buffer; } /* As lex_entire_line(), but only returns the part of the current line - that hasn't already been tokenized. - If END_DOT is non-null, stores nonzero into *END_DOT if the line - ends with a terminal dot, or zero if it doesn't. */ + that hasn't already been tokenized. */ const char * -lex_rest_of_line (int *end_dot) +lex_rest_of_line (const struct lexer *lexer) { - if (end_dot) - *end_dot = dot; - return prog; + return lexer->prog; } -/* Causes the rest of the current input line to be ignored for - tokenization purposes. */ -void -lex_discard_line (void) +/* Returns true if the current line ends in a terminal dot, + false otherwise. */ +bool +lex_end_dot (const struct lexer *lexer) { - prog = ds_end (&getl_buf); - dot = false; - put_token = 0; + return lexer->dot; } -/* Sets the current position in the current line to P, which must be - in getl_buf. */ +/* Causes the rest of the current input line to be ignored for + tokenization purposes. */ void -lex_set_prog (char *p) +lex_discard_line (struct lexer *lexer) { - prog = p; + ds_cstr (&lexer->line_buffer); /* Ensures ds_end points to something valid */ + lexer->prog = ds_end (&lexer->line_buffer); + lexer->dot = false; + lexer->put_token = 0; } + /* Discards the rest of the current command. When we're reading commands from a file, we skip tokens until a terminal dot or EOF. @@ -746,15 +833,15 @@ lex_set_prog (char *p) the user doesn't want to finish typing a command that will be ignored anyway. */ void -lex_discard_rest_of_command (void) +lex_discard_rest_of_command (struct lexer *lexer) { - if (!getl_is_interactive ()) + if (!getl_is_interactive (lexer->ss)) { - while (token != T_STOP && token != '.') - lex_get (); + while (lexer->token != T_STOP && lexer->token != T_ENDCMD) + lex_get (lexer); } - else - lex_discard_line (); + else + lex_discard_line (lexer); } /* Weird line reading functions. */ @@ -780,7 +867,7 @@ strip_comments (struct string *string) else if (*cp == '\'' || *cp == '"') quote = *cp; } - + /* If we're not inside a quotation, check for comment. */ if (quote == EOF) { @@ -799,7 +886,7 @@ strip_comments (struct string *string) continue; } } - + /* Check commenting. */ if (in_comment) *cp = ' '; @@ -807,182 +894,242 @@ strip_comments (struct string *string) } } +/* Prepares LINE, which is subject to the given SYNTAX rules, for + tokenization by stripping comments and determining whether it + is the beginning or end of a command and storing into + *LINE_STARTS_COMMAND and *LINE_ENDS_COMMAND appropriately. */ +void +lex_preprocess_line (struct string *line, + enum syntax_mode syntax, + bool *line_starts_command, + bool *line_ends_command) +{ + strip_comments (line); + ds_rtrim (line, ss_cstr (CC_SPACES)); + *line_ends_command = ds_chomp (line, '.') || ds_is_empty (line); + *line_starts_command = false; + if (syntax == GETL_BATCH) + { + int first = ds_first (line); + *line_starts_command = !c_isspace (first); + if (first == '+' || first == '-') + *ds_data (line) = ' '; + } +} + +/* Reads a line, without performing any preprocessing. */ +bool +lex_get_line_raw (struct lexer *lexer) +{ + bool ok = getl_read_line (lexer->ss, &lexer->line_buffer); + if (ok) + { + const char *line = ds_cstr (&lexer->line_buffer); + text_item_submit (text_item_create (TEXT_ITEM_SYNTAX, line)); + } + else + lexer->prog = NULL; + return ok; +} + /* Reads a line for use by the tokenizer, and preprocesses it by removing comments, stripping trailing whitespace and the terminal dot, and removing leading indentors. */ bool -lex_get_line (void) +lex_get_line (struct lexer *lexer) { - struct string *line = &getl_buf; - bool interactive; + bool line_starts_command; - if (!getl_read_line (&interactive)) + if (!lex_get_line_raw (lexer)) return false; - strip_comments (line); - ds_rtrim (line, ss_cstr (CC_SPACES)); - - /* Check for and remove terminal dot. */ - dot = (ds_chomp (line, get_endcmd ()) - || (ds_is_empty (line) && get_nulline ())); - - /* Strip leading indentors or insert a terminal dot (unless the - line was obtained interactively). */ - if (!interactive) - { - int first = ds_first (line); - - if (first == '+' || first == '-') - *ds_data (line) = ' '; - else if (first != EOF && !isspace (first)) - put_token = '.'; - } + lex_preprocess_line (&lexer->line_buffer, + lex_current_syntax_mode (lexer), + &line_starts_command, &lexer->dot); - prog = ds_cstr (line); + if (line_starts_command) + lexer->put_token = T_ENDCMD; + lexer->prog = ds_cstr (&lexer->line_buffer); return true; } /* Token names. */ -/* Returns the name of a token in a static buffer. */ +/* Returns the name of a token. */ const char * -lex_token_name (int token) +lex_token_name (enum token_type token) { - if (token >= T_FIRST_KEYWORD && token <= T_LAST_KEYWORD) - return keywords[token - T_FIRST_KEYWORD]; - - if (token < 256) + switch (token) { - static char t[2]; - t[0] = token; - return t; + case T_ID: + case T_POS_NUM: + case T_NEG_NUM: + case T_STRING: + case TOKEN_N_TYPES: + NOT_REACHED (); + + case T_STOP: + return ""; + + case T_ENDCMD: + return "."; + + case T_PLUS: + return "+"; + + case T_DASH: + return "-"; + + case T_ASTERISK: + return "*"; + + case T_SLASH: + return "/"; + + case T_EQUALS: + return "="; + + case T_LPAREN: + return "("; + + case T_RPAREN: + return ")"; + + case T_LBRACK: + return "["; + + case T_RBRACK: + return "]"; + + case T_COMMA: + return ","; + + case T_AND: + return "AND"; + + case T_OR: + return "OR"; + + case T_NOT: + return "NOT"; + + case T_EQ: + return "EQ"; + + case T_GE: + return ">="; + + case T_GT: + return ">"; + + case T_LE: + return "<="; + + case T_LT: + return "<"; + + case T_NE: + return "~="; + + case T_ALL: + return "ALL"; + + case T_BY: + return "BY"; + + case T_TO: + return "TO"; + + case T_WITH: + return "WITH"; + + case T_EXP: + return "**"; } - return _(""); + NOT_REACHED (); } /* Returns an ASCII representation of the current token as a malloc()'d string. */ char * -lex_token_representation (void) +lex_token_representation (struct lexer *lexer) { char *token_rep; - - switch (token) + + switch (lexer->token) { case T_ID: case T_POS_NUM: case T_NEG_NUM: - return ds_xstrdup (&tokstr); - break; + return ss_xstrdup (lex_tokss (lexer)); case T_STRING: { + struct substring ss; int hexstring = 0; char *sp, *dp; - for (sp = ds_cstr (&tokstr); sp < ds_end (&tokstr); sp++) - if (!isprint ((unsigned char) *sp)) + ss = lex_tokss (lexer); + for (sp = ss_data (ss); sp < ss_end (ss); sp++) + if (!c_isprint ((unsigned char) *sp)) { hexstring = 1; break; } - - token_rep = xmalloc (2 + ds_length (&tokstr) * 2 + 1 + 1); + + token_rep = xmalloc (2 + ss_length (ss) * 2 + 1 + 1); dp = token_rep; if (hexstring) *dp++ = 'X'; *dp++ = '\''; - if (!hexstring) - for (sp = ds_cstr (&tokstr); *sp; ) + for (sp = ss_data (ss); sp < ss_end (ss); sp++) + if (!hexstring) { if (*sp == '\'') *dp++ = '\''; - *dp++ = (unsigned char) *sp++; + *dp++ = (unsigned char) *sp; } - else - for (sp = ds_cstr (&tokstr); sp < ds_end (&tokstr); sp++) + else { *dp++ = (((unsigned char) *sp) >> 4)["0123456789ABCDEF"]; *dp++ = (((unsigned char) *sp) & 15)["0123456789ABCDEF"]; } *dp++ = '\''; *dp = '\0'; - + return token_rep; } - break; - - case T_STOP: - token_rep = xmalloc (1); - *token_rep = '\0'; - return token_rep; - - case T_EXP: - return xstrdup ("**"); default: - if (token >= T_FIRST_KEYWORD && token <= T_LAST_KEYWORD) - return xstrdup (keywords [token - T_FIRST_KEYWORD]); - else - { - token_rep = xmalloc (2); - token_rep[0] = token; - token_rep[1] = '\0'; - return token_rep; - } + return xstrdup (lex_token_name (lexer->token)); } - - NOT_REACHED (); } /* Really weird functions. */ -/* Most of the time, a `-' is a lead-in to a negative number. But - sometimes it's actually part of the syntax. If a dash can be part - of syntax then this function is called to rip it off of a - number. */ -void -lex_negative_to_dash (void) -{ - if (token == T_NEG_NUM) - { - token = T_POS_NUM; - tokval = -tokval; - ds_assign_substring (&tokstr, ds_substr (&tokstr, 1, SIZE_MAX)); - save_token (); - token = '-'; - } -} - -/* We're not at eof any more. */ -void -lex_reset_eof (void) -{ - eof = false; -} - /* Skip a COMMENT command. */ void -lex_skip_comment (void) +lex_skip_comment (struct lexer *lexer) { for (;;) { - if (!lex_get_line ()) + if (!lex_get_line (lexer)) { - put_token = T_STOP; - eof = true; + lexer->put_token = T_STOP; + lexer->prog = NULL; return; } - - if (put_token == '.') + + if (lexer->put_token == T_ENDCMD) break; - prog = ds_end (&getl_buf); - if (dot) + ds_cstr (&lexer->line_buffer); /* Ensures ds_end will point to a valid char */ + lexer->prog = ds_end (&lexer->line_buffer); + if (lexer->dot) break; } } @@ -993,7 +1140,8 @@ lex_skip_comment (void) hex digits, according to TYPE. The string is converted to characters having the specified values. */ static void -convert_numeric_string_to_char_string (enum string_type type) +convert_numeric_string_to_char_string (struct lexer *lexer, + enum string_type type) { const char *base_name; int base; @@ -1002,7 +1150,7 @@ convert_numeric_string_to_char_string (enum string_type type) size_t i; char *p; - switch (type) + switch (type) { case BINARY_STRING: base_name = _("binary"); @@ -1022,19 +1170,19 @@ convert_numeric_string_to_char_string (enum string_type type) default: NOT_REACHED (); } - - byte_cnt = ds_length (&tokstr) / chars_per_byte; - if (ds_length (&tokstr) % chars_per_byte) - msg (SE, _("String of %s digits has %d characters, which is not a " + + byte_cnt = ds_length (&lexer->tokstr) / chars_per_byte; + if (ds_length (&lexer->tokstr) % chars_per_byte) + msg (SE, _("String of %s digits has %zu characters, which is not a " "multiple of %d."), - base_name, ds_length (&tokstr), chars_per_byte); + base_name, ds_length (&lexer->tokstr), chars_per_byte); - p = ds_cstr (&tokstr); + p = ds_cstr (&lexer->tokstr); for (i = 0; i < byte_cnt; i++) { int value; int j; - + value = 0; for (j = 0; j < chars_per_byte; j++, p++) { @@ -1059,85 +1207,88 @@ convert_numeric_string_to_char_string (enum string_type type) value = value * base + v; } - ds_cstr (&tokstr)[i] = (unsigned char) value; + ds_cstr (&lexer->tokstr)[i] = (unsigned char) value; } - ds_truncate (&tokstr, byte_cnt); + ds_truncate (&lexer->tokstr, byte_cnt); } /* Parses a string from the input buffer into tokstr. The input - buffer pointer prog must point to the initial single or double + buffer pointer lexer->prog must point to the initial single or double quote. TYPE indicates the type of string to be parsed. Returns token type. */ -static int -parse_string (enum string_type type) +static int +parse_string (struct lexer *lexer, enum string_type type) { + if (type != CHARACTER_STRING) + lexer->prog++; + /* Accumulate the entire string, joining sections indicated by + signs. */ for (;;) { /* Single or double quote. */ - int c = *prog++; - + int c = *lexer->prog++; + /* Accumulate section. */ for (;;) { /* Check end of line. */ - if (*prog == '\0') + if (*lexer->prog == '\0') { msg (SE, _("Unterminated string constant.")); goto finish; } - + /* Double quote characters to embed them in strings. */ - if (*prog == c) + if (*lexer->prog == c) { - if (prog[1] == c) - prog++; + if (lexer->prog[1] == c) + lexer->prog++; else break; } - ds_put_char (&tokstr, *prog++); + ds_put_byte (&lexer->tokstr, *lexer->prog++); } - prog++; + lexer->prog++; /* Skip whitespace after final quote mark. */ - if (eof) + if (lexer->prog == NULL) break; for (;;) { - while (isspace ((unsigned char) *prog)) - prog++; - if (*prog) + while (c_isspace ((unsigned char) *lexer->prog)) + lexer->prog++; + if (*lexer->prog) break; - if (dot) + if (lexer->dot) goto finish; - if (!lex_get_line ()) + if (!lex_get_line (lexer)) goto finish; } /* Skip plus sign. */ - if (*prog != '+') + if (*lexer->prog != '+') break; - prog++; + lexer->prog++; /* Skip whitespace after plus sign. */ - if (eof) + if (lexer->prog == NULL) break; for (;;) { - while (isspace ((unsigned char) *prog)) - prog++; - if (*prog) + while (c_isspace ((unsigned char) *lexer->prog)) + lexer->prog++; + if (*lexer->prog) break; - if (dot) + if (lexer->dot) goto finish; - if (!lex_get_line ()) + if (!lex_get_line (lexer)) { msg (SE, _("Unexpected end of file in string concatenation.")); goto finish; @@ -1145,7 +1296,7 @@ parse_string (enum string_type type) } /* Ensure that a valid string follows. */ - if (*prog != '\'' && *prog != '"') + if (*lexer->prog != '\'' && *lexer->prog != '"') { msg (SE, _("String expected following `+'.")); goto finish; @@ -1156,84 +1307,64 @@ parse_string (enum string_type type) into one large string. */ finish: if (type != CHARACTER_STRING) - convert_numeric_string_to_char_string (type); - - if (ds_length (&tokstr) > 255) - { - msg (SE, _("String exceeds 255 characters in length (%d characters)."), - ds_length (&tokstr)); - ds_truncate (&tokstr, 255); - } - - { - /* FIXME. */ - size_t i; - int warned = 0; - - for (i = 0; i < ds_length (&tokstr); i++) - if (ds_cstr (&tokstr)[i] == 0) - { - if (!warned) - { - msg (SE, _("Sorry, literal strings may not contain null " - "characters. Replacing with spaces.")); - warned = 1; - } - ds_cstr (&tokstr)[i] = ' '; - } - } + convert_numeric_string_to_char_string (lexer, type); return T_STRING; } - -#if DUMP_TOKENS -/* Reads one token from the lexer and writes a textual representation - on stdout for debugging purposes. */ -static void -dump_token (void) -{ - { - const char *curfn; - int curln; - - getl_location (&curfn, &curln); - if (curfn) - fprintf (stderr, "%s:%d\t", curfn, curln); - } - - switch (token) - { - case T_ID: - fprintf (stderr, "ID\t%s\n", tokid); - break; - - case T_POS_NUM: - case T_NEG_NUM: - fprintf (stderr, "NUM\t%f\n", tokval); - break; + +/* Token Accessor Functions */ - case T_STRING: - fprintf (stderr, "STRING\t\"%s\"\n", ds_cstr (&tokstr)); - break; +enum token_type +lex_token (const struct lexer *lexer) +{ + return lexer->token; +} - case T_STOP: - fprintf (stderr, "STOP\n"); - break; +double +lex_tokval (const struct lexer *lexer) +{ + return lexer->tokval; +} - case T_EXP: - fprintf (stderr, "MISC\tEXP\""); - break; +/* Returns the null-terminated string value associated with LEXER's current + token. For a T_ID token, this is the identifier, and for a T_STRING token, + this is the string. For other tokens the value is undefined. */ +const char * +lex_tokcstr (const struct lexer *lexer) +{ + return ds_cstr (&lexer->tokstr); +} - case 0: - fprintf (stderr, "MISC\tEOF\n"); - break; +/* Returns the string value associated with LEXER's current token. For a T_ID + token, this is the identifier, and for a T_STRING token, this is the string. + For other tokens the value is undefined. */ +struct substring +lex_tokss (const struct lexer *lexer) +{ + return ds_ss (&lexer->tokstr); +} - default: - if (token >= T_FIRST_KEYWORD && token <= T_LAST_KEYWORD) - fprintf (stderr, "KEYWORD\t%s\n", lex_token_name (token)); - else - fprintf (stderr, "PUNCT\t%c\n", token); - break; +/* If the lexer is positioned at the (pseudo)identifier S, which + may contain a hyphen ('-'), skips it and returns true. Each + half of the identifier may be abbreviated to its first three + letters. + Otherwise, returns false. */ +bool +lex_match_hyphenated_word (struct lexer *lexer, const char *s) +{ + const char *hyphen = strchr (s, '-'); + if (hyphen == NULL) + return lex_match_id (lexer, s); + else if (lexer->token != T_ID + || !lex_id_match (ss_buffer (s, hyphen - s), lex_tokss (lexer)) + || lex_look_ahead (lexer) != T_DASH) + return false; + else + { + lex_get (lexer); + lex_force_match (lexer, T_DASH); + lex_force_match_id (lexer, hyphen + 1); + return true; } } -#endif /* DUMP_TOKENS */ +