during the process of parsing; the lower levels of the tree always
come first in the result.
- Note that malloc's and realloc's in this file are transformed to
- xmalloc and xrealloc respectively by the same sed command in the
- makefile that remaps any other malloc/realloc inserted by the parser
- generator. Doing this with #defines and trying to control the interaction
- with include files (<malloc.h> and <stdlib.h> for example) just became
- too messy, particularly when such includes can be inserted at random
- times by the parser generator.
-
- Also note that the language accepted by this parser is more liberal
+ Note that the language accepted by this parser is more liberal
than the one accepted by an actual Chill compiler. For example, the
language rule that a simple name string can not be one of the reserved
simple name strings is not enforced (e.g "case" is not treated as a
*/
#include "defs.h"
-#include <string.h>
+#include "gdb_string.h"
#include <ctype.h>
#include "expression.h"
#include "language.h"
#include "symfile.h" /* Required by objfiles.h. */
#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
+#ifdef __GNUC__
+#define INLINE __inline__
+#endif
+
typedef union
{
enum ch_terminal {
END_TOKEN = 0,
/* '\001' ... '\xff' come first. */
+ OPEN_PAREN = '(',
TOKEN_NOT_READ = 999,
INTEGER_LITERAL,
BOOLEAN_LITERAL,
};
/* Forward declarations. */
-static void parse_expr ();
-static void parse_primval ();
-static void parse_untyped_expr ();
-static int parse_opt_untyped_expr ();
-static void parse_if_expression_body PARAMS((void));
+
static void write_lower_upper_value PARAMS ((enum exp_opcode, struct type *));
-static enum ch_terminal ch_lex ();
+static enum ch_terminal match_bitstring_literal PARAMS ((void));
+static enum ch_terminal match_integer_literal PARAMS ((void));
+static enum ch_terminal match_character_literal PARAMS ((void));
+static enum ch_terminal match_string_literal PARAMS ((void));
+static enum ch_terminal match_float_literal PARAMS ((void));
+static enum ch_terminal match_float_literal PARAMS ((void));
+static int decode_integer_literal PARAMS ((LONGEST *, char **));
+static int decode_integer_value PARAMS ((int, char **, LONGEST *));
+static char *match_simple_name_string PARAMS ((void));
+static void growbuf_by_size PARAMS ((int));
+static void parse_untyped_expr PARAMS ((void));
+static void parse_if_expression PARAMS ((void));
+static void parse_else_alternative PARAMS ((void));
+static void parse_then_alternative PARAMS ((void));
+static void parse_expr PARAMS ((void));
+static void parse_operand0 PARAMS ((void));
+static void parse_operand1 PARAMS ((void));
+static void parse_operand2 PARAMS ((void));
+static void parse_operand3 PARAMS ((void));
+static void parse_operand4 PARAMS ((void));
+static void parse_operand5 PARAMS ((void));
+static void parse_operand6 PARAMS ((void));
+static void parse_primval PARAMS ((void));
+static void parse_tuple PARAMS ((struct type *));
+static void parse_opt_element_list PARAMS ((void));
+static void parse_tuple_element PARAMS ((void));
+static void parse_named_record_element PARAMS ((void));
+static void parse_call PARAMS ((void));
+static struct type *parse_mode_or_normal_call PARAMS ((void));
+#if 0
+static struct type *parse_mode_call PARAMS ((void));
+#endif
+static void parse_unary_call PARAMS ((void));
+static int parse_opt_untyped_expr PARAMS ((void));
+static void parse_case_label PARAMS ((void));
+static int expect PARAMS ((enum ch_terminal, char *));
+static void parse_expr PARAMS ((void));
+static void parse_primval PARAMS ((void));
+static void parse_untyped_expr PARAMS ((void));
+static int parse_opt_untyped_expr PARAMS ((void));
+static void parse_if_expression_body PARAMS((void));
+static enum ch_terminal ch_lex PARAMS ((void));
+INLINE static enum ch_terminal PEEK_TOKEN PARAMS ((void));
+static enum ch_terminal peek_token_ PARAMS ((int));
+static void forward_token_ PARAMS ((void));
+static void require PARAMS ((enum ch_terminal));
+static int check_token PARAMS ((enum ch_terminal));
#define MAX_LOOK_AHEAD 2
static enum ch_terminal terminal_buffer[MAX_LOOK_AHEAD+1] = {
/*int current_token, lookahead_token;*/
-#ifdef __GNUC__
-__inline__
-#endif
-static enum ch_terminal
+INLINE static enum ch_terminal
PEEK_TOKEN()
{
if (terminal_buffer[0] == TOKEN_NOT_READ)
return terminal_buffer[i];
}
+#if 0
+
static void
pushback_token (code, node)
enum ch_terminal code;
val_buffer[0] = node;
}
+#endif
+
static void
forward_token_()
{
/* Skip the next token.
if it isn't TOKEN, the parser is broken. */
-void
+static void
require(token)
enum ch_terminal token;
{
FORWARD_TOKEN();
}
-int
+static int
check_token (token)
enum ch_terminal token;
{
/* return 0 if expected token was not found,
else return 1.
*/
-int
-expect(token, message)
+static int
+expect (token, message)
enum ch_terminal token;
char *message;
{
if (message)
error (message);
else if (token < 256)
- error ("syntax error - expected a '%c' here '%s'", token, lexptr);
+ error ("syntax error - expected a '%c' here \"%s\"", token, lexptr);
else
error ("syntax error");
return 0;
/* Parse NAME '(' MODENAME ')'. */
-struct type *
+#if 0
+
+static struct type *
parse_mode_call ()
{
struct type *type;
return type;
}
-struct type *
+#endif
+
+static struct type *
parse_mode_or_normal_call ()
{
struct type *type;
}
else
arglist_len = 0;
- expect (')', "expected ')' here");
+ expect (')', NULL);
arg_count = end_arglist ();
write_exp_elt_opcode (MULTI_SUBSCRIPT);
write_exp_elt_longcst (arg_count);
static void
parse_named_record_element ()
{
- struct stoken label = PEEK_LVAL ().sval;
- expect (FIELD_NAME, "expected a field name here `%s'", lexptr);
+ struct stoken label;
+ char buf[256];
+
+ label = PEEK_LVAL ().sval;
+ sprintf (buf, "expected a field name here `%s'", lexptr);
+ expect (FIELD_NAME, buf);
if (check_token (','))
parse_named_record_element ();
else if (check_token (':'))
write_exp_elt_opcode (OP_ARRAY);
if (mode)
{
+ struct type *type = check_typedef (mode);
+ if (TYPE_CODE (type) != TYPE_CODE_ARRAY
+ && TYPE_CODE (type) != TYPE_CODE_STRUCT
+ && TYPE_CODE (type) != TYPE_CODE_SET)
+ error ("invalid tuple mode");
write_exp_elt_opcode (UNOP_CAST);
write_exp_elt_type (mode);
write_exp_elt_opcode (UNOP_CAST);
case CHARACTER_LITERAL:
write_exp_elt_opcode (OP_LONG);
write_exp_elt_type (PEEK_LVAL ().typed_val.type);
- write_exp_elt_longcst ((LONGEST) (PEEK_LVAL ().typed_val.val));
+ write_exp_elt_longcst (PEEK_LVAL ().typed_val.val);
write_exp_elt_opcode (OP_LONG);
FORWARD_TOKEN ();
break;
if (PEEK_TOKEN () != TYPENAME)
error ("missing MODENAME after ARRAY()");
type = PEEK_LVAL().tsym.type;
+ FORWARD_TOKEN ();
expect ('(', NULL);
parse_expr ();
expect (')', "missing right parenthesis");
write_exp_elt_type (builtin_type_int);
write_exp_elt_opcode (UNOP_CAST);
break;
+ case CARD:
+ parse_unary_call ();
+ write_exp_elt_opcode (UNOP_CARD);
+ break;
+ case MAX_TOKEN:
+ parse_unary_call ();
+ write_exp_elt_opcode (UNOP_CHMAX);
+ break;
+ case MIN_TOKEN:
+ parse_unary_call ();
+ write_exp_elt_opcode (UNOP_CHMIN);
+ break;
case PRED: op_name = "PRED"; goto unimplemented_unary_builtin;
case SUCC: op_name = "SUCC"; goto unimplemented_unary_builtin;
case ABS: op_name = "ABS"; goto unimplemented_unary_builtin;
- case CARD: op_name = "CARD"; goto unimplemented_unary_builtin;
- case MAX_TOKEN: op_name = "MAX"; goto unimplemented_unary_builtin;
- case MIN_TOKEN: op_name = "MIN"; goto unimplemented_unary_builtin;
unimplemented_unary_builtin:
parse_unary_call ();
error ("not implemented: %s builtin function", op_name);
}
write_exp_elt_opcode (UNOP_IND);
continue;
- case '(':
+ case OPEN_PAREN:
parse_call ();
continue;
case CHARACTER_STRING_LITERAL:
write_exp_elt_longcst (1);
write_exp_elt_opcode (MULTI_SUBSCRIPT);
continue;
+ case END_TOKEN:
+ case TOKEN_NOT_READ:
+ case INTEGER_LITERAL:
+ case BOOLEAN_LITERAL:
+ case FLOAT_LITERAL:
+ case GENERAL_PROCEDURE_NAME:
+ case LOCATION_NAME:
+ case EMPTINESS_LITERAL:
+ case TYPENAME:
+ case CASE:
+ case OF:
+ case ESAC:
+ case LOGIOR:
+ case ORIF:
+ case LOGXOR:
+ case LOGAND:
+ case ANDIF:
+ case NOTEQUAL:
+ case GEQ:
+ case LEQ:
+ case IN:
+ case SLASH_SLASH:
+ case MOD:
+ case REM:
+ case NOT:
+ case RECEIVE:
+ case UP:
+ case IF:
+ case THEN:
+ case ELSE:
+ case FI:
+ case ELSIF:
+ case ILLEGAL_TOKEN:
+ case NUM:
+ case PRED:
+ case SUCC:
+ case ABS:
+ case CARD:
+ case MAX_TOKEN:
+ case MIN_TOKEN:
+ case ADDR_TOKEN:
+ case SIZE:
+ case UPPER:
+ case LOWER:
+ case LENGTH:
+ case ARRAY:
+ case GDB_VARIABLE:
+ case GDB_ASSIGNMENT:
+ break;
}
break;
}
tempbufsize += growby;
if (tempbuf == NULL)
{
- tempbuf = (char *) malloc (tempbufsize);
+ tempbuf = (char *) xmalloc (tempbufsize);
}
else
{
- tempbuf = (char *) realloc (tempbuf, tempbufsize);
+ tempbuf = (char *) xrealloc (tempbuf, tempbufsize);
}
}
match_string_literal ()
{
char *tokptr = lexptr;
+ int in_ctrlseq = 0;
+ LONGEST ival;
for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
{
CHECKBUF (1);
- if (*tokptr == *lexptr)
+ tryagain: ;
+ if (in_ctrlseq)
{
- if (*(tokptr + 1) == *lexptr)
+ /* skip possible whitespaces */
+ while ((*tokptr == ' ' || *tokptr == '\t') && *tokptr)
+ tokptr++;
+ if (*tokptr == ')')
{
+ in_ctrlseq = 0;
tokptr++;
+ goto tryagain;
+ }
+ else if (*tokptr != ',')
+ error ("Invalid control sequence");
+ tokptr++;
+ /* skip possible whitespaces */
+ while ((*tokptr == ' ' || *tokptr == '\t') && *tokptr)
+ tokptr++;
+ if (!decode_integer_literal (&ival, &tokptr))
+ error ("Invalid control sequence");
+ tokptr--;
+ }
+ else if (*tokptr == *lexptr)
+ {
+ if (*(tokptr + 1) == *lexptr)
+ {
+ ival = *tokptr++;
}
else
{
break;
}
}
- tempbuf[tempbufindex++] = *tokptr;
+ else if (*tokptr == '^')
+ {
+ if (*(tokptr + 1) == '(')
+ {
+ in_ctrlseq = 1;
+ tokptr += 2;
+ if (!decode_integer_literal (&ival, &tokptr))
+ error ("Invalid control sequence");
+ tokptr--;
+ }
+ else if (*(tokptr + 1) == '^')
+ ival = *tokptr++;
+ else
+ error ("Invalid control sequence");
+ }
+ else
+ ival = *tokptr;
+ tempbuf[tempbufindex++] = ival;
}
+ if (in_ctrlseq)
+ error ("Invalid control sequence");
+
if (*tokptr == '\0' /* no terminator */
|| (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
{
Note that more than a single character, enclosed in single quotes, is
a string literal.
- Also note that the control sequence form is not in GNU Chill since it
- is ambiguous with the string literal form using single quotes. I.E.
- is '^(7)' a character literal or a string literal. In theory it it
- possible to tell by context, but GNU Chill doesn't accept the control
- sequence form, so neither do we (for now the code is disabled).
-
Returns CHARACTER_LITERAL if a match is found.
*/
/* Determine which form we have, either a control sequence or the
single character form. */
- if ((*tokptr == '^') && (*(tokptr + 1) == '('))
+ if (*tokptr == '^')
{
-#if 0 /* Disable, see note above. -fnf */
- /* Match and decode a control sequence. Return zero if we don't
- find a valid integer literal, or if the next unconsumed character
- after the integer literal is not the trailing ')'.
- FIXME: We currently don't handle the multiple integer literal
- form. */
- tokptr += 2;
- if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
+ if (*(tokptr + 1) == '(')
{
- return (0);
+ /* Match and decode a control sequence. Return zero if we don't
+ find a valid integer literal, or if the next unconsumed character
+ after the integer literal is not the trailing ')'. */
+ tokptr += 2;
+ if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
+ {
+ return (0);
+ }
}
-#else
- return (0);
-#endif
+ else if (*(tokptr + 1) == '^')
+ {
+ ival = *tokptr;
+ tokptr += 2;
+ }
+ else
+ /* fail */
+ error ("Invalid control sequence");
+ }
+ else if (*tokptr == '\'')
+ {
+ /* this must be duplicated */
+ ival = *tokptr;
+ tokptr += 2;
}
else
{
ival = *tokptr++;
}
-
+
/* The trailing quote has not yet been consumed. If we don't find
it, then we have no match. */
else
{
yylval.typed_val.val = ival;
-#ifdef CC_HAS_LONG_LONG
- if (ival > 2147483647 || ival < -2147483648)
+#if defined(CC_HAS_LONG_LONG) && defined(__STDC__)
+ if (ival > (LONGEST)2147483647U || ival < -(LONGEST)2147483648U)
yylval.typed_val.type = builtin_type_long_long;
else
#endif
digit += 10;
break;
default:
- error ("Invalid character in bitstring or integer.");
+ /* this is not a bitstring literal, probably an integer */
+ return 0;
}
if (digit >= 1 << bits_per_char)
{
case LOC_OPTIMIZED_OUT:
error ("Symbol \"%s\" names no location.", inputname);
break;
+ case LOC_UNRESOLVED:
+ error ("unhandled SYMBOL_CLASS in ch_lex()");
+ break;
}
}
else if (!have_full_symbols () && !have_partial_symbols ())
write_exp_elt_opcode (opcode);
else
{
- extern LONGEST type_lower_upper ();
struct type *result_type;
LONGEST val = type_lower_upper (opcode, type, &result_type);
write_exp_elt_opcode (OP_LONG);