ada-lex.l 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* FLEX lexer for Ada expressions, for GDB. -*- c++ -*-
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /*----------------------------------------------------------------------*/
  15. /* The converted version of this file is to be included in ada-exp.y, */
  16. /* the Ada parser for gdb. The function yylex obtains characters from */
  17. /* the global pointer lexptr. It returns a syntactic category for */
  18. /* each successive token and places a semantic value into yylval */
  19. /* (ada-lval), defined by the parser. */
  20. DIG [0-9]
  21. NUM10 ({DIG}({DIG}|_)*)
  22. HEXDIG [0-9a-f]
  23. NUM16 ({HEXDIG}({HEXDIG}|_)*)
  24. OCTDIG [0-7]
  25. LETTER [a-z_]
  26. ID ({LETTER}({LETTER}|{DIG}|[\x80-\xff])*|"<"{LETTER}({LETTER}|{DIG})*">")
  27. WHITE [ \t\n]
  28. TICK ("'"{WHITE}*)
  29. GRAPHIC [a-z0-9 #&'()*+,-./:;<>=_|!$%?@\[\]\\^`{}~]
  30. OPER ([-+*/=<>&]|"<="|">="|"**"|"/="|"and"|"or"|"xor"|"not"|"mod"|"rem"|"abs")
  31. EXP (e[+-]{NUM10})
  32. POSEXP (e"+"?{NUM10})
  33. /* This must agree with COMPLETION_CHAR below. See the comment there
  34. for the explanation. */
  35. COMPLETE "\001"
  36. NOT_COMPLETE [^\001]
  37. %{
  38. #include "diagnostics.h"
  39. /* Some old versions of flex generate code that uses the "register" keyword,
  40. which clang warns about. This was observed for example with flex 2.5.35,
  41. as shipped with macOS 10.12. The same happens with flex 2.5.37 and g++ 11
  42. which defaults to ISO C++17, that does not allow register storage class
  43. specifiers. */
  44. DIAGNOSTIC_PUSH
  45. DIAGNOSTIC_IGNORE_DEPRECATED_REGISTER
  46. #define NUMERAL_WIDTH 256
  47. #define LONGEST_SIGN ((ULONGEST) 1 << (sizeof(LONGEST) * HOST_CHAR_BIT - 1))
  48. /* Temporary staging for numeric literals. */
  49. static char numbuf[NUMERAL_WIDTH];
  50. static void canonicalizeNumeral (char *s1, const char *);
  51. static struct stoken processString (const char*, int);
  52. static int processInt (struct parser_state *, const char *, const char *,
  53. const char *);
  54. static int processReal (struct parser_state *, const char *);
  55. static struct stoken processId (const char *, int);
  56. static int processAttribute (const char *);
  57. static int find_dot_all (const char *);
  58. static void rewind_to_char (int);
  59. #undef YY_DECL
  60. #define YY_DECL static int yylex ( void )
  61. /* Flex generates a static function "input" which is not used.
  62. Defining YY_NO_INPUT comments it out. */
  63. #define YY_NO_INPUT
  64. /* When completing, we'll return a special character at the end of the
  65. input, to signal the completion position to the lexer. This is
  66. done because flex does not have a generally useful way to detect
  67. EOF in a pattern. This variable records whether the special
  68. character has been emitted. */
  69. static bool returned_complete = false;
  70. /* The character we use to represent the completion point. */
  71. #define COMPLETE_CHAR '\001'
  72. #undef YY_INPUT
  73. #define YY_INPUT(BUF, RESULT, MAX_SIZE) \
  74. if ( *pstate->lexptr == '\000' ) \
  75. { \
  76. if (pstate->parse_completion && !returned_complete) \
  77. { \
  78. returned_complete = true; \
  79. *(BUF) = COMPLETE_CHAR; \
  80. (RESULT) = 1; \
  81. } \
  82. else \
  83. (RESULT) = YY_NULL; \
  84. } \
  85. else \
  86. { \
  87. *(BUF) = *pstate->lexptr == COMPLETE_CHAR ? ' ' : *pstate->lexptr; \
  88. (RESULT) = 1; \
  89. pstate->lexptr += 1; \
  90. }
  91. /* Depth of parentheses. */
  92. static int paren_depth;
  93. %}
  94. %option case-insensitive interactive nodefault noyywrap
  95. %s BEFORE_QUAL_QUOTE
  96. %%
  97. {WHITE} { }
  98. "--".* { yyterminate(); }
  99. {NUM10}{POSEXP} {
  100. canonicalizeNumeral (numbuf, yytext);
  101. char *e_ptr = strrchr (numbuf, 'e');
  102. *e_ptr = '\0';
  103. return processInt (pstate, nullptr, numbuf, e_ptr + 1);
  104. }
  105. {NUM10} {
  106. canonicalizeNumeral (numbuf, yytext);
  107. return processInt (pstate, NULL, numbuf, NULL);
  108. }
  109. {NUM10}"#"{HEXDIG}({HEXDIG}|_)*"#"{POSEXP} {
  110. canonicalizeNumeral (numbuf, yytext);
  111. char *e_ptr = strrchr (numbuf, 'e');
  112. *e_ptr = '\0';
  113. return processInt (pstate, numbuf,
  114. strchr (numbuf, '#') + 1,
  115. e_ptr + 1);
  116. }
  117. /* The "llf" is a gdb extension to allow a floating-point
  118. constant to be written in some other base. The
  119. floating-point number is formed by reinterpreting the
  120. bytes, allowing direct control over the bits. */
  121. {NUM10}(l{0,2}f)?"#"{HEXDIG}({HEXDIG}|_)*"#" {
  122. canonicalizeNumeral (numbuf, yytext);
  123. return processInt (pstate, numbuf, strchr (numbuf, '#') + 1,
  124. NULL);
  125. }
  126. "0x"{HEXDIG}+ {
  127. canonicalizeNumeral (numbuf, yytext+2);
  128. return processInt (pstate, "16#", numbuf, NULL);
  129. }
  130. {NUM10}"."{NUM10}{EXP} {
  131. canonicalizeNumeral (numbuf, yytext);
  132. return processReal (pstate, numbuf);
  133. }
  134. {NUM10}"."{NUM10} {
  135. canonicalizeNumeral (numbuf, yytext);
  136. return processReal (pstate, numbuf);
  137. }
  138. {NUM10}"#"{NUM16}"."{NUM16}"#"{EXP} {
  139. error (_("Based real literals not implemented yet."));
  140. }
  141. {NUM10}"#"{NUM16}"."{NUM16}"#" {
  142. error (_("Based real literals not implemented yet."));
  143. }
  144. <INITIAL>"'"({GRAPHIC}|\")"'" {
  145. yylval.typed_val.val = yytext[1];
  146. yylval.typed_val.type = type_for_char (pstate, yytext[1]);
  147. return CHARLIT;
  148. }
  149. <INITIAL>"'[\""{HEXDIG}{2,}"\"]'" {
  150. ULONGEST v = strtoulst (yytext+3, nullptr, 16);
  151. yylval.typed_val.val = v;
  152. yylval.typed_val.type = type_for_char (pstate, v);
  153. return CHARLIT;
  154. }
  155. /* Note that we don't handle bracket sequences of more than 2
  156. digits here. Currently there's no support for wide or
  157. wide-wide strings. */
  158. \"({GRAPHIC}|"[\""({HEXDIG}{2,}|\")"\"]")*\" {
  159. yylval.sval = processString (yytext+1, yyleng-2);
  160. return STRING;
  161. }
  162. \" {
  163. error (_("ill-formed or non-terminated string literal"));
  164. }
  165. if {
  166. rewind_to_char ('i');
  167. return 0;
  168. }
  169. task {
  170. rewind_to_char ('t');
  171. return 0;
  172. }
  173. thread{WHITE}+{DIG} {
  174. /* This keyword signals the end of the expression and
  175. will be processed separately. */
  176. rewind_to_char ('t');
  177. return 0;
  178. }
  179. /* ADA KEYWORDS */
  180. abs { return ABS; }
  181. and { return _AND_; }
  182. else { return ELSE; }
  183. in { return IN; }
  184. mod { return MOD; }
  185. new { return NEW; }
  186. not { return NOT; }
  187. null { return NULL_PTR; }
  188. or { return OR; }
  189. others { return OTHERS; }
  190. rem { return REM; }
  191. then { return THEN; }
  192. xor { return XOR; }
  193. /* BOOLEAN "KEYWORDS" */
  194. /* True and False are not keywords in Ada, but rather enumeration constants.
  195. However, the boolean type is no longer represented as an enum, so True
  196. and False are no longer defined in symbol tables. We compromise by
  197. making them keywords (when bare). */
  198. true { return TRUEKEYWORD; }
  199. false { return FALSEKEYWORD; }
  200. /* ATTRIBUTES */
  201. {TICK}([a-z][a-z_]*)?{COMPLETE}? { BEGIN INITIAL; return processAttribute (yytext); }
  202. /* PUNCTUATION */
  203. "=>" { return ARROW; }
  204. ".." { return DOTDOT; }
  205. "**" { return STARSTAR; }
  206. ":=" { return ASSIGN; }
  207. "/=" { return NOTEQUAL; }
  208. "<=" { return LEQ; }
  209. ">=" { return GEQ; }
  210. <BEFORE_QUAL_QUOTE>"'"/{NOT_COMPLETE} { BEGIN INITIAL; return '\''; }
  211. [-&*+{}@/:<>=|;\[\]] { return yytext[0]; }
  212. "," { if (paren_depth == 0 && pstate->comma_terminates)
  213. {
  214. rewind_to_char (',');
  215. return 0;
  216. }
  217. else
  218. return ',';
  219. }
  220. "(" { paren_depth += 1; return '('; }
  221. ")" { if (paren_depth == 0)
  222. {
  223. rewind_to_char (')');
  224. return 0;
  225. }
  226. else
  227. {
  228. paren_depth -= 1;
  229. return ')';
  230. }
  231. }
  232. "."{WHITE}*{ID}{COMPLETE}? {
  233. yylval.sval = processId (yytext+1, yyleng-1);
  234. if (yytext[yyleng - 1] == COMPLETE_CHAR)
  235. return DOT_COMPLETE;
  236. return DOT_ID;
  237. }
  238. "."{WHITE}*{COMPLETE} {
  239. yylval.sval.ptr = "";
  240. yylval.sval.length = 0;
  241. return DOT_COMPLETE;
  242. }
  243. {ID}({WHITE}*"."{WHITE}*({ID}|\"{OPER}\"))*(" "*"'"|{COMPLETE})? {
  244. int all_posn = find_dot_all (yytext);
  245. if (all_posn == -1 && yytext[yyleng-1] == '\'')
  246. {
  247. BEGIN BEFORE_QUAL_QUOTE;
  248. yyless (yyleng-1);
  249. }
  250. else if (all_posn >= 0)
  251. yyless (all_posn);
  252. bool is_completion = yytext[yyleng - 1] == COMPLETE_CHAR;
  253. yylval.sval = processId (yytext, yyleng);
  254. return is_completion ? NAME_COMPLETE : NAME;
  255. }
  256. /* GDB EXPRESSION CONSTRUCTS */
  257. "'"[^']+"'"{WHITE}*:: {
  258. yyless (yyleng - 2);
  259. yylval.sval = processId (yytext, yyleng);
  260. return NAME;
  261. }
  262. "::" { return COLONCOLON; }
  263. /* REGISTERS AND GDB CONVENIENCE VARIABLES */
  264. "$"({LETTER}|{DIG}|"$")* {
  265. yylval.sval.ptr = yytext;
  266. yylval.sval.length = yyleng;
  267. return DOLLAR_VARIABLE;
  268. }
  269. /* CATCH-ALL ERROR CASE */
  270. . { error (_("Invalid character '%s' in expression."), yytext); }
  271. %%
  272. #include <ctype.h>
  273. /* Initialize the lexer for processing new expression. */
  274. static void
  275. lexer_init (FILE *inp)
  276. {
  277. BEGIN INITIAL;
  278. paren_depth = 0;
  279. returned_complete = false;
  280. yyrestart (inp);
  281. }
  282. /* Copy S2 to S1, removing all underscores, and downcasing all letters. */
  283. static void
  284. canonicalizeNumeral (char *s1, const char *s2)
  285. {
  286. for (; *s2 != '\000'; s2 += 1)
  287. {
  288. if (*s2 != '_')
  289. {
  290. *s1 = tolower(*s2);
  291. s1 += 1;
  292. }
  293. }
  294. s1[0] = '\000';
  295. }
  296. /* Interprets the prefix of NUM that consists of digits of the given BASE
  297. as an integer of that BASE, with the string EXP as an exponent.
  298. Puts value in yylval, and returns INT, if the string is valid. Causes
  299. an error if the number is improperly formated. BASE, if NULL, defaults
  300. to "10", and EXP to "1". The EXP does not contain a leading 'e' or 'E'.
  301. */
  302. static int
  303. processInt (struct parser_state *par_state, const char *base0,
  304. const char *num0, const char *exp0)
  305. {
  306. long exp;
  307. int base;
  308. /* For the based literal with an "f" prefix, we'll return a
  309. floating-point number. This counts the the number of "l"s seen,
  310. to decide the width of the floating-point number to return. -1
  311. means no "f". */
  312. int floating_point_l_count = -1;
  313. if (base0 == NULL)
  314. base = 10;
  315. else
  316. {
  317. char *end_of_base;
  318. base = strtol (base0, &end_of_base, 10);
  319. if (base < 2 || base > 16)
  320. error (_("Invalid base: %d."), base);
  321. while (*end_of_base == 'l')
  322. {
  323. ++floating_point_l_count;
  324. ++end_of_base;
  325. }
  326. /* This assertion is ensured by the pattern. */
  327. gdb_assert (floating_point_l_count == -1 || *end_of_base == 'f');
  328. if (*end_of_base == 'f')
  329. {
  330. ++end_of_base;
  331. ++floating_point_l_count;
  332. }
  333. /* This assertion is ensured by the pattern. */
  334. gdb_assert (*end_of_base == '#');
  335. }
  336. if (exp0 == NULL)
  337. exp = 0;
  338. else
  339. exp = strtol(exp0, (char **) NULL, 10);
  340. gdb_mpz result;
  341. while (isxdigit (*num0))
  342. {
  343. int dig = fromhex (*num0);
  344. if (dig >= base)
  345. error (_("Invalid digit `%c' in based literal"), *num0);
  346. mpz_mul_ui (result.val, result.val, base);
  347. mpz_add_ui (result.val, result.val, dig);
  348. ++num0;
  349. }
  350. while (exp > 0)
  351. {
  352. mpz_mul_ui (result.val, result.val, base);
  353. exp -= 1;
  354. }
  355. if (floating_point_l_count > -1)
  356. {
  357. struct type *fp_type;
  358. if (floating_point_l_count == 0)
  359. fp_type = language_lookup_primitive_type (par_state->language (),
  360. par_state->gdbarch (),
  361. "float");
  362. else if (floating_point_l_count == 1)
  363. fp_type = language_lookup_primitive_type (par_state->language (),
  364. par_state->gdbarch (),
  365. "long_float");
  366. else
  367. {
  368. /* This assertion is ensured by the pattern. */
  369. gdb_assert (floating_point_l_count == 2);
  370. fp_type = language_lookup_primitive_type (par_state->language (),
  371. par_state->gdbarch (),
  372. "long_long_float");
  373. }
  374. yylval.typed_val_float.type = fp_type;
  375. result.write (gdb::make_array_view (yylval.typed_val_float.val,
  376. TYPE_LENGTH (fp_type)),
  377. type_byte_order (fp_type),
  378. true);
  379. return FLOAT;
  380. }
  381. gdb_mpz maxval (ULONGEST_MAX / base);
  382. if (mpz_cmp (result.val, maxval.val) > 0)
  383. error (_("Integer literal out of range"));
  384. LONGEST value = result.as_integer<LONGEST> ();
  385. if ((value >> (gdbarch_int_bit (par_state->gdbarch ())-1)) == 0)
  386. yylval.typed_val.type = type_int (par_state);
  387. else if ((value >> (gdbarch_long_bit (par_state->gdbarch ())-1)) == 0)
  388. yylval.typed_val.type = type_long (par_state);
  389. else if (((value >> (gdbarch_long_bit (par_state->gdbarch ())-1)) >> 1) == 0)
  390. {
  391. /* We have a number representable as an unsigned integer quantity.
  392. For consistency with the C treatment, we will treat it as an
  393. anonymous modular (unsigned) quantity. Alas, the types are such
  394. that we need to store .val as a signed quantity. Sorry
  395. for the mess, but C doesn't officially guarantee that a simple
  396. assignment does the trick (no, it doesn't; read the reference manual).
  397. */
  398. yylval.typed_val.type
  399. = builtin_type (par_state->gdbarch ())->builtin_unsigned_long;
  400. if (value & LONGEST_SIGN)
  401. yylval.typed_val.val =
  402. (LONGEST) (value & ~LONGEST_SIGN)
  403. - (LONGEST_SIGN>>1) - (LONGEST_SIGN>>1);
  404. else
  405. yylval.typed_val.val = (LONGEST) value;
  406. return INT;
  407. }
  408. else
  409. yylval.typed_val.type = type_long_long (par_state);
  410. yylval.typed_val.val = value;
  411. return INT;
  412. }
  413. static int
  414. processReal (struct parser_state *par_state, const char *num0)
  415. {
  416. yylval.typed_val_float.type = type_long_double (par_state);
  417. bool parsed = parse_float (num0, strlen (num0),
  418. yylval.typed_val_float.type,
  419. yylval.typed_val_float.val);
  420. gdb_assert (parsed);
  421. return FLOAT;
  422. }
  423. /* Store a canonicalized version of NAME0[0..LEN-1] in yylval.ssym. The
  424. resulting string is valid until the next call to ada_parse. If
  425. NAME0 contains the substring "___", it is assumed to be already
  426. encoded and the resulting name is equal to it. Similarly, if the name
  427. starts with '<', it is copied verbatim. Otherwise, it differs
  428. from NAME0 in that:
  429. + Characters between '...' are transfered verbatim to yylval.ssym.
  430. + Trailing "'" characters in quoted sequences are removed (a leading quote is
  431. preserved to indicate that the name is not to be GNAT-encoded).
  432. + Unquoted whitespace is removed.
  433. + Unquoted alphabetic characters are mapped to lower case.
  434. Result is returned as a struct stoken, but for convenience, the string
  435. is also null-terminated. Result string valid until the next call of
  436. ada_parse.
  437. */
  438. static struct stoken
  439. processId (const char *name0, int len)
  440. {
  441. char *name = (char *) obstack_alloc (&temp_parse_space, len + 11);
  442. int i0, i;
  443. struct stoken result;
  444. result.ptr = name;
  445. while (len > 0 && isspace (name0[len-1]))
  446. len -= 1;
  447. if (name0[0] == '<' || strstr (name0, "___") != NULL)
  448. {
  449. strncpy (name, name0, len);
  450. name[len] = '\000';
  451. result.length = len;
  452. return result;
  453. }
  454. bool in_quotes = false;
  455. i = i0 = 0;
  456. while (i0 < len)
  457. {
  458. if (name0[i0] == COMPLETE_CHAR)
  459. {
  460. /* Just ignore. */
  461. ++i0;
  462. }
  463. else if (in_quotes)
  464. name[i++] = name0[i0++];
  465. else if (isalnum (name0[i0]))
  466. {
  467. name[i] = tolower (name0[i0]);
  468. i += 1; i0 += 1;
  469. }
  470. else if (isspace (name0[i0]))
  471. i0 += 1;
  472. else if (name0[i0] == '\'')
  473. {
  474. /* Copy the starting quote, but not the ending quote. */
  475. if (!in_quotes)
  476. name[i++] = name0[i0++];
  477. in_quotes = !in_quotes;
  478. }
  479. else
  480. name[i++] = name0[i0++];
  481. }
  482. name[i] = '\000';
  483. result.length = i;
  484. return result;
  485. }
  486. /* Return TEXT[0..LEN-1], a string literal without surrounding quotes,
  487. with special hex character notations replaced with characters.
  488. Result valid until the next call to ada_parse. */
  489. static struct stoken
  490. processString (const char *text, int len)
  491. {
  492. const char *p;
  493. char *q;
  494. const char *lim = text + len;
  495. struct stoken result;
  496. q = (char *) obstack_alloc (&temp_parse_space, len);
  497. result.ptr = q;
  498. p = text;
  499. while (p < lim)
  500. {
  501. if (p[0] == '[' && p[1] == '"' && p+2 < lim)
  502. {
  503. if (p[2] == '"') /* "...["""]... */
  504. {
  505. *q = '"';
  506. p += 4;
  507. }
  508. else
  509. {
  510. const char *end;
  511. ULONGEST chr = strtoulst (p + 2, &end, 16);
  512. if (chr > 0xff)
  513. error (_("wide strings are not yet supported"));
  514. *q = (char) chr;
  515. p = end + 1;
  516. }
  517. }
  518. else
  519. *q = *p;
  520. q += 1;
  521. p += 1;
  522. }
  523. result.length = q - result.ptr;
  524. return result;
  525. }
  526. /* Returns the position within STR of the '.' in a
  527. '.{WHITE}*all' component of a dotted name, or -1 if there is none.
  528. Note: we actually don't need this routine, since 'all' can never be an
  529. Ada identifier. Thus, looking up foo.all or foo.all.x as a name
  530. must fail, and will eventually be interpreted as (foo).all or
  531. (foo).all.x. However, this does avoid an extraneous lookup. */
  532. static int
  533. find_dot_all (const char *str)
  534. {
  535. int i;
  536. for (i = 0; str[i] != '\000'; i++)
  537. if (str[i] == '.')
  538. {
  539. int i0 = i;
  540. do
  541. i += 1;
  542. while (isspace (str[i]));
  543. if (strncasecmp (str + i, "all", 3) == 0
  544. && !isalnum (str[i + 3]) && str[i + 3] != '_')
  545. return i0;
  546. }
  547. return -1;
  548. }
  549. /* Returns non-zero iff string SUBSEQ matches a subsequence of STR, ignoring
  550. case. */
  551. static int
  552. subseqMatch (const char *subseq, const char *str)
  553. {
  554. if (subseq[0] == '\0')
  555. return 1;
  556. else if (str[0] == '\0')
  557. return 0;
  558. else if (tolower (subseq[0]) == tolower (str[0]))
  559. return subseqMatch (subseq+1, str+1) || subseqMatch (subseq, str+1);
  560. else
  561. return subseqMatch (subseq, str+1);
  562. }
  563. static struct { const char *name; int code; }
  564. attributes[] = {
  565. { "address", TICK_ADDRESS },
  566. { "unchecked_access", TICK_ACCESS },
  567. { "unrestricted_access", TICK_ACCESS },
  568. { "access", TICK_ACCESS },
  569. { "first", TICK_FIRST },
  570. { "last", TICK_LAST },
  571. { "length", TICK_LENGTH },
  572. { "max", TICK_MAX },
  573. { "min", TICK_MIN },
  574. { "modulus", TICK_MODULUS },
  575. { "pos", TICK_POS },
  576. { "range", TICK_RANGE },
  577. { "size", TICK_SIZE },
  578. { "tag", TICK_TAG },
  579. { "val", TICK_VAL },
  580. };
  581. /* Return the syntactic code corresponding to the attribute name or
  582. abbreviation STR. */
  583. static int
  584. processAttribute (const char *str)
  585. {
  586. gdb_assert (*str == '\'');
  587. ++str;
  588. while (isspace (*str))
  589. ++str;
  590. int len = strlen (str);
  591. if (len > 0 && str[len - 1] == COMPLETE_CHAR)
  592. {
  593. /* This is enforced by YY_INPUT. */
  594. gdb_assert (pstate->parse_completion);
  595. yylval.sval.ptr = obstack_strndup (&temp_parse_space, str, len - 1);
  596. yylval.sval.length = len - 1;
  597. return TICK_COMPLETE;
  598. }
  599. for (const auto &item : attributes)
  600. if (strcasecmp (str, item.name) == 0)
  601. return item.code;
  602. gdb::optional<int> found;
  603. for (const auto &item : attributes)
  604. if (subseqMatch (str, item.name))
  605. {
  606. if (!found.has_value ())
  607. found = item.code;
  608. else
  609. error (_("ambiguous attribute name: `%s'"), str);
  610. }
  611. if (!found.has_value ())
  612. error (_("unrecognized attribute: `%s'"), str);
  613. return *found;
  614. }
  615. bool
  616. ada_tick_completer::complete (struct expression *exp,
  617. completion_tracker &tracker)
  618. {
  619. completion_list output;
  620. for (const auto &item : attributes)
  621. {
  622. if (strncasecmp (item.name, m_name.c_str (), m_name.length ()) == 0)
  623. output.emplace_back (xstrdup (item.name));
  624. }
  625. tracker.add_completions (std::move (output));
  626. return true;
  627. }
  628. /* Back up lexptr by yyleng and then to the rightmost occurrence of
  629. character CH, case-folded (there must be one). WARNING: since
  630. lexptr points to the next input character that Flex has not yet
  631. transferred to its internal buffer, the use of this function
  632. depends on the assumption that Flex calls YY_INPUT only when it is
  633. logically necessary to do so (thus, there is no reading ahead
  634. farther than needed to identify the next token.) */
  635. static void
  636. rewind_to_char (int ch)
  637. {
  638. pstate->lexptr -= yyleng;
  639. while (toupper (*pstate->lexptr) != toupper (ch))
  640. pstate->lexptr -= 1;
  641. yyrestart (NULL);
  642. }
  643. /* Dummy definition to suppress warnings about unused static definitions. */
  644. typedef void (*dummy_function) ();
  645. dummy_function ada_flex_use[] =
  646. {
  647. (dummy_function) yyunput
  648. };
  649. DIAGNOSTIC_POP