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