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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /*----------------------------------------------------------------------*/
24 /* The converted version of this file is to be included in ada-exp.y, */
25 /* the Ada parser for gdb. The function yylex obtains characters from */
26 /* the global pointer lexptr. It returns a syntactic category for */
27 /* each successive token and places a semantic value into yylval */
28 /* (ada-lval), defined by the parser. */
31 NUM10 ({DIG}({DIG}|_)*)
33 NUM16 ({HEXDIG}({HEXDIG}|_)*)
36 ID ({LETTER}({LETTER}|{DIG})*|"<"{LETTER}({LETTER}|{DIG})*">")
39 GRAPHIC [a-z0-9 #&'()*+,-./:;<>=_|!$%?@\[\]\\^`{}~]
40 OPER ([-+*/=<>&]|"<="|">="|"**"|"/="|"and"|"or"|"xor"|"not"|"mod"|"rem"|"abs")
47 #define NUMERAL_WIDTH 256
48 #define LONGEST_SIGN ((ULONGEST) 1 << (sizeof(LONGEST) * HOST_CHAR_BIT - 1))
50 /* Temporary staging for numeric literals. */
51 static char numbuf[NUMERAL_WIDTH];
52 static void canonicalizeNumeral (char *s1, const char *);
53 static int processInt (const char *, const char *, const char *);
54 static int processReal (const char *);
55 static int processId (const char *, int);
56 static int processAttribute (const char *);
57 static int find_dot_all (const char *);
60 #define YY_DECL static int yylex ( void )
63 #define YY_INPUT(BUF, RESULT, MAX_SIZE) \
64 if ( *lexptr == '\000' ) \
73 static char *tempbuf = NULL;
74 static int tempbufsize = 0;
75 static int tempbuf_len;
76 static struct block *left_block_context;
78 static void resize_tempbuf (unsigned int);
80 static void block_lookup (char *, char *);
82 static int name_lookup (char *, char *, int *, int);
84 static int find_dot_all (const char *);
88 %option case-insensitive interactive nodefault
90 %s IN_STRING BEFORE_QUAL_QUOTE
96 "--".* { yyterminate(); }
99 canonicalizeNumeral (numbuf, yytext);
100 return processInt (NULL, numbuf, strrchr(numbuf, 'e')+1);
104 canonicalizeNumeral (numbuf, yytext);
105 return processInt (NULL, numbuf, NULL);
108 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#"{POSEXP} {
109 canonicalizeNumeral (numbuf, yytext);
110 return processInt (numbuf,
111 strchr (numbuf, '#') + 1,
112 strrchr(numbuf, '#') + 1);
115 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#" {
116 canonicalizeNumeral (numbuf, yytext);
117 return processInt (numbuf, strchr (numbuf, '#') + 1, NULL);
121 canonicalizeNumeral (numbuf, yytext+2);
122 return processInt ("16#", numbuf, NULL);
126 {NUM10}"."{NUM10}{EXP} {
127 canonicalizeNumeral (numbuf, yytext);
128 return processReal (numbuf);
132 canonicalizeNumeral (numbuf, yytext);
133 return processReal (numbuf);
136 {NUM10}"#"{NUM16}"."{NUM16}"#"{EXP} {
137 error ("Based real literals not implemented yet.");
140 {NUM10}"#"{NUM16}"."{NUM16}"#" {
141 error ("Based real literals not implemented yet.");
144 <INITIAL>"'"({GRAPHIC}|\")"'" {
145 yylval.typed_val.type = type_char ();
146 yylval.typed_val.val = yytext[1];
150 <INITIAL>"'[\""{HEXDIG}{2}"\"]'" {
152 yylval.typed_val.type = type_char ();
153 sscanf (yytext+3, "%2x", &v);
154 yylval.typed_val.val = v;
163 <IN_STRING>{GRAPHIC}*\" {
164 resize_tempbuf (yyleng+tempbuf_len);
165 strncpy (tempbuf+tempbuf_len, yytext, yyleng-1);
166 tempbuf_len += yyleng-1;
167 yylval.sval.ptr = tempbuf;
168 yylval.sval.length = tempbuf_len;
173 <IN_STRING>{GRAPHIC}*"[\""{HEXDIG}{2}"\"]" {
175 resize_tempbuf (yyleng-5+tempbuf_len+1);
176 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
177 sscanf(yytext+yyleng-4, "%2x", &n);
178 tempbuf[yyleng-6+tempbuf_len] = (char) n;
179 tempbuf_len += yyleng-5;
182 <IN_STRING>{GRAPHIC}*"[\"\"\"]" {
184 resize_tempbuf (yyleng-4+tempbuf_len+1);
185 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
186 tempbuf[yyleng-5+tempbuf_len] = '"';
187 tempbuf_len += yyleng-4;
191 while (*lexptr != 'i' && *lexptr != 'I')
200 and { return _AND_; }
201 else { return ELSE; }
206 null { return NULL_PTR; }
209 then { return THEN; }
214 {TICK}[a-zA-Z][a-zA-Z]+ { return processAttribute (yytext+1); }
218 "=>" { return ARROW; }
219 ".." { return DOTDOT; }
220 "**" { return STARSTAR; }
221 ":=" { return ASSIGN; }
222 "/=" { return NOTEQUAL; }
226 <BEFORE_QUAL_QUOTE>"'" { BEGIN INITIAL; return '\''; }
228 [-&*+./:<>=|;\[\]] { return yytext[0]; }
230 "," { if (paren_depth == 0 && comma_terminates)
240 "(" { paren_depth += 1; return '('; }
241 ")" { if (paren_depth == 0)
254 "."{WHITE}*all { return DOT_ALL; }
257 processId (yytext+1, yyleng-1);
261 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*(" "*"'")? {
262 int all_posn = find_dot_all (yytext);
263 int token_type, segments, k;
266 if (all_posn == -1 && yytext[yyleng-1] == '\'')
271 } while (yytext[yyleng-1] == ' ');
278 processId(yytext, yyleng);
279 segments = name_lookup (ada_encode (yylval.ssym.stoken.ptr),
280 yylval.ssym.stoken.ptr,
282 MAX_RENAMING_CHAIN_LENGTH);
283 left_block_context = NULL;
284 for (k = yyleng; segments > 0 && k > 0; k -= 1)
286 if (yytext[k-1] == '.')
291 error ("confused by name %s", yytext);
294 BEGIN BEFORE_QUAL_QUOTE;
298 /* GDB EXPRESSION CONSTRUCTS */
301 "'"[^']+"'"{WHITE}*:: {
302 processId(yytext, yyleng-2);
303 block_lookup (yylval.ssym.stoken.ptr, yylval.ssym.stoken.ptr);
307 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*{WHITE}*:: {
308 processId(yytext, yyleng-2);
309 block_lookup (ada_encode (yylval.ssym.stoken.ptr),
310 yylval.ssym.stoken.ptr);
314 [{}@] { return yytext[0]; }
316 /* REGISTERS AND GDB CONVENIENCE VARIABLES */
318 "$"({LETTER}|{DIG}|"$")* {
319 yylval.sval.ptr = yytext;
320 yylval.sval.length = yyleng;
321 return SPECIAL_VARIABLE;
324 /* CATCH-ALL ERROR CASE */
326 . { error ("Invalid character '%s' in expression.", yytext); }
330 #include "gdb_string.h"
332 /* Initialize the lexer for processing new expression */
334 lexer_init (FILE *inp)
341 /* Make sure that tempbuf points at an array at least N characters long. */
344 resize_tempbuf (unsigned int n)
348 tempbufsize = (n+63) & ~63;
349 tempbuf = xrealloc (tempbuf, tempbufsize);
353 /* Copy S2 to S1, removing all underscores, and downcasing all letters. */
356 canonicalizeNumeral (char *s1, const char *s2)
358 for (; *s2 != '\000'; s2 += 1)
369 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
371 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
372 where 2 <= BASE <= 16. */
375 is_digit_in_base (unsigned char digit, int base)
377 if (!isxdigit (digit))
380 return (isdigit (digit) && digit < base + '0');
382 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
386 digit_to_int (unsigned char c)
391 return tolower (c) - 'a' + 10;
394 /* As for strtoul, but for ULONGEST results. */
396 strtoulst (const char *num, const char **trailer, int base)
398 unsigned int high_part;
403 if (base < 2 || base > 16)
408 lim = base - 1 + '0';
410 result = high_part = 0;
411 for (i = 0; is_digit_in_base (num[i], base); i += 1)
413 result = result*base + digit_to_int (num[i]);
414 high_part = high_part*base + (unsigned int) (result >> HIGH_BYTE_POSN);
415 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
416 if (high_part > 0xff)
419 result = high_part = 0;
427 return result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
432 /* Interprets the prefix of NUM that consists of digits of the given BASE
433 as an integer of that BASE, with the string EXP as an exponent.
434 Puts value in yylval, and returns INT, if the string is valid. Causes
435 an error if the number is improperly formated. BASE, if NULL, defaults
436 to "10", and EXP to "1". The EXP does not contain a leading 'e' or 'E'. */
439 processInt (const char *base0, const char *num0, const char *exp0)
451 base = strtol (base0, (char **) NULL, 10);
452 if (base < 2 || base > 16)
453 error ("Invalid base: %d.", base);
459 exp = strtol(exp0, (char **) NULL, 10);
462 result = strtoulst (num0, (const char **) &trailer, base);
464 error ("Integer literal out of range");
465 if (isxdigit(*trailer))
466 error ("Invalid digit `%c' in based literal", *trailer);
470 if (result > (ULONG_MAX / base))
471 error ("Integer literal out of range");
476 if ((result >> (TARGET_INT_BIT-1)) == 0)
477 yylval.typed_val.type = type_int ();
478 else if ((result >> (TARGET_LONG_BIT-1)) == 0)
479 yylval.typed_val.type = type_long ();
480 else if (((result >> (TARGET_LONG_BIT-1)) >> 1) == 0)
482 /* We have a number representable as an unsigned integer quantity.
483 For consistency with the C treatment, we will treat it as an
484 anonymous modular (unsigned) quantity. Alas, the types are such
485 that we need to store .val as a signed quantity. Sorry
486 for the mess, but C doesn't officially guarantee that a simple
487 assignment does the trick (no, it doesn't; read the reference manual).
489 yylval.typed_val.type = builtin_type_unsigned_long;
490 if (result & LONGEST_SIGN)
491 yylval.typed_val.val =
492 (LONGEST) (result & ~LONGEST_SIGN)
493 - (LONGEST_SIGN>>1) - (LONGEST_SIGN>>1);
495 yylval.typed_val.val = (LONGEST) result;
499 yylval.typed_val.type = type_long_long ();
501 yylval.typed_val.val = (LONGEST) result;
505 #if defined (PRINTF_HAS_LONG_DOUBLE)
506 # undef PRINTF_HAS_LONG_DOUBLE
507 # define PRINTF_HAS_LONG_DOUBLE 1
509 # define PRINTF_HAS_LONG_DOUBLE 0
513 processReal (const char *num0)
515 #if defined (PRINTF_HAS_LONG_DOUBLE)
516 if (sizeof (DOUBLEST) > sizeof (double))
517 sscanf (num0, "%Lg", &yylval.typed_val_float.dval);
522 sscanf (num0, "%lg", &temp);
523 yylval.typed_val_float.dval = temp;
526 yylval.typed_val_float.type = type_float ();
527 if (sizeof(DOUBLEST) >= TARGET_DOUBLE_BIT / TARGET_CHAR_BIT)
528 yylval.typed_val_float.type = type_double ();
529 if (sizeof(DOUBLEST) >= TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT)
530 yylval.typed_val_float.type = type_long_double ();
536 processId (const char *name0, int len)
538 char *name = obstack_alloc (&temp_parse_space, len + 11);
541 while (len > 0 && isspace (name0[len-1]))
546 if (isalnum (name0[i0]))
548 name[i] = tolower (name0[i0]);
551 else switch (name0[i0])
562 while (i0 < len && name0[i0] != '\'')
571 while (i0 < len && name0[i0] != '>')
582 yylval.ssym.sym = NULL;
583 yylval.ssym.stoken.ptr = name;
584 yylval.ssym.stoken.length = i;
589 block_lookup (char *name, char *err_name)
591 struct ada_symbol_info *syms;
593 struct symtab *symtab;
594 nsyms = ada_lookup_symbol_list (name, left_block_context,
596 if (left_block_context == NULL &&
597 (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK))
598 symtab = lookup_symtab (name);
603 left_block_context = yylval.bval =
604 BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
605 else if (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK)
607 if (left_block_context == NULL)
608 error ("No file or function \"%s\".", err_name);
610 error ("No function \"%s\" in specified context.", err_name);
614 left_block_context = yylval.bval = SYMBOL_BLOCK_VALUE (syms[0].sym);
616 warning ("Function name \"%s\" ambiguous here", err_name);
620 /* Look up NAME0 (assumed to be encoded) as a name in VAR_DOMAIN,
621 setting *TOKEN_TYPE to NAME or TYPENAME, depending on what is
622 found. Try first the entire name, then the name without the last
623 segment (i.e., after the last .id), etc., and return the number of
624 segments that had to be removed to get a match. Try only the full
625 name if it starts with "standard__". Calls error if no
626 matches are found, using ERR_NAME in any error message. When
627 exactly one symbol match is found, it is placed in yylval. When
628 the symbol is a renaming, follow at most DEPTH steps to find the
629 ultimate definition; cause error if depth exceeded. */
632 name_lookup (char *name0, char *err_name, int *token_type, int depth)
634 struct ada_symbol_info *syms;
636 int len0 = strlen (name0);
637 char *name = obsavestring (name0, len0, &temp_parse_space);
642 error ("Could not find renamed symbol \"%s\"", err_name);
644 yylval.ssym.stoken.ptr = name;
645 yylval.ssym.stoken.length = strlen (name);
646 for (segments = 0; ; segments += 1)
648 struct type *preferred_type;
649 int i, preferred_index;
651 if (left_block_context == NULL)
652 nsyms = ada_lookup_symbol_list (name, expression_context_block,
655 nsyms = ada_lookup_symbol_list (name, left_block_context,
659 /* Check for a type renaming. */
661 if (nsyms == 1 && !ada_is_object_renaming (syms[0].sym))
663 struct symbol *renaming_sym =
664 ada_find_renaming_symbol (SYMBOL_LINKAGE_NAME (syms[0].sym),
667 if (renaming_sym != NULL)
668 syms[0].sym = renaming_sym;
671 /* Check for a type definition. */
673 /* Look for a symbol that doesn't denote void. This is (I think) a */
674 /* temporary kludge to get around problems in GNAT output. */
675 preferred_index = -1; preferred_type = NULL;
676 for (i = 0; i < nsyms; i += 1)
677 switch (SYMBOL_CLASS (syms[i].sym))
680 if (ada_prefer_type (SYMBOL_TYPE (syms[i].sym), preferred_type))
683 preferred_type = SYMBOL_TYPE (syms[i].sym);
690 case LOC_REGPARM_ADDR:
694 case LOC_BASEREG_ARG:
696 case LOC_COMPUTED_ARG:
701 if (preferred_type != NULL)
703 if (TYPE_CODE (preferred_type) == TYPE_CODE_VOID)
704 error ("`%s' matches only void type name(s)",
706 else if (ada_is_object_renaming (syms[preferred_index].sym))
708 yylval.ssym.sym = syms[preferred_index].sym;
709 *token_type = OBJECT_RENAMING;
712 else if (ada_renaming_type (SYMBOL_TYPE (syms[preferred_index].sym))
717 = ada_simple_renamed_entity (syms[preferred_index].sym);
719 = (char *) obstack_alloc (&temp_parse_space,
720 strlen (renaming) + len0
721 - yylval.ssym.stoken.length + 1);
722 strcpy (new_name, renaming);
724 strcat (new_name, name0 + yylval.ssym.stoken.length);
725 result = name_lookup (new_name, err_name, token_type, depth - 1);
726 if (result > segments)
727 error ("Confused by renamed symbol.");
730 else if (segments == 0)
732 yylval.tval = preferred_type;
733 *token_type = TYPENAME;
740 type = language_lookup_primitive_type_by_name (current_language,
743 if (type == NULL && strcmp ("system__address", name) == 0)
744 type = type_system_address ();
747 /* First check to see if we have a regular definition of this
748 type that just didn't happen to have been read yet. */
751 char *expanded_name =
752 (char *) alloca (strlen (name) + sizeof ("standard__"));
753 strcpy (expanded_name, "standard__");
754 strcat (expanded_name, name);
755 sym = ada_lookup_symbol (expanded_name, NULL,
756 VAR_DOMAIN, NULL, NULL);
757 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
758 type = SYMBOL_TYPE (sym);
761 *token_type = TYPENAME;
770 yylval.ssym.sym = syms[0].sym;
771 yylval.ssym.msym = NULL;
772 yylval.ssym.block = syms[0].block;
775 else if (nsyms == 0) {
777 yylval.ssym.msym = ada_lookup_simple_minsym (name);
778 if (yylval.ssym.msym != NULL)
780 yylval.ssym.sym = NULL;
781 yylval.ssym.block = NULL;
787 && strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
788 error ("No definition of \"%s\" found.", err_name);
790 for (i = yylval.ssym.stoken.length - 1; i > 0; i -= 1)
795 yylval.ssym.stoken.length = i;
798 else if (name[i] == '_' && name[i-1] == '_')
802 yylval.ssym.stoken.length = i;
808 if (!have_full_symbols () && !have_partial_symbols ()
809 && left_block_context == NULL)
810 error ("No symbol table is loaded. Use the \"file\" command.");
811 if (left_block_context == NULL)
812 error ("No definition of \"%s\" in current context.",
815 error ("No definition of \"%s\" in specified context.",
822 yylval.ssym.sym = NULL;
823 yylval.ssym.msym = NULL;
824 if (left_block_context == NULL)
825 yylval.ssym.block = expression_context_block;
827 yylval.ssym.block = left_block_context;
833 /* Returns the position within STR of the '.' in a
834 '.{WHITE}*all' component of a dotted name, or -1 if there is none. */
836 find_dot_all (const char *str)
839 for (i = 0; str[i] != '\000'; i += 1)
846 while (isspace (str[i]));
847 if (strcmp (str+i, "all") == 0
848 && ! isalnum (str[i+3]) && str[i+3] != '_')
855 /* Returns non-zero iff string SUBSEQ matches a subsequence of STR, ignoring
859 subseqMatch (const char *subseq, const char *str)
861 if (subseq[0] == '\0')
863 else if (str[0] == '\0')
865 else if (tolower (subseq[0]) == tolower (str[0]))
866 return subseqMatch (subseq+1, str+1) || subseqMatch (subseq, str+1);
868 return subseqMatch (subseq, str+1);
872 static struct { const char *name; int code; }
874 { "address", TICK_ADDRESS },
875 { "unchecked_access", TICK_ACCESS },
876 { "unrestricted_access", TICK_ACCESS },
877 { "access", TICK_ACCESS },
878 { "first", TICK_FIRST },
879 { "last", TICK_LAST },
880 { "length", TICK_LENGTH },
883 { "modulus", TICK_MODULUS },
885 { "range", TICK_RANGE },
886 { "size", TICK_SIZE },
892 /* Return the syntactic code corresponding to the attribute name or
896 processAttribute (const char *str)
900 for (i = 0; attributes[i].code != -1; i += 1)
901 if (strcasecmp (str, attributes[i].name) == 0)
902 return attributes[i].code;
904 for (i = 0, k = -1; attributes[i].code != -1; i += 1)
905 if (subseqMatch (str, attributes[i].name))
910 error ("ambiguous attribute name: `%s'", str);
913 error ("unrecognized attribute: `%s'", str);
915 return attributes[k].code;
924 /* Dummy definition to suppress warnings about unused static definitions. */
925 typedef void (*dummy_function) ();
926 dummy_function ada_flex_use[] =
928 (dummy_function) yyunput