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. */
29 /* Run flex with (at least) the -i option (case-insensitive), and the -I */
30 /* option (interactive---no unnecessary lookahead). */
33 NUM10 ({DIG}({DIG}|_)*)
35 NUM16 ({HEXDIG}({HEXDIG}|_)*)
38 ID ({LETTER}({LETTER}|{DIG})*|"<"{LETTER}({LETTER}|{DIG})*">")
41 GRAPHIC [a-z0-9 #&'()*+,-./:;<>=_|!$%?@\[\]\\^`{}~]
42 OPER ([-+*/=<>&]|"<="|">="|"**"|"/="|"and"|"or"|"xor"|"not"|"mod"|"rem"|"abs")
48 #define malloc xmalloc
51 #define NUMERAL_WIDTH 256
52 #define LONGEST_SIGN ((ULONGEST) 1 << (sizeof(LONGEST) * HOST_CHAR_BIT - 1))
54 /* Temporary staging for numeric literals. */
55 static char numbuf[NUMERAL_WIDTH];
56 static void canonicalizeNumeral (char *s1, const char *);
57 static int processInt (const char *, const char *, const char *);
58 static int processReal (const char *);
59 static int processId (const char *, int);
60 static int processAttribute (const char *);
61 static int find_dot_all (const char *);
64 #define YY_DECL static int yylex ( void )
67 #define YY_INPUT(BUF, RESULT, MAX_SIZE) \
68 if ( *lexptr == '\000' ) \
77 static char *tempbuf = NULL;
78 static int tempbufsize = 0;
79 static int tempbuf_len;
80 static struct block *left_block_context;
82 static void resize_tempbuf (unsigned int);
84 static void block_lookup (char *, char *);
86 static int name_lookup (char *, char *, int *, int);
88 static int find_dot_all (const char *);
92 %s IN_STRING BEFORE_QUAL_QUOTE
98 "--".* { yyterminate(); }
101 canonicalizeNumeral (numbuf, yytext);
102 return processInt (NULL, numbuf, strrchr(numbuf, 'e')+1);
106 canonicalizeNumeral (numbuf, yytext);
107 return processInt (NULL, numbuf, NULL);
110 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#"{POSEXP} {
111 canonicalizeNumeral (numbuf, yytext);
112 return processInt (numbuf,
113 strchr (numbuf, '#') + 1,
114 strrchr(numbuf, '#') + 1);
117 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#" {
118 canonicalizeNumeral (numbuf, yytext);
119 return processInt (numbuf, strchr (numbuf, '#') + 1, NULL);
123 canonicalizeNumeral (numbuf, yytext+2);
124 return processInt ("16#", numbuf, NULL);
128 {NUM10}"."{NUM10}{EXP} {
129 canonicalizeNumeral (numbuf, yytext);
130 return processReal (numbuf);
134 canonicalizeNumeral (numbuf, yytext);
135 return processReal (numbuf);
138 {NUM10}"#"{NUM16}"."{NUM16}"#"{EXP} {
139 error ("Based real literals not implemented yet.");
142 {NUM10}"#"{NUM16}"."{NUM16}"#" {
143 error ("Based real literals not implemented yet.");
146 <INITIAL>"'"({GRAPHIC}|\")"'" {
147 yylval.typed_val.type = builtin_type_ada_char;
148 yylval.typed_val.val = yytext[1];
152 <INITIAL>"'[\""{HEXDIG}{2}"\"]'" {
154 yylval.typed_val.type = builtin_type_ada_char;
155 sscanf (yytext+3, "%2x", &v);
156 yylval.typed_val.val = v;
160 \"{OPER}\"/{WHITE}*"(" { return processId (yytext, yyleng); }
167 <IN_STRING>{GRAPHIC}*\" {
168 resize_tempbuf (yyleng+tempbuf_len);
169 strncpy (tempbuf+tempbuf_len, yytext, yyleng-1);
170 tempbuf_len += yyleng-1;
171 yylval.sval.ptr = tempbuf;
172 yylval.sval.length = tempbuf_len;
177 <IN_STRING>{GRAPHIC}*"[\""{HEXDIG}{2}"\"]" {
179 resize_tempbuf (yyleng-5+tempbuf_len+1);
180 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
181 sscanf(yytext+yyleng-4, "%2x", &n);
182 tempbuf[yyleng-6+tempbuf_len] = (char) n;
183 tempbuf_len += yyleng-5;
186 <IN_STRING>{GRAPHIC}*"[\"\"\"]" {
188 resize_tempbuf (yyleng-4+tempbuf_len+1);
189 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
190 tempbuf[yyleng-5+tempbuf_len] = '"';
191 tempbuf_len += yyleng-4;
195 while (*lexptr != 'i' && *lexptr != 'I')
204 and { return _AND_; }
205 else { return ELSE; }
210 null { return NULL_PTR; }
213 then { return THEN; }
218 {TICK}[a-zA-Z][a-zA-Z]+ { return processAttribute (yytext+1); }
222 "=>" { return ARROW; }
223 ".." { return DOTDOT; }
224 "**" { return STARSTAR; }
225 ":=" { return ASSIGN; }
226 "/=" { return NOTEQUAL; }
230 <BEFORE_QUAL_QUOTE>"'" { BEGIN INITIAL; return '\''; }
232 [-&*+./:<>=|;\[\]] { return yytext[0]; }
234 "," { if (paren_depth == 0 && comma_terminates)
244 "(" { paren_depth += 1; return '('; }
245 ")" { if (paren_depth == 0)
258 "."{WHITE}*all { return DOT_ALL; }
261 processId (yytext+1, yyleng-1);
265 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*(" "*"'")? {
266 int all_posn = find_dot_all (yytext);
267 int token_type, segments, k;
270 if (all_posn == -1 && yytext[yyleng-1] == '\'')
275 } while (yytext[yyleng-1] == ' ');
282 processId(yytext, yyleng);
283 segments = name_lookup (ada_encode (yylval.ssym.stoken.ptr),
284 yylval.ssym.stoken.ptr,
286 MAX_RENAMING_CHAIN_LENGTH);
287 left_block_context = NULL;
288 for (k = yyleng; segments > 0 && k > 0; k -= 1)
290 if (yytext[k-1] == '.')
295 error ("confused by name %s", yytext);
298 BEGIN BEFORE_QUAL_QUOTE;
302 /* GDB EXPRESSION CONSTRUCTS */
305 "'"[^']+"'"{WHITE}*:: {
306 processId(yytext, yyleng-2);
307 block_lookup (yylval.ssym.stoken.ptr, yylval.ssym.stoken.ptr);
311 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*{WHITE}*:: {
312 processId(yytext, yyleng-2);
313 block_lookup (ada_encode (yylval.ssym.stoken.ptr),
314 yylval.ssym.stoken.ptr);
318 [{}@] { return yytext[0]; }
320 /* REGISTERS AND GDB CONVENIENCE VARIABLES */
322 "$"({LETTER}|{DIG}|"$")* {
323 yylval.sval.ptr = yytext;
324 yylval.sval.length = yyleng;
325 return SPECIAL_VARIABLE;
328 /* CATCH-ALL ERROR CASE */
330 . { error ("Invalid character '%s' in expression.", yytext); }
334 #include "gdb_string.h"
336 /* Initialize the lexer for processing new expression */
338 lexer_init (FILE *inp)
345 /* Make sure that tempbuf points at an array at least N characters long. */
348 resize_tempbuf (unsigned int n)
352 tempbufsize = (n+63) & ~63;
353 tempbuf = (char *) xrealloc (tempbuf, tempbufsize);
357 /* Copy S2 to S1, removing all underscores, and downcasing all letters. */
360 canonicalizeNumeral (char *s1, const char *s2)
362 for (; *s2 != '\000'; s2 += 1)
373 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
375 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
376 where 2 <= BASE <= 16. */
379 is_digit_in_base (unsigned char digit, int base)
381 if (!isxdigit (digit))
384 return (isdigit (digit) && digit < base + '0');
386 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
390 digit_to_int (unsigned char c)
395 return tolower (c) - 'a' + 10;
398 /* As for strtoul, but for ULONGEST results. */
400 strtoulst (const char *num, const char **trailer, int base)
402 unsigned int high_part;
407 if (base < 2 || base > 16)
412 lim = base - 1 + '0';
414 result = high_part = 0;
415 for (i = 0; is_digit_in_base (num[i], base); i += 1)
417 result = result*base + digit_to_int (num[i]);
418 high_part = high_part*base + (unsigned int) (result >> HIGH_BYTE_POSN);
419 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
420 if (high_part > 0xff)
423 result = high_part = 0;
431 return result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
436 /* Interprets the prefix of NUM that consists of digits of the given BASE
437 as an integer of that BASE, with the string EXP as an exponent.
438 Puts value in yylval, and returns INT, if the string is valid. Causes
439 an error if the number is improperly formated. BASE, if NULL, defaults
440 to "10", and EXP to "1". The EXP does not contain a leading 'e' or 'E'. */
443 processInt (const char *base0, const char *num0, const char *exp0)
455 base = strtol (base0, (char **) NULL, 10);
456 if (base < 2 || base > 16)
457 error ("Invalid base: %d.", base);
463 exp = strtol(exp0, (char **) NULL, 10);
466 result = strtoulst (num0, (const char **) &trailer, base);
468 error ("Integer literal out of range");
469 if (isxdigit(*trailer))
470 error ("Invalid digit `%c' in based literal", *trailer);
474 if (result > (ULONG_MAX / base))
475 error ("Integer literal out of range");
480 if ((result >> (TARGET_INT_BIT-1)) == 0)
481 yylval.typed_val.type = builtin_type_ada_int;
482 else if ((result >> (TARGET_LONG_BIT-1)) == 0)
483 yylval.typed_val.type = builtin_type_ada_long;
484 else if (((result >> (TARGET_LONG_BIT-1)) >> 1) == 0)
486 /* We have a number representable as an unsigned integer quantity.
487 For consistency with the C treatment, we will treat it as an
488 anonymous modular (unsigned) quantity. Alas, the types are such
489 that we need to store .val as a signed quantity. Sorry
490 for the mess, but C doesn't officially guarantee that a simple
491 assignment does the trick (no, it doesn't; read the reference manual).
493 yylval.typed_val.type = builtin_type_unsigned_long;
494 if (result & LONGEST_SIGN)
495 yylval.typed_val.val =
496 (LONGEST) (result & ~LONGEST_SIGN)
497 - (LONGEST_SIGN>>1) - (LONGEST_SIGN>>1);
499 yylval.typed_val.val = (LONGEST) result;
503 yylval.typed_val.type = builtin_type_ada_long_long;
505 yylval.typed_val.val = (LONGEST) result;
509 #if defined (PRINTF_HAS_LONG_DOUBLE)
510 # undef PRINTF_HAS_LONG_DOUBLE
511 # define PRINTF_HAS_LONG_DOUBLE 1
513 # define PRINTF_HAS_LONG_DOUBLE 0
517 processReal (const char *num0)
519 #if defined (PRINTF_HAS_LONG_DOUBLE)
520 if (sizeof (DOUBLEST) > sizeof (double))
521 sscanf (num0, "%Lg", &yylval.typed_val_float.dval);
526 sscanf (num0, "%lg", &temp);
527 yylval.typed_val_float.dval = temp;
530 yylval.typed_val_float.type = builtin_type_ada_float;
531 if (sizeof(DOUBLEST) >= TARGET_DOUBLE_BIT / TARGET_CHAR_BIT)
532 yylval.typed_val_float.type = builtin_type_ada_double;
533 if (sizeof(DOUBLEST) >= TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT)
534 yylval.typed_val_float.type = builtin_type_ada_long_double;
540 processId (const char *name0, int len)
542 char *name = obstack_alloc (&temp_parse_space, len + 11);
545 while (len > 0 && isspace (name0[len-1]))
550 if (isalnum (name0[i0]))
552 name[i] = tolower (name0[i0]);
555 else switch (name0[i0])
566 while (i0 < len && name0[i0] != '\'')
575 while (i0 < len && name0[i0] != '>')
586 yylval.ssym.sym = NULL;
587 yylval.ssym.stoken.ptr = name;
588 yylval.ssym.stoken.length = i;
593 block_lookup (char *name, char *err_name)
595 struct ada_symbol_info *syms;
597 struct symtab *symtab;
598 nsyms = ada_lookup_symbol_list (name, left_block_context,
600 if (left_block_context == NULL &&
601 (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK))
602 symtab = lookup_symtab (name);
607 left_block_context = yylval.bval =
608 BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
609 else if (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK)
611 if (left_block_context == NULL)
612 error ("No file or function \"%s\".", err_name);
614 error ("No function \"%s\" in specified context.", err_name);
618 left_block_context = yylval.bval = SYMBOL_BLOCK_VALUE (syms[0].sym);
620 warning ("Function name \"%s\" ambiguous here", err_name);
624 /* Look up NAME0 (assumed to be encoded) as a name in VAR_DOMAIN,
625 setting *TOKEN_TYPE to NAME or TYPENAME, depending on what is
626 found. Try first the entire name, then the name without the last
627 segment (i.e., after the last .id), etc., and return the number of
628 segments that had to be removed to get a match. Try only the full
629 name if it starts with "standard__". Calls error if no
630 matches are found, using ERR_NAME in any error message. When
631 exactly one symbol match is found, it is placed in yylval. When
632 the symbol is a renaming, follow at most DEPTH steps to find the
633 ultimate definition; cause error if depth exceeded. */
636 name_lookup (char *name0, char *err_name, int *token_type, int depth)
638 struct ada_symbol_info *syms;
640 int len0 = strlen (name0);
641 char *name = obsavestring (name0, len0, &temp_parse_space);
646 error ("Could not find renamed symbol \"%s\"", err_name);
648 yylval.ssym.stoken.ptr = name;
649 yylval.ssym.stoken.length = strlen (name);
650 for (segments = 0; ; segments += 1)
652 struct type *preferred_type;
653 int i, preferred_index;
655 if (left_block_context == NULL)
656 nsyms = ada_lookup_symbol_list (name, expression_context_block,
659 nsyms = ada_lookup_symbol_list (name, left_block_context,
663 /* Check for a type renaming. */
665 if (nsyms == 1 && !ada_is_object_renaming (syms[0].sym))
667 struct symbol *renaming_sym =
668 ada_find_renaming_symbol (SYMBOL_LINKAGE_NAME (syms[0].sym),
671 if (renaming_sym != NULL)
672 syms[0].sym = renaming_sym;
675 /* Check for a type definition. */
677 /* Look for a symbol that doesn't denote void. This is (I think) a */
678 /* temporary kludge to get around problems in GNAT output. */
679 preferred_index = -1; preferred_type = NULL;
680 for (i = 0; i < nsyms; i += 1)
681 switch (SYMBOL_CLASS (syms[i].sym))
684 if (ada_prefer_type (SYMBOL_TYPE (syms[i].sym), preferred_type))
687 preferred_type = SYMBOL_TYPE (syms[i].sym);
694 case LOC_REGPARM_ADDR:
698 case LOC_BASEREG_ARG:
700 case LOC_COMPUTED_ARG:
705 if (preferred_type != NULL)
707 if (TYPE_CODE (preferred_type) == TYPE_CODE_VOID)
708 error ("`%s' matches only void type name(s)",
710 else if (ada_is_object_renaming (syms[preferred_index].sym))
712 yylval.ssym.sym = syms[preferred_index].sym;
713 *token_type = OBJECT_RENAMING;
716 else if (ada_renaming_type (SYMBOL_TYPE (syms[preferred_index].sym))
721 = ada_simple_renamed_entity (syms[preferred_index].sym);
723 = (char *) obstack_alloc (&temp_parse_space,
724 strlen (renaming) + len0
725 - yylval.ssym.stoken.length + 1);
726 strcpy (new_name, renaming);
728 strcat (new_name, name0 + yylval.ssym.stoken.length);
729 result = name_lookup (new_name, err_name, token_type, depth - 1);
730 if (result > segments)
731 error ("Confused by renamed symbol.");
734 else if (segments == 0)
736 yylval.tval = preferred_type;
737 *token_type = TYPENAME;
744 type = language_lookup_primitive_type_by_name (current_language,
747 if (type == NULL && strcmp ("system__address", name) == 0)
748 type = builtin_type_ada_system_address;
751 /* First check to see if we have a regular definition of this
752 type that just didn't happen to have been read yet. */
755 char *expanded_name =
756 (char *) alloca (strlen (name) + sizeof ("standard__"));
757 strcpy (expanded_name, "standard__");
758 strcat (expanded_name, name);
759 sym = ada_lookup_symbol (expanded_name, NULL,
760 VAR_DOMAIN, NULL, NULL);
761 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
762 type = SYMBOL_TYPE (sym);
765 *token_type = TYPENAME;
774 yylval.ssym.sym = syms[0].sym;
775 yylval.ssym.msym = NULL;
776 yylval.ssym.block = syms[0].block;
779 else if (nsyms == 0) {
781 yylval.ssym.msym = ada_lookup_simple_minsym (name);
782 if (yylval.ssym.msym != NULL)
784 yylval.ssym.sym = NULL;
785 yylval.ssym.block = NULL;
791 && strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
792 error ("No definition of \"%s\" found.", err_name);
794 for (i = yylval.ssym.stoken.length - 1; i > 0; i -= 1)
799 yylval.ssym.stoken.length = i;
802 else if (name[i] == '_' && name[i-1] == '_')
806 yylval.ssym.stoken.length = i;
812 if (!have_full_symbols () && !have_partial_symbols ()
813 && left_block_context == NULL)
814 error ("No symbol table is loaded. Use the \"file\" command.");
815 if (left_block_context == NULL)
816 error ("No definition of \"%s\" in current context.",
819 error ("No definition of \"%s\" in specified context.",
826 yylval.ssym.sym = NULL;
827 yylval.ssym.msym = NULL;
828 if (left_block_context == NULL)
829 yylval.ssym.block = expression_context_block;
831 yylval.ssym.block = left_block_context;
837 /* Returns the position within STR of the '.' in a
838 '.{WHITE}*all' component of a dotted name, or -1 if there is none. */
840 find_dot_all (const char *str)
843 for (i = 0; str[i] != '\000'; i += 1)
850 while (isspace (str[i]));
851 if (strcmp (str+i, "all") == 0
852 && ! isalnum (str[i+3]) && str[i+3] != '_')
859 /* Returns non-zero iff string SUBSEQ matches a subsequence of STR, ignoring
863 subseqMatch (const char *subseq, const char *str)
865 if (subseq[0] == '\0')
867 else if (str[0] == '\0')
869 else if (tolower (subseq[0]) == tolower (str[0]))
870 return subseqMatch (subseq+1, str+1) || subseqMatch (subseq, str+1);
872 return subseqMatch (subseq, str+1);
876 static struct { const char *name; int code; }
878 { "address", TICK_ADDRESS },
879 { "unchecked_access", TICK_ACCESS },
880 { "unrestricted_access", TICK_ACCESS },
881 { "access", TICK_ACCESS },
882 { "first", TICK_FIRST },
883 { "last", TICK_LAST },
884 { "length", TICK_LENGTH },
887 { "modulus", TICK_MODULUS },
889 { "range", TICK_RANGE },
890 { "size", TICK_SIZE },
896 /* Return the syntactic code corresponding to the attribute name or
900 processAttribute (const char *str)
904 for (i = 0; attributes[i].code != -1; i += 1)
905 if (strcasecmp (str, attributes[i].name) == 0)
906 return attributes[i].code;
908 for (i = 0, k = -1; attributes[i].code != -1; i += 1)
909 if (subseqMatch (str, attributes[i].name))
914 error ("ambiguous attribute name: `%s'", str);
917 error ("unrecognized attribute: `%s'", str);
919 return attributes[k].code;