2008-02-21 Pedro Alves <pedro@codesorcery.com>
[binutils-gdb.git] / gdb / m2-exp.y
blob2e29c77eccbcaabe35ff4cb467e1d0a8b39fd7bc
1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999,
3 2000, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Generated from expread.y (now c-exp.y) and contributed by the Department
5 of Computer Science at the State University of New York at Buffalo, 1991.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* Parse a Modula-2 expression from text in a string,
25 and return the result as a struct expression pointer.
26 That structure contains arithmetic operations in reverse polish,
27 with constants represented by operations that are followed by special data.
28 See expression.h for the details of the format.
29 What is important here is that it can be built up sequentially
30 during the process of parsing; the lower levels of the tree always
31 come first in the result.
33 Note that malloc's and realloc's in this file are transformed to
34 xmalloc and xrealloc respectively by the same sed command in the
35 makefile that remaps any other malloc/realloc inserted by the parser
36 generator. Doing this with #defines and trying to control the interaction
37 with include files (<malloc.h> and <stdlib.h> for example) just became
38 too messy, particularly when such includes can be inserted at random
39 times by the parser generator. */
43 #include "defs.h"
44 #include "gdb_string.h"
45 #include "expression.h"
46 #include "language.h"
47 #include "value.h"
48 #include "parser-defs.h"
49 #include "m2-lang.h"
50 #include "bfd.h" /* Required by objfiles.h. */
51 #include "symfile.h" /* Required by objfiles.h. */
52 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 #include "block.h"
55 #define parse_type builtin_type (parse_gdbarch)
56 #define parse_m2_type builtin_m2_type (parse_gdbarch)
58 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
59 as well as gratuitiously global symbol names, so we can have multiple
60 yacc generated parsers in gdb. Note that these are only the variables
61 produced by yacc. If other parser generators (bison, byacc, etc) produce
62 additional global names that conflict at link time, then those parser
63 generators need to be fixed instead of adding those names to this list. */
65 #define yymaxdepth m2_maxdepth
66 #define yyparse m2_parse
67 #define yylex m2_lex
68 #define yyerror m2_error
69 #define yylval m2_lval
70 #define yychar m2_char
71 #define yydebug m2_debug
72 #define yypact m2_pact
73 #define yyr1 m2_r1
74 #define yyr2 m2_r2
75 #define yydef m2_def
76 #define yychk m2_chk
77 #define yypgo m2_pgo
78 #define yyact m2_act
79 #define yyexca m2_exca
80 #define yyerrflag m2_errflag
81 #define yynerrs m2_nerrs
82 #define yyps m2_ps
83 #define yypv m2_pv
84 #define yys m2_s
85 #define yy_yys m2_yys
86 #define yystate m2_state
87 #define yytmp m2_tmp
88 #define yyv m2_v
89 #define yy_yyv m2_yyv
90 #define yyval m2_val
91 #define yylloc m2_lloc
92 #define yyreds m2_reds /* With YYDEBUG defined */
93 #define yytoks m2_toks /* With YYDEBUG defined */
94 #define yyname m2_name /* With YYDEBUG defined */
95 #define yyrule m2_rule /* With YYDEBUG defined */
96 #define yylhs m2_yylhs
97 #define yylen m2_yylen
98 #define yydefred m2_yydefred
99 #define yydgoto m2_yydgoto
100 #define yysindex m2_yysindex
101 #define yyrindex m2_yyrindex
102 #define yygindex m2_yygindex
103 #define yytable m2_yytable
104 #define yycheck m2_yycheck
106 #ifndef YYDEBUG
107 #define YYDEBUG 1 /* Default to yydebug support */
108 #endif
110 #define YYFPRINTF parser_fprintf
112 int yyparse (void);
114 static int yylex (void);
116 void yyerror (char *);
118 #if 0
119 static char *make_qualname (char *, char *);
120 #endif
122 static int parse_number (int);
124 /* The sign of the number being parsed. */
125 static int number_sign = 1;
127 /* The block that the module specified by the qualifer on an identifer is
128 contained in, */
129 #if 0
130 static struct block *modblock=0;
131 #endif
135 /* Although the yacc "value" of an expression is not used,
136 since the result is stored in the structure being created,
137 other node types do have values. */
139 %union
141 LONGEST lval;
142 ULONGEST ulval;
143 DOUBLEST dval;
144 struct symbol *sym;
145 struct type *tval;
146 struct stoken sval;
147 int voidval;
148 struct block *bval;
149 enum exp_opcode opcode;
150 struct internalvar *ivar;
152 struct type **tvec;
153 int *ivec;
156 %type <voidval> exp type_exp start set
157 %type <voidval> variable
158 %type <tval> type
159 %type <bval> block
160 %type <sym> fblock
162 %token <lval> INT HEX ERROR
163 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
164 %token <dval> FLOAT
166 /* Both NAME and TYPENAME tokens represent symbols in the input,
167 and both convey their data as strings.
168 But a TYPENAME is a string that happens to be defined as a typedef
169 or builtin type name (such as int or char)
170 and a NAME is any other symbol.
172 Contexts where this distinction is not important can use the
173 nonterminal "name", which matches either NAME or TYPENAME. */
175 %token <sval> STRING
176 %token <sval> NAME BLOCKNAME IDENT VARNAME
177 %token <sval> TYPENAME
179 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
180 %token TSIZE
181 %token INC DEC INCL EXCL
183 /* The GDB scope operator */
184 %token COLONCOLON
186 %token <voidval> INTERNAL_VAR
188 /* M2 tokens */
189 %left ','
190 %left ABOVE_COMMA
191 %nonassoc ASSIGN
192 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
193 %left OROR
194 %left LOGICAL_AND '&'
195 %left '@'
196 %left '+' '-'
197 %left '*' '/' DIV MOD
198 %right UNARY
199 %right '^' DOT '[' '('
200 %right NOT '~'
201 %left COLONCOLON QID
202 /* This is not an actual token ; it is used for precedence.
203 %right QID
209 start : exp
210 | type_exp
213 type_exp: type
214 { write_exp_elt_opcode(OP_TYPE);
215 write_exp_elt_type($1);
216 write_exp_elt_opcode(OP_TYPE);
220 /* Expressions */
222 exp : exp '^' %prec UNARY
223 { write_exp_elt_opcode (UNOP_IND); }
226 exp : '-'
227 { number_sign = -1; }
228 exp %prec UNARY
229 { number_sign = 1;
230 write_exp_elt_opcode (UNOP_NEG); }
233 exp : '+' exp %prec UNARY
234 { write_exp_elt_opcode(UNOP_PLUS); }
237 exp : not_exp exp %prec UNARY
238 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
241 not_exp : NOT
242 | '~'
245 exp : CAP '(' exp ')'
246 { write_exp_elt_opcode (UNOP_CAP); }
249 exp : ORD '(' exp ')'
250 { write_exp_elt_opcode (UNOP_ORD); }
253 exp : ABS '(' exp ')'
254 { write_exp_elt_opcode (UNOP_ABS); }
257 exp : HIGH '(' exp ')'
258 { write_exp_elt_opcode (UNOP_HIGH); }
261 exp : MIN_FUNC '(' type ')'
262 { write_exp_elt_opcode (UNOP_MIN);
263 write_exp_elt_type ($3);
264 write_exp_elt_opcode (UNOP_MIN); }
267 exp : MAX_FUNC '(' type ')'
268 { write_exp_elt_opcode (UNOP_MAX);
269 write_exp_elt_type ($3);
270 write_exp_elt_opcode (UNOP_MIN); }
273 exp : FLOAT_FUNC '(' exp ')'
274 { write_exp_elt_opcode (UNOP_FLOAT); }
277 exp : VAL '(' type ',' exp ')'
278 { write_exp_elt_opcode (BINOP_VAL);
279 write_exp_elt_type ($3);
280 write_exp_elt_opcode (BINOP_VAL); }
283 exp : CHR '(' exp ')'
284 { write_exp_elt_opcode (UNOP_CHR); }
287 exp : ODD '(' exp ')'
288 { write_exp_elt_opcode (UNOP_ODD); }
291 exp : TRUNC '(' exp ')'
292 { write_exp_elt_opcode (UNOP_TRUNC); }
295 exp : TSIZE '(' exp ')'
296 { write_exp_elt_opcode (UNOP_SIZEOF); }
299 exp : SIZE exp %prec UNARY
300 { write_exp_elt_opcode (UNOP_SIZEOF); }
304 exp : INC '(' exp ')'
305 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
308 exp : INC '(' exp ',' exp ')'
309 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
310 write_exp_elt_opcode(BINOP_ADD);
311 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
314 exp : DEC '(' exp ')'
315 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
318 exp : DEC '(' exp ',' exp ')'
319 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
320 write_exp_elt_opcode(BINOP_SUB);
321 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
324 exp : exp DOT NAME
325 { write_exp_elt_opcode (STRUCTOP_STRUCT);
326 write_exp_string ($3);
327 write_exp_elt_opcode (STRUCTOP_STRUCT); }
330 exp : set
333 exp : exp IN set
334 { error("Sets are not implemented.");}
337 exp : INCL '(' exp ',' exp ')'
338 { error("Sets are not implemented.");}
341 exp : EXCL '(' exp ',' exp ')'
342 { error("Sets are not implemented.");}
345 set : '{' arglist '}'
346 { error("Sets are not implemented.");}
347 | type '{' arglist '}'
348 { error("Sets are not implemented.");}
352 /* Modula-2 array subscript notation [a,b,c...] */
353 exp : exp '['
354 /* This function just saves the number of arguments
355 that follow in the list. It is *not* specific to
356 function types */
357 { start_arglist(); }
358 non_empty_arglist ']' %prec DOT
359 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
360 write_exp_elt_longcst ((LONGEST) end_arglist());
361 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
364 exp : exp '[' exp ']'
365 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
368 exp : exp '('
369 /* This is to save the value of arglist_len
370 being accumulated by an outer function call. */
371 { start_arglist (); }
372 arglist ')' %prec DOT
373 { write_exp_elt_opcode (OP_FUNCALL);
374 write_exp_elt_longcst ((LONGEST) end_arglist ());
375 write_exp_elt_opcode (OP_FUNCALL); }
378 arglist :
381 arglist : exp
382 { arglist_len = 1; }
385 arglist : arglist ',' exp %prec ABOVE_COMMA
386 { arglist_len++; }
389 non_empty_arglist
390 : exp
391 { arglist_len = 1; }
394 non_empty_arglist
395 : non_empty_arglist ',' exp %prec ABOVE_COMMA
396 { arglist_len++; }
399 /* GDB construct */
400 exp : '{' type '}' exp %prec UNARY
401 { write_exp_elt_opcode (UNOP_MEMVAL);
402 write_exp_elt_type ($2);
403 write_exp_elt_opcode (UNOP_MEMVAL); }
406 exp : type '(' exp ')' %prec UNARY
407 { write_exp_elt_opcode (UNOP_CAST);
408 write_exp_elt_type ($1);
409 write_exp_elt_opcode (UNOP_CAST); }
412 exp : '(' exp ')'
416 /* Binary operators in order of decreasing precedence. Note that some
417 of these operators are overloaded! (ie. sets) */
419 /* GDB construct */
420 exp : exp '@' exp
421 { write_exp_elt_opcode (BINOP_REPEAT); }
424 exp : exp '*' exp
425 { write_exp_elt_opcode (BINOP_MUL); }
428 exp : exp '/' exp
429 { write_exp_elt_opcode (BINOP_DIV); }
432 exp : exp DIV exp
433 { write_exp_elt_opcode (BINOP_INTDIV); }
436 exp : exp MOD exp
437 { write_exp_elt_opcode (BINOP_REM); }
440 exp : exp '+' exp
441 { write_exp_elt_opcode (BINOP_ADD); }
444 exp : exp '-' exp
445 { write_exp_elt_opcode (BINOP_SUB); }
448 exp : exp '=' exp
449 { write_exp_elt_opcode (BINOP_EQUAL); }
452 exp : exp NOTEQUAL exp
453 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
454 | exp '#' exp
455 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
458 exp : exp LEQ exp
459 { write_exp_elt_opcode (BINOP_LEQ); }
462 exp : exp GEQ exp
463 { write_exp_elt_opcode (BINOP_GEQ); }
466 exp : exp '<' exp
467 { write_exp_elt_opcode (BINOP_LESS); }
470 exp : exp '>' exp
471 { write_exp_elt_opcode (BINOP_GTR); }
474 exp : exp LOGICAL_AND exp
475 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
478 exp : exp OROR exp
479 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
482 exp : exp ASSIGN exp
483 { write_exp_elt_opcode (BINOP_ASSIGN); }
487 /* Constants */
489 exp : M2_TRUE
490 { write_exp_elt_opcode (OP_BOOL);
491 write_exp_elt_longcst ((LONGEST) $1);
492 write_exp_elt_opcode (OP_BOOL); }
495 exp : M2_FALSE
496 { write_exp_elt_opcode (OP_BOOL);
497 write_exp_elt_longcst ((LONGEST) $1);
498 write_exp_elt_opcode (OP_BOOL); }
501 exp : INT
502 { write_exp_elt_opcode (OP_LONG);
503 write_exp_elt_type (parse_m2_type->builtin_int);
504 write_exp_elt_longcst ((LONGEST) $1);
505 write_exp_elt_opcode (OP_LONG); }
508 exp : UINT
510 write_exp_elt_opcode (OP_LONG);
511 write_exp_elt_type (parse_m2_type->builtin_card);
512 write_exp_elt_longcst ((LONGEST) $1);
513 write_exp_elt_opcode (OP_LONG);
517 exp : CHAR
518 { write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_type (parse_m2_type->builtin_char);
520 write_exp_elt_longcst ((LONGEST) $1);
521 write_exp_elt_opcode (OP_LONG); }
525 exp : FLOAT
526 { write_exp_elt_opcode (OP_DOUBLE);
527 write_exp_elt_type (parse_m2_type->builtin_real);
528 write_exp_elt_dblcst ($1);
529 write_exp_elt_opcode (OP_DOUBLE); }
532 exp : variable
535 exp : SIZE '(' type ')' %prec UNARY
536 { write_exp_elt_opcode (OP_LONG);
537 write_exp_elt_type (parse_type->builtin_int);
538 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
539 write_exp_elt_opcode (OP_LONG); }
542 exp : STRING
543 { write_exp_elt_opcode (OP_M2_STRING);
544 write_exp_string ($1);
545 write_exp_elt_opcode (OP_M2_STRING); }
548 /* This will be used for extensions later. Like adding modules. */
549 block : fblock
550 { $$ = SYMBOL_BLOCK_VALUE($1); }
553 fblock : BLOCKNAME
554 { struct symbol *sym
555 = lookup_symbol (copy_name ($1), expression_context_block,
556 VAR_DOMAIN, 0);
557 $$ = sym;}
561 /* GDB scope operator */
562 fblock : block COLONCOLON BLOCKNAME
563 { struct symbol *tem
564 = lookup_symbol (copy_name ($3), $1,
565 VAR_DOMAIN, 0);
566 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
567 error ("No function \"%s\" in specified context.",
568 copy_name ($3));
569 $$ = tem;
573 /* Useful for assigning to PROCEDURE variables */
574 variable: fblock
575 { write_exp_elt_opcode(OP_VAR_VALUE);
576 write_exp_elt_block (NULL);
577 write_exp_elt_sym ($1);
578 write_exp_elt_opcode (OP_VAR_VALUE); }
581 /* GDB internal ($foo) variable */
582 variable: INTERNAL_VAR
585 /* GDB scope operator */
586 variable: block COLONCOLON NAME
587 { struct symbol *sym;
588 sym = lookup_symbol (copy_name ($3), $1,
589 VAR_DOMAIN, 0);
590 if (sym == 0)
591 error ("No symbol \"%s\" in specified context.",
592 copy_name ($3));
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); }
601 /* Base case for variables. */
602 variable: NAME
603 { struct symbol *sym;
604 int is_a_field_of_this;
606 sym = lookup_symbol (copy_name ($1),
607 expression_context_block,
608 VAR_DOMAIN,
609 &is_a_field_of_this);
610 if (sym)
612 if (symbol_read_needs_frame (sym))
614 if (innermost_block == 0 ||
615 contained_in (block_found,
616 innermost_block))
617 innermost_block = block_found;
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);
628 else
630 struct minimal_symbol *msymbol;
631 char *arg = copy_name ($1);
633 msymbol =
634 lookup_minimal_symbol (arg, NULL, NULL);
635 if (msymbol != NULL)
636 write_exp_msymbol (msymbol);
637 else if (!have_full_symbols () && !have_partial_symbols ())
638 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
639 else
640 error ("No symbol \"%s\" in current context.",
641 copy_name ($1));
646 type
647 : TYPENAME
648 { $$ = lookup_typename (copy_name ($1),
649 expression_context_block, 0); }
655 #if 0 /* FIXME! */
657 overflow(a,b)
658 long a,b;
660 return (MAX_OF_TYPE(parse_m2_type->builtin_int) - b) < a;
664 uoverflow(a,b)
665 unsigned long a,b;
667 return (MAX_OF_TYPE(parse_m2_type->builtin_card) - b) < a;
669 #endif /* FIXME */
671 /* Take care of parsing a number (anything that starts with a digit).
672 Set yylval and return the token type; update lexptr.
673 LEN is the number of characters in it. */
675 /*** Needs some error checking for the float case ***/
677 static int
678 parse_number (olen)
679 int olen;
681 char *p = lexptr;
682 LONGEST n = 0;
683 LONGEST prevn = 0;
684 int c,i,ischar=0;
685 int base = input_radix;
686 int len = olen;
687 int unsigned_p = number_sign == 1 ? 1 : 0;
689 if(p[len-1] == 'H')
691 base = 16;
692 len--;
694 else if(p[len-1] == 'C' || p[len-1] == 'B')
696 base = 8;
697 ischar = p[len-1] == 'C';
698 len--;
701 /* Scan the number */
702 for (c = 0; c < len; c++)
704 if (p[c] == '.' && base == 10)
706 /* It's a float since it contains a point. */
707 yylval.dval = atof (p);
708 lexptr += len;
709 return FLOAT;
711 if (p[c] == '.' && base != 10)
712 error("Floating point numbers must be base 10.");
713 if (base == 10 && (p[c] < '0' || p[c] > '9'))
714 error("Invalid digit \'%c\' in number.",p[c]);
717 while (len-- > 0)
719 c = *p++;
720 n *= base;
721 if( base == 8 && (c == '8' || c == '9'))
722 error("Invalid digit \'%c\' in octal number.",c);
723 if (c >= '0' && c <= '9')
724 i = c - '0';
725 else
727 if (base == 16 && c >= 'A' && c <= 'F')
728 i = c - 'A' + 10;
729 else
730 return ERROR;
732 n+=i;
733 if(i >= base)
734 return ERROR;
735 if(!unsigned_p && number_sign == 1 && (prevn >= n))
736 unsigned_p=1; /* Try something unsigned */
737 /* Don't do the range check if n==i and i==0, since that special
738 case will give an overflow error. */
739 if(RANGE_CHECK && n!=i && i)
741 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
742 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
743 range_error("Overflow on numeric constant.");
745 prevn=n;
748 lexptr = p;
749 if(*p == 'B' || *p == 'C' || *p == 'H')
750 lexptr++; /* Advance past B,C or H */
752 if (ischar)
754 yylval.ulval = n;
755 return CHAR;
757 else if ( unsigned_p && number_sign == 1)
759 yylval.ulval = n;
760 return UINT;
762 else if((unsigned_p && (n<0))) {
763 range_error("Overflow on numeric constant -- number too large.");
764 /* But, this can return if range_check == range_warn. */
766 yylval.lval = n;
767 return INT;
771 /* Some tokens */
773 static struct
775 char name[2];
776 int token;
777 } tokentab2[] =
779 { {'<', '>'}, NOTEQUAL },
780 { {':', '='}, ASSIGN },
781 { {'<', '='}, LEQ },
782 { {'>', '='}, GEQ },
783 { {':', ':'}, COLONCOLON },
787 /* Some specific keywords */
789 struct keyword {
790 char keyw[10];
791 int token;
794 static struct keyword keytab[] =
796 {"OR" , OROR },
797 {"IN", IN },/* Note space after IN */
798 {"AND", LOGICAL_AND},
799 {"ABS", ABS },
800 {"CHR", CHR },
801 {"DEC", DEC },
802 {"NOT", NOT },
803 {"DIV", DIV },
804 {"INC", INC },
805 {"MAX", MAX_FUNC },
806 {"MIN", MIN_FUNC },
807 {"MOD", MOD },
808 {"ODD", ODD },
809 {"CAP", CAP },
810 {"ORD", ORD },
811 {"VAL", VAL },
812 {"EXCL", EXCL },
813 {"HIGH", HIGH },
814 {"INCL", INCL },
815 {"SIZE", SIZE },
816 {"FLOAT", FLOAT_FUNC },
817 {"TRUNC", TRUNC },
818 {"TSIZE", SIZE },
822 /* Read one token, getting characters through lexptr. */
824 /* This is where we will check to make sure that the language and the operators used are
825 compatible */
827 static int
828 yylex ()
830 int c;
831 int namelen;
832 int i;
833 char *tokstart;
834 char quote;
836 retry:
838 prev_lexptr = lexptr;
840 tokstart = lexptr;
843 /* See if it is a special token of length 2 */
844 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
845 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
847 lexptr += 2;
848 return tokentab2[i].token;
851 switch (c = *tokstart)
853 case 0:
854 return 0;
856 case ' ':
857 case '\t':
858 case '\n':
859 lexptr++;
860 goto retry;
862 case '(':
863 paren_depth++;
864 lexptr++;
865 return c;
867 case ')':
868 if (paren_depth == 0)
869 return 0;
870 paren_depth--;
871 lexptr++;
872 return c;
874 case ',':
875 if (comma_terminates && paren_depth == 0)
876 return 0;
877 lexptr++;
878 return c;
880 case '.':
881 /* Might be a floating point number. */
882 if (lexptr[1] >= '0' && lexptr[1] <= '9')
883 break; /* Falls into number code. */
884 else
886 lexptr++;
887 return DOT;
890 /* These are character tokens that appear as-is in the YACC grammar */
891 case '+':
892 case '-':
893 case '*':
894 case '/':
895 case '^':
896 case '<':
897 case '>':
898 case '[':
899 case ']':
900 case '=':
901 case '{':
902 case '}':
903 case '#':
904 case '@':
905 case '~':
906 case '&':
907 lexptr++;
908 return c;
910 case '\'' :
911 case '"':
912 quote = c;
913 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
914 if (c == '\\')
916 c = tokstart[++namelen];
917 if (c >= '0' && c <= '9')
919 c = tokstart[++namelen];
920 if (c >= '0' && c <= '9')
921 c = tokstart[++namelen];
924 if(c != quote)
925 error("Unterminated string or character constant.");
926 yylval.sval.ptr = tokstart + 1;
927 yylval.sval.length = namelen - 1;
928 lexptr += namelen + 1;
930 if(namelen == 2) /* Single character */
932 yylval.ulval = tokstart[1];
933 return CHAR;
935 else
936 return STRING;
939 /* Is it a number? */
940 /* Note: We have already dealt with the case of the token '.'.
941 See case '.' above. */
942 if ((c >= '0' && c <= '9'))
944 /* It's a number. */
945 int got_dot = 0, got_e = 0;
946 char *p = tokstart;
947 int toktype;
949 for (++p ;; ++p)
951 if (!got_e && (*p == 'e' || *p == 'E'))
952 got_dot = got_e = 1;
953 else if (!got_dot && *p == '.')
954 got_dot = 1;
955 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
956 && (*p == '-' || *p == '+'))
957 /* This is the sign of the exponent, not the end of the
958 number. */
959 continue;
960 else if ((*p < '0' || *p > '9') &&
961 (*p < 'A' || *p > 'F') &&
962 (*p != 'H')) /* Modula-2 hexadecimal number */
963 break;
965 toktype = parse_number (p - tokstart);
966 if (toktype == ERROR)
968 char *err_copy = (char *) alloca (p - tokstart + 1);
970 memcpy (err_copy, tokstart, p - tokstart);
971 err_copy[p - tokstart] = 0;
972 error ("Invalid number \"%s\".", err_copy);
974 lexptr = p;
975 return toktype;
978 if (!(c == '_' || c == '$'
979 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
980 /* We must have come across a bad character (e.g. ';'). */
981 error ("Invalid character '%c' in expression.", c);
983 /* It's a name. See how long it is. */
984 namelen = 0;
985 for (c = tokstart[namelen];
986 (c == '_' || c == '$' || (c >= '0' && c <= '9')
987 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
988 c = tokstart[++namelen])
991 /* The token "if" terminates the expression and is NOT
992 removed from the input stream. */
993 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
995 return 0;
998 lexptr += namelen;
1000 /* Lookup special keywords */
1001 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
1002 if (namelen == strlen (keytab[i].keyw)
1003 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
1004 return keytab[i].token;
1006 yylval.sval.ptr = tokstart;
1007 yylval.sval.length = namelen;
1009 if (*tokstart == '$')
1011 write_dollar_variable (yylval.sval);
1012 return INTERNAL_VAR;
1015 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1016 functions. If this is not so, then ...
1017 Use token-type TYPENAME for symbols that happen to be defined
1018 currently as names of types; NAME for other symbols.
1019 The caller is not constrained to care about the distinction. */
1023 char *tmp = copy_name (yylval.sval);
1024 struct symbol *sym;
1026 if (lookup_partial_symtab (tmp))
1027 return BLOCKNAME;
1028 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0);
1029 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1030 return BLOCKNAME;
1031 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1032 return TYPENAME;
1034 if(sym)
1036 switch(SYMBOL_CLASS (sym))
1038 case LOC_STATIC:
1039 case LOC_REGISTER:
1040 case LOC_ARG:
1041 case LOC_REF_ARG:
1042 case LOC_REGPARM_ADDR:
1043 case LOC_LOCAL:
1044 case LOC_CONST:
1045 case LOC_CONST_BYTES:
1046 case LOC_OPTIMIZED_OUT:
1047 case LOC_COMPUTED:
1048 return NAME;
1050 case LOC_TYPEDEF:
1051 return TYPENAME;
1053 case LOC_BLOCK:
1054 return BLOCKNAME;
1056 case LOC_UNDEF:
1057 error("internal: Undefined class in m2lex()");
1059 case LOC_LABEL:
1060 case LOC_UNRESOLVED:
1061 error("internal: Unforseen case in m2lex()");
1063 default:
1064 error ("unhandled token in m2lex()");
1065 break;
1068 else
1070 /* Built-in BOOLEAN type. This is sort of a hack. */
1071 if (strncmp (tokstart, "TRUE", 4) == 0)
1073 yylval.ulval = 1;
1074 return M2_TRUE;
1076 else if (strncmp (tokstart, "FALSE", 5) == 0)
1078 yylval.ulval = 0;
1079 return M2_FALSE;
1083 /* Must be another type of name... */
1084 return NAME;
1088 #if 0 /* Unused */
1089 static char *
1090 make_qualname(mod,ident)
1091 char *mod, *ident;
1093 char *new = malloc(strlen(mod)+strlen(ident)+2);
1095 strcpy(new,mod);
1096 strcat(new,".");
1097 strcat(new,ident);
1098 return new;
1100 #endif /* 0 */
1102 void
1103 yyerror (msg)
1104 char *msg;
1106 if (prev_lexptr)
1107 lexptr = prev_lexptr;
1109 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);