1 /* Parser for GNU CHILL (CCITT High-Level Language) -*- C -*-
2 Copyright (C) 1992, 1993, 1995 Free Software Foundation, Inc.
4 This file is part of GDB.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Parse a Chill expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 Note that the language accepted by this parser is more liberal
30 than the one accepted by an actual Chill compiler. For example, the
31 language rule that a simple name string can not be one of the reserved
32 simple name strings is not enforced (e.g "case" is not treated as a
33 reserved name). Another example is that Chill is a strongly typed
34 language, and certain expressions that violate the type constraints
35 may still be evaluated if gdb can do so in a meaningful manner, while
36 such expressions would be rejected by the compiler. The reason for
37 this more liberal behavior is the philosophy that the debugger
38 is intended to be a tool that is used by the programmer when things
39 go wrong, and as such, it should provide as few artificial barriers
40 to it's use as possible. If it can do something meaningful, even
41 something that violates language contraints that are enforced by the
42 compiler, it should do so without complaint.
47 #include "gdb_string.h"
49 #include "expression.h"
52 #include "parser-defs.h"
54 #include "bfd.h" /* Required by objfiles.h. */
55 #include "symfile.h" /* Required by objfiles.h. */
56 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
59 #define INLINE __inline__
66 unsigned LONGEST ulval;
81 /* '\001' ... '\xff' come first. */
88 GENERAL_PROCEDURE_NAME,
91 CHARACTER_STRING_LITERAL,
137 /* Forward declarations. */
139 static void write_lower_upper_value PARAMS ((enum exp_opcode, struct type *));
140 static enum ch_terminal match_bitstring_literal PARAMS ((void));
141 static enum ch_terminal match_integer_literal PARAMS ((void));
142 static enum ch_terminal match_character_literal PARAMS ((void));
143 static enum ch_terminal match_string_literal PARAMS ((void));
144 static enum ch_terminal match_float_literal PARAMS ((void));
145 static enum ch_terminal match_float_literal PARAMS ((void));
146 static int decode_integer_literal PARAMS ((LONGEST *, char **));
147 static int decode_integer_value PARAMS ((int, char **, LONGEST *));
148 static char *match_simple_name_string PARAMS ((void));
149 static void growbuf_by_size PARAMS ((int));
150 static void parse_untyped_expr PARAMS ((void));
151 static void parse_if_expression PARAMS ((void));
152 static void parse_else_alternative PARAMS ((void));
153 static void parse_then_alternative PARAMS ((void));
154 static void parse_expr PARAMS ((void));
155 static void parse_operand0 PARAMS ((void));
156 static void parse_operand1 PARAMS ((void));
157 static void parse_operand2 PARAMS ((void));
158 static void parse_operand3 PARAMS ((void));
159 static void parse_operand4 PARAMS ((void));
160 static void parse_operand5 PARAMS ((void));
161 static void parse_operand6 PARAMS ((void));
162 static void parse_primval PARAMS ((void));
163 static void parse_tuple PARAMS ((struct type *));
164 static void parse_opt_element_list PARAMS ((void));
165 static void parse_tuple_element PARAMS ((void));
166 static void parse_named_record_element PARAMS ((void));
167 static void parse_call PARAMS ((void));
168 static struct type *parse_mode_or_normal_call PARAMS ((void));
170 static struct type *parse_mode_call PARAMS ((void));
172 static void parse_unary_call PARAMS ((void));
173 static int parse_opt_untyped_expr PARAMS ((void));
174 static void parse_case_label PARAMS ((void));
175 static int expect PARAMS ((enum ch_terminal, char *));
176 static void parse_expr PARAMS ((void));
177 static void parse_primval PARAMS ((void));
178 static void parse_untyped_expr PARAMS ((void));
179 static int parse_opt_untyped_expr PARAMS ((void));
180 static void parse_if_expression_body PARAMS((void));
181 static enum ch_terminal ch_lex PARAMS ((void));
182 INLINE static enum ch_terminal PEEK_TOKEN PARAMS ((void));
183 static enum ch_terminal peek_token_ PARAMS ((int));
184 static void forward_token_ PARAMS ((void));
185 static void require PARAMS ((enum ch_terminal));
186 static int check_token PARAMS ((enum ch_terminal));
188 #define MAX_LOOK_AHEAD 2
189 static enum ch_terminal terminal_buffer[MAX_LOOK_AHEAD+1] = {
190 TOKEN_NOT_READ, TOKEN_NOT_READ, TOKEN_NOT_READ};
191 static YYSTYPE yylval;
192 static YYSTYPE val_buffer[MAX_LOOK_AHEAD+1];
194 /*int current_token, lookahead_token;*/
196 INLINE static enum ch_terminal
199 if (terminal_buffer[0] == TOKEN_NOT_READ)
201 terminal_buffer[0] = ch_lex ();
202 val_buffer[0] = yylval;
204 return terminal_buffer[0];
206 #define PEEK_LVAL() val_buffer[0]
207 #define PEEK_TOKEN1() peek_token_(1)
208 #define PEEK_TOKEN2() peek_token_(2)
209 static enum ch_terminal
213 if (i > MAX_LOOK_AHEAD)
214 fatal ("internal error - too much lookahead");
215 if (terminal_buffer[i] == TOKEN_NOT_READ)
217 terminal_buffer[i] = ch_lex ();
218 val_buffer[i] = yylval;
220 return terminal_buffer[i];
226 pushback_token (code, node)
227 enum ch_terminal code;
231 if (terminal_buffer[MAX_LOOK_AHEAD] != TOKEN_NOT_READ)
232 fatal ("internal error - cannot pushback token");
233 for (i = MAX_LOOK_AHEAD; i > 0; i--)
235 terminal_buffer[i] = terminal_buffer[i - 1];
236 val_buffer[i] = val_buffer[i - 1];
238 terminal_buffer[0] = code;
239 val_buffer[0] = node;
248 for (i = 0; i < MAX_LOOK_AHEAD; i++)
250 terminal_buffer[i] = terminal_buffer[i+1];
251 val_buffer[i] = val_buffer[i+1];
253 terminal_buffer[MAX_LOOK_AHEAD] = TOKEN_NOT_READ;
255 #define FORWARD_TOKEN() forward_token_()
257 /* Skip the next token.
258 if it isn't TOKEN, the parser is broken. */
262 enum ch_terminal token;
264 if (PEEK_TOKEN() != token)
267 sprintf (buf, "internal parser error - expected token %d", (int)token);
275 enum ch_terminal token;
277 if (PEEK_TOKEN() != token)
283 /* return 0 if expected token was not found,
287 expect (token, message)
288 enum ch_terminal token;
291 if (PEEK_TOKEN() != token)
295 else if (token < 256)
296 error ("syntax error - expected a '%c' here \"%s\"", token, lexptr);
298 error ("syntax error");
308 parse_opt_name_string (allow_all)
309 int allow_all; /* 1 if ALL is allowed as a postfix */
311 int token = PEEK_TOKEN();
315 if (token == ALL && allow_all)
326 token = PEEK_TOKEN();
330 token = PEEK_TOKEN();
331 if (token == ALL && allow_all)
332 return get_identifier3(IDENTIFIER_POINTER (name), "!", "*");
336 error ("'%s!' is not followed by an identifier",
337 IDENTIFIER_POINTER (name));
340 name = get_identifier3(IDENTIFIER_POINTER(name),
341 "!", IDENTIFIER_POINTER(PEEK_LVAL()));
346 parse_simple_name_string ()
348 int token = PEEK_TOKEN();
352 error ("expected a name here");
353 return error_mark_node;
363 tree name = parse_opt_name_string (0);
367 error ("expected a name string here");
368 return error_mark_node;
371 /* Matches: <name_string>
372 Returns if pass 1: the identifier.
373 Returns if pass 2: a decl or value for identifier. */
378 tree name = parse_name_string ();
379 if (pass == 1 || ignoring)
383 tree decl = lookup_name (name);
384 if (decl == NULL_TREE)
386 error ("`%s' undeclared", IDENTIFIER_POINTER (name));
387 return error_mark_node;
389 else if (TREE_CODE (TREE_TYPE (decl)) == ERROR_MARK)
390 return error_mark_node;
391 else if (TREE_CODE (decl) == CONST_DECL)
392 return DECL_INITIAL (decl);
393 else if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
394 return convert_from_reference (decl);
403 pushback_paren_expr (expr)
406 if (pass == 1 && !ignoring)
407 expr = build1 (PAREN_EXPR, NULL_TREE, expr);
408 pushback_token (EXPR, expr);
412 /* Matches: <case label> */
417 if (check_token (ELSE))
418 error ("ELSE in tuples labels not implemented");
419 /* Does not handle the case of a mode name. FIXME */
421 if (check_token (':'))
424 write_exp_elt_opcode (BINOP_RANGE);
429 parse_opt_untyped_expr ()
431 switch (PEEK_TOKEN ())
438 parse_untyped_expr ();
452 /* Parse NAME '(' MODENAME ')'. */
462 if (PEEK_TOKEN () != TYPENAME)
463 error ("expect MODENAME here `%s'", lexptr);
464 type = PEEK_LVAL().tsym.type;
473 parse_mode_or_normal_call ()
478 if (PEEK_TOKEN () == TYPENAME)
480 type = PEEK_LVAL().tsym.type;
492 /* Parse something that looks like a function call.
493 Assume we have parsed the function, and are at the '('. */
500 /* This is to save the value of arglist_len
501 being accumulated for each dimension. */
503 if (parse_opt_untyped_expr ())
505 int tok = PEEK_TOKEN ();
507 if (tok == UP || tok == ':')
511 expect (')', "expected ')' to terminate slice");
513 write_exp_elt_opcode (tok == UP ? TERNOP_SLICE_COUNT
517 while (check_token (','))
519 parse_untyped_expr ();
526 arg_count = end_arglist ();
527 write_exp_elt_opcode (MULTI_SUBSCRIPT);
528 write_exp_elt_longcst (arg_count);
529 write_exp_elt_opcode (MULTI_SUBSCRIPT);
533 parse_named_record_element ()
538 label = PEEK_LVAL ().sval;
539 sprintf (buf, "expected a field name here `%s'", lexptr);
540 expect (FIELD_NAME, buf);
541 if (check_token (','))
542 parse_named_record_element ();
543 else if (check_token (':'))
546 error ("syntax error near `%s' in named record tuple element", lexptr);
547 write_exp_elt_opcode (OP_LABELED);
548 write_exp_string (label);
549 write_exp_elt_opcode (OP_LABELED);
552 /* Returns one or nore TREE_LIST nodes, in reverse order. */
555 parse_tuple_element ()
557 if (PEEK_TOKEN () == FIELD_NAME)
559 /* Parse a labelled structure tuple. */
560 parse_named_record_element ();
564 if (check_token ('('))
566 if (check_token ('*'))
568 expect (')', "missing ')' after '*' case label list");
569 error ("(*) not implemented in case label list");
574 while (check_token (','))
577 write_exp_elt_opcode (BINOP_COMMA);
583 parse_untyped_expr ();
584 if (check_token (':'))
586 /* A powerset range or a labeled Array. */
587 parse_untyped_expr ();
588 write_exp_elt_opcode (BINOP_RANGE);
592 /* Matches: a COMMA-separated list of tuple elements.
593 Returns a list (of TREE_LIST nodes). */
595 parse_opt_element_list ()
598 if (PEEK_TOKEN () == ']')
602 parse_tuple_element ();
604 if (PEEK_TOKEN () == ']')
606 if (!check_token (','))
607 error ("bad syntax in tuple");
611 /* Parses: '[' elements ']'
612 If modename is non-NULL it prefixed the tuple. */
620 parse_opt_element_list ();
621 expect (']', "missing ']' after tuple");
622 write_exp_elt_opcode (OP_ARRAY);
623 write_exp_elt_longcst ((LONGEST) 0);
624 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
625 write_exp_elt_opcode (OP_ARRAY);
628 struct type *type = check_typedef (mode);
629 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
630 && TYPE_CODE (type) != TYPE_CODE_STRUCT
631 && TYPE_CODE (type) != TYPE_CODE_SET)
632 error ("invalid tuple mode");
633 write_exp_elt_opcode (UNOP_CAST);
634 write_exp_elt_type (mode);
635 write_exp_elt_opcode (UNOP_CAST);
645 switch (PEEK_TOKEN ())
647 case INTEGER_LITERAL:
648 case CHARACTER_LITERAL:
649 write_exp_elt_opcode (OP_LONG);
650 write_exp_elt_type (PEEK_LVAL ().typed_val.type);
651 write_exp_elt_longcst (PEEK_LVAL ().typed_val.val);
652 write_exp_elt_opcode (OP_LONG);
655 case BOOLEAN_LITERAL:
656 write_exp_elt_opcode (OP_BOOL);
657 write_exp_elt_longcst ((LONGEST) PEEK_LVAL ().ulval);
658 write_exp_elt_opcode (OP_BOOL);
662 write_exp_elt_opcode (OP_DOUBLE);
663 write_exp_elt_type (builtin_type_double);
664 write_exp_elt_dblcst (PEEK_LVAL ().dval);
665 write_exp_elt_opcode (OP_DOUBLE);
668 case EMPTINESS_LITERAL:
669 write_exp_elt_opcode (OP_LONG);
670 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
671 write_exp_elt_longcst (0);
672 write_exp_elt_opcode (OP_LONG);
675 case CHARACTER_STRING_LITERAL:
676 write_exp_elt_opcode (OP_STRING);
677 write_exp_string (PEEK_LVAL ().sval);
678 write_exp_elt_opcode (OP_STRING);
681 case BIT_STRING_LITERAL:
682 write_exp_elt_opcode (OP_BITSTRING);
683 write_exp_bitstring (PEEK_LVAL ().sval);
684 write_exp_elt_opcode (OP_BITSTRING);
689 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
690 which casts to an artificial array. */
693 if (PEEK_TOKEN () != TYPENAME)
694 error ("missing MODENAME after ARRAY()");
695 type = PEEK_LVAL().tsym.type;
699 expect (')', "missing right parenthesis");
700 type = create_array_type ((struct type *) NULL, type,
701 create_range_type ((struct type *) NULL,
702 builtin_type_int, 0, 0));
703 TYPE_ARRAY_UPPER_BOUND_TYPE(type) = BOUND_CANNOT_BE_DETERMINED;
704 write_exp_elt_opcode (UNOP_CAST);
705 write_exp_elt_type (type);
706 write_exp_elt_opcode (UNOP_CAST);
718 expect (')', "missing right parenthesis");
723 case GENERAL_PROCEDURE_NAME:
725 write_exp_elt_opcode (OP_VAR_VALUE);
726 write_exp_elt_block (NULL);
727 write_exp_elt_sym (PEEK_LVAL ().ssym.sym);
728 write_exp_elt_opcode (OP_VAR_VALUE);
731 case GDB_VARIABLE: /* gdb specific */
736 write_exp_elt_opcode (UNOP_CAST);
737 write_exp_elt_type (builtin_type_int);
738 write_exp_elt_opcode (UNOP_CAST);
742 write_exp_elt_opcode (UNOP_CARD);
746 write_exp_elt_opcode (UNOP_CHMAX);
750 write_exp_elt_opcode (UNOP_CHMIN);
752 case PRED: op_name = "PRED"; goto unimplemented_unary_builtin;
753 case SUCC: op_name = "SUCC"; goto unimplemented_unary_builtin;
754 case ABS: op_name = "ABS"; goto unimplemented_unary_builtin;
755 unimplemented_unary_builtin:
757 error ("not implemented: %s builtin function", op_name);
761 write_exp_elt_opcode (UNOP_ADDR);
764 type = parse_mode_or_normal_call ();
766 { write_exp_elt_opcode (OP_LONG);
767 write_exp_elt_type (builtin_type_int);
768 CHECK_TYPEDEF (type);
769 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH (type));
770 write_exp_elt_opcode (OP_LONG);
773 write_exp_elt_opcode (UNOP_SIZEOF);
782 type = parse_mode_or_normal_call ();
783 write_lower_upper_value (op, type);
787 write_exp_elt_opcode (UNOP_LENGTH);
790 type = PEEK_LVAL ().tsym.type;
792 switch (PEEK_TOKEN())
800 expect (')', "missing right parenthesis");
801 write_exp_elt_opcode (UNOP_CAST);
802 write_exp_elt_type (type);
803 write_exp_elt_opcode (UNOP_CAST);
806 error ("typename in invalid context");
811 error ("invalid expression syntax at `%s'", lexptr);
815 switch (PEEK_TOKEN ())
818 write_exp_elt_opcode (STRUCTOP_STRUCT);
819 write_exp_string (PEEK_LVAL ().sval);
820 write_exp_elt_opcode (STRUCTOP_STRUCT);
825 if (PEEK_TOKEN () == TYPENAME)
827 type = PEEK_LVAL ().tsym.type;
828 write_exp_elt_opcode (UNOP_CAST);
829 write_exp_elt_type (lookup_pointer_type (type));
830 write_exp_elt_opcode (UNOP_CAST);
833 write_exp_elt_opcode (UNOP_IND);
838 case CHARACTER_STRING_LITERAL:
839 case CHARACTER_LITERAL:
840 case BIT_STRING_LITERAL:
841 /* Handle string repetition. (See comment in parse_operand5.) */
843 write_exp_elt_opcode (MULTI_SUBSCRIPT);
844 write_exp_elt_longcst (1);
845 write_exp_elt_opcode (MULTI_SUBSCRIPT);
849 case INTEGER_LITERAL:
850 case BOOLEAN_LITERAL:
852 case GENERAL_PROCEDURE_NAME:
854 case EMPTINESS_LITERAL:
905 if (check_token (RECEIVE))
908 error ("not implemented: RECEIVE expression");
910 else if (check_token (POINTER))
913 write_exp_elt_opcode (UNOP_ADDR);
923 /* We are supposed to be looking for a <string repetition operator>,
924 but in general we can't distinguish that from a parenthesized
925 expression. This is especially difficult if we allow the
926 string operand to be a constant expression (as requested by
927 some users), and not just a string literal.
928 Consider: LPRN expr RPRN LPRN expr RPRN
929 Is that a function call or string repetition?
930 Instead, we handle string repetition in parse_primval,
931 and build_generalized_call. */
932 switch (PEEK_TOKEN())
934 case NOT: op = UNOP_LOGICAL_NOT; break;
935 case '-': op = UNOP_NEG; break;
943 write_exp_elt_opcode (op);
953 switch (PEEK_TOKEN())
955 case '*': op = BINOP_MUL; break;
956 case '/': op = BINOP_DIV; break;
957 case MOD: op = BINOP_MOD; break;
958 case REM: op = BINOP_REM; break;
964 write_exp_elt_opcode (op);
975 switch (PEEK_TOKEN())
977 case '+': op = BINOP_ADD; break;
978 case '-': op = BINOP_SUB; break;
979 case SLASH_SLASH: op = BINOP_CONCAT; break;
985 write_exp_elt_opcode (op);
996 if (check_token (IN))
999 write_exp_elt_opcode (BINOP_IN);
1003 switch (PEEK_TOKEN())
1005 case '>': op = BINOP_GTR; break;
1006 case GEQ: op = BINOP_GEQ; break;
1007 case '<': op = BINOP_LESS; break;
1008 case LEQ: op = BINOP_LEQ; break;
1009 case '=': op = BINOP_EQUAL; break;
1010 case NOTEQUAL: op = BINOP_NOTEQUAL; break;
1016 write_exp_elt_opcode (op);
1028 switch (PEEK_TOKEN())
1030 case LOGAND: op = BINOP_BITWISE_AND; break;
1031 case ANDIF: op = BINOP_LOGICAL_AND; break;
1037 write_exp_elt_opcode (op);
1048 switch (PEEK_TOKEN())
1050 case LOGIOR: op = BINOP_BITWISE_IOR; break;
1051 case LOGXOR: op = BINOP_BITWISE_XOR; break;
1052 case ORIF: op = BINOP_LOGICAL_OR; break;
1058 write_exp_elt_opcode (op);
1066 if (check_token (GDB_ASSIGNMENT))
1069 write_exp_elt_opcode (BINOP_ASSIGN);
1074 parse_then_alternative ()
1076 expect (THEN, "missing 'THEN' in 'IF' expression");
1081 parse_else_alternative ()
1083 if (check_token (ELSIF))
1084 parse_if_expression_body ();
1085 else if (check_token (ELSE))
1088 error ("missing ELSE/ELSIF in IF expression");
1091 /* Matches: <boolean expression> <then alternative> <else alternative> */
1094 parse_if_expression_body ()
1097 parse_then_alternative ();
1098 parse_else_alternative ();
1099 write_exp_elt_opcode (TERNOP_COND);
1103 parse_if_expression ()
1106 parse_if_expression_body ();
1107 expect (FI, "missing 'FI' at end of conditional expression");
1110 /* An <untyped_expr> is a superset of <expr>. It also includes
1111 <conditional expressions> and untyped <tuples>, whose types
1112 are not given by their constituents. Hence, these are only
1113 allowed in certain contexts that expect a certain type.
1114 You should call convert() to fix up the <untyped_expr>. */
1117 parse_untyped_expr ()
1119 switch (PEEK_TOKEN())
1122 parse_if_expression ();
1125 error ("not implemented: CASE expression");
1127 switch (PEEK_TOKEN1())
1135 parse_untyped_expr ();
1136 expect (')', "missing ')'");
1149 terminal_buffer[0] = TOKEN_NOT_READ;
1150 if (PEEK_TOKEN () == TYPENAME && PEEK_TOKEN1 () == END_TOKEN)
1152 write_exp_elt_opcode(OP_TYPE);
1153 write_exp_elt_type(PEEK_LVAL ().tsym.type);
1154 write_exp_elt_opcode(OP_TYPE);
1159 if (terminal_buffer[0] != END_TOKEN)
1161 if (comma_terminates && terminal_buffer[0] == ',')
1162 lexptr--; /* Put the comma back. */
1164 error ("Junk after end of expression.");
1170 /* Implementation of a dynamically expandable buffer for processing input
1171 characters acquired through lexptr and building a value to return in
1174 static char *tempbuf; /* Current buffer contents */
1175 static int tempbufsize; /* Size of allocated buffer */
1176 static int tempbufindex; /* Current index into buffer */
1178 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1180 #define CHECKBUF(size) \
1182 if (tempbufindex + (size) >= tempbufsize) \
1184 growbuf_by_size (size); \
1188 /* Grow the static temp buffer if necessary, including allocating the first one
1192 growbuf_by_size (count)
1197 growby = max (count, GROWBY_MIN_SIZE);
1198 tempbufsize += growby;
1199 if (tempbuf == NULL)
1201 tempbuf = (char *) xmalloc (tempbufsize);
1205 tempbuf = (char *) xrealloc (tempbuf, tempbufsize);
1209 /* Try to consume a simple name string token. If successful, returns
1210 a pointer to a nullbyte terminated copy of the name that can be used
1211 in symbol table lookups. If not successful, returns NULL. */
1214 match_simple_name_string ()
1216 char *tokptr = lexptr;
1218 if (isalpha (*tokptr) || *tokptr == '_')
1223 } while (isalnum (*tokptr) || (*tokptr == '_'));
1224 yylval.sval.ptr = lexptr;
1225 yylval.sval.length = tokptr - lexptr;
1227 result = copy_name (yylval.sval);
1233 /* Start looking for a value composed of valid digits as set by the base
1234 in use. Note that '_' characters are valid anywhere, in any quantity,
1235 and are simply ignored. Since we must find at least one valid digit,
1236 or reject this token as an integer literal, we keep track of how many
1237 digits we have encountered. */
1240 decode_integer_value (base, tokptrptr, ivalptr)
1245 char *tokptr = *tokptrptr;
1249 while (*tokptr != '\0')
1253 temp = tolower (temp);
1259 case '0': case '1': case '2': case '3': case '4':
1260 case '5': case '6': case '7': case '8': case '9':
1263 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1279 /* Found something not in domain for current base. */
1280 tokptr--; /* Unconsume what gave us indigestion. */
1285 /* If we didn't find any digits, then we don't have a valid integer
1286 value, so reject the entire token. Otherwise, update the lexical
1287 scan pointer, and return non-zero for success. */
1295 *tokptrptr = tokptr;
1301 decode_integer_literal (valptr, tokptrptr)
1305 char *tokptr = *tokptrptr;
1308 int explicit_base = 0;
1310 /* Look for an explicit base specifier, which is optional. */
1343 /* If we found an explicit base ensure that the character after the
1344 explicit base is a single quote. */
1346 if (explicit_base && (*tokptr++ != '\''))
1351 /* Attempt to decode whatever follows as an integer value in the
1352 indicated base, updating the token pointer in the process and
1353 computing the value into ival. Also, if we have an explicit
1354 base, then the next character must not be a single quote, or we
1355 have a bitstring literal, so reject the entire token in this case.
1356 Otherwise, update the lexical scan pointer, and return non-zero
1359 if (!decode_integer_value (base, &tokptr, &ival))
1363 else if (explicit_base && (*tokptr == '\''))
1370 *tokptrptr = tokptr;
1375 /* If it wasn't for the fact that floating point values can contain '_'
1376 characters, we could just let strtod do all the hard work by letting it
1377 try to consume as much of the current token buffer as possible and
1378 find a legal conversion. Unfortunately we need to filter out the '_'
1379 characters before calling strtod, which we do by copying the other
1380 legal chars to a local buffer to be converted. However since we also
1381 need to keep track of where the last unconsumed character in the input
1382 buffer is, we have transfer only as many characters as may compose a
1383 legal floating point value. */
1385 static enum ch_terminal
1386 match_float_literal ()
1388 char *tokptr = lexptr;
1392 extern double strtod ();
1394 /* Make local buffer in which to build the string to convert. This is
1395 required because underscores are valid in chill floating point numbers
1396 but not in the string passed to strtod to convert. The string will be
1397 no longer than our input string. */
1399 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1401 /* Transfer all leading digits to the conversion buffer, discarding any
1404 while (isdigit (*tokptr) || *tokptr == '_')
1413 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1414 of whether we found any leading digits, and we simply accept it and
1415 continue on to look for the fractional part and/or exponent. One of
1416 [eEdD] is legal only if we have seen digits, and means that there
1417 is no fractional part. If we find neither of these, then this is
1418 not a floating point number, so return failure. */
1423 /* Accept and then look for fractional part and/or exponent. */
1436 goto collect_exponent;
1444 /* We found a '.', copy any fractional digits to the conversion buffer, up
1445 to the first nondigit, non-underscore character. */
1447 while (isdigit (*tokptr) || *tokptr == '_')
1456 /* Look for an exponent, which must start with one of [eEdD]. If none
1457 is found, jump directly to trying to convert what we have collected
1474 /* Accept an optional '-' or '+' following one of [eEdD]. */
1477 if (*tokptr == '+' || *tokptr == '-')
1479 *copy++ = *tokptr++;
1482 /* Now copy an exponent into the conversion buffer. Note that at the
1483 moment underscores are *not* allowed in exponents. */
1485 while (isdigit (*tokptr))
1487 *copy++ = *tokptr++;
1490 /* If we transfered any chars to the conversion buffer, try to interpret its
1491 contents as a floating point value. If any characters remain, then we
1492 must not have a valid floating point string. */
1498 dval = strtod (buf, ©);
1503 return (FLOAT_LITERAL);
1509 /* Recognize a string literal. A string literal is a sequence
1510 of characters enclosed in matching single or double quotes, except that
1511 a single character inside single quotes is a character literal, which
1512 we reject as a string literal. To embed the terminator character inside
1513 a string, it is simply doubled (I.E. "this""is""one""string") */
1515 static enum ch_terminal
1516 match_string_literal ()
1518 char *tokptr = lexptr;
1522 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1528 /* skip possible whitespaces */
1529 while ((*tokptr == ' ' || *tokptr == '\t') && *tokptr)
1537 else if (*tokptr != ',')
1538 error ("Invalid control sequence");
1540 /* skip possible whitespaces */
1541 while ((*tokptr == ' ' || *tokptr == '\t') && *tokptr)
1543 if (!decode_integer_literal (&ival, &tokptr))
1544 error ("Invalid control sequence");
1547 else if (*tokptr == *lexptr)
1549 if (*(tokptr + 1) == *lexptr)
1558 else if (*tokptr == '^')
1560 if (*(tokptr + 1) == '(')
1564 if (!decode_integer_literal (&ival, &tokptr))
1565 error ("Invalid control sequence");
1568 else if (*(tokptr + 1) == '^')
1571 error ("Invalid control sequence");
1575 tempbuf[tempbufindex++] = ival;
1578 error ("Invalid control sequence");
1580 if (*tokptr == '\0' /* no terminator */
1581 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1587 tempbuf[tempbufindex] = '\0';
1588 yylval.sval.ptr = tempbuf;
1589 yylval.sval.length = tempbufindex;
1591 return (CHARACTER_STRING_LITERAL);
1595 /* Recognize a character literal. A character literal is single character
1596 or a control sequence, enclosed in single quotes. A control sequence
1597 is a comma separated list of one or more integer literals, enclosed
1598 in parenthesis and introduced with a circumflex character.
1600 EX: 'a' '^(7)' '^(7,8)'
1602 As a GNU chill extension, the syntax C'xx' is also recognized as a
1603 character literal, where xx is a hex value for the character.
1605 Note that more than a single character, enclosed in single quotes, is
1608 Returns CHARACTER_LITERAL if a match is found.
1611 static enum ch_terminal
1612 match_character_literal ()
1614 char *tokptr = lexptr;
1617 if ((*tokptr == 'c' || *tokptr == 'C') && (*(tokptr + 1) == '\''))
1619 /* We have a GNU chill extension form, so skip the leading "C'",
1620 decode the hex value, and then ensure that we have a trailing
1621 single quote character. */
1623 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1629 else if (*tokptr == '\'')
1633 /* Determine which form we have, either a control sequence or the
1634 single character form. */
1638 if (*(tokptr + 1) == '(')
1640 /* Match and decode a control sequence. Return zero if we don't
1641 find a valid integer literal, or if the next unconsumed character
1642 after the integer literal is not the trailing ')'. */
1644 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1649 else if (*(tokptr + 1) == '^')
1656 error ("Invalid control sequence");
1658 else if (*tokptr == '\'')
1660 /* this must be duplicated */
1669 /* The trailing quote has not yet been consumed. If we don't find
1670 it, then we have no match. */
1672 if (*tokptr++ != '\'')
1679 /* Not a character literal. */
1682 yylval.typed_val.val = ival;
1683 yylval.typed_val.type = builtin_type_chill_char;
1685 return (CHARACTER_LITERAL);
1688 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1689 Note that according to 5.2.4.2, a single "_" is also a valid integer
1690 literal, however GNU-chill requires there to be at least one "digit"
1691 in any integer literal. */
1693 static enum ch_terminal
1694 match_integer_literal ()
1696 char *tokptr = lexptr;
1699 if (!decode_integer_literal (&ival, &tokptr))
1705 yylval.typed_val.val = ival;
1706 #if defined(CC_HAS_LONG_LONG) && defined(__STDC__)
1707 if (ival > (LONGEST)2147483647U || ival < -(LONGEST)2147483648U)
1708 yylval.typed_val.type = builtin_type_long_long;
1711 yylval.typed_val.type = builtin_type_int;
1713 return (INTEGER_LITERAL);
1717 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1718 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1719 literal, however GNU-chill requires there to be at least one "digit"
1720 in any bit-string literal. */
1722 static enum ch_terminal
1723 match_bitstring_literal ()
1725 register char *tokptr = lexptr;
1735 /* Look for the required explicit base specifier. */
1756 /* Ensure that the character after the explicit base is a single quote. */
1758 if (*tokptr++ != '\'')
1763 while (*tokptr != '\0' && *tokptr != '\'')
1766 if (isupper (digit))
1767 digit = tolower (digit);
1773 case '0': case '1': case '2': case '3': case '4':
1774 case '5': case '6': case '7': case '8': case '9':
1777 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1782 /* this is not a bitstring literal, probably an integer */
1785 if (digit >= 1 << bits_per_char)
1787 /* Found something not in domain for current base. */
1788 error ("Too-large digit in bitstring or integer.");
1792 /* Extract bits from digit, packing them into the bitstring byte. */
1793 int k = TARGET_BYTE_ORDER == BIG_ENDIAN ? bits_per_char - 1 : 0;
1794 for (; TARGET_BYTE_ORDER == BIG_ENDIAN ? k >= 0 : k < bits_per_char;
1795 TARGET_BYTE_ORDER == BIG_ENDIAN ? k-- : k++)
1798 if (digit & (1 << k))
1800 tempbuf[tempbufindex] |=
1801 (TARGET_BYTE_ORDER == BIG_ENDIAN)
1802 ? (1 << (HOST_CHAR_BIT - 1 - bitoffset))
1806 if (bitoffset == HOST_CHAR_BIT)
1811 tempbuf[tempbufindex] = 0;
1817 /* Verify that we consumed everything up to the trailing single quote,
1818 and that we found some bits (IE not just underbars). */
1820 if (*tokptr++ != '\'')
1826 yylval.sval.ptr = tempbuf;
1827 yylval.sval.length = bitcount;
1829 return (BIT_STRING_LITERAL);
1839 static const struct token idtokentab[] =
1842 { "length", LENGTH },
1853 { "max", MAX_TOKEN },
1854 { "min", MIN_TOKEN },
1863 { "addr", ADDR_TOKEN },
1864 { "null", EMPTINESS_LITERAL }
1867 static const struct token tokentab2[] =
1869 { ":=", GDB_ASSIGNMENT },
1870 { "//", SLASH_SLASH },
1877 /* Read one token, getting characters through lexptr. */
1878 /* This is where we will check to make sure that the language and the
1879 operators used are compatible. */
1881 static enum ch_terminal
1885 enum ch_terminal token;
1889 /* Skip over any leading whitespace. */
1890 while (isspace (*lexptr))
1894 /* Look for special single character cases which can't be the first
1895 character of some other multicharacter token. */
1912 /* Look for characters which start a particular kind of multicharacter
1913 token, such as a character literal, register name, convenience
1914 variable name, string literal, etc. */
1919 /* First try to match a string literal, which is any
1920 sequence of characters enclosed in matching single or double
1921 quotes, except that a single character inside single quotes
1922 is a character literal, so we have to catch that case also. */
1923 token = match_string_literal ();
1928 if (*lexptr == '\'')
1930 token = match_character_literal ();
1939 token = match_character_literal ();
1946 yylval.sval.ptr = lexptr;
1949 } while (isalnum (*lexptr) || *lexptr == '_' || *lexptr == '$');
1950 yylval.sval.length = lexptr - yylval.sval.ptr;
1951 write_dollar_variable (yylval.sval);
1952 return GDB_VARIABLE;
1955 /* See if it is a special token of length 2. */
1956 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1958 if (STREQN (lexptr, tokentab2[i].operator, 2))
1961 return (tokentab2[i].token);
1964 /* Look for single character cases which which could be the first
1965 character of some other multicharacter token, but aren't, or we
1966 would already have found it. */
1976 /* Look for a float literal before looking for an integer literal, so
1977 we match as much of the input stream as possible. */
1978 token = match_float_literal ();
1983 token = match_bitstring_literal ();
1988 token = match_integer_literal ();
1994 /* Try to match a simple name string, and if a match is found, then
1995 further classify what sort of name it is and return an appropriate
1996 token. Note that attempting to match a simple name string consumes
1997 the token from lexptr, so we can't back out if we later find that
1998 we can't classify what sort of name it is. */
2000 inputname = match_simple_name_string ();
2002 if (inputname != NULL)
2004 char *simplename = (char*) alloca (strlen (inputname) + 1);
2006 char *dptr = simplename, *sptr = inputname;
2007 for (; *sptr; sptr++)
2008 *dptr++ = isupper (*sptr) ? tolower(*sptr) : *sptr;
2011 /* See if it is a reserved identifier. */
2012 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
2014 if (STREQ (simplename, idtokentab[i].operator))
2016 return (idtokentab[i].token);
2020 /* Look for other special tokens. */
2021 if (STREQ (simplename, "true"))
2024 return (BOOLEAN_LITERAL);
2026 if (STREQ (simplename, "false"))
2029 return (BOOLEAN_LITERAL);
2032 sym = lookup_symbol (inputname, expression_context_block,
2033 VAR_NAMESPACE, (int *) NULL,
2034 (struct symtab **) NULL);
2035 if (sym == NULL && strcmp (inputname, simplename) != 0)
2037 sym = lookup_symbol (simplename, expression_context_block,
2038 VAR_NAMESPACE, (int *) NULL,
2039 (struct symtab **) NULL);
2043 yylval.ssym.stoken.ptr = NULL;
2044 yylval.ssym.stoken.length = 0;
2045 yylval.ssym.sym = sym;
2046 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
2047 switch (SYMBOL_CLASS (sym))
2050 /* Found a procedure name. */
2051 return (GENERAL_PROCEDURE_NAME);
2053 /* Found a global or local static variable. */
2054 return (LOCATION_NAME);
2059 case LOC_REGPARM_ADDR:
2063 case LOC_BASEREG_ARG:
2064 if (innermost_block == NULL
2065 || contained_in (block_found, innermost_block))
2067 innermost_block = block_found;
2069 return (LOCATION_NAME);
2073 return (LOCATION_NAME);
2076 yylval.tsym.type = SYMBOL_TYPE (sym);
2079 case LOC_CONST_BYTES:
2080 case LOC_OPTIMIZED_OUT:
2081 error ("Symbol \"%s\" names no location.", inputname);
2083 case LOC_UNRESOLVED:
2084 error ("unhandled SYMBOL_CLASS in ch_lex()");
2088 else if (!have_full_symbols () && !have_partial_symbols ())
2090 error ("No symbol table is loaded. Use the \"file\" command.");
2094 error ("No symbol \"%s\" in current context.", inputname);
2098 /* Catch single character tokens which are not part of some
2103 case '.': /* Not float for example. */
2105 while (isspace (*lexptr)) lexptr++;
2106 inputname = match_simple_name_string ();
2112 return (ILLEGAL_TOKEN);
2116 write_lower_upper_value (opcode, type)
2117 enum exp_opcode opcode; /* Either UNOP_LOWER or UNOP_UPPER */
2121 write_exp_elt_opcode (opcode);
2124 struct type *result_type;
2125 LONGEST val = type_lower_upper (opcode, type, &result_type);
2126 write_exp_elt_opcode (OP_LONG);
2127 write_exp_elt_type (result_type);
2128 write_exp_elt_longcst (val);
2129 write_exp_elt_opcode (OP_LONG);