m2-exp.y 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /* YACC grammar for Modula-2 expressions, for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. Generated from expread.y (now c-exp.y) and contributed by the Department
  4. of Computer Science at the State University of New York at Buffalo, 1991.
  5. 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.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* Parse a Modula-2 expression from text in a string,
  17. and return the result as a struct expression pointer.
  18. That structure contains arithmetic operations in reverse polish,
  19. with constants represented by operations that are followed by special data.
  20. See expression.h for the details of the format.
  21. What is important here is that it can be built up sequentially
  22. during the process of parsing; the lower levels of the tree always
  23. come first in the result.
  24. Note that malloc's and realloc's in this file are transformed to
  25. xmalloc and xrealloc respectively by the same sed command in the
  26. makefile that remaps any other malloc/realloc inserted by the parser
  27. generator. Doing this with #defines and trying to control the interaction
  28. with include files (<malloc.h> and <stdlib.h> for example) just became
  29. too messy, particularly when such includes can be inserted at random
  30. times by the parser generator. */
  31. %{
  32. #include "defs.h"
  33. #include "expression.h"
  34. #include "language.h"
  35. #include "value.h"
  36. #include "parser-defs.h"
  37. #include "m2-lang.h"
  38. #include "bfd.h" /* Required by objfiles.h. */
  39. #include "symfile.h" /* Required by objfiles.h. */
  40. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  41. #include "block.h"
  42. #include "m2-exp.h"
  43. #define parse_type(ps) builtin_type (ps->gdbarch ())
  44. #define parse_m2_type(ps) builtin_m2_type (ps->gdbarch ())
  45. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
  46. etc). */
  47. #define GDB_YY_REMAP_PREFIX m2_
  48. #include "yy-remap.h"
  49. /* The state of the parser, used internally when we are parsing the
  50. expression. */
  51. static struct parser_state *pstate = NULL;
  52. int yyparse (void);
  53. static int yylex (void);
  54. static void yyerror (const char *);
  55. static int parse_number (int);
  56. /* The sign of the number being parsed. */
  57. static int number_sign = 1;
  58. using namespace expr;
  59. %}
  60. /* Although the yacc "value" of an expression is not used,
  61. since the result is stored in the structure being created,
  62. other node types do have values. */
  63. %union
  64. {
  65. LONGEST lval;
  66. ULONGEST ulval;
  67. gdb_byte val[16];
  68. struct symbol *sym;
  69. struct type *tval;
  70. struct stoken sval;
  71. int voidval;
  72. const struct block *bval;
  73. enum exp_opcode opcode;
  74. struct internalvar *ivar;
  75. struct type **tvec;
  76. int *ivec;
  77. }
  78. %type <voidval> exp type_exp start set
  79. %type <voidval> variable
  80. %type <tval> type
  81. %type <bval> block
  82. %type <sym> fblock
  83. %token <lval> INT HEX ERROR
  84. %token <ulval> UINT M2_TRUE M2_FALSE CHAR
  85. %token <val> FLOAT
  86. /* Both NAME and TYPENAME tokens represent symbols in the input,
  87. and both convey their data as strings.
  88. But a TYPENAME is a string that happens to be defined as a typedef
  89. or builtin type name (such as int or char)
  90. and a NAME is any other symbol.
  91. Contexts where this distinction is not important can use the
  92. nonterminal "name", which matches either NAME or TYPENAME. */
  93. %token <sval> STRING
  94. %token <sval> NAME BLOCKNAME IDENT VARNAME
  95. %token <sval> TYPENAME
  96. %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
  97. %token TSIZE
  98. %token INC DEC INCL EXCL
  99. /* The GDB scope operator */
  100. %token COLONCOLON
  101. %token <sval> DOLLAR_VARIABLE
  102. /* M2 tokens */
  103. %left ','
  104. %left ABOVE_COMMA
  105. %nonassoc ASSIGN
  106. %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
  107. %left OROR
  108. %left LOGICAL_AND '&'
  109. %left '@'
  110. %left '+' '-'
  111. %left '*' '/' DIV MOD
  112. %right UNARY
  113. %right '^' DOT '[' '('
  114. %right NOT '~'
  115. %left COLONCOLON QID
  116. /* This is not an actual token ; it is used for precedence.
  117. %right QID
  118. */
  119. %%
  120. start : exp
  121. | type_exp
  122. ;
  123. type_exp: type
  124. { pstate->push_new<type_operation> ($1); }
  125. ;
  126. /* Expressions */
  127. exp : exp '^' %prec UNARY
  128. { pstate->wrap<unop_ind_operation> (); }
  129. ;
  130. exp : '-'
  131. { number_sign = -1; }
  132. exp %prec UNARY
  133. { number_sign = 1;
  134. pstate->wrap<unary_neg_operation> (); }
  135. ;
  136. exp : '+' exp %prec UNARY
  137. { pstate->wrap<unary_plus_operation> (); }
  138. ;
  139. exp : not_exp exp %prec UNARY
  140. { pstate->wrap<unary_logical_not_operation> (); }
  141. ;
  142. not_exp : NOT
  143. | '~'
  144. ;
  145. exp : CAP '(' exp ')'
  146. { error (_("CAP function is not implemented")); }
  147. ;
  148. exp : ORD '(' exp ')'
  149. { error (_("ORD function is not implemented")); }
  150. ;
  151. exp : ABS '(' exp ')'
  152. { error (_("ABS function is not implemented")); }
  153. ;
  154. exp : HIGH '(' exp ')'
  155. { pstate->wrap<m2_unop_high_operation> (); }
  156. ;
  157. exp : MIN_FUNC '(' type ')'
  158. { error (_("MIN function is not implemented")); }
  159. ;
  160. exp : MAX_FUNC '(' type ')'
  161. { error (_("MAX function is not implemented")); }
  162. ;
  163. exp : FLOAT_FUNC '(' exp ')'
  164. { error (_("FLOAT function is not implemented")); }
  165. ;
  166. exp : VAL '(' type ',' exp ')'
  167. { error (_("VAL function is not implemented")); }
  168. ;
  169. exp : CHR '(' exp ')'
  170. { error (_("CHR function is not implemented")); }
  171. ;
  172. exp : ODD '(' exp ')'
  173. { error (_("ODD function is not implemented")); }
  174. ;
  175. exp : TRUNC '(' exp ')'
  176. { error (_("TRUNC function is not implemented")); }
  177. ;
  178. exp : TSIZE '(' exp ')'
  179. { pstate->wrap<unop_sizeof_operation> (); }
  180. ;
  181. exp : SIZE exp %prec UNARY
  182. { pstate->wrap<unop_sizeof_operation> (); }
  183. ;
  184. exp : INC '(' exp ')'
  185. { pstate->wrap<preinc_operation> (); }
  186. ;
  187. exp : INC '(' exp ',' exp ')'
  188. {
  189. operation_up rhs = pstate->pop ();
  190. operation_up lhs = pstate->pop ();
  191. pstate->push_new<assign_modify_operation>
  192. (BINOP_ADD, std::move (lhs), std::move (rhs));
  193. }
  194. ;
  195. exp : DEC '(' exp ')'
  196. { pstate->wrap<predec_operation> (); }
  197. ;
  198. exp : DEC '(' exp ',' exp ')'
  199. {
  200. operation_up rhs = pstate->pop ();
  201. operation_up lhs = pstate->pop ();
  202. pstate->push_new<assign_modify_operation>
  203. (BINOP_SUB, std::move (lhs), std::move (rhs));
  204. }
  205. ;
  206. exp : exp DOT NAME
  207. {
  208. pstate->push_new<structop_operation>
  209. (pstate->pop (), copy_name ($3));
  210. }
  211. ;
  212. exp : set
  213. ;
  214. exp : exp IN set
  215. { error (_("Sets are not implemented."));}
  216. ;
  217. exp : INCL '(' exp ',' exp ')'
  218. { error (_("Sets are not implemented."));}
  219. ;
  220. exp : EXCL '(' exp ',' exp ')'
  221. { error (_("Sets are not implemented."));}
  222. ;
  223. set : '{' arglist '}'
  224. { error (_("Sets are not implemented."));}
  225. | type '{' arglist '}'
  226. { error (_("Sets are not implemented."));}
  227. ;
  228. /* Modula-2 array subscript notation [a,b,c...]. */
  229. exp : exp '['
  230. /* This function just saves the number of arguments
  231. that follow in the list. It is *not* specific to
  232. function types */
  233. { pstate->start_arglist(); }
  234. non_empty_arglist ']' %prec DOT
  235. {
  236. gdb_assert (pstate->arglist_len > 0);
  237. std::vector<operation_up> args
  238. = pstate->pop_vector (pstate->end_arglist ());
  239. pstate->push_new<multi_subscript_operation>
  240. (pstate->pop (), std::move (args));
  241. }
  242. ;
  243. exp : exp '('
  244. /* This is to save the value of arglist_len
  245. being accumulated by an outer function call. */
  246. { pstate->start_arglist (); }
  247. arglist ')' %prec DOT
  248. {
  249. std::vector<operation_up> args
  250. = pstate->pop_vector (pstate->end_arglist ());
  251. pstate->push_new<funcall_operation>
  252. (pstate->pop (), std::move (args));
  253. }
  254. ;
  255. arglist :
  256. ;
  257. arglist : exp
  258. { pstate->arglist_len = 1; }
  259. ;
  260. arglist : arglist ',' exp %prec ABOVE_COMMA
  261. { pstate->arglist_len++; }
  262. ;
  263. non_empty_arglist
  264. : exp
  265. { pstate->arglist_len = 1; }
  266. ;
  267. non_empty_arglist
  268. : non_empty_arglist ',' exp %prec ABOVE_COMMA
  269. { pstate->arglist_len++; }
  270. ;
  271. /* GDB construct */
  272. exp : '{' type '}' exp %prec UNARY
  273. {
  274. pstate->push_new<unop_memval_operation>
  275. (pstate->pop (), $2);
  276. }
  277. ;
  278. exp : type '(' exp ')' %prec UNARY
  279. {
  280. pstate->push_new<unop_cast_operation>
  281. (pstate->pop (), $1);
  282. }
  283. ;
  284. exp : '(' exp ')'
  285. { }
  286. ;
  287. /* Binary operators in order of decreasing precedence. Note that some
  288. of these operators are overloaded! (ie. sets) */
  289. /* GDB construct */
  290. exp : exp '@' exp
  291. { pstate->wrap2<repeat_operation> (); }
  292. ;
  293. exp : exp '*' exp
  294. { pstate->wrap2<mul_operation> (); }
  295. ;
  296. exp : exp '/' exp
  297. { pstate->wrap2<div_operation> (); }
  298. ;
  299. exp : exp DIV exp
  300. { pstate->wrap2<intdiv_operation> (); }
  301. ;
  302. exp : exp MOD exp
  303. { pstate->wrap2<rem_operation> (); }
  304. ;
  305. exp : exp '+' exp
  306. { pstate->wrap2<add_operation> (); }
  307. ;
  308. exp : exp '-' exp
  309. { pstate->wrap2<sub_operation> (); }
  310. ;
  311. exp : exp '=' exp
  312. { pstate->wrap2<equal_operation> (); }
  313. ;
  314. exp : exp NOTEQUAL exp
  315. { pstate->wrap2<notequal_operation> (); }
  316. | exp '#' exp
  317. { pstate->wrap2<notequal_operation> (); }
  318. ;
  319. exp : exp LEQ exp
  320. { pstate->wrap2<leq_operation> (); }
  321. ;
  322. exp : exp GEQ exp
  323. { pstate->wrap2<geq_operation> (); }
  324. ;
  325. exp : exp '<' exp
  326. { pstate->wrap2<less_operation> (); }
  327. ;
  328. exp : exp '>' exp
  329. { pstate->wrap2<gtr_operation> (); }
  330. ;
  331. exp : exp LOGICAL_AND exp
  332. { pstate->wrap2<logical_and_operation> (); }
  333. ;
  334. exp : exp OROR exp
  335. { pstate->wrap2<logical_or_operation> (); }
  336. ;
  337. exp : exp ASSIGN exp
  338. { pstate->wrap2<assign_operation> (); }
  339. ;
  340. /* Constants */
  341. exp : M2_TRUE
  342. { pstate->push_new<bool_operation> ($1); }
  343. ;
  344. exp : M2_FALSE
  345. { pstate->push_new<bool_operation> ($1); }
  346. ;
  347. exp : INT
  348. {
  349. pstate->push_new<long_const_operation>
  350. (parse_m2_type (pstate)->builtin_int, $1);
  351. }
  352. ;
  353. exp : UINT
  354. {
  355. pstate->push_new<long_const_operation>
  356. (parse_m2_type (pstate)->builtin_card, $1);
  357. }
  358. ;
  359. exp : CHAR
  360. {
  361. pstate->push_new<long_const_operation>
  362. (parse_m2_type (pstate)->builtin_char, $1);
  363. }
  364. ;
  365. exp : FLOAT
  366. {
  367. float_data data;
  368. std::copy (std::begin ($1), std::end ($1),
  369. std::begin (data));
  370. pstate->push_new<float_const_operation>
  371. (parse_m2_type (pstate)->builtin_real, data);
  372. }
  373. ;
  374. exp : variable
  375. ;
  376. exp : SIZE '(' type ')' %prec UNARY
  377. {
  378. pstate->push_new<long_const_operation>
  379. (parse_m2_type (pstate)->builtin_int,
  380. TYPE_LENGTH ($3));
  381. }
  382. ;
  383. exp : STRING
  384. { error (_("strings are not implemented")); }
  385. ;
  386. /* This will be used for extensions later. Like adding modules. */
  387. block : fblock
  388. { $$ = SYMBOL_BLOCK_VALUE($1); }
  389. ;
  390. fblock : BLOCKNAME
  391. { struct symbol *sym
  392. = lookup_symbol (copy_name ($1).c_str (),
  393. pstate->expression_context_block,
  394. VAR_DOMAIN, 0).symbol;
  395. $$ = sym;}
  396. ;
  397. /* GDB scope operator */
  398. fblock : block COLONCOLON BLOCKNAME
  399. { struct symbol *tem
  400. = lookup_symbol (copy_name ($3).c_str (), $1,
  401. VAR_DOMAIN, 0).symbol;
  402. if (!tem || tem->aclass () != LOC_BLOCK)
  403. error (_("No function \"%s\" in specified context."),
  404. copy_name ($3).c_str ());
  405. $$ = tem;
  406. }
  407. ;
  408. /* Useful for assigning to PROCEDURE variables */
  409. variable: fblock
  410. {
  411. block_symbol sym { $1, nullptr };
  412. pstate->push_new<var_value_operation> (sym);
  413. }
  414. ;
  415. /* GDB internal ($foo) variable */
  416. variable: DOLLAR_VARIABLE
  417. { pstate->push_dollar ($1); }
  418. ;
  419. /* GDB scope operator */
  420. variable: block COLONCOLON NAME
  421. { struct block_symbol sym
  422. = lookup_symbol (copy_name ($3).c_str (), $1,
  423. VAR_DOMAIN, 0);
  424. if (sym.symbol == 0)
  425. error (_("No symbol \"%s\" in specified context."),
  426. copy_name ($3).c_str ());
  427. if (symbol_read_needs_frame (sym.symbol))
  428. pstate->block_tracker->update (sym);
  429. pstate->push_new<var_value_operation> (sym);
  430. }
  431. ;
  432. /* Base case for variables. */
  433. variable: NAME
  434. { struct block_symbol sym;
  435. struct field_of_this_result is_a_field_of_this;
  436. std::string name = copy_name ($1);
  437. sym
  438. = lookup_symbol (name.c_str (),
  439. pstate->expression_context_block,
  440. VAR_DOMAIN,
  441. &is_a_field_of_this);
  442. pstate->push_symbol (name.c_str (), sym);
  443. }
  444. ;
  445. type
  446. : TYPENAME
  447. { $$
  448. = lookup_typename (pstate->language (),
  449. copy_name ($1).c_str (),
  450. pstate->expression_context_block,
  451. 0);
  452. }
  453. ;
  454. %%
  455. /* Take care of parsing a number (anything that starts with a digit).
  456. Set yylval and return the token type; update lexptr.
  457. LEN is the number of characters in it. */
  458. /*** Needs some error checking for the float case ***/
  459. static int
  460. parse_number (int olen)
  461. {
  462. const char *p = pstate->lexptr;
  463. LONGEST n = 0;
  464. LONGEST prevn = 0;
  465. int c,i,ischar=0;
  466. int base = input_radix;
  467. int len = olen;
  468. int unsigned_p = number_sign == 1 ? 1 : 0;
  469. if(p[len-1] == 'H')
  470. {
  471. base = 16;
  472. len--;
  473. }
  474. else if(p[len-1] == 'C' || p[len-1] == 'B')
  475. {
  476. base = 8;
  477. ischar = p[len-1] == 'C';
  478. len--;
  479. }
  480. /* Scan the number */
  481. for (c = 0; c < len; c++)
  482. {
  483. if (p[c] == '.' && base == 10)
  484. {
  485. /* It's a float since it contains a point. */
  486. if (!parse_float (p, len,
  487. parse_m2_type (pstate)->builtin_real,
  488. yylval.val))
  489. return ERROR;
  490. pstate->lexptr += len;
  491. return FLOAT;
  492. }
  493. if (p[c] == '.' && base != 10)
  494. error (_("Floating point numbers must be base 10."));
  495. if (base == 10 && (p[c] < '0' || p[c] > '9'))
  496. error (_("Invalid digit \'%c\' in number."),p[c]);
  497. }
  498. while (len-- > 0)
  499. {
  500. c = *p++;
  501. n *= base;
  502. if( base == 8 && (c == '8' || c == '9'))
  503. error (_("Invalid digit \'%c\' in octal number."),c);
  504. if (c >= '0' && c <= '9')
  505. i = c - '0';
  506. else
  507. {
  508. if (base == 16 && c >= 'A' && c <= 'F')
  509. i = c - 'A' + 10;
  510. else
  511. return ERROR;
  512. }
  513. n+=i;
  514. if(i >= base)
  515. return ERROR;
  516. if(!unsigned_p && number_sign == 1 && (prevn >= n))
  517. unsigned_p=1; /* Try something unsigned */
  518. /* Don't do the range check if n==i and i==0, since that special
  519. case will give an overflow error. */
  520. if(RANGE_CHECK && n!=i && i)
  521. {
  522. if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
  523. ((!unsigned_p && number_sign==-1) && -prevn <= -n))
  524. range_error (_("Overflow on numeric constant."));
  525. }
  526. prevn=n;
  527. }
  528. pstate->lexptr = p;
  529. if(*p == 'B' || *p == 'C' || *p == 'H')
  530. pstate->lexptr++; /* Advance past B,C or H */
  531. if (ischar)
  532. {
  533. yylval.ulval = n;
  534. return CHAR;
  535. }
  536. else if ( unsigned_p && number_sign == 1)
  537. {
  538. yylval.ulval = n;
  539. return UINT;
  540. }
  541. else if((unsigned_p && (n<0))) {
  542. range_error (_("Overflow on numeric constant -- number too large."));
  543. /* But, this can return if range_check == range_warn. */
  544. }
  545. yylval.lval = n;
  546. return INT;
  547. }
  548. /* Some tokens */
  549. static struct
  550. {
  551. char name[2];
  552. int token;
  553. } tokentab2[] =
  554. {
  555. { {'<', '>'}, NOTEQUAL },
  556. { {':', '='}, ASSIGN },
  557. { {'<', '='}, LEQ },
  558. { {'>', '='}, GEQ },
  559. { {':', ':'}, COLONCOLON },
  560. };
  561. /* Some specific keywords */
  562. struct keyword {
  563. char keyw[10];
  564. int token;
  565. };
  566. static struct keyword keytab[] =
  567. {
  568. {"OR" , OROR },
  569. {"IN", IN },/* Note space after IN */
  570. {"AND", LOGICAL_AND},
  571. {"ABS", ABS },
  572. {"CHR", CHR },
  573. {"DEC", DEC },
  574. {"NOT", NOT },
  575. {"DIV", DIV },
  576. {"INC", INC },
  577. {"MAX", MAX_FUNC },
  578. {"MIN", MIN_FUNC },
  579. {"MOD", MOD },
  580. {"ODD", ODD },
  581. {"CAP", CAP },
  582. {"ORD", ORD },
  583. {"VAL", VAL },
  584. {"EXCL", EXCL },
  585. {"HIGH", HIGH },
  586. {"INCL", INCL },
  587. {"SIZE", SIZE },
  588. {"FLOAT", FLOAT_FUNC },
  589. {"TRUNC", TRUNC },
  590. {"TSIZE", SIZE },
  591. };
  592. /* Depth of parentheses. */
  593. static int paren_depth;
  594. /* Read one token, getting characters through lexptr. */
  595. /* This is where we will check to make sure that the language and the
  596. operators used are compatible */
  597. static int
  598. yylex (void)
  599. {
  600. int c;
  601. int namelen;
  602. int i;
  603. const char *tokstart;
  604. char quote;
  605. retry:
  606. pstate->prev_lexptr = pstate->lexptr;
  607. tokstart = pstate->lexptr;
  608. /* See if it is a special token of length 2 */
  609. for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
  610. if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
  611. {
  612. pstate->lexptr += 2;
  613. return tokentab2[i].token;
  614. }
  615. switch (c = *tokstart)
  616. {
  617. case 0:
  618. return 0;
  619. case ' ':
  620. case '\t':
  621. case '\n':
  622. pstate->lexptr++;
  623. goto retry;
  624. case '(':
  625. paren_depth++;
  626. pstate->lexptr++;
  627. return c;
  628. case ')':
  629. if (paren_depth == 0)
  630. return 0;
  631. paren_depth--;
  632. pstate->lexptr++;
  633. return c;
  634. case ',':
  635. if (pstate->comma_terminates && paren_depth == 0)
  636. return 0;
  637. pstate->lexptr++;
  638. return c;
  639. case '.':
  640. /* Might be a floating point number. */
  641. if (pstate->lexptr[1] >= '0' && pstate->lexptr[1] <= '9')
  642. break; /* Falls into number code. */
  643. else
  644. {
  645. pstate->lexptr++;
  646. return DOT;
  647. }
  648. /* These are character tokens that appear as-is in the YACC grammar */
  649. case '+':
  650. case '-':
  651. case '*':
  652. case '/':
  653. case '^':
  654. case '<':
  655. case '>':
  656. case '[':
  657. case ']':
  658. case '=':
  659. case '{':
  660. case '}':
  661. case '#':
  662. case '@':
  663. case '~':
  664. case '&':
  665. pstate->lexptr++;
  666. return c;
  667. case '\'' :
  668. case '"':
  669. quote = c;
  670. for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
  671. if (c == '\\')
  672. {
  673. c = tokstart[++namelen];
  674. if (c >= '0' && c <= '9')
  675. {
  676. c = tokstart[++namelen];
  677. if (c >= '0' && c <= '9')
  678. c = tokstart[++namelen];
  679. }
  680. }
  681. if(c != quote)
  682. error (_("Unterminated string or character constant."));
  683. yylval.sval.ptr = tokstart + 1;
  684. yylval.sval.length = namelen - 1;
  685. pstate->lexptr += namelen + 1;
  686. if(namelen == 2) /* Single character */
  687. {
  688. yylval.ulval = tokstart[1];
  689. return CHAR;
  690. }
  691. else
  692. return STRING;
  693. }
  694. /* Is it a number? */
  695. /* Note: We have already dealt with the case of the token '.'.
  696. See case '.' above. */
  697. if ((c >= '0' && c <= '9'))
  698. {
  699. /* It's a number. */
  700. int got_dot = 0, got_e = 0;
  701. const char *p = tokstart;
  702. int toktype;
  703. for (++p ;; ++p)
  704. {
  705. if (!got_e && (*p == 'e' || *p == 'E'))
  706. got_dot = got_e = 1;
  707. else if (!got_dot && *p == '.')
  708. got_dot = 1;
  709. else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  710. && (*p == '-' || *p == '+'))
  711. /* This is the sign of the exponent, not the end of the
  712. number. */
  713. continue;
  714. else if ((*p < '0' || *p > '9') &&
  715. (*p < 'A' || *p > 'F') &&
  716. (*p != 'H')) /* Modula-2 hexadecimal number */
  717. break;
  718. }
  719. toktype = parse_number (p - tokstart);
  720. if (toktype == ERROR)
  721. {
  722. char *err_copy = (char *) alloca (p - tokstart + 1);
  723. memcpy (err_copy, tokstart, p - tokstart);
  724. err_copy[p - tokstart] = 0;
  725. error (_("Invalid number \"%s\"."), err_copy);
  726. }
  727. pstate->lexptr = p;
  728. return toktype;
  729. }
  730. if (!(c == '_' || c == '$'
  731. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  732. /* We must have come across a bad character (e.g. ';'). */
  733. error (_("Invalid character '%c' in expression."), c);
  734. /* It's a name. See how long it is. */
  735. namelen = 0;
  736. for (c = tokstart[namelen];
  737. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  738. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  739. c = tokstart[++namelen])
  740. ;
  741. /* The token "if" terminates the expression and is NOT
  742. removed from the input stream. */
  743. if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  744. {
  745. return 0;
  746. }
  747. pstate->lexptr += namelen;
  748. /* Lookup special keywords */
  749. for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
  750. if (namelen == strlen (keytab[i].keyw)
  751. && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
  752. return keytab[i].token;
  753. yylval.sval.ptr = tokstart;
  754. yylval.sval.length = namelen;
  755. if (*tokstart == '$')
  756. return DOLLAR_VARIABLE;
  757. /* Use token-type BLOCKNAME for symbols that happen to be defined as
  758. functions. If this is not so, then ...
  759. Use token-type TYPENAME for symbols that happen to be defined
  760. currently as names of types; NAME for other symbols.
  761. The caller is not constrained to care about the distinction. */
  762. {
  763. std::string tmp = copy_name (yylval.sval);
  764. struct symbol *sym;
  765. if (lookup_symtab (tmp.c_str ()))
  766. return BLOCKNAME;
  767. sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
  768. VAR_DOMAIN, 0).symbol;
  769. if (sym && sym->aclass () == LOC_BLOCK)
  770. return BLOCKNAME;
  771. if (lookup_typename (pstate->language (),
  772. tmp.c_str (), pstate->expression_context_block, 1))
  773. return TYPENAME;
  774. if(sym)
  775. {
  776. switch(sym->aclass ())
  777. {
  778. case LOC_STATIC:
  779. case LOC_REGISTER:
  780. case LOC_ARG:
  781. case LOC_REF_ARG:
  782. case LOC_REGPARM_ADDR:
  783. case LOC_LOCAL:
  784. case LOC_CONST:
  785. case LOC_CONST_BYTES:
  786. case LOC_OPTIMIZED_OUT:
  787. case LOC_COMPUTED:
  788. return NAME;
  789. case LOC_TYPEDEF:
  790. return TYPENAME;
  791. case LOC_BLOCK:
  792. return BLOCKNAME;
  793. case LOC_UNDEF:
  794. error (_("internal: Undefined class in m2lex()"));
  795. case LOC_LABEL:
  796. case LOC_UNRESOLVED:
  797. error (_("internal: Unforseen case in m2lex()"));
  798. default:
  799. error (_("unhandled token in m2lex()"));
  800. break;
  801. }
  802. }
  803. else
  804. {
  805. /* Built-in BOOLEAN type. This is sort of a hack. */
  806. if (startswith (tokstart, "TRUE"))
  807. {
  808. yylval.ulval = 1;
  809. return M2_TRUE;
  810. }
  811. else if (startswith (tokstart, "FALSE"))
  812. {
  813. yylval.ulval = 0;
  814. return M2_FALSE;
  815. }
  816. }
  817. /* Must be another type of name... */
  818. return NAME;
  819. }
  820. }
  821. int
  822. m2_language::parser (struct parser_state *par_state) const
  823. {
  824. /* Setting up the parser state. */
  825. scoped_restore pstate_restore = make_scoped_restore (&pstate);
  826. gdb_assert (par_state != NULL);
  827. pstate = par_state;
  828. paren_depth = 0;
  829. int result = yyparse ();
  830. if (!result)
  831. pstate->set_operation (pstate->pop ());
  832. return result;
  833. }
  834. static void
  835. yyerror (const char *msg)
  836. {
  837. if (pstate->prev_lexptr)
  838. pstate->lexptr = pstate->prev_lexptr;
  839. error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
  840. }