1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright 1992, 1993, 1994 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 malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator.
37 Also note that the language accepted by this parser is more liberal
38 than the one accepted by an actual Chill compiler. For example, the
39 language rule that a simple name string can not be one of the reserved
40 simple name strings is not enforced (e.g "case" is not treated as a
41 reserved name). Another example is that Chill is a strongly typed
42 language, and certain expressions that violate the type constraints
43 may still be evaluated if gdb can do so in a meaningful manner, while
44 such expressions would be rejected by the compiler. The reason for
45 this more liberal behavior is the philosophy that the debugger
46 is intended to be a tool that is used by the programmer when things
47 go wrong, and as such, it should provide as few artificial barriers
48 to it's use as possible. If it can do something meaningful, even
49 something that violates language contraints that are enforced by the
50 compiler, it should do so without complaint.
59 #include "expression.h"
62 #include "parser-defs.h"
64 #include "bfd.h" /* Required by objfiles.h. */
65 #include "symfile.h" /* Required by objfiles.h. */
66 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
68 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
69 as well as gratuitiously global symbol names, so we can have multiple
70 yacc generated parsers in gdb. Note that these are only the variables
71 produced by yacc. If other parser generators (bison, byacc, etc) produce
72 additional global names that conflict at link time, then those parser
73 generators need to be fixed instead of adding those names to this list. */
75 #define yymaxdepth chill_maxdepth
76 #define yyparse chill_parse
77 #define yylex chill_lex
78 #define yyerror chill_error
79 #define yylval chill_lval
80 #define yychar chill_char
81 #define yydebug chill_debug
82 #define yypact chill_pact
85 #define yydef chill_def
86 #define yychk chill_chk
87 #define yypgo chill_pgo
88 #define yyact chill_act
89 #define yyexca chill_exca
90 #define yyerrflag chill_errflag
91 #define yynerrs chill_nerrs
95 #define yy_yys chill_yys
96 #define yystate chill_state
97 #define yytmp chill_tmp
99 #define yy_yyv chill_yyv
100 #define yyval chill_val
101 #define yylloc chill_lloc
102 #define yyreds chill_reds /* With YYDEBUG defined */
103 #define yytoks chill_toks /* With YYDEBUG defined */
104 #define yylhs chill_yylhs
105 #define yylen chill_yylen
106 #define yydefred chill_yydefred
107 #define yydgoto chill_yydgoto
108 #define yysindex chill_yysindex
109 #define yyrindex chill_yyrindex
110 #define yygindex chill_yygindex
111 #define yytable chill_yytable
112 #define yycheck chill_yycheck
115 #define YYDEBUG 0 /* Default to no yydebug support */
119 write_lower_upper_value PARAMS ((enum exp_opcode, struct type *type));
122 yyparse PARAMS ((void));
125 yylex PARAMS ((void));
128 yyerror PARAMS ((char *));
132 /* Although the yacc "value" of an expression is not used,
133 since the result is stored in the structure being created,
134 other node types do have values. */
139 unsigned LONGEST ulval;
149 struct symtoken ssym;
152 enum exp_opcode opcode;
153 struct internalvar *ivar;
159 %token <typed_val> INTEGER_LITERAL
160 %token <ulval> BOOLEAN_LITERAL
161 %token <typed_val> CHARACTER_LITERAL
162 %token <dval> FLOAT_LITERAL
163 %token <ssym> GENERAL_PROCEDURE_NAME
164 %token <ssym> LOCATION_NAME
165 %token <voidval> EMPTINESS_LITERAL
166 %token <sval> CHARACTER_STRING_LITERAL
167 %token <sval> BIT_STRING_LITERAL
168 %token <tsym> TYPENAME
169 %token <sval> FIELD_NAME
174 %token <voidval> CASE
176 %token <voidval> ESAC
177 %token <voidval> LOGIOR
178 %token <voidval> ORIF
179 %token <voidval> LOGXOR
180 %token <voidval> LOGAND
181 %token <voidval> ANDIF
183 %token <voidval> NOTEQUAL
193 %token <voidval> SLASH_SLASH
197 %token <voidval> POINTER
198 %token <voidval> RECEIVE
205 %token <voidval> THEN
206 %token <voidval> ELSE
208 %token <voidval> ELSIF
209 %token <voidval> ILLEGAL_TOKEN
211 %token <voidval> PRED
212 %token <voidval> SUCC
214 %token <voidval> CARD
215 %token <voidval> MAX_TOKEN
216 %token <voidval> MIN_TOKEN
217 %token <voidval> ADDR_TOKEN
218 %token <voidval> SIZE
219 %token <voidval> UPPER
220 %token <voidval> LOWER
221 %token <voidval> LENGTH
222 %token <voidval> ARRAY
224 /* Tokens which are not Chill tokens used in expressions, but rather GDB
225 specific things that we recognize in the same context as Chill tokens
226 (register names for example). */
228 %token <voidval> GDB_VARIABLE /* Convenience variable */
229 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
231 %type <voidval> access_name
232 %type <voidval> primitive_value
233 %type <voidval> value_name
234 %type <voidval> literal
235 %type <voidval> tuple
236 %type <voidval> slice
237 %type <voidval> expression_conversion
238 %type <voidval> value_built_in_routine_call
239 %type <voidval> parenthesised_expression
240 %type <voidval> value
241 %type <voidval> expression
242 %type <voidval> conditional_expression
243 %type <voidval> then_alternative
244 %type <voidval> else_alternative
245 %type <voidval> operand_0
246 %type <voidval> operand_1
247 %type <voidval> operand_2
248 %type <voidval> operand_3
249 %type <voidval> operand_4
250 %type <voidval> operand_5
251 %type <voidval> operand_6
252 %type <voidval> expression_list
253 %type <tval> mode_argument
254 %type <voidval> single_assignment_action
255 %type <tsym> mode_name
259 %type <voidval> undefined_value
260 %type <voidval> array_mode_name
261 %type <voidval> string_mode_name
262 %type <voidval> variant_structure_mode_name
271 { write_exp_elt_opcode(OP_TYPE);
272 write_exp_elt_type($1.type);
273 write_exp_elt_opcode(OP_TYPE);}
285 access_name : LOCATION_NAME
287 write_exp_elt_opcode (OP_VAR_VALUE);
288 write_exp_elt_block (NULL);
289 write_exp_elt_sym ($1.sym);
290 write_exp_elt_opcode (OP_VAR_VALUE);
292 | GDB_VARIABLE /* gdb specific */
297 expression_list : expression
301 | expression_list ',' expression
307 maybe_expression_list: /* EMPTY */
317 primitive_value_lparen: primitive_value '('
318 /* This is to save the value of arglist_len
319 being accumulated for each dimension. */
320 { start_arglist (); }
324 { $$ = end_arglist (); }
329 | primitive_value_lparen maybe_expression_list rparen
331 write_exp_elt_opcode (MULTI_SUBSCRIPT);
332 write_exp_elt_longcst ($3);
333 write_exp_elt_opcode (MULTI_SUBSCRIPT);
335 | primitive_value FIELD_NAME
336 { write_exp_elt_opcode (STRUCTOP_STRUCT);
337 write_exp_string ($2);
338 write_exp_elt_opcode (STRUCTOP_STRUCT);
340 | primitive_value POINTER
342 write_exp_elt_opcode (UNOP_IND);
344 | primitive_value POINTER mode_name
346 write_exp_elt_opcode (UNOP_CAST);
347 write_exp_elt_type (lookup_pointer_type ($3.type));
348 write_exp_elt_opcode (UNOP_CAST);
349 write_exp_elt_opcode (UNOP_IND);
355 | expression_conversion
356 | value_built_in_routine_call
363 | parenthesised_expression
368 value_name : GENERAL_PROCEDURE_NAME
370 write_exp_elt_opcode (OP_VAR_VALUE);
371 write_exp_elt_block (NULL);
372 write_exp_elt_sym ($1.sym);
373 write_exp_elt_opcode (OP_VAR_VALUE);
379 literal : INTEGER_LITERAL
381 write_exp_elt_opcode (OP_LONG);
382 write_exp_elt_type ($1.type);
383 write_exp_elt_longcst ((LONGEST) ($1.val));
384 write_exp_elt_opcode (OP_LONG);
388 write_exp_elt_opcode (OP_BOOL);
389 write_exp_elt_longcst ((LONGEST) $1);
390 write_exp_elt_opcode (OP_BOOL);
394 write_exp_elt_opcode (OP_LONG);
395 write_exp_elt_type ($1.type);
396 write_exp_elt_longcst ((LONGEST) ($1.val));
397 write_exp_elt_opcode (OP_LONG);
401 write_exp_elt_opcode (OP_DOUBLE);
402 write_exp_elt_type (builtin_type_double);
403 write_exp_elt_dblcst ($1);
404 write_exp_elt_opcode (OP_DOUBLE);
408 struct type *void_ptr_type
409 = lookup_pointer_type (builtin_type_void);
410 write_exp_elt_opcode (OP_LONG);
411 write_exp_elt_type (void_ptr_type);
412 write_exp_elt_longcst (0);
413 write_exp_elt_opcode (OP_LONG);
415 | CHARACTER_STRING_LITERAL
417 write_exp_elt_opcode (OP_STRING);
418 write_exp_string ($1);
419 write_exp_elt_opcode (OP_STRING);
423 write_exp_elt_opcode (OP_BITSTRING);
424 write_exp_bitstring ($1);
425 write_exp_elt_opcode (OP_BITSTRING);
431 tuple_element : expression
432 | named_record_element
435 named_record_element: FIELD_NAME ',' named_record_element
436 { write_exp_elt_opcode (OP_LABELED);
437 write_exp_string ($1);
438 write_exp_elt_opcode (OP_LABELED);
440 | FIELD_NAME ':' expression
441 { write_exp_elt_opcode (OP_LABELED);
442 write_exp_string ($1);
443 write_exp_elt_opcode (OP_LABELED);
447 tuple_elements : tuple_element
451 | tuple_elements ',' tuple_element
457 maybe_tuple_elements : tuple_elements
462 { start_arglist (); }
463 maybe_tuple_elements ']'
465 write_exp_elt_opcode (OP_ARRAY);
466 write_exp_elt_longcst ((LONGEST) 0);
467 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
468 write_exp_elt_opcode (OP_ARRAY);
472 { start_arglist (); }
473 maybe_tuple_elements ']'
475 write_exp_elt_opcode (OP_ARRAY);
476 write_exp_elt_longcst ((LONGEST) 0);
477 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
478 write_exp_elt_opcode (OP_ARRAY);
480 write_exp_elt_opcode (UNOP_CAST);
481 write_exp_elt_type ($1.type);
482 write_exp_elt_opcode (UNOP_CAST);
490 slice: primitive_value_lparen expression ':' expression rparen
492 write_exp_elt_opcode (TERNOP_SLICE);
494 | primitive_value_lparen expression UP expression rparen
496 write_exp_elt_opcode (TERNOP_SLICE_COUNT);
502 expression_conversion: mode_name parenthesised_expression
504 write_exp_elt_opcode (UNOP_CAST);
505 write_exp_elt_type ($1.type);
506 write_exp_elt_opcode (UNOP_CAST);
508 | ARRAY '(' ')' mode_name parenthesised_expression
509 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
510 which casts to an artificial array. */
512 struct type *range_type
513 = create_range_type ((struct type *) NULL,
514 builtin_type_int, 0, 0);
515 struct type *array_type
516 = create_array_type ((struct type *) NULL,
517 $4.type, range_type);
518 TYPE_ARRAY_UPPER_BOUND_TYPE(array_type)
519 = BOUND_CANNOT_BE_DETERMINED;
520 write_exp_elt_opcode (UNOP_CAST);
521 write_exp_elt_type (array_type);
522 write_exp_elt_opcode (UNOP_CAST);
528 parenthesised_expression: '(' expression ')'
533 expression : operand_0
534 | single_assignment_action
535 | conditional_expression
538 conditional_expression : IF expression then_alternative else_alternative FI
539 { write_exp_elt_opcode (TERNOP_COND); }
541 | CASE case_selector_list OF value_case_alternative ELSE expression ESAC
542 { error ("not implemented: CASE expression" }
546 then_alternative: THEN expression
549 else_alternative: ELSE expression
550 | ELSIF expression then_alternative else_alternative
551 { write_exp_elt_opcode (TERNOP_COND); }
556 operand_0 : operand_1
557 | operand_0 LOGIOR operand_1
559 write_exp_elt_opcode (BINOP_BITWISE_IOR);
561 | operand_0 ORIF operand_1
563 write_exp_elt_opcode (BINOP_LOGICAL_OR);
565 | operand_0 LOGXOR operand_1
567 write_exp_elt_opcode (BINOP_BITWISE_XOR);
573 operand_1 : operand_2
574 | operand_1 LOGAND operand_2
576 write_exp_elt_opcode (BINOP_BITWISE_AND);
578 | operand_1 ANDIF operand_2
580 write_exp_elt_opcode (BINOP_LOGICAL_AND);
586 operand_2 : operand_3
587 | operand_2 '=' operand_3
589 write_exp_elt_opcode (BINOP_EQUAL);
591 | operand_2 NOTEQUAL operand_3
593 write_exp_elt_opcode (BINOP_NOTEQUAL);
595 | operand_2 '>' operand_3
597 write_exp_elt_opcode (BINOP_GTR);
599 | operand_2 GTR operand_3
601 write_exp_elt_opcode (BINOP_GEQ);
603 | operand_2 '<' operand_3
605 write_exp_elt_opcode (BINOP_LESS);
607 | operand_2 LEQ operand_3
609 write_exp_elt_opcode (BINOP_LEQ);
611 | operand_2 IN operand_3
613 write_exp_elt_opcode (BINOP_IN);
620 operand_3 : operand_4
621 | operand_3 '+' operand_4
623 write_exp_elt_opcode (BINOP_ADD);
625 | operand_3 '-' operand_4
627 write_exp_elt_opcode (BINOP_SUB);
629 | operand_3 SLASH_SLASH operand_4
631 write_exp_elt_opcode (BINOP_CONCAT);
637 operand_4 : operand_5
638 | operand_4 '*' operand_5
640 write_exp_elt_opcode (BINOP_MUL);
642 | operand_4 '/' operand_5
644 write_exp_elt_opcode (BINOP_DIV);
646 | operand_4 MOD operand_5
648 write_exp_elt_opcode (BINOP_MOD);
650 | operand_4 REM operand_5
652 write_exp_elt_opcode (BINOP_REM);
658 operand_5 : operand_6
661 write_exp_elt_opcode (UNOP_NEG);
665 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
667 | parenthesised_expression literal
668 /* We require the string operand to be a literal, to avoid some
669 nasty parsing ambiguities. */
671 write_exp_elt_opcode (BINOP_CONCAT);
677 operand_6 : POINTER primitive_value
679 write_exp_elt_opcode (UNOP_ADDR);
682 { error ("not implemented: RECEIVE expression"); }
689 single_assignment_action :
690 primitive_value GDB_ASSIGNMENT value
692 write_exp_elt_opcode (BINOP_ASSIGN);
698 value_built_in_routine_call :
699 NUM '(' expression ')'
701 write_exp_elt_opcode (UNOP_CAST);
702 write_exp_elt_type (builtin_type_int);
703 write_exp_elt_opcode (UNOP_CAST);
705 | PRED '(' expression ')'
706 { error ("not implemented: PRED builtin function"); }
707 | SUCC '(' expression ')'
708 { error ("not implemented: SUCC builtin function"); }
709 | ADDR_TOKEN '(' expression ')'
710 { write_exp_elt_opcode (UNOP_ADDR); }
711 | ABS '(' expression ')'
712 { error ("not implemented: ABS builtin function"); }
713 | CARD '(' expression ')'
714 { error ("not implemented: CARD builtin function"); }
715 | MAX_TOKEN '(' expression ')'
716 { error ("not implemented: MAX builtin function"); }
717 | MIN_TOKEN '(' expression ')'
718 { error ("not implemented: MIN builtin function"); }
719 | SIZE '(' expression ')'
720 { write_exp_elt_opcode (UNOP_SIZEOF); }
721 | SIZE '(' mode_argument ')'
722 { write_exp_elt_opcode (OP_LONG);
723 write_exp_elt_type (builtin_type_int);
724 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
725 write_exp_elt_opcode (OP_LONG); }
726 | LOWER '(' mode_argument ')'
727 { write_lower_upper_value (UNOP_LOWER, $3); }
728 | UPPER '(' mode_argument ')'
729 { write_lower_upper_value (UNOP_UPPER, $3); }
730 | LOWER '(' expression ')'
731 { write_exp_elt_opcode (UNOP_LOWER); }
732 | UPPER '(' expression ')'
733 { write_exp_elt_opcode (UNOP_UPPER); }
734 | LENGTH '(' expression ')'
735 { write_exp_elt_opcode (UNOP_LENGTH); }
738 mode_argument : mode_name
743 | array_mode_name '(' expression ')'
745 | string_mode_name '(' expression ')'
747 | variant_structure_mode_name '(' expression_list ')'
757 /* Implementation of a dynamically expandable buffer for processing input
758 characters acquired through lexptr and building a value to return in
761 static char *tempbuf; /* Current buffer contents */
762 static int tempbufsize; /* Size of allocated buffer */
763 static int tempbufindex; /* Current index into buffer */
765 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
767 #define CHECKBUF(size) \
769 if (tempbufindex + (size) >= tempbufsize) \
771 growbuf_by_size (size); \
775 /* Grow the static temp buffer if necessary, including allocating the first one
779 growbuf_by_size (count)
784 growby = max (count, GROWBY_MIN_SIZE);
785 tempbufsize += growby;
788 tempbuf = (char *) malloc (tempbufsize);
792 tempbuf = (char *) realloc (tempbuf, tempbufsize);
796 /* Try to consume a simple name string token. If successful, returns
797 a pointer to a nullbyte terminated copy of the name that can be used
798 in symbol table lookups. If not successful, returns NULL. */
801 match_simple_name_string ()
803 char *tokptr = lexptr;
805 if (isalpha (*tokptr) || *tokptr == '_')
810 } while (isalnum (*tokptr) || (*tokptr == '_'));
811 yylval.sval.ptr = lexptr;
812 yylval.sval.length = tokptr - lexptr;
814 result = copy_name (yylval.sval);
820 /* Start looking for a value composed of valid digits as set by the base
821 in use. Note that '_' characters are valid anywhere, in any quantity,
822 and are simply ignored. Since we must find at least one valid digit,
823 or reject this token as an integer literal, we keep track of how many
824 digits we have encountered. */
827 decode_integer_value (base, tokptrptr, ivalptr)
832 char *tokptr = *tokptrptr;
836 while (*tokptr != '\0')
840 temp = tolower (temp);
846 case '0': case '1': case '2': case '3': case '4':
847 case '5': case '6': case '7': case '8': case '9':
850 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
866 /* Found something not in domain for current base. */
867 tokptr--; /* Unconsume what gave us indigestion. */
872 /* If we didn't find any digits, then we don't have a valid integer
873 value, so reject the entire token. Otherwise, update the lexical
874 scan pointer, and return non-zero for success. */
888 decode_integer_literal (valptr, tokptrptr)
892 char *tokptr = *tokptrptr;
895 int explicit_base = 0;
897 /* Look for an explicit base specifier, which is optional. */
930 /* If we found an explicit base ensure that the character after the
931 explicit base is a single quote. */
933 if (explicit_base && (*tokptr++ != '\''))
938 /* Attempt to decode whatever follows as an integer value in the
939 indicated base, updating the token pointer in the process and
940 computing the value into ival. Also, if we have an explicit
941 base, then the next character must not be a single quote, or we
942 have a bitstring literal, so reject the entire token in this case.
943 Otherwise, update the lexical scan pointer, and return non-zero
946 if (!decode_integer_value (base, &tokptr, &ival))
950 else if (explicit_base && (*tokptr == '\''))
962 /* If it wasn't for the fact that floating point values can contain '_'
963 characters, we could just let strtod do all the hard work by letting it
964 try to consume as much of the current token buffer as possible and
965 find a legal conversion. Unfortunately we need to filter out the '_'
966 characters before calling strtod, which we do by copying the other
967 legal chars to a local buffer to be converted. However since we also
968 need to keep track of where the last unconsumed character in the input
969 buffer is, we have transfer only as many characters as may compose a
970 legal floating point value. */
973 match_float_literal ()
975 char *tokptr = lexptr;
979 extern double strtod ();
981 /* Make local buffer in which to build the string to convert. This is
982 required because underscores are valid in chill floating point numbers
983 but not in the string passed to strtod to convert. The string will be
984 no longer than our input string. */
986 copy = buf = (char *) alloca (strlen (tokptr) + 1);
988 /* Transfer all leading digits to the conversion buffer, discarding any
991 while (isdigit (*tokptr) || *tokptr == '_')
1000 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1001 of whether we found any leading digits, and we simply accept it and
1002 continue on to look for the fractional part and/or exponent. One of
1003 [eEdD] is legal only if we have seen digits, and means that there
1004 is no fractional part. If we find neither of these, then this is
1005 not a floating point number, so return failure. */
1010 /* Accept and then look for fractional part and/or exponent. */
1023 goto collect_exponent;
1031 /* We found a '.', copy any fractional digits to the conversion buffer, up
1032 to the first nondigit, non-underscore character. */
1034 while (isdigit (*tokptr) || *tokptr == '_')
1043 /* Look for an exponent, which must start with one of [eEdD]. If none
1044 is found, jump directly to trying to convert what we have collected
1061 /* Accept an optional '-' or '+' following one of [eEdD]. */
1064 if (*tokptr == '+' || *tokptr == '-')
1066 *copy++ = *tokptr++;
1069 /* Now copy an exponent into the conversion buffer. Note that at the
1070 moment underscores are *not* allowed in exponents. */
1072 while (isdigit (*tokptr))
1074 *copy++ = *tokptr++;
1077 /* If we transfered any chars to the conversion buffer, try to interpret its
1078 contents as a floating point value. If any characters remain, then we
1079 must not have a valid floating point string. */
1085 dval = strtod (buf, ©);
1090 return (FLOAT_LITERAL);
1096 /* Recognize a string literal. A string literal is a sequence
1097 of characters enclosed in matching single or double quotes, except that
1098 a single character inside single quotes is a character literal, which
1099 we reject as a string literal. To embed the terminator character inside
1100 a string, it is simply doubled (I.E. "this""is""one""string") */
1103 match_string_literal ()
1105 char *tokptr = lexptr;
1107 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1110 if (*tokptr == *lexptr)
1112 if (*(tokptr + 1) == *lexptr)
1121 tempbuf[tempbufindex++] = *tokptr;
1123 if (*tokptr == '\0' /* no terminator */
1124 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1130 tempbuf[tempbufindex] = '\0';
1131 yylval.sval.ptr = tempbuf;
1132 yylval.sval.length = tempbufindex;
1134 return (CHARACTER_STRING_LITERAL);
1138 /* Recognize a character literal. A character literal is single character
1139 or a control sequence, enclosed in single quotes. A control sequence
1140 is a comma separated list of one or more integer literals, enclosed
1141 in parenthesis and introduced with a circumflex character.
1143 EX: 'a' '^(7)' '^(7,8)'
1145 As a GNU chill extension, the syntax C'xx' is also recognized as a
1146 character literal, where xx is a hex value for the character.
1148 Note that more than a single character, enclosed in single quotes, is
1151 Also note that the control sequence form is not in GNU Chill since it
1152 is ambiguous with the string literal form using single quotes. I.E.
1153 is '^(7)' a character literal or a string literal. In theory it it
1154 possible to tell by context, but GNU Chill doesn't accept the control
1155 sequence form, so neither do we (for now the code is disabled).
1157 Returns CHARACTER_LITERAL if a match is found.
1161 match_character_literal ()
1163 char *tokptr = lexptr;
1166 if ((*tokptr == 'c' || *tokptr == 'C') && (*(tokptr + 1) == '\''))
1168 /* We have a GNU chill extension form, so skip the leading "C'",
1169 decode the hex value, and then ensure that we have a trailing
1170 single quote character. */
1172 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1178 else if (*tokptr == '\'')
1182 /* Determine which form we have, either a control sequence or the
1183 single character form. */
1185 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1187 #if 0 /* Disable, see note above. -fnf */
1188 /* Match and decode a control sequence. Return zero if we don't
1189 find a valid integer literal, or if the next unconsumed character
1190 after the integer literal is not the trailing ')'.
1191 FIXME: We currently don't handle the multiple integer literal
1194 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1207 /* The trailing quote has not yet been consumed. If we don't find
1208 it, then we have no match. */
1210 if (*tokptr++ != '\'')
1217 /* Not a character literal. */
1220 yylval.typed_val.val = ival;
1221 yylval.typed_val.type = builtin_type_chill_char;
1223 return (CHARACTER_LITERAL);
1226 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1227 Note that according to 5.2.4.2, a single "_" is also a valid integer
1228 literal, however GNU-chill requires there to be at least one "digit"
1229 in any integer literal. */
1232 match_integer_literal ()
1234 char *tokptr = lexptr;
1237 if (!decode_integer_literal (&ival, &tokptr))
1243 yylval.typed_val.val = ival;
1244 yylval.typed_val.type = builtin_type_int;
1246 return (INTEGER_LITERAL);
1250 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1251 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1252 literal, however GNU-chill requires there to be at least one "digit"
1253 in any bit-string literal. */
1256 match_bitstring_literal ()
1258 register char *tokptr = lexptr;
1268 /* Look for the required explicit base specifier. */
1289 /* Ensure that the character after the explicit base is a single quote. */
1291 if (*tokptr++ != '\'')
1296 while (*tokptr != '\0' && *tokptr != '\'')
1299 if (isupper (digit))
1300 digit = tolower (digit);
1306 case '0': case '1': case '2': case '3': case '4':
1307 case '5': case '6': case '7': case '8': case '9':
1310 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1318 if (digit >= 1 << bits_per_char)
1320 /* Found something not in domain for current base. */
1325 /* Extract bits from digit, packing them into the bitstring byte. */
1326 int k = TARGET_BYTE_ORDER == BIG_ENDIAN ? bits_per_char - 1 : 0;
1327 for (; TARGET_BYTE_ORDER == BIG_ENDIAN ? k >= 0 : k < bits_per_char;
1328 TARGET_BYTE_ORDER == BIG_ENDIAN ? k-- : k++)
1331 if (digit & (1 << k))
1333 tempbuf[tempbufindex] |=
1334 (TARGET_BYTE_ORDER == BIG_ENDIAN)
1335 ? (1 << (HOST_CHAR_BIT - 1 - bitoffset))
1339 if (bitoffset == HOST_CHAR_BIT)
1344 tempbuf[tempbufindex] = 0;
1350 /* Verify that we consumed everything up to the trailing single quote,
1351 and that we found some bits (IE not just underbars). */
1353 if (*tokptr++ != '\'')
1359 yylval.sval.ptr = tempbuf;
1360 yylval.sval.length = bitcount;
1362 return (BIT_STRING_LITERAL);
1372 static const struct token idtokentab[] =
1375 { "length", LENGTH },
1386 { "max", MAX_TOKEN },
1387 { "min", MIN_TOKEN },
1396 { "addr", ADDR_TOKEN },
1397 { "null", EMPTINESS_LITERAL }
1400 static const struct token tokentab2[] =
1402 { ":=", GDB_ASSIGNMENT },
1403 { "//", SLASH_SLASH },
1410 /* Read one token, getting characters through lexptr. */
1411 /* This is where we will check to make sure that the language and the
1412 operators used are compatible. */
1422 /* Skip over any leading whitespace. */
1423 while (isspace (*lexptr))
1427 /* Look for special single character cases which can't be the first
1428 character of some other multicharacter token. */
1445 /* Look for characters which start a particular kind of multicharacter
1446 token, such as a character literal, register name, convenience
1447 variable name, string literal, etc. */
1452 /* First try to match a string literal, which is any
1453 sequence of characters enclosed in matching single or double
1454 quotes, except that a single character inside single quotes
1455 is a character literal, so we have to catch that case also. */
1456 token = match_string_literal ();
1461 if (*lexptr == '\'')
1463 token = match_character_literal ();
1472 token = match_character_literal ();
1479 yylval.sval.ptr = lexptr;
1482 } while (isalnum (*lexptr) || (lexptr == '_'));
1483 yylval.sval.length = lexptr - yylval.sval.ptr;
1484 write_dollar_variable (yylval.sval);
1485 return GDB_VARIABLE;
1488 /* See if it is a special token of length 2. */
1489 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1491 if (STREQN (lexptr, tokentab2[i].operator, 2))
1494 return (tokentab2[i].token);
1497 /* Look for single character cases which which could be the first
1498 character of some other multicharacter token, but aren't, or we
1499 would already have found it. */
1509 /* Look for a float literal before looking for an integer literal, so
1510 we match as much of the input stream as possible. */
1511 token = match_float_literal ();
1516 token = match_bitstring_literal ();
1521 token = match_integer_literal ();
1527 /* Try to match a simple name string, and if a match is found, then
1528 further classify what sort of name it is and return an appropriate
1529 token. Note that attempting to match a simple name string consumes
1530 the token from lexptr, so we can't back out if we later find that
1531 we can't classify what sort of name it is. */
1533 inputname = match_simple_name_string ();
1535 if (inputname != NULL)
1537 char *simplename = (char*) alloca (strlen (inputname) + 1);
1539 char *dptr = simplename, *sptr = inputname;
1540 for (; *sptr; sptr++)
1541 *dptr++ = isupper (*sptr) ? tolower(*sptr) : *sptr;
1544 /* See if it is a reserved identifier. */
1545 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
1547 if (STREQ (simplename, idtokentab[i].operator))
1549 return (idtokentab[i].token);
1553 /* Look for other special tokens. */
1554 if (STREQ (simplename, "true"))
1557 return (BOOLEAN_LITERAL);
1559 if (STREQ (simplename, "false"))
1562 return (BOOLEAN_LITERAL);
1565 sym = lookup_symbol (inputname, expression_context_block,
1566 VAR_NAMESPACE, (int *) NULL,
1567 (struct symtab **) NULL);
1568 if (sym == NULL && strcmp (inputname, simplename) != 0)
1570 sym = lookup_symbol (simplename, expression_context_block,
1571 VAR_NAMESPACE, (int *) NULL,
1572 (struct symtab **) NULL);
1576 yylval.ssym.stoken.ptr = NULL;
1577 yylval.ssym.stoken.length = 0;
1578 yylval.ssym.sym = sym;
1579 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1580 switch (SYMBOL_CLASS (sym))
1583 /* Found a procedure name. */
1584 return (GENERAL_PROCEDURE_NAME);
1586 /* Found a global or local static variable. */
1587 return (LOCATION_NAME);
1592 case LOC_REGPARM_ADDR:
1596 case LOC_BASEREG_ARG:
1597 if (innermost_block == NULL
1598 || contained_in (block_found, innermost_block))
1600 innermost_block = block_found;
1602 return (LOCATION_NAME);
1606 return (LOCATION_NAME);
1609 yylval.tsym.type = SYMBOL_TYPE (sym);
1612 case LOC_CONST_BYTES:
1613 case LOC_OPTIMIZED_OUT:
1614 error ("Symbol \"%s\" names no location.", inputname);
1618 else if (!have_full_symbols () && !have_partial_symbols ())
1620 error ("No symbol table is loaded. Use the \"file\" command.");
1624 error ("No symbol \"%s\" in current context.", inputname);
1628 /* Catch single character tokens which are not part of some
1633 case '.': /* Not float for example. */
1635 while (isspace (*lexptr)) lexptr++;
1636 inputname = match_simple_name_string ();
1642 return (ILLEGAL_TOKEN);
1646 write_lower_upper_value (opcode, type)
1647 enum exp_opcode opcode; /* Either UNOP_LOWER or UNOP_UPPER */
1650 extern LONGEST type_lower_upper ();
1651 struct type *result_type;
1652 LONGEST val = type_lower_upper (opcode, type, &result_type);
1653 write_exp_elt_opcode (OP_LONG);
1654 write_exp_elt_type (result_type);
1655 write_exp_elt_longcst (val);
1656 write_exp_elt_opcode (OP_LONG);
1663 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);