1 /* FLEX lexer for Ada expressions, for GDB.
2 Copyright (C) 1994-2014 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 3 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, see <http://www.gnu.org/licenses/>. */
19 /*----------------------------------------------------------------------*/
21 /* The converted version of this file is to be included in ada-exp.y, */
22 /* the Ada parser for gdb. The function yylex obtains characters from */
23 /* the global pointer lexptr. It returns a syntactic category for */
24 /* each successive token and places a semantic value into yylval */
25 /* (ada-lval), defined by the parser. */
28 NUM10 ({DIG}({DIG}|_)*)
30 NUM16 ({HEXDIG}({HEXDIG}|_)*)
33 ID ({LETTER}({LETTER}|{DIG})*|"<"{LETTER}({LETTER}|{DIG})*">")
36 GRAPHIC [a-z0-9 #&'()*+,-./:;<>=_|!$%?@\[\]\\^`{}~]
37 OPER ([-+*/=<>&]|"<="|">="|"**"|"/="|"and"|"or"|"xor"|"not"|"mod"|"rem"|"abs")
44 #define NUMERAL_WIDTH 256
45 #define LONGEST_SIGN ((ULONGEST) 1 << (sizeof(LONGEST) * HOST_CHAR_BIT - 1))
47 /* Temporary staging for numeric literals. */
48 static char numbuf[NUMERAL_WIDTH];
49 static void canonicalizeNumeral (char *s1, const char *);
50 static struct stoken processString (const char*, int);
51 static int processInt (struct parser_state *, const char *, const char *,
53 static int processReal (struct parser_state *, const char *);
54 static struct stoken processId (const char *, int);
55 static int processAttribute (const char *);
56 static int find_dot_all (const char *);
57 static void rewind_to_char (int);
60 #define YY_DECL static int yylex ( void )
62 /* Flex generates a static function "input" which is not used.
63 Defining YY_NO_INPUT comments it out. */
67 #define YY_INPUT(BUF, RESULT, MAX_SIZE) \
68 if ( *lexptr == '\000' ) \
77 static int find_dot_all (const char *);
81 %option case-insensitive interactive nodefault
89 "--".* { yyterminate(); }
92 canonicalizeNumeral (numbuf, yytext);
93 return processInt (pstate, NULL, numbuf,
94 strrchr (numbuf, 'e') + 1);
98 canonicalizeNumeral (numbuf, yytext);
99 return processInt (pstate, NULL, numbuf, NULL);
102 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#"{POSEXP} {
103 canonicalizeNumeral (numbuf, yytext);
104 return processInt (pstate, numbuf,
105 strchr (numbuf, '#') + 1,
106 strrchr(numbuf, '#') + 1);
109 {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#" {
110 canonicalizeNumeral (numbuf, yytext);
111 return processInt (pstate, numbuf, strchr (numbuf, '#') + 1,
116 canonicalizeNumeral (numbuf, yytext+2);
117 return processInt (pstate, "16#", numbuf, NULL);
121 {NUM10}"."{NUM10}{EXP} {
122 canonicalizeNumeral (numbuf, yytext);
123 return processReal (pstate, numbuf);
127 canonicalizeNumeral (numbuf, yytext);
128 return processReal (pstate, numbuf);
131 {NUM10}"#"{NUM16}"."{NUM16}"#"{EXP} {
132 error (_("Based real literals not implemented yet."));
135 {NUM10}"#"{NUM16}"."{NUM16}"#" {
136 error (_("Based real literals not implemented yet."));
139 <INITIAL>"'"({GRAPHIC}|\")"'" {
140 yylval.typed_val.type = type_char (pstate);
141 yylval.typed_val.val = yytext[1];
145 <INITIAL>"'[\""{HEXDIG}{2}"\"]'" {
147 yylval.typed_val.type = type_char (pstate);
148 sscanf (yytext+3, "%2x", &v);
149 yylval.typed_val.val = v;
153 \"({GRAPHIC}|"[\""({HEXDIG}{2}|\")"\"]")*\" {
154 yylval.sval = processString (yytext+1, yyleng-2);
159 error (_("ill-formed or non-terminated string literal"));
164 rewind_to_char ('i');
169 rewind_to_char ('t');
173 thread{WHITE}+{DIG} {
174 /* This keyword signals the end of the expression and
175 will be processed separately. */
176 rewind_to_char ('t');
183 and { return _AND_; }
184 else { return ELSE; }
189 null { return NULL_PTR; }
191 others { return OTHERS; }
193 then { return THEN; }
196 /* BOOLEAN "KEYWORDS" */
198 /* True and False are not keywords in Ada, but rather enumeration constants.
199 However, the boolean type is no longer represented as an enum, so True
200 and False are no longer defined in symbol tables. We compromise by
201 making them keywords (when bare). */
203 true { return TRUEKEYWORD; }
204 false { return FALSEKEYWORD; }
208 {TICK}[a-zA-Z][a-zA-Z]+ { return processAttribute (yytext+1); }
212 "=>" { return ARROW; }
213 ".." { return DOTDOT; }
214 "**" { return STARSTAR; }
215 ":=" { return ASSIGN; }
216 "/=" { return NOTEQUAL; }
220 <BEFORE_QUAL_QUOTE>"'" { BEGIN INITIAL; return '\''; }
222 [-&*+./:<>=|;\[\]] { return yytext[0]; }
224 "," { if (paren_depth == 0 && comma_terminates)
226 rewind_to_char (',');
233 "(" { paren_depth += 1; return '('; }
234 ")" { if (paren_depth == 0)
236 rewind_to_char (')');
246 "."{WHITE}*all { return DOT_ALL; }
249 yylval.sval = processId (yytext+1, yyleng-1);
253 {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*(" "*"'")? {
254 int all_posn = find_dot_all (yytext);
256 if (all_posn == -1 && yytext[yyleng-1] == '\'')
258 BEGIN BEFORE_QUAL_QUOTE;
261 else if (all_posn >= 0)
263 yylval.sval = processId (yytext, yyleng);
268 /* GDB EXPRESSION CONSTRUCTS */
270 "'"[^']+"'"{WHITE}*:: {
272 yylval.sval = processId (yytext, yyleng);
276 "::" { return COLONCOLON; }
278 [{}@] { return yytext[0]; }
280 /* REGISTERS AND GDB CONVENIENCE VARIABLES */
282 "$"({LETTER}|{DIG}|"$")* {
283 yylval.sval.ptr = yytext;
284 yylval.sval.length = yyleng;
285 return SPECIAL_VARIABLE;
288 /* CATCH-ALL ERROR CASE */
290 . { error (_("Invalid character '%s' in expression."), yytext); }
294 /* Initialize the lexer for processing new expression. */
297 lexer_init (FILE *inp)
304 /* Copy S2 to S1, removing all underscores, and downcasing all letters. */
307 canonicalizeNumeral (char *s1, const char *s2)
309 for (; *s2 != '\000'; s2 += 1)
320 /* Interprets the prefix of NUM that consists of digits of the given BASE
321 as an integer of that BASE, with the string EXP as an exponent.
322 Puts value in yylval, and returns INT, if the string is valid. Causes
323 an error if the number is improperly formated. BASE, if NULL, defaults
324 to "10", and EXP to "1". The EXP does not contain a leading 'e' or 'E'.
328 processInt (struct parser_state *par_state, const char *base0,
329 const char *num0, const char *exp0)
340 base = strtol (base0, (char **) NULL, 10);
341 if (base < 2 || base > 16)
342 error (_("Invalid base: %d."), base);
348 exp = strtol(exp0, (char **) NULL, 10);
351 result = strtoulst (num0, &trailer, base);
353 error (_("Integer literal out of range"));
354 if (isxdigit(*trailer))
355 error (_("Invalid digit `%c' in based literal"), *trailer);
359 if (result > (ULONG_MAX / base))
360 error (_("Integer literal out of range"));
365 if ((result >> (gdbarch_int_bit (parse_gdbarch (par_state))-1)) == 0)
366 yylval.typed_val.type = type_int (par_state);
367 else if ((result >> (gdbarch_long_bit (parse_gdbarch (par_state))-1)) == 0)
368 yylval.typed_val.type = type_long (par_state);
369 else if (((result >> (gdbarch_long_bit (parse_gdbarch (par_state))-1)) >> 1) == 0)
371 /* We have a number representable as an unsigned integer quantity.
372 For consistency with the C treatment, we will treat it as an
373 anonymous modular (unsigned) quantity. Alas, the types are such
374 that we need to store .val as a signed quantity. Sorry
375 for the mess, but C doesn't officially guarantee that a simple
376 assignment does the trick (no, it doesn't; read the reference manual).
378 yylval.typed_val.type
379 = builtin_type (parse_gdbarch (par_state))->builtin_unsigned_long;
380 if (result & LONGEST_SIGN)
381 yylval.typed_val.val =
382 (LONGEST) (result & ~LONGEST_SIGN)
383 - (LONGEST_SIGN>>1) - (LONGEST_SIGN>>1);
385 yylval.typed_val.val = (LONGEST) result;
389 yylval.typed_val.type = type_long_long (par_state);
391 yylval.typed_val.val = (LONGEST) result;
396 processReal (struct parser_state *par_state, const char *num0)
398 sscanf (num0, "%" DOUBLEST_SCAN_FORMAT, &yylval.typed_val_float.dval);
400 yylval.typed_val_float.type = type_float (par_state);
401 if (sizeof(DOUBLEST) >= gdbarch_double_bit (parse_gdbarch (par_state))
403 yylval.typed_val_float.type = type_double (par_state);
404 if (sizeof(DOUBLEST) >= gdbarch_long_double_bit (parse_gdbarch (par_state))
406 yylval.typed_val_float.type = type_long_double (par_state);
412 /* Store a canonicalized version of NAME0[0..LEN-1] in yylval.ssym. The
413 resulting string is valid until the next call to ada_parse. If
414 NAME0 contains the substring "___", it is assumed to be already
415 encoded and the resulting name is equal to it. Otherwise, it differs
417 + Characters between '...' or <...> are transfered verbatim to
419 + <, >, and trailing "'" characters in quoted sequences are removed
420 (a leading quote is preserved to indicate that the name is not to be
422 + Unquoted whitespace is removed.
423 + Unquoted alphabetic characters are mapped to lower case.
424 Result is returned as a struct stoken, but for convenience, the string
425 is also null-terminated. Result string valid until the next call of
429 processId (const char *name0, int len)
431 char *name = obstack_alloc (&temp_parse_space, len + 11);
433 struct stoken result;
436 while (len > 0 && isspace (name0[len-1]))
439 if (strstr (name0, "___") != NULL)
441 strncpy (name, name0, len);
450 if (isalnum (name0[i0]))
452 name[i] = tolower (name0[i0]);
455 else switch (name0[i0])
470 while (i0 < len && name0[i0] != '\'');
475 while (i0 < len && name0[i0] != '>')
490 /* Return TEXT[0..LEN-1], a string literal without surrounding quotes,
491 with special hex character notations replaced with characters.
492 Result valid until the next call to ada_parse. */
495 processString (const char *text, int len)
499 const char *lim = text + len;
500 struct stoken result;
502 q = obstack_alloc (&temp_parse_space, len);
507 if (p[0] == '[' && p[1] == '"' && p+2 < lim)
509 if (p[2] == '"') /* "...["""]... */
517 sscanf (p+2, "%2x", &chr);
527 result.length = q - result.ptr;
531 /* Returns the position within STR of the '.' in a
532 '.{WHITE}*all' component of a dotted name, or -1 if there is none.
533 Note: we actually don't need this routine, since 'all' can never be an
534 Ada identifier. Thus, looking up foo.all or foo.all.x as a name
535 must fail, and will eventually be interpreted as (foo).all or
536 (foo).all.x. However, this does avoid an extraneous lookup. */
539 find_dot_all (const char *str)
543 for (i = 0; str[i] != '\000'; i++)
550 while (isspace (str[i]));
552 if (strncasecmp (str + i, "all", 3) == 0
553 && !isalnum (str[i + 3]) && str[i + 3] != '_')
559 /* Returns non-zero iff string SUBSEQ matches a subsequence of STR, ignoring
563 subseqMatch (const char *subseq, const char *str)
565 if (subseq[0] == '\0')
567 else if (str[0] == '\0')
569 else if (tolower (subseq[0]) == tolower (str[0]))
570 return subseqMatch (subseq+1, str+1) || subseqMatch (subseq, str+1);
572 return subseqMatch (subseq, str+1);
576 static struct { const char *name; int code; }
578 { "address", TICK_ADDRESS },
579 { "unchecked_access", TICK_ACCESS },
580 { "unrestricted_access", TICK_ACCESS },
581 { "access", TICK_ACCESS },
582 { "first", TICK_FIRST },
583 { "last", TICK_LAST },
584 { "length", TICK_LENGTH },
587 { "modulus", TICK_MODULUS },
589 { "range", TICK_RANGE },
590 { "size", TICK_SIZE },
596 /* Return the syntactic code corresponding to the attribute name or
600 processAttribute (const char *str)
604 for (i = 0; attributes[i].code != -1; i += 1)
605 if (strcasecmp (str, attributes[i].name) == 0)
606 return attributes[i].code;
608 for (i = 0, k = -1; attributes[i].code != -1; i += 1)
609 if (subseqMatch (str, attributes[i].name))
614 error (_("ambiguous attribute name: `%s'"), str);
617 error (_("unrecognized attribute: `%s'"), str);
619 return attributes[k].code;
622 /* Back up lexptr by yyleng and then to the rightmost occurrence of
623 character CH, case-folded (there must be one). WARNING: since
624 lexptr points to the next input character that Flex has not yet
625 transferred to its internal buffer, the use of this function
626 depends on the assumption that Flex calls YY_INPUT only when it is
627 logically necessary to do so (thus, there is no reading ahead
628 farther than needed to identify the next token.) */
631 rewind_to_char (int ch)
634 while (toupper (*lexptr) != toupper (ch))
645 /* Dummy definition to suppress warnings about unused static definitions. */
646 typedef void (*dummy_function) ();
647 dummy_function ada_flex_use[] =
649 (dummy_function) yyunput