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")
46 #define NUMERAL_WIDTH 256
47 #define LONGEST_SIGN ((ULONGEST) 1 << (sizeof(LONGEST) * HOST_CHAR_BIT - 1))
49 /* Temporary staging for numeric literals. */
50 static char numbuf[NUMERAL_WIDTH];
51 static void canonicalizeNumeral (char *s1, const char *);
52 static int processInt (const char *, const char *, const char *);
53 static int processReal (const char *);
54 static int processId (const char *, int);
55 static int processAttribute (const char *);
56 static int find_dot_all (const char *);
59 #define YY_DECL static int yylex ( void )
62 #define YY_INPUT(BUF, RESULT, MAX_SIZE) \
63 if ( *lexptr == '\000' ) \
72 static char *tempbuf = NULL;
73 static int tempbufsize = 0;
74 static int tempbuf_len;
75 static struct block *left_block_context;
77 static void resize_tempbuf (unsigned int);
79 static void block_lookup (char *, char *);
81 static int name_lookup (char *, char *, int *, int);
83 static int find_dot_all (const char *);
87 %option case-insensitive interactive nodefault
89 %s IN_STRING BEFORE_QUAL_QUOTE
95 "--".* { yyterminate(); }
98 canonicalizeNumeral (numbuf, yytext);
99 return processInt (NULL, numbuf, strrchr(numbuf, 'e')+1);
103 canonicalizeNumeral (numbuf, yytext);
104 return processInt (NULL, numbuf, NULL);
107 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#"{POSEXP} {
108 canonicalizeNumeral (numbuf, yytext);
109 return processInt (numbuf,
110 strchr (numbuf, '#') + 1,
111 strrchr(numbuf, '#') + 1);
114 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#" {
115 canonicalizeNumeral (numbuf, yytext);
116 return processInt (numbuf, strchr (numbuf, '#') + 1, NULL);
120 canonicalizeNumeral (numbuf, yytext+2);
121 return processInt ("16#", numbuf, NULL);
125 {NUM10}"."{NUM10}{EXP} {
126 canonicalizeNumeral (numbuf, yytext);
127 return processReal (numbuf);
131 canonicalizeNumeral (numbuf, yytext);
132 return processReal (numbuf);
135 {NUM10}"#"{NUM16}"."{NUM16}"#"{EXP} {
136 error ("Based real literals not implemented yet.");
139 {NUM10}"#"{NUM16}"."{NUM16}"#" {
140 error ("Based real literals not implemented yet.");
143 <INITIAL>"'"({GRAPHIC}|\")"'" {
144 yylval.typed_val.type = type_char ();
145 yylval.typed_val.val = yytext[1];
149 <INITIAL>"'[\""{HEXDIG}{2}"\"]'" {
151 yylval.typed_val.type = type_char ();
152 sscanf (yytext+3, "%2x", &v);
153 yylval.typed_val.val = v;
157 \"{OPER}\"/{WHITE}*"(" { return processId (yytext, yyleng); }
164 <IN_STRING>{GRAPHIC}*\" {
165 resize_tempbuf (yyleng+tempbuf_len);
166 strncpy (tempbuf+tempbuf_len, yytext, yyleng-1);
167 tempbuf_len += yyleng-1;
168 yylval.sval.ptr = tempbuf;
169 yylval.sval.length = tempbuf_len;
174 <IN_STRING>{GRAPHIC}*"[\""{HEXDIG}{2}"\"]" {
176 resize_tempbuf (yyleng-5+tempbuf_len+1);
177 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
178 sscanf(yytext+yyleng-4, "%2x", &n);
179 tempbuf[yyleng-6+tempbuf_len] = (char) n;
180 tempbuf_len += yyleng-5;
183 <IN_STRING>{GRAPHIC}*"[\"\"\"]" {
185 resize_tempbuf (yyleng-4+tempbuf_len+1);
186 strncpy (tempbuf+tempbuf_len, yytext, yyleng-6);
187 tempbuf[yyleng-5+tempbuf_len] = '"';
188 tempbuf_len += yyleng-4;
192 while (*lexptr != 'i' && *lexptr != 'I')
201 and { return _AND_; }
202 else { return ELSE; }
207 null { return NULL_PTR; }
210 then { return THEN; }
215 {TICK}[a-zA-Z][a-zA-Z]+ { return processAttribute (yytext+1); }
219 "=>" { return ARROW; }
220 ".." { return DOTDOT; }
221 "**" { return STARSTAR; }
222 ":=" { return ASSIGN; }
223 "/=" { return NOTEQUAL; }
227 <BEFORE_QUAL_QUOTE>"'" { BEGIN INITIAL; return '\''; }
229 [-&*+./:<>=|;\[\]] { return yytext[0]; }
231 "," { if (paren_depth == 0 && comma_terminates)
241 "(" { paren_depth += 1; return '('; }
242 ")" { if (paren_depth == 0)
255 "."{WHITE}*all { return DOT_ALL; }
258 processId (yytext+1, yyleng-1);
262 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*(" "*"'")? {
263 int all_posn = find_dot_all (yytext);
264 int token_type, segments, k;
267 if (all_posn == -1 && yytext[yyleng-1] == '\'')
272 } while (yytext[yyleng-1] == ' ');
279 processId(yytext, yyleng);
280 segments = name_lookup (ada_encode (yylval.ssym.stoken.ptr),
281 yylval.ssym.stoken.ptr,
283 MAX_RENAMING_CHAIN_LENGTH);
284 left_block_context = NULL;
285 for (k = yyleng; segments > 0 && k > 0; k -= 1)
287 if (yytext[k-1] == '.')
292 error ("confused by name %s", yytext);
295 BEGIN BEFORE_QUAL_QUOTE;
299 /* GDB EXPRESSION CONSTRUCTS */
302 "'"[^']+"'"{WHITE}*:: {
303 processId(yytext, yyleng-2);
304 block_lookup (yylval.ssym.stoken.ptr, yylval.ssym.stoken.ptr);
308 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*{WHITE}*:: {
309 processId(yytext, yyleng-2);
310 block_lookup (ada_encode (yylval.ssym.stoken.ptr),
311 yylval.ssym.stoken.ptr);
315 [{}@] { return yytext[0]; }
317 /* REGISTERS AND GDB CONVENIENCE VARIABLES */
319 "$"({LETTER}|{DIG}|"$")* {
320 yylval.sval.ptr = yytext;
321 yylval.sval.length = yyleng;
322 return SPECIAL_VARIABLE;
325 /* CATCH-ALL ERROR CASE */
327 . { error ("Invalid character '%s' in expression.", yytext); }
331 #include "gdb_string.h"
333 /* Initialize the lexer for processing new expression */
335 lexer_init (FILE *inp)
342 /* Make sure that tempbuf points at an array at least N characters long. */
345 resize_tempbuf (unsigned int n)
349 tempbufsize = (n+63) & ~63;
350 tempbuf = (char *) realloc (tempbuf, tempbufsize);
354 /* Copy S2 to S1, removing all underscores, and downcasing all letters. */
357 canonicalizeNumeral (char *s1, const char *s2)
359 for (; *s2 != '\000'; s2 += 1)
370 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
372 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
373 where 2 <= BASE <= 16. */
376 is_digit_in_base (unsigned char digit, int base)
378 if (!isxdigit (digit))
381 return (isdigit (digit) && digit < base + '0');
383 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
387 digit_to_int (unsigned char c)
392 return tolower (c) - 'a' + 10;
395 /* As for strtoul, but for ULONGEST results. */
397 strtoulst (const char *num, const char **trailer, int base)
399 unsigned int high_part;
404 if (base < 2 || base > 16)
409 lim = base - 1 + '0';
411 result = high_part = 0;
412 for (i = 0; is_digit_in_base (num[i], base); i += 1)
414 result = result*base + digit_to_int (num[i]);
415 high_part = high_part*base + (unsigned int) (result >> HIGH_BYTE_POSN);
416 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
417 if (high_part > 0xff)
420 result = high_part = 0;
428 return result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
433 /* Interprets the prefix of NUM that consists of digits of the given BASE
434 as an integer of that BASE, with the string EXP as an exponent.
435 Puts value in yylval, and returns INT, if the string is valid. Causes
436 an error if the number is improperly formated. BASE, if NULL, defaults
437 to "10", and EXP to "1". The EXP does not contain a leading 'e' or 'E'. */
440 processInt (const char *base0, const char *num0, const char *exp0)
452 base = strtol (base0, (char **) NULL, 10);
453 if (base < 2 || base > 16)
454 error ("Invalid base: %d.", base);
460 exp = strtol(exp0, (char **) NULL, 10);
463 result = strtoulst (num0, (const char **) &trailer, base);
465 error ("Integer literal out of range");
466 if (isxdigit(*trailer))
467 error ("Invalid digit `%c' in based literal", *trailer);
471 if (result > (ULONG_MAX / base))
472 error ("Integer literal out of range");
477 if ((result >> (TARGET_INT_BIT-1)) == 0)
478 yylval.typed_val.type = type_int ();
479 else if ((result >> (TARGET_LONG_BIT-1)) == 0)
480 yylval.typed_val.type = type_long ();
481 else if (((result >> (TARGET_LONG_BIT-1)) >> 1) == 0)
483 /* We have a number representable as an unsigned integer quantity.
484 For consistency with the C treatment, we will treat it as an
485 anonymous modular (unsigned) quantity. Alas, the types are such
486 that we need to store .val as a signed quantity. Sorry
487 for the mess, but C doesn't officially guarantee that a simple
488 assignment does the trick (no, it doesn't; read the reference manual).
490 yylval.typed_val.type = builtin_type_unsigned_long;
491 if (result & LONGEST_SIGN)
492 yylval.typed_val.val =
493 (LONGEST) (result & ~LONGEST_SIGN)
494 - (LONGEST_SIGN>>1) - (LONGEST_SIGN>>1);
496 yylval.typed_val.val = (LONGEST) result;
500 yylval.typed_val.type = type_long_long ();
502 yylval.typed_val.val = (LONGEST) result;
506 #if defined (PRINTF_HAS_LONG_DOUBLE)
507 # undef PRINTF_HAS_LONG_DOUBLE
508 # define PRINTF_HAS_LONG_DOUBLE 1
510 # define PRINTF_HAS_LONG_DOUBLE 0
514 processReal (const char *num0)
516 #if defined (PRINTF_HAS_LONG_DOUBLE)
517 if (sizeof (DOUBLEST) > sizeof (double))
518 sscanf (num0, "%Lg", &yylval.typed_val_float.dval);
523 sscanf (num0, "%lg", &temp);
524 yylval.typed_val_float.dval = temp;
527 yylval.typed_val_float.type = type_float ();
528 if (sizeof(DOUBLEST) >= TARGET_DOUBLE_BIT / TARGET_CHAR_BIT)
529 yylval.typed_val_float.type = type_double ();
530 if (sizeof(DOUBLEST) >= TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT)
531 yylval.typed_val_float.type = type_long_double ();
537 processId (const char *name0, int len)
539 char *name = obstack_alloc (&temp_parse_space, len + 11);
542 while (len > 0 && isspace (name0[len-1]))
547 if (isalnum (name0[i0]))
549 name[i] = tolower (name0[i0]);
552 else switch (name0[i0])
563 while (i0 < len && name0[i0] != '\'')
572 while (i0 < len && name0[i0] != '>')
583 yylval.ssym.sym = NULL;
584 yylval.ssym.stoken.ptr = name;
585 yylval.ssym.stoken.length = i;
590 block_lookup (char *name, char *err_name)
592 struct ada_symbol_info *syms;
594 struct symtab *symtab;
595 nsyms = ada_lookup_symbol_list (name, left_block_context,
597 if (left_block_context == NULL &&
598 (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK))
599 symtab = lookup_symtab (name);
604 left_block_context = yylval.bval =
605 BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
606 else if (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK)
608 if (left_block_context == NULL)
609 error ("No file or function \"%s\".", err_name);
611 error ("No function \"%s\" in specified context.", err_name);
615 left_block_context = yylval.bval = SYMBOL_BLOCK_VALUE (syms[0].sym);
617 warning ("Function name \"%s\" ambiguous here", err_name);
621 /* Look up NAME0 (assumed to be encoded) as a name in VAR_DOMAIN,
622 setting *TOKEN_TYPE to NAME or TYPENAME, depending on what is
623 found. Try first the entire name, then the name without the last
624 segment (i.e., after the last .id), etc., and return the number of
625 segments that had to be removed to get a match. Try only the full
626 name if it starts with "standard__". Calls error if no
627 matches are found, using ERR_NAME in any error message. When
628 exactly one symbol match is found, it is placed in yylval. When
629 the symbol is a renaming, follow at most DEPTH steps to find the
630 ultimate definition; cause error if depth exceeded. */
633 name_lookup (char *name0, char *err_name, int *token_type, int depth)
635 struct ada_symbol_info *syms;
637 int len0 = strlen (name0);
638 char *name = obsavestring (name0, len0, &temp_parse_space);
643 error ("Could not find renamed symbol \"%s\"", err_name);
645 yylval.ssym.stoken.ptr = name;
646 yylval.ssym.stoken.length = strlen (name);
647 for (segments = 0; ; segments += 1)
649 struct type *preferred_type;
650 int i, preferred_index;
652 if (left_block_context == NULL)
653 nsyms = ada_lookup_symbol_list (name, expression_context_block,
656 nsyms = ada_lookup_symbol_list (name, left_block_context,
660 /* Check for a type renaming. */
662 if (nsyms == 1 && !ada_is_object_renaming (syms[0].sym))
664 struct symbol *renaming_sym =
665 ada_find_renaming_symbol (SYMBOL_LINKAGE_NAME (syms[0].sym),
668 if (renaming_sym != NULL)
669 syms[0].sym = renaming_sym;
672 /* Check for a type definition. */
674 /* Look for a symbol that doesn't denote void. This is (I think) a */
675 /* temporary kludge to get around problems in GNAT output. */
676 preferred_index = -1; preferred_type = NULL;
677 for (i = 0; i < nsyms; i += 1)
678 switch (SYMBOL_CLASS (syms[i].sym))
681 if (ada_prefer_type (SYMBOL_TYPE (syms[i].sym), preferred_type))
684 preferred_type = SYMBOL_TYPE (syms[i].sym);
691 case LOC_REGPARM_ADDR:
695 case LOC_BASEREG_ARG:
697 case LOC_COMPUTED_ARG:
702 if (preferred_type != NULL)
704 if (TYPE_CODE (preferred_type) == TYPE_CODE_VOID)
705 error ("`%s' matches only void type name(s)",
707 else if (ada_is_object_renaming (syms[preferred_index].sym))
709 yylval.ssym.sym = syms[preferred_index].sym;
710 *token_type = OBJECT_RENAMING;
713 else if (ada_renaming_type (SYMBOL_TYPE (syms[preferred_index].sym))
718 = ada_simple_renamed_entity (syms[preferred_index].sym);
720 = (char *) obstack_alloc (&temp_parse_space,
721 strlen (renaming) + len0
722 - yylval.ssym.stoken.length + 1);
723 strcpy (new_name, renaming);
725 strcat (new_name, name0 + yylval.ssym.stoken.length);
726 result = name_lookup (new_name, err_name, token_type, depth - 1);
727 if (result > segments)
728 error ("Confused by renamed symbol.");
731 else if (segments == 0)
733 yylval.tval = preferred_type;
734 *token_type = TYPENAME;
741 type = language_lookup_primitive_type_by_name (current_language,
744 if (type == NULL && strcmp ("system__address", name) == 0)
745 type = type_system_address ();
748 /* First check to see if we have a regular definition of this
749 type that just didn't happen to have been read yet. */
752 char *expanded_name =
753 (char *) alloca (strlen (name) + sizeof ("standard__"));
754 strcpy (expanded_name, "standard__");
755 strcat (expanded_name, name);
756 sym = ada_lookup_symbol (expanded_name, NULL,
757 VAR_DOMAIN, NULL, NULL);
758 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
759 type = SYMBOL_TYPE (sym);
762 *token_type = TYPENAME;
771 yylval.ssym.sym = syms[0].sym;
772 yylval.ssym.msym = NULL;
773 yylval.ssym.block = syms[0].block;
776 else if (nsyms == 0) {
778 yylval.ssym.msym = ada_lookup_simple_minsym (name);
779 if (yylval.ssym.msym != NULL)
781 yylval.ssym.sym = NULL;
782 yylval.ssym.block = NULL;
788 && strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
789 error ("No definition of \"%s\" found.", err_name);
791 for (i = yylval.ssym.stoken.length - 1; i > 0; i -= 1)
796 yylval.ssym.stoken.length = i;
799 else if (name[i] == '_' && name[i-1] == '_')
803 yylval.ssym.stoken.length = i;
809 if (!have_full_symbols () && !have_partial_symbols ()
810 && left_block_context == NULL)
811 error ("No symbol table is loaded. Use the \"file\" command.");
812 if (left_block_context == NULL)
813 error ("No definition of \"%s\" in current context.",
816 error ("No definition of \"%s\" in specified context.",
823 yylval.ssym.sym = NULL;
824 yylval.ssym.msym = NULL;
825 if (left_block_context == NULL)
826 yylval.ssym.block = expression_context_block;
828 yylval.ssym.block = left_block_context;
834 /* Returns the position within STR of the '.' in a
835 '.{WHITE}*all' component of a dotted name, or -1 if there is none. */
837 find_dot_all (const char *str)
840 for (i = 0; str[i] != '\000'; i += 1)
847 while (isspace (str[i]));
848 if (strcmp (str+i, "all") == 0
849 && ! isalnum (str[i+3]) && str[i+3] != '_')
856 /* Returns non-zero iff string SUBSEQ matches a subsequence of STR, ignoring
860 subseqMatch (const char *subseq, const char *str)
862 if (subseq[0] == '\0')
864 else if (str[0] == '\0')
866 else if (tolower (subseq[0]) == tolower (str[0]))
867 return subseqMatch (subseq+1, str+1) || subseqMatch (subseq, str+1);
869 return subseqMatch (subseq, str+1);
873 static struct { const char *name; int code; }
875 { "address", TICK_ADDRESS },
876 { "unchecked_access", TICK_ACCESS },
877 { "unrestricted_access", TICK_ACCESS },
878 { "access", TICK_ACCESS },
879 { "first", TICK_FIRST },
880 { "last", TICK_LAST },
881 { "length", TICK_LENGTH },
884 { "modulus", TICK_MODULUS },
886 { "range", TICK_RANGE },
887 { "size", TICK_SIZE },
893 /* Return the syntactic code corresponding to the attribute name or
897 processAttribute (const char *str)
901 for (i = 0; attributes[i].code != -1; i += 1)
902 if (strcasecmp (str, attributes[i].name) == 0)
903 return attributes[i].code;
905 for (i = 0, k = -1; attributes[i].code != -1; i += 1)
906 if (subseqMatch (str, attributes[i].name))
911 error ("ambiguous attribute name: `%s'", str);
914 error ("unrecognized attribute: `%s'", str);
916 return attributes[k].code;
925 /* Dummy definition to suppress warnings about unused static definitions. */
926 typedef void (*dummy_function) ();
927 dummy_function ada_flex_use[] =
929 (dummy_function) yyrealloc, (dummy_function) yyunput