1 /* FLEX lexer for Ada expressions, for GDB.
2 Copyright (C) 1994, 1997, 1998, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /*----------------------------------------------------------------------*/
23 /* The converted version of this file is to be included in ada-exp.y, */
24 /* the Ada parser for gdb. The function yylex obtains characters from */
25 /* the global pointer lexptr. It returns a syntactic category for */
26 /* each successive token and places a semantic value into yylval */
27 /* (ada-lval), defined by the parser. */
30 NUM10 ({DIG}({DIG}|_)*)
32 NUM16 ({HEXDIG}({HEXDIG}|_)*)
35 ID ({LETTER}({LETTER}|{DIG})*|"<"{LETTER}({LETTER}|{DIG})*">")
38 GRAPHIC [a-z0-9 #&'()*+,-./:;<>=_|!$%?@\[\]\\^`{}~]
39 OPER ([-+*/=<>&]|"<="|">="|"**"|"/="|"and"|"or"|"xor"|"not"|"mod"|"rem"|"abs")
45 #define malloc xmalloc
48 #define NUMERAL_WIDTH 256
49 #define LONGEST_SIGN ((ULONGEST) 1 << (sizeof(LONGEST) * HOST_CHAR_BIT - 1))
51 /* Temporary staging for numeric literals. */
52 static char numbuf[NUMERAL_WIDTH];
53 static void canonicalizeNumeral (char *s1, const char *);
54 static int processInt (const char *, const char *, const char *);
55 static int processReal (const char *);
56 static int processId (const char *, int);
57 static int processAttribute (const char *);
58 static int find_dot_all (const char *);
61 #define YY_DECL static int yylex ( void )
64 #define YY_INPUT(BUF, RESULT, MAX_SIZE) \
65 if ( *lexptr == '\000' ) \
74 static char *tempbuf = NULL;
75 static int tempbufsize = 0;
76 static int tempbuf_len;
77 static struct block *left_block_context;
79 static void resize_tempbuf (unsigned int);
81 static void block_lookup (char *, char *);
83 static int name_lookup (char *, char *, int *, int);
85 static int find_dot_all (const char *);
89 %option case-insensitive interactive nodefault
91 %s IN_STRING BEFORE_QUAL_QUOTE
97 "--".* { yyterminate(); }
100 canonicalizeNumeral (numbuf, yytext);
101 return processInt (NULL, numbuf, strrchr(numbuf, 'e')+1);
105 canonicalizeNumeral (numbuf, yytext);
106 return processInt (NULL, numbuf, NULL);
109 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#"{POSEXP} {
110 canonicalizeNumeral (numbuf, yytext);
111 return processInt (numbuf,
112 strchr (numbuf, '#') + 1,
113 strrchr(numbuf, '#') + 1);
116 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#" {
117 canonicalizeNumeral (numbuf, yytext);
118 return processInt (numbuf, strchr (numbuf, '#') + 1, NULL);
122 canonicalizeNumeral (numbuf, yytext+2);
123 return processInt ("16#", numbuf, NULL);
127 {NUM10}"."{NUM10}{EXP} {
128 canonicalizeNumeral (numbuf, yytext);
129 return processReal (numbuf);
133 canonicalizeNumeral (numbuf, yytext);
134 return processReal (numbuf);
137 {NUM10}"#"{NUM16}"."{NUM16}"#"{EXP} {
138 error ("Based real literals not implemented yet.");
141 {NUM10}"#"{NUM16}"."{NUM16}"#" {
142 error ("Based real literals not implemented yet.");
145 <INITIAL>"'"({GRAPHIC}|\")"'" {
146 yylval.typed_val.type = type_char ();
147 yylval.typed_val.val = yytext[1];
151 <INITIAL>"'[\""{HEXDIG}{2}"\"]'" {
153 yylval.typed_val.type = type_char ();
154 sscanf (yytext+3, "%2x", &v);
155 yylval.typed_val.val = v;
159 \"{OPER}\"/{WHITE}*"(" { return processId (yytext, yyleng); }
166 <IN_STRING>{GRAPHIC}*\" {
167 resize_tempbuf (yyleng+tempbuf_len);
168 strncpy (tempbuf+tempbuf_len, yytext, yyleng-1);
169 tempbuf_len += yyleng-1;
170 yylval.sval.ptr = tempbuf;
171 yylval.sval.length = tempbuf_len;
176 <IN_STRING>{GRAPHIC}*"[\""{HEXDIG}{2}"\"]" {
178 resize_tempbuf (yyleng-5+tempbuf_len+1);
179 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
180 sscanf(yytext+yyleng-4, "%2x", &n);
181 tempbuf[yyleng-6+tempbuf_len] = (char) n;
182 tempbuf_len += yyleng-5;
185 <IN_STRING>{GRAPHIC}*"[\"\"\"]" {
187 resize_tempbuf (yyleng-4+tempbuf_len+1);
188 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
189 tempbuf[yyleng-5+tempbuf_len] = '"';
190 tempbuf_len += yyleng-4;
194 while (*lexptr != 'i' && *lexptr != 'I')
203 and { return _AND_; }
204 else { return ELSE; }
209 null { return NULL_PTR; }
212 then { return THEN; }
217 {TICK}[a-zA-Z][a-zA-Z]+ { return processAttribute (yytext+1); }
221 "=>" { return ARROW; }
222 ".." { return DOTDOT; }
223 "**" { return STARSTAR; }
224 ":=" { return ASSIGN; }
225 "/=" { return NOTEQUAL; }
229 <BEFORE_QUAL_QUOTE>"'" { BEGIN INITIAL; return '\''; }
231 [-&*+./:<>=|;\[\]] { return yytext[0]; }
233 "," { if (paren_depth == 0 && comma_terminates)
243 "(" { paren_depth += 1; return '('; }
244 ")" { if (paren_depth == 0)
257 "."{WHITE}*all { return DOT_ALL; }
260 processId (yytext+1, yyleng-1);
264 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*(" "*"'")? {
265 int all_posn = find_dot_all (yytext);
266 int token_type, segments, k;
269 if (all_posn == -1 && yytext[yyleng-1] == '\'')
274 } while (yytext[yyleng-1] == ' ');
281 processId(yytext, yyleng);
282 segments = name_lookup (ada_encode (yylval.ssym.stoken.ptr),
283 yylval.ssym.stoken.ptr,
285 MAX_RENAMING_CHAIN_LENGTH);
286 left_block_context = NULL;
287 for (k = yyleng; segments > 0 && k > 0; k -= 1)
289 if (yytext[k-1] == '.')
294 error ("confused by name %s", yytext);
297 BEGIN BEFORE_QUAL_QUOTE;
301 /* GDB EXPRESSION CONSTRUCTS */
304 "'"[^']+"'"{WHITE}*:: {
305 processId(yytext, yyleng-2);
306 block_lookup (yylval.ssym.stoken.ptr, yylval.ssym.stoken.ptr);
310 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*{WHITE}*:: {
311 processId(yytext, yyleng-2);
312 block_lookup (ada_encode (yylval.ssym.stoken.ptr),
313 yylval.ssym.stoken.ptr);
317 [{}@] { return yytext[0]; }
319 /* REGISTERS AND GDB CONVENIENCE VARIABLES */
321 "$"({LETTER}|{DIG}|"$")* {
322 yylval.sval.ptr = yytext;
323 yylval.sval.length = yyleng;
324 return SPECIAL_VARIABLE;
327 /* CATCH-ALL ERROR CASE */
329 . { error ("Invalid character '%s' in expression.", yytext); }
333 #include "gdb_string.h"
335 /* Initialize the lexer for processing new expression */
337 lexer_init (FILE *inp)
344 /* Make sure that tempbuf points at an array at least N characters long. */
347 resize_tempbuf (unsigned int n)
351 tempbufsize = (n+63) & ~63;
352 tempbuf = (char *) xrealloc (tempbuf, tempbufsize);
356 /* Copy S2 to S1, removing all underscores, and downcasing all letters. */
359 canonicalizeNumeral (char *s1, const char *s2)
361 for (; *s2 != '\000'; s2 += 1)
372 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
374 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
375 where 2 <= BASE <= 16. */
378 is_digit_in_base (unsigned char digit, int base)
380 if (!isxdigit (digit))
383 return (isdigit (digit) && digit < base + '0');
385 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
389 digit_to_int (unsigned char c)
394 return tolower (c) - 'a' + 10;
397 /* As for strtoul, but for ULONGEST results. */
399 strtoulst (const char *num, const char **trailer, int base)
401 unsigned int high_part;
406 if (base < 2 || base > 16)
411 lim = base - 1 + '0';
413 result = high_part = 0;
414 for (i = 0; is_digit_in_base (num[i], base); i += 1)
416 result = result*base + digit_to_int (num[i]);
417 high_part = high_part*base + (unsigned int) (result >> HIGH_BYTE_POSN);
418 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
419 if (high_part > 0xff)
422 result = high_part = 0;
430 return result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
435 /* Interprets the prefix of NUM that consists of digits of the given BASE
436 as an integer of that BASE, with the string EXP as an exponent.
437 Puts value in yylval, and returns INT, if the string is valid. Causes
438 an error if the number is improperly formated. BASE, if NULL, defaults
439 to "10", and EXP to "1". The EXP does not contain a leading 'e' or 'E'. */
442 processInt (const char *base0, const char *num0, const char *exp0)
454 base = strtol (base0, (char **) NULL, 10);
455 if (base < 2 || base > 16)
456 error ("Invalid base: %d.", base);
462 exp = strtol(exp0, (char **) NULL, 10);
465 result = strtoulst (num0, (const char **) &trailer, base);
467 error ("Integer literal out of range");
468 if (isxdigit(*trailer))
469 error ("Invalid digit `%c' in based literal", *trailer);
473 if (result > (ULONG_MAX / base))
474 error ("Integer literal out of range");
479 if ((result >> (TARGET_INT_BIT-1)) == 0)
480 yylval.typed_val.type = type_int ();
481 else if ((result >> (TARGET_LONG_BIT-1)) == 0)
482 yylval.typed_val.type = type_long ();
483 else if (((result >> (TARGET_LONG_BIT-1)) >> 1) == 0)
485 /* We have a number representable as an unsigned integer quantity.
486 For consistency with the C treatment, we will treat it as an
487 anonymous modular (unsigned) quantity. Alas, the types are such
488 that we need to store .val as a signed quantity. Sorry
489 for the mess, but C doesn't officially guarantee that a simple
490 assignment does the trick (no, it doesn't; read the reference manual).
492 yylval.typed_val.type = builtin_type_unsigned_long;
493 if (result & LONGEST_SIGN)
494 yylval.typed_val.val =
495 (LONGEST) (result & ~LONGEST_SIGN)
496 - (LONGEST_SIGN>>1) - (LONGEST_SIGN>>1);
498 yylval.typed_val.val = (LONGEST) result;
502 yylval.typed_val.type = type_long_long ();
504 yylval.typed_val.val = (LONGEST) result;
508 #if defined (PRINTF_HAS_LONG_DOUBLE)
509 # undef PRINTF_HAS_LONG_DOUBLE
510 # define PRINTF_HAS_LONG_DOUBLE 1
512 # define PRINTF_HAS_LONG_DOUBLE 0
516 processReal (const char *num0)
518 #if defined (PRINTF_HAS_LONG_DOUBLE)
519 if (sizeof (DOUBLEST) > sizeof (double))
520 sscanf (num0, "%Lg", &yylval.typed_val_float.dval);
525 sscanf (num0, "%lg", &temp);
526 yylval.typed_val_float.dval = temp;
529 yylval.typed_val_float.type = type_float ();
530 if (sizeof(DOUBLEST) >= TARGET_DOUBLE_BIT / TARGET_CHAR_BIT)
531 yylval.typed_val_float.type = type_double ();
532 if (sizeof(DOUBLEST) >= TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT)
533 yylval.typed_val_float.type = type_long_double ();
539 processId (const char *name0, int len)
541 char *name = obstack_alloc (&temp_parse_space, len + 11);
544 while (len > 0 && isspace (name0[len-1]))
549 if (isalnum (name0[i0]))
551 name[i] = tolower (name0[i0]);
554 else switch (name0[i0])
565 while (i0 < len && name0[i0] != '\'')
574 while (i0 < len && name0[i0] != '>')
585 yylval.ssym.sym = NULL;
586 yylval.ssym.stoken.ptr = name;
587 yylval.ssym.stoken.length = i;
592 block_lookup (char *name, char *err_name)
594 struct ada_symbol_info *syms;
596 struct symtab *symtab;
597 nsyms = ada_lookup_symbol_list (name, left_block_context,
599 if (left_block_context == NULL &&
600 (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK))
601 symtab = lookup_symtab (name);
606 left_block_context = yylval.bval =
607 BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
608 else if (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK)
610 if (left_block_context == NULL)
611 error ("No file or function \"%s\".", err_name);
613 error ("No function \"%s\" in specified context.", err_name);
617 left_block_context = yylval.bval = SYMBOL_BLOCK_VALUE (syms[0].sym);
619 warning ("Function name \"%s\" ambiguous here", err_name);
623 /* Look up NAME0 (assumed to be encoded) as a name in VAR_DOMAIN,
624 setting *TOKEN_TYPE to NAME or TYPENAME, depending on what is
625 found. Try first the entire name, then the name without the last
626 segment (i.e., after the last .id), etc., and return the number of
627 segments that had to be removed to get a match. Try only the full
628 name if it starts with "standard__". Calls error if no
629 matches are found, using ERR_NAME in any error message. When
630 exactly one symbol match is found, it is placed in yylval. When
631 the symbol is a renaming, follow at most DEPTH steps to find the
632 ultimate definition; cause error if depth exceeded. */
635 name_lookup (char *name0, char *err_name, int *token_type, int depth)
637 struct ada_symbol_info *syms;
639 int len0 = strlen (name0);
640 char *name = obsavestring (name0, len0, &temp_parse_space);
645 error ("Could not find renamed symbol \"%s\"", err_name);
647 yylval.ssym.stoken.ptr = name;
648 yylval.ssym.stoken.length = strlen (name);
649 for (segments = 0; ; segments += 1)
651 struct type *preferred_type;
652 int i, preferred_index;
654 if (left_block_context == NULL)
655 nsyms = ada_lookup_symbol_list (name, expression_context_block,
658 nsyms = ada_lookup_symbol_list (name, left_block_context,
662 /* Check for a type renaming. */
664 if (nsyms == 1 && !ada_is_object_renaming (syms[0].sym))
666 struct symbol *renaming_sym =
667 ada_find_renaming_symbol (SYMBOL_LINKAGE_NAME (syms[0].sym),
670 if (renaming_sym != NULL)
671 syms[0].sym = renaming_sym;
674 /* Check for a type definition. */
676 /* Look for a symbol that doesn't denote void. This is (I think) a */
677 /* temporary kludge to get around problems in GNAT output. */
678 preferred_index = -1; preferred_type = NULL;
679 for (i = 0; i < nsyms; i += 1)
680 switch (SYMBOL_CLASS (syms[i].sym))
683 if (ada_prefer_type (SYMBOL_TYPE (syms[i].sym), preferred_type))
686 preferred_type = SYMBOL_TYPE (syms[i].sym);
693 case LOC_REGPARM_ADDR:
697 case LOC_BASEREG_ARG:
699 case LOC_COMPUTED_ARG:
704 if (preferred_type != NULL)
706 if (TYPE_CODE (preferred_type) == TYPE_CODE_VOID)
707 error ("`%s' matches only void type name(s)",
709 else if (ada_is_object_renaming (syms[preferred_index].sym))
711 yylval.ssym.sym = syms[preferred_index].sym;
712 *token_type = OBJECT_RENAMING;
715 else if (ada_renaming_type (SYMBOL_TYPE (syms[preferred_index].sym))
720 = ada_simple_renamed_entity (syms[preferred_index].sym);
722 = (char *) obstack_alloc (&temp_parse_space,
723 strlen (renaming) + len0
724 - yylval.ssym.stoken.length + 1);
725 strcpy (new_name, renaming);
727 strcat (new_name, name0 + yylval.ssym.stoken.length);
728 result = name_lookup (new_name, err_name, token_type, depth - 1);
729 if (result > segments)
730 error ("Confused by renamed symbol.");
733 else if (segments == 0)
735 yylval.tval = preferred_type;
736 *token_type = TYPENAME;
743 type = language_lookup_primitive_type_by_name (current_language,
746 if (type == NULL && strcmp ("system__address", name) == 0)
747 type = type_system_address ();
750 /* First check to see if we have a regular definition of this
751 type that just didn't happen to have been read yet. */
754 char *expanded_name =
755 (char *) alloca (strlen (name) + sizeof ("standard__"));
756 strcpy (expanded_name, "standard__");
757 strcat (expanded_name, name);
758 sym = ada_lookup_symbol (expanded_name, NULL,
759 VAR_DOMAIN, NULL, NULL);
760 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
761 type = SYMBOL_TYPE (sym);
764 *token_type = TYPENAME;
773 yylval.ssym.sym = syms[0].sym;
774 yylval.ssym.msym = NULL;
775 yylval.ssym.block = syms[0].block;
778 else if (nsyms == 0) {
780 yylval.ssym.msym = ada_lookup_simple_minsym (name);
781 if (yylval.ssym.msym != NULL)
783 yylval.ssym.sym = NULL;
784 yylval.ssym.block = NULL;
790 && strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
791 error ("No definition of \"%s\" found.", err_name);
793 for (i = yylval.ssym.stoken.length - 1; i > 0; i -= 1)
798 yylval.ssym.stoken.length = i;
801 else if (name[i] == '_' && name[i-1] == '_')
805 yylval.ssym.stoken.length = i;
811 if (!have_full_symbols () && !have_partial_symbols ()
812 && left_block_context == NULL)
813 error ("No symbol table is loaded. Use the \"file\" command.");
814 if (left_block_context == NULL)
815 error ("No definition of \"%s\" in current context.",
818 error ("No definition of \"%s\" in specified context.",
825 yylval.ssym.sym = NULL;
826 yylval.ssym.msym = NULL;
827 if (left_block_context == NULL)
828 yylval.ssym.block = expression_context_block;
830 yylval.ssym.block = left_block_context;
836 /* Returns the position within STR of the '.' in a
837 '.{WHITE}*all' component of a dotted name, or -1 if there is none. */
839 find_dot_all (const char *str)
842 for (i = 0; str[i] != '\000'; i += 1)
849 while (isspace (str[i]));
850 if (strcmp (str+i, "all") == 0
851 && ! isalnum (str[i+3]) && str[i+3] != '_')
858 /* Returns non-zero iff string SUBSEQ matches a subsequence of STR, ignoring
862 subseqMatch (const char *subseq, const char *str)
864 if (subseq[0] == '\0')
866 else if (str[0] == '\0')
868 else if (tolower (subseq[0]) == tolower (str[0]))
869 return subseqMatch (subseq+1, str+1) || subseqMatch (subseq, str+1);
871 return subseqMatch (subseq, str+1);
875 static struct { const char *name; int code; }
877 { "address", TICK_ADDRESS },
878 { "unchecked_access", TICK_ACCESS },
879 { "unrestricted_access", TICK_ACCESS },
880 { "access", TICK_ACCESS },
881 { "first", TICK_FIRST },
882 { "last", TICK_LAST },
883 { "length", TICK_LENGTH },
886 { "modulus", TICK_MODULUS },
888 { "range", TICK_RANGE },
889 { "size", TICK_SIZE },
895 /* Return the syntactic code corresponding to the attribute name or
899 processAttribute (const char *str)
903 for (i = 0; attributes[i].code != -1; i += 1)
904 if (strcasecmp (str, attributes[i].name) == 0)
905 return attributes[i].code;
907 for (i = 0, k = -1; attributes[i].code != -1; i += 1)
908 if (subseqMatch (str, attributes[i].name))
913 error ("ambiguous attribute name: `%s'", str);
916 error ("unrecognized attribute: `%s'", str);
918 return attributes[k].code;