Include scoped_fd.h in debuginfod-support.h
[deliverable/binutils-gdb.git] / gdb / m2-exp.y
1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986-2021 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
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
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
36 times by the parser generator. */
37
38 %{
39
40 #include "defs.h"
41 #include "expression.h"
42 #include "language.h"
43 #include "value.h"
44 #include "parser-defs.h"
45 #include "m2-lang.h"
46 #include "bfd.h" /* Required by objfiles.h. */
47 #include "symfile.h" /* Required by objfiles.h. */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
49 #include "block.h"
50
51 #define parse_type(ps) builtin_type (ps->gdbarch ())
52 #define parse_m2_type(ps) builtin_m2_type (ps->gdbarch ())
53
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
55 etc). */
56 #define GDB_YY_REMAP_PREFIX m2_
57 #include "yy-remap.h"
58
59 /* The state of the parser, used internally when we are parsing the
60 expression. */
61
62 static struct parser_state *pstate = NULL;
63
64 int yyparse (void);
65
66 static int yylex (void);
67
68 static void yyerror (const char *);
69
70 static int parse_number (int);
71
72 /* The sign of the number being parsed. */
73 static int number_sign = 1;
74
75 %}
76
77 /* Although the yacc "value" of an expression is not used,
78 since the result is stored in the structure being created,
79 other node types do have values. */
80
81 %union
82 {
83 LONGEST lval;
84 ULONGEST ulval;
85 gdb_byte val[16];
86 struct symbol *sym;
87 struct type *tval;
88 struct stoken sval;
89 int voidval;
90 const struct block *bval;
91 enum exp_opcode opcode;
92 struct internalvar *ivar;
93
94 struct type **tvec;
95 int *ivec;
96 }
97
98 %type <voidval> exp type_exp start set
99 %type <voidval> variable
100 %type <tval> type
101 %type <bval> block
102 %type <sym> fblock
103
104 %token <lval> INT HEX ERROR
105 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
106 %token <val> FLOAT
107
108 /* Both NAME and TYPENAME tokens represent symbols in the input,
109 and both convey their data as strings.
110 But a TYPENAME is a string that happens to be defined as a typedef
111 or builtin type name (such as int or char)
112 and a NAME is any other symbol.
113
114 Contexts where this distinction is not important can use the
115 nonterminal "name", which matches either NAME or TYPENAME. */
116
117 %token <sval> STRING
118 %token <sval> NAME BLOCKNAME IDENT VARNAME
119 %token <sval> TYPENAME
120
121 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
122 %token TSIZE
123 %token INC DEC INCL EXCL
124
125 /* The GDB scope operator */
126 %token COLONCOLON
127
128 %token <sval> DOLLAR_VARIABLE
129
130 /* M2 tokens */
131 %left ','
132 %left ABOVE_COMMA
133 %nonassoc ASSIGN
134 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
135 %left OROR
136 %left LOGICAL_AND '&'
137 %left '@'
138 %left '+' '-'
139 %left '*' '/' DIV MOD
140 %right UNARY
141 %right '^' DOT '[' '('
142 %right NOT '~'
143 %left COLONCOLON QID
144 /* This is not an actual token ; it is used for precedence.
145 %right QID
146 */
147
148 \f
149 %%
150
151 start : exp
152 | type_exp
153 ;
154
155 type_exp: type
156 { write_exp_elt_opcode (pstate, OP_TYPE);
157 write_exp_elt_type (pstate, $1);
158 write_exp_elt_opcode (pstate, OP_TYPE);
159 }
160 ;
161
162 /* Expressions */
163
164 exp : exp '^' %prec UNARY
165 { write_exp_elt_opcode (pstate, UNOP_IND); }
166 ;
167
168 exp : '-'
169 { number_sign = -1; }
170 exp %prec UNARY
171 { number_sign = 1;
172 write_exp_elt_opcode (pstate, UNOP_NEG); }
173 ;
174
175 exp : '+' exp %prec UNARY
176 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
177 ;
178
179 exp : not_exp exp %prec UNARY
180 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
181 ;
182
183 not_exp : NOT
184 | '~'
185 ;
186
187 exp : CAP '(' exp ')'
188 { write_exp_elt_opcode (pstate, UNOP_CAP); }
189 ;
190
191 exp : ORD '(' exp ')'
192 { write_exp_elt_opcode (pstate, UNOP_ORD); }
193 ;
194
195 exp : ABS '(' exp ')'
196 { write_exp_elt_opcode (pstate, UNOP_ABS); }
197 ;
198
199 exp : HIGH '(' exp ')'
200 { write_exp_elt_opcode (pstate, UNOP_HIGH); }
201 ;
202
203 exp : MIN_FUNC '(' type ')'
204 { write_exp_elt_opcode (pstate, UNOP_MIN);
205 write_exp_elt_type (pstate, $3);
206 write_exp_elt_opcode (pstate, UNOP_MIN); }
207 ;
208
209 exp : MAX_FUNC '(' type ')'
210 { write_exp_elt_opcode (pstate, UNOP_MAX);
211 write_exp_elt_type (pstate, $3);
212 write_exp_elt_opcode (pstate, UNOP_MAX); }
213 ;
214
215 exp : FLOAT_FUNC '(' exp ')'
216 { write_exp_elt_opcode (pstate, UNOP_FLOAT); }
217 ;
218
219 exp : VAL '(' type ',' exp ')'
220 { write_exp_elt_opcode (pstate, BINOP_VAL);
221 write_exp_elt_type (pstate, $3);
222 write_exp_elt_opcode (pstate, BINOP_VAL); }
223 ;
224
225 exp : CHR '(' exp ')'
226 { write_exp_elt_opcode (pstate, UNOP_CHR); }
227 ;
228
229 exp : ODD '(' exp ')'
230 { write_exp_elt_opcode (pstate, UNOP_ODD); }
231 ;
232
233 exp : TRUNC '(' exp ')'
234 { write_exp_elt_opcode (pstate, UNOP_TRUNC); }
235 ;
236
237 exp : TSIZE '(' exp ')'
238 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
239 ;
240
241 exp : SIZE exp %prec UNARY
242 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
243 ;
244
245
246 exp : INC '(' exp ')'
247 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
248 ;
249
250 exp : INC '(' exp ',' exp ')'
251 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
252 write_exp_elt_opcode (pstate, BINOP_ADD);
253 write_exp_elt_opcode (pstate,
254 BINOP_ASSIGN_MODIFY); }
255 ;
256
257 exp : DEC '(' exp ')'
258 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT);}
259 ;
260
261 exp : DEC '(' exp ',' exp ')'
262 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
263 write_exp_elt_opcode (pstate, BINOP_SUB);
264 write_exp_elt_opcode (pstate,
265 BINOP_ASSIGN_MODIFY); }
266 ;
267
268 exp : exp DOT NAME
269 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
270 write_exp_string (pstate, $3);
271 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
272 ;
273
274 exp : set
275 ;
276
277 exp : exp IN set
278 { error (_("Sets are not implemented."));}
279 ;
280
281 exp : INCL '(' exp ',' exp ')'
282 { error (_("Sets are not implemented."));}
283 ;
284
285 exp : EXCL '(' exp ',' exp ')'
286 { error (_("Sets are not implemented."));}
287 ;
288
289 set : '{' arglist '}'
290 { error (_("Sets are not implemented."));}
291 | type '{' arglist '}'
292 { error (_("Sets are not implemented."));}
293 ;
294
295
296 /* Modula-2 array subscript notation [a,b,c...]. */
297 exp : exp '['
298 /* This function just saves the number of arguments
299 that follow in the list. It is *not* specific to
300 function types */
301 { pstate->start_arglist(); }
302 non_empty_arglist ']' %prec DOT
303 {
304 gdb_assert (pstate->arglist_len > 0);
305 write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
306 write_exp_elt_longcst (pstate,
307 pstate->end_arglist());
308 write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
309 }
310 ;
311
312 exp : exp '('
313 /* This is to save the value of arglist_len
314 being accumulated by an outer function call. */
315 { pstate->start_arglist (); }
316 arglist ')' %prec DOT
317 { write_exp_elt_opcode (pstate, OP_FUNCALL);
318 write_exp_elt_longcst (pstate,
319 pstate->end_arglist ());
320 write_exp_elt_opcode (pstate, OP_FUNCALL); }
321 ;
322
323 arglist :
324 ;
325
326 arglist : exp
327 { pstate->arglist_len = 1; }
328 ;
329
330 arglist : arglist ',' exp %prec ABOVE_COMMA
331 { pstate->arglist_len++; }
332 ;
333
334 non_empty_arglist
335 : exp
336 { pstate->arglist_len = 1; }
337 ;
338
339 non_empty_arglist
340 : non_empty_arglist ',' exp %prec ABOVE_COMMA
341 { pstate->arglist_len++; }
342 ;
343
344 /* GDB construct */
345 exp : '{' type '}' exp %prec UNARY
346 { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
347 write_exp_elt_type (pstate, $2);
348 write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
349 ;
350
351 exp : type '(' exp ')' %prec UNARY
352 { write_exp_elt_opcode (pstate, UNOP_CAST);
353 write_exp_elt_type (pstate, $1);
354 write_exp_elt_opcode (pstate, UNOP_CAST); }
355 ;
356
357 exp : '(' exp ')'
358 { }
359 ;
360
361 /* Binary operators in order of decreasing precedence. Note that some
362 of these operators are overloaded! (ie. sets) */
363
364 /* GDB construct */
365 exp : exp '@' exp
366 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
367 ;
368
369 exp : exp '*' exp
370 { write_exp_elt_opcode (pstate, BINOP_MUL); }
371 ;
372
373 exp : exp '/' exp
374 { write_exp_elt_opcode (pstate, BINOP_DIV); }
375 ;
376
377 exp : exp DIV exp
378 { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
379 ;
380
381 exp : exp MOD exp
382 { write_exp_elt_opcode (pstate, BINOP_REM); }
383 ;
384
385 exp : exp '+' exp
386 { write_exp_elt_opcode (pstate, BINOP_ADD); }
387 ;
388
389 exp : exp '-' exp
390 { write_exp_elt_opcode (pstate, BINOP_SUB); }
391 ;
392
393 exp : exp '=' exp
394 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
395 ;
396
397 exp : exp NOTEQUAL exp
398 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
399 | exp '#' exp
400 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
401 ;
402
403 exp : exp LEQ exp
404 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
405 ;
406
407 exp : exp GEQ exp
408 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
409 ;
410
411 exp : exp '<' exp
412 { write_exp_elt_opcode (pstate, BINOP_LESS); }
413 ;
414
415 exp : exp '>' exp
416 { write_exp_elt_opcode (pstate, BINOP_GTR); }
417 ;
418
419 exp : exp LOGICAL_AND exp
420 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
421 ;
422
423 exp : exp OROR exp
424 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
425 ;
426
427 exp : exp ASSIGN exp
428 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
429 ;
430
431
432 /* Constants */
433
434 exp : M2_TRUE
435 { write_exp_elt_opcode (pstate, OP_BOOL);
436 write_exp_elt_longcst (pstate, (LONGEST) $1);
437 write_exp_elt_opcode (pstate, OP_BOOL); }
438 ;
439
440 exp : M2_FALSE
441 { write_exp_elt_opcode (pstate, OP_BOOL);
442 write_exp_elt_longcst (pstate, (LONGEST) $1);
443 write_exp_elt_opcode (pstate, OP_BOOL); }
444 ;
445
446 exp : INT
447 { write_exp_elt_opcode (pstate, OP_LONG);
448 write_exp_elt_type (pstate,
449 parse_m2_type (pstate)->builtin_int);
450 write_exp_elt_longcst (pstate, (LONGEST) $1);
451 write_exp_elt_opcode (pstate, OP_LONG); }
452 ;
453
454 exp : UINT
455 {
456 write_exp_elt_opcode (pstate, OP_LONG);
457 write_exp_elt_type (pstate,
458 parse_m2_type (pstate)
459 ->builtin_card);
460 write_exp_elt_longcst (pstate, (LONGEST) $1);
461 write_exp_elt_opcode (pstate, OP_LONG);
462 }
463 ;
464
465 exp : CHAR
466 { write_exp_elt_opcode (pstate, OP_LONG);
467 write_exp_elt_type (pstate,
468 parse_m2_type (pstate)
469 ->builtin_char);
470 write_exp_elt_longcst (pstate, (LONGEST) $1);
471 write_exp_elt_opcode (pstate, OP_LONG); }
472 ;
473
474
475 exp : FLOAT
476 { write_exp_elt_opcode (pstate, OP_FLOAT);
477 write_exp_elt_type (pstate,
478 parse_m2_type (pstate)
479 ->builtin_real);
480 write_exp_elt_floatcst (pstate, $1);
481 write_exp_elt_opcode (pstate, OP_FLOAT); }
482 ;
483
484 exp : variable
485 ;
486
487 exp : SIZE '(' type ')' %prec UNARY
488 { write_exp_elt_opcode (pstate, OP_LONG);
489 write_exp_elt_type (pstate,
490 parse_type (pstate)->builtin_int);
491 write_exp_elt_longcst (pstate,
492 (LONGEST) TYPE_LENGTH ($3));
493 write_exp_elt_opcode (pstate, OP_LONG); }
494 ;
495
496 exp : STRING
497 { write_exp_elt_opcode (pstate, OP_M2_STRING);
498 write_exp_string (pstate, $1);
499 write_exp_elt_opcode (pstate, OP_M2_STRING); }
500 ;
501
502 /* This will be used for extensions later. Like adding modules. */
503 block : fblock
504 { $$ = SYMBOL_BLOCK_VALUE($1); }
505 ;
506
507 fblock : BLOCKNAME
508 { struct symbol *sym
509 = lookup_symbol (copy_name ($1).c_str (),
510 pstate->expression_context_block,
511 VAR_DOMAIN, 0).symbol;
512 $$ = sym;}
513 ;
514
515
516 /* GDB scope operator */
517 fblock : block COLONCOLON BLOCKNAME
518 { struct symbol *tem
519 = lookup_symbol (copy_name ($3).c_str (), $1,
520 VAR_DOMAIN, 0).symbol;
521 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
522 error (_("No function \"%s\" in specified context."),
523 copy_name ($3).c_str ());
524 $$ = tem;
525 }
526 ;
527
528 /* Useful for assigning to PROCEDURE variables */
529 variable: fblock
530 { write_exp_elt_opcode (pstate, OP_VAR_VALUE);
531 write_exp_elt_block (pstate, NULL);
532 write_exp_elt_sym (pstate, $1);
533 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
534 ;
535
536 /* GDB internal ($foo) variable */
537 variable: DOLLAR_VARIABLE
538 { write_dollar_variable (pstate, $1); }
539 ;
540
541 /* GDB scope operator */
542 variable: block COLONCOLON NAME
543 { struct block_symbol sym
544 = lookup_symbol (copy_name ($3).c_str (), $1,
545 VAR_DOMAIN, 0);
546
547 if (sym.symbol == 0)
548 error (_("No symbol \"%s\" in specified context."),
549 copy_name ($3).c_str ());
550 if (symbol_read_needs_frame (sym.symbol))
551 pstate->block_tracker->update (sym);
552
553 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
554 write_exp_elt_block (pstate, sym.block);
555 write_exp_elt_sym (pstate, sym.symbol);
556 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
557 ;
558
559 /* Base case for variables. */
560 variable: NAME
561 { struct block_symbol sym;
562 struct field_of_this_result is_a_field_of_this;
563
564 std::string name = copy_name ($1);
565 sym
566 = lookup_symbol (name.c_str (),
567 pstate->expression_context_block,
568 VAR_DOMAIN,
569 &is_a_field_of_this);
570
571 write_exp_symbol_reference (pstate, name.c_str (),
572 sym);
573 }
574 ;
575
576 type
577 : TYPENAME
578 { $$
579 = lookup_typename (pstate->language (),
580 copy_name ($1).c_str (),
581 pstate->expression_context_block,
582 0);
583 }
584
585 ;
586
587 %%
588
589 /* Take care of parsing a number (anything that starts with a digit).
590 Set yylval and return the token type; update lexptr.
591 LEN is the number of characters in it. */
592
593 /*** Needs some error checking for the float case ***/
594
595 static int
596 parse_number (int olen)
597 {
598 const char *p = pstate->lexptr;
599 LONGEST n = 0;
600 LONGEST prevn = 0;
601 int c,i,ischar=0;
602 int base = input_radix;
603 int len = olen;
604 int unsigned_p = number_sign == 1 ? 1 : 0;
605
606 if(p[len-1] == 'H')
607 {
608 base = 16;
609 len--;
610 }
611 else if(p[len-1] == 'C' || p[len-1] == 'B')
612 {
613 base = 8;
614 ischar = p[len-1] == 'C';
615 len--;
616 }
617
618 /* Scan the number */
619 for (c = 0; c < len; c++)
620 {
621 if (p[c] == '.' && base == 10)
622 {
623 /* It's a float since it contains a point. */
624 if (!parse_float (p, len,
625 parse_m2_type (pstate)->builtin_real,
626 yylval.val))
627 return ERROR;
628
629 pstate->lexptr += len;
630 return FLOAT;
631 }
632 if (p[c] == '.' && base != 10)
633 error (_("Floating point numbers must be base 10."));
634 if (base == 10 && (p[c] < '0' || p[c] > '9'))
635 error (_("Invalid digit \'%c\' in number."),p[c]);
636 }
637
638 while (len-- > 0)
639 {
640 c = *p++;
641 n *= base;
642 if( base == 8 && (c == '8' || c == '9'))
643 error (_("Invalid digit \'%c\' in octal number."),c);
644 if (c >= '0' && c <= '9')
645 i = c - '0';
646 else
647 {
648 if (base == 16 && c >= 'A' && c <= 'F')
649 i = c - 'A' + 10;
650 else
651 return ERROR;
652 }
653 n+=i;
654 if(i >= base)
655 return ERROR;
656 if(!unsigned_p && number_sign == 1 && (prevn >= n))
657 unsigned_p=1; /* Try something unsigned */
658 /* Don't do the range check if n==i and i==0, since that special
659 case will give an overflow error. */
660 if(RANGE_CHECK && n!=i && i)
661 {
662 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
663 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
664 range_error (_("Overflow on numeric constant."));
665 }
666 prevn=n;
667 }
668
669 pstate->lexptr = p;
670 if(*p == 'B' || *p == 'C' || *p == 'H')
671 pstate->lexptr++; /* Advance past B,C or H */
672
673 if (ischar)
674 {
675 yylval.ulval = n;
676 return CHAR;
677 }
678 else if ( unsigned_p && number_sign == 1)
679 {
680 yylval.ulval = n;
681 return UINT;
682 }
683 else if((unsigned_p && (n<0))) {
684 range_error (_("Overflow on numeric constant -- number too large."));
685 /* But, this can return if range_check == range_warn. */
686 }
687 yylval.lval = n;
688 return INT;
689 }
690
691
692 /* Some tokens */
693
694 static struct
695 {
696 char name[2];
697 int token;
698 } tokentab2[] =
699 {
700 { {'<', '>'}, NOTEQUAL },
701 { {':', '='}, ASSIGN },
702 { {'<', '='}, LEQ },
703 { {'>', '='}, GEQ },
704 { {':', ':'}, COLONCOLON },
705
706 };
707
708 /* Some specific keywords */
709
710 struct keyword {
711 char keyw[10];
712 int token;
713 };
714
715 static struct keyword keytab[] =
716 {
717 {"OR" , OROR },
718 {"IN", IN },/* Note space after IN */
719 {"AND", LOGICAL_AND},
720 {"ABS", ABS },
721 {"CHR", CHR },
722 {"DEC", DEC },
723 {"NOT", NOT },
724 {"DIV", DIV },
725 {"INC", INC },
726 {"MAX", MAX_FUNC },
727 {"MIN", MIN_FUNC },
728 {"MOD", MOD },
729 {"ODD", ODD },
730 {"CAP", CAP },
731 {"ORD", ORD },
732 {"VAL", VAL },
733 {"EXCL", EXCL },
734 {"HIGH", HIGH },
735 {"INCL", INCL },
736 {"SIZE", SIZE },
737 {"FLOAT", FLOAT_FUNC },
738 {"TRUNC", TRUNC },
739 {"TSIZE", SIZE },
740 };
741
742
743 /* Depth of parentheses. */
744 static int paren_depth;
745
746 /* Read one token, getting characters through lexptr. */
747
748 /* This is where we will check to make sure that the language and the
749 operators used are compatible */
750
751 static int
752 yylex (void)
753 {
754 int c;
755 int namelen;
756 int i;
757 const char *tokstart;
758 char quote;
759
760 retry:
761
762 pstate->prev_lexptr = pstate->lexptr;
763
764 tokstart = pstate->lexptr;
765
766
767 /* See if it is a special token of length 2 */
768 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
769 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
770 {
771 pstate->lexptr += 2;
772 return tokentab2[i].token;
773 }
774
775 switch (c = *tokstart)
776 {
777 case 0:
778 return 0;
779
780 case ' ':
781 case '\t':
782 case '\n':
783 pstate->lexptr++;
784 goto retry;
785
786 case '(':
787 paren_depth++;
788 pstate->lexptr++;
789 return c;
790
791 case ')':
792 if (paren_depth == 0)
793 return 0;
794 paren_depth--;
795 pstate->lexptr++;
796 return c;
797
798 case ',':
799 if (pstate->comma_terminates && paren_depth == 0)
800 return 0;
801 pstate->lexptr++;
802 return c;
803
804 case '.':
805 /* Might be a floating point number. */
806 if (pstate->lexptr[1] >= '0' && pstate->lexptr[1] <= '9')
807 break; /* Falls into number code. */
808 else
809 {
810 pstate->lexptr++;
811 return DOT;
812 }
813
814 /* These are character tokens that appear as-is in the YACC grammar */
815 case '+':
816 case '-':
817 case '*':
818 case '/':
819 case '^':
820 case '<':
821 case '>':
822 case '[':
823 case ']':
824 case '=':
825 case '{':
826 case '}':
827 case '#':
828 case '@':
829 case '~':
830 case '&':
831 pstate->lexptr++;
832 return c;
833
834 case '\'' :
835 case '"':
836 quote = c;
837 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
838 if (c == '\\')
839 {
840 c = tokstart[++namelen];
841 if (c >= '0' && c <= '9')
842 {
843 c = tokstart[++namelen];
844 if (c >= '0' && c <= '9')
845 c = tokstart[++namelen];
846 }
847 }
848 if(c != quote)
849 error (_("Unterminated string or character constant."));
850 yylval.sval.ptr = tokstart + 1;
851 yylval.sval.length = namelen - 1;
852 pstate->lexptr += namelen + 1;
853
854 if(namelen == 2) /* Single character */
855 {
856 yylval.ulval = tokstart[1];
857 return CHAR;
858 }
859 else
860 return STRING;
861 }
862
863 /* Is it a number? */
864 /* Note: We have already dealt with the case of the token '.'.
865 See case '.' above. */
866 if ((c >= '0' && c <= '9'))
867 {
868 /* It's a number. */
869 int got_dot = 0, got_e = 0;
870 const char *p = tokstart;
871 int toktype;
872
873 for (++p ;; ++p)
874 {
875 if (!got_e && (*p == 'e' || *p == 'E'))
876 got_dot = got_e = 1;
877 else if (!got_dot && *p == '.')
878 got_dot = 1;
879 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
880 && (*p == '-' || *p == '+'))
881 /* This is the sign of the exponent, not the end of the
882 number. */
883 continue;
884 else if ((*p < '0' || *p > '9') &&
885 (*p < 'A' || *p > 'F') &&
886 (*p != 'H')) /* Modula-2 hexadecimal number */
887 break;
888 }
889 toktype = parse_number (p - tokstart);
890 if (toktype == ERROR)
891 {
892 char *err_copy = (char *) alloca (p - tokstart + 1);
893
894 memcpy (err_copy, tokstart, p - tokstart);
895 err_copy[p - tokstart] = 0;
896 error (_("Invalid number \"%s\"."), err_copy);
897 }
898 pstate->lexptr = p;
899 return toktype;
900 }
901
902 if (!(c == '_' || c == '$'
903 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
904 /* We must have come across a bad character (e.g. ';'). */
905 error (_("Invalid character '%c' in expression."), c);
906
907 /* It's a name. See how long it is. */
908 namelen = 0;
909 for (c = tokstart[namelen];
910 (c == '_' || c == '$' || (c >= '0' && c <= '9')
911 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
912 c = tokstart[++namelen])
913 ;
914
915 /* The token "if" terminates the expression and is NOT
916 removed from the input stream. */
917 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
918 {
919 return 0;
920 }
921
922 pstate->lexptr += namelen;
923
924 /* Lookup special keywords */
925 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
926 if (namelen == strlen (keytab[i].keyw)
927 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
928 return keytab[i].token;
929
930 yylval.sval.ptr = tokstart;
931 yylval.sval.length = namelen;
932
933 if (*tokstart == '$')
934 return DOLLAR_VARIABLE;
935
936 /* Use token-type BLOCKNAME for symbols that happen to be defined as
937 functions. If this is not so, then ...
938 Use token-type TYPENAME for symbols that happen to be defined
939 currently as names of types; NAME for other symbols.
940 The caller is not constrained to care about the distinction. */
941 {
942 std::string tmp = copy_name (yylval.sval);
943 struct symbol *sym;
944
945 if (lookup_symtab (tmp.c_str ()))
946 return BLOCKNAME;
947 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
948 VAR_DOMAIN, 0).symbol;
949 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
950 return BLOCKNAME;
951 if (lookup_typename (pstate->language (),
952 tmp.c_str (), pstate->expression_context_block, 1))
953 return TYPENAME;
954
955 if(sym)
956 {
957 switch(SYMBOL_CLASS (sym))
958 {
959 case LOC_STATIC:
960 case LOC_REGISTER:
961 case LOC_ARG:
962 case LOC_REF_ARG:
963 case LOC_REGPARM_ADDR:
964 case LOC_LOCAL:
965 case LOC_CONST:
966 case LOC_CONST_BYTES:
967 case LOC_OPTIMIZED_OUT:
968 case LOC_COMPUTED:
969 return NAME;
970
971 case LOC_TYPEDEF:
972 return TYPENAME;
973
974 case LOC_BLOCK:
975 return BLOCKNAME;
976
977 case LOC_UNDEF:
978 error (_("internal: Undefined class in m2lex()"));
979
980 case LOC_LABEL:
981 case LOC_UNRESOLVED:
982 error (_("internal: Unforseen case in m2lex()"));
983
984 default:
985 error (_("unhandled token in m2lex()"));
986 break;
987 }
988 }
989 else
990 {
991 /* Built-in BOOLEAN type. This is sort of a hack. */
992 if (strncmp (tokstart, "TRUE", 4) == 0)
993 {
994 yylval.ulval = 1;
995 return M2_TRUE;
996 }
997 else if (strncmp (tokstart, "FALSE", 5) == 0)
998 {
999 yylval.ulval = 0;
1000 return M2_FALSE;
1001 }
1002 }
1003
1004 /* Must be another type of name... */
1005 return NAME;
1006 }
1007 }
1008
1009 int
1010 m2_language::parser (struct parser_state *par_state) const
1011 {
1012 /* Setting up the parser state. */
1013 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1014 gdb_assert (par_state != NULL);
1015 pstate = par_state;
1016 paren_depth = 0;
1017
1018 return yyparse ();
1019 }
1020
1021 static void
1022 yyerror (const char *msg)
1023 {
1024 if (pstate->prev_lexptr)
1025 pstate->lexptr = pstate->prev_lexptr;
1026
1027 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
1028 }
This page took 0.073503 seconds and 4 git commands to generate.