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