2 * PARSE3.C - The expression parser
4 * (c)Copyright 1993-2014, Matthew Dillon, All Rights Reserved. See the
5 * COPYRIGHT file at the base of the distribution.
10 static Exp *pushOper(Parse *p, Exp **op, Exp **val,
11 int t, string_t id, int flags);
12 static Exp *pushValue(Parse *p, Exp **op, Exp **val, int t, string_t id);
13 static void pushValueExp(Exp **op, Exp **val, Exp *exp);
14 static int popExp(Exp **op, Exp **val);
17 ParseExp(Parse *p, int t, Exp **pexp)
23 int mode = 1; /* start in unary mode */
27 while ((t & TOKF_ERROR) == 0 && !done) {
33 case TOK_ASS: /* syntax error */
34 case TOK_ANDAND: /* syntax error */
35 case TOK_OROR: /* syntax error */
36 case TOK_DOT: /* syntax error */
37 case TOK_STRIND: /* syntax error */
38 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
40 case TOK_OPER: /* unary operator */
42 * We have to deal with pointer indirection.
43 * Regenerate the token using only the first
44 * character if it is a '*'
46 switch(p->p_Token.t_Ptr[0]) {
48 t = LexRedactToken(&p->p_Token, 1);
52 t = LexRedactToken(&p->p_Token, 1);
58 pushOper(p, &opStack, &valStack, t,
59 StrTableToken(&p->p_Token), EXF_UNARY);
60 t = LexToken(&p->p_Token);
62 case TOK_OBRACKET: /* syntax error */
63 case TOK_CBRACKET: /* syntax error */
64 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
66 case TOK_OPAREN: /* ( [id:]exp[, [id:]exp]* ) */
68 * Parenthesized expression: Either a compound
69 * expression (which might degenerate back
70 * into a normal expression in the resolver),
73 * (typeof(...)) is supported as a cast.
75 t2 = LexPeekToken(&p->p_Token);
76 if (t2 == TOK_TYPEOF) {
79 t = LexSkipToken(&p->p_Token,
81 assert(t == TOK_TYPEOF);
82 t = ParseExp(p, t, &save2);
83 t = LexSkipToken(&p->p_Token,
87 * This is nasty, but easy to parse
88 * so... allow selection on a cast
89 * as if it were a type.
92 tmp = pushValue(p, &opStack,
98 tmp = pushOper(p, &opStack,
105 tmp->ex_Type = save2->ex_Type;
106 tmp->ex_Flags |= save2->ex_Flags &
109 if (save2->ex_Flags & EXF_PARSE_TYPE)
113 } else if ((t2 & (TOKF_SCOPE_QUAL |
122 t = LexSkipToken(&p->p_Token,
125 t = ParseScopeQual(p, t, &scope);
126 t = ParseStorageQual(p, t, &sqflags);
127 if (scope.s_Flags & SCOPE_LVALUE)
131 t = ParseType(p, t, &type, sqflags);
132 t = ParseDeclType(p, t, &type,
137 t = LexSkipToken(&p->p_Token,
140 * This is nasty, but easy to parse
141 * so... allow selection on a cast
142 * as if it were a type.
145 tmp = pushValue(p, &opStack,
151 tmp = pushOper(p, &opStack,
158 tmp->ex_Flags |= EXF_PARSE_TYPE;
160 t = ParseParenthesizedExp(p, t,
162 pushValueExp(&opStack, &valStack, tmp);
166 case TOK_DSTRING: /* value */
167 case TOK_SSTRING: /* value */
168 case TOK_BSTRING: /* value */
169 case TOK_INTEGER: /* value */
170 case TOK_FLOAT: /* value */
171 pushValue(p, &opStack, &valStack,
172 t, StrTableToken(&p->p_Token));
173 t = LexToken(&p->p_Token);
180 * We can distinguish between expressions and
181 * types by identifying a scope or storage
182 * qualifier or a type-flagged identifier.
185 t = LexToken(&p->p_Token);
187 t2 = LexPeekToken(&p->p_Token);
191 if (t == TOK_OPAREN &&
192 ((t2 & (TOKF_SCOPE_QUAL |
202 t = LexSkipToken(&p->p_Token,
205 t = ParseScopeQual(p, t, &scope);
206 t = ParseStorageQual(p, t, &sqflags);
207 if (scope.s_Flags & SCOPE_LVALUE)
211 tmp = WrapExp(&p->p_Token, NULL, save);
212 t = ParseType(p, t, &type, sqflags);
213 t = ParseDeclType(p, t, &type, NULL,
218 tmp->ex_Flags |= EXF_PARSE_TYPE |
221 t = LexSkipToken(&p->p_Token,
223 t = ParseExp(p, t, &tmp);
225 tmp->ex_Flags |= EXF_UNARY;
226 tmp = WrapExp(&p->p_Token, tmp, save);
230 * XXX removed because this can cause dynamic
231 * type resolution at run-time to occur.
233 if (t == TOK_COMMA) {
235 * remove storage qualifiers
237 t = LexToken(&p->p_Token);
238 t = ParseStorageQual(p, t,
239 &tmp->ex_AuxFlags[0]);
241 if (t == TOK_COMMA) {
243 * add storage qualifiers
245 t = LexToken(&p->p_Token);
246 t = ParseStorageQual(p, t,
247 &tmp->ex_AuxFlags[1]);
251 tmp->ex_Flags |= EXF_UNARY;
252 pushValueExp(&opStack, &valStack, tmp);
253 t = LexSkipToken(&p->p_Token, TOK_CPAREN);
256 case TOK_ID: /* value: semantic id */
257 case TOK_CLASSID: /* value: semantic id */
258 pushValue(p, &opStack, &valStack,
259 t, StrTableToken(&p->p_Token));
260 t = LexToken(&p->p_Token);
265 * Identifies the procedure's argument space.
267 pushValue(p, &opStack, &valStack, t, NULL);
268 t = LexToken(&p->p_Token);
273 * Identifies the procedure's return storage.
275 pushValue(p, &opStack, &valStack, t, NULL);
276 t = LexToken(&p->p_Token);
279 case TOK_CPAREN: /* syntax error */
280 case TOK_CBRACE: /* syntax error */
281 case TOK_COMMA: /* syntax error */
282 case TOK_SEMI: /* syntax error */
283 case TOK_COLON: /* syntax error */
285 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
294 case TOK_ANDAND: /* binary operator */
295 case TOK_OROR: /* binary operator */
296 case TOK_DOT: /* binary operator */
297 case TOK_STRIND: /* binary operator */
298 case TOK_OPER: /* binary operator */
299 pushOper(p, &opStack, &valStack, t,
300 StrTableToken(&p->p_Token),
302 t = LexToken(&p->p_Token);
305 case TOK_OBRACKET: /* '[' array operator */
306 t = LexToken(&p->p_Token);
308 t = ParseExp(p, t, &tmp);
309 t = LexSkipToken(&p->p_Token, TOK_CBRACKET);
310 pushOper(p, &opStack, &valStack,
311 TOK_OBRACKET, NULL, EXF_BINARY);
312 pushValueExp(&opStack, &valStack, tmp);
314 case TOK_CBRACKET: /* ']' terminator */
319 * Procedure call. In order to avoid
320 * fubar((x, y)) confusion we do not allow
321 * ParseParenthesizedExp() to generate a
322 * compound exp encapsulation for comma
323 * delimited case. Instead we do it ourselves.
325 t = ParseParenthesizedExp(p, t, &tmp, 0);
326 tmp = ExpToCompoundExp(tmp, TOK_COMPOUND);
327 pushOper(p, &opStack, &valStack,
328 TOK_CALL, NULL, EXF_BINARY);
329 pushValueExp(&opStack, &valStack, tmp);
331 case TOK_ID: /* syntax error */
332 case TOK_CLASSID: /* syntax error */
333 t = LexError(&p->p_Token,
334 TOK_ERR_EXP_SYNTAX_MAYTYPE);
336 case TOK_DSTRING: /* syntax error */
337 case TOK_SSTRING: /* syntax error */
338 case TOK_BSTRING: /* syntax error */
339 case TOK_INTEGER: /* syntax error */
340 case TOK_FLOAT: /* syntax error */
341 case TOK_SIZEOF: /* syntax error */
342 case TOK_ARYSIZE: /* syntax error */
343 case TOK_TYPEOF: /* syntax error */
344 case TOK_SELF: /* syntax error */
345 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
347 case TOK_CPAREN: /* terminator */
348 case TOK_CBRACE: /* terminator */
349 case TOK_COMMA: /* terminator */
350 case TOK_SEMI: /* terminator */
351 case TOK_COLON: /* terminator */
355 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
361 if (popExp(&opStack, &valStack) < 0) {
362 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
365 if (valStack == NULL) {
366 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
367 } else if (valStack->ex_Next != NULL) {
368 t = LexError(&p->p_Token, TOK_ERR_EXP_SYNTAX);
369 FreeExp(valStack->ex_Next);
370 valStack->ex_Next = NULL;
377 * ParseParenthesizedExp() - parse a parenthesized expression.
379 * This will parse a parenthesized, possibly compound expression. docomp
380 * determines whether the returned expression should be collapsed into
381 * TOK_COMPOUND when there is more then one element, or whether this
382 * should be left up to the caller. Note that if you *REQUIRE* the
383 * result to be compound, you should pass docomp as 0 and call
384 * ExpToCompoundExp() unconditionally on the returned expression
385 * to avoid confusion.
387 * An empty expression returns a TOK_VOIDEXP exp node.
389 * Note: A single expressive element is considered to be compound if it
393 ParseParenthesizedExp(Parse *p, int t, Exp **pexp, int docomp)
397 t = LexSkipToken(&p->p_Token, TOK_OPAREN);
398 if (t != TOK_CPAREN) {
401 string_t argId = NULL;
402 if (t == TOK_ID || t == TOK_CLASSID/* XXX */) {
404 token_t t2 = p->p_Token;
406 if (LexToken(&t2) == TOK_COLON) {
407 argId = StrTableToken(&p->p_Token);
409 LexToken(&p->p_Token);
410 t = LexSkipToken(&p->p_Token,
414 t = ParseExp(p, t, pnext);
416 (*pnext)->ex_ArgId = argId;
417 pnext = &(*pnext)->ex_Next;
421 t = LexToken(&p->p_Token);
424 t = LexSkipToken(&p->p_Token, TOK_CPAREN);
426 if (docomp && (tmp->ex_ArgId || tmp->ex_Next))
427 tmp = ExpToCompoundExp(tmp, TOK_COMPOUND);
429 tmp = AllocExp(&p->p_Token);
430 tmp->ex_Token = TOK_VOIDEXP;
437 ParseNotExp(Parse *p, int t, Exp **pexp)
439 t = ParseExp(p, t, pexp);
440 if ((t & TOKF_ERROR) == 0)
441 *pexp = ExpToNotExp(*pexp);
446 pushOper(Parse *p, Exp **op, Exp **val, int t, string_t id, int flags)
450 int rtl = 0; /* right-to-left */
452 if (flags & EXF_BINARY) {
458 * If the operator contains '+' or '-' its precedence
459 * is EXPREC_PLUS. If the operator contains '<', '=',
460 * or '>' its precedence is EXPREC_COMPARE
461 * (overriding '+' or '-'). Otherwise the operator
464 if (strpbrk(id, "+-") != NULL)
466 if (strpbrk(id, "<=>") != NULL)
467 prec = EXPREC_COMPARE;
474 prec = EXPREC_CLOSURE;
480 prec = EXPREC_ANDAND;
490 dassert_parse(p, TOK_ERR_UNKNOWN, 0);
498 * Construct the expression
500 exp = AllocExp(&p->p_Token);
504 exp->ex_Flags |= flags;
507 * Pop-off any operators with a >= precedence. Handle
508 * special right-to-left ordering for assignments and unary
509 * operators simply by delaying the pop.
512 if ((*op)->ex_Prec > prec) {
516 if (rtl == 0 && (*op)->ex_Prec == prec) {
528 pushValue(Parse *p, Exp **op __unused, Exp **val, int t, string_t id)
530 Exp *exp = AllocExp(&p->p_Token);
540 pushValueExp(Exp **op __unused, Exp **val, Exp *exp)
547 popExp(Exp **op, Exp **val)
552 if ((exp = *op) != NULL) {
553 if (exp->ex_Flags & EXF_BINARY) {
555 Exp *lhs = (rhs) ? rhs->ex_Next : NULL;
558 * The expression parser will generate an error if
559 * this situation occurs so we can just return.
571 switch(exp->ex_Token) {
574 * Lhs may be a type, class, module,
577 * Rhs must be an identifier which we
578 * convert to TOK_STRUCT_ID.
580 dassert_exp(rhs, rhs->ex_Token ==
584 rhs->ex_Token = TOK_STRUCT_ID;
588 * Lhs must an lvalue or rvalue (not
589 * checked), Rhs must must be an
590 * identifier which we convert to
593 dassert_exp(rhs, rhs->ex_Token ==
597 rhs->ex_Token = TOK_STRUCT_ID;
601 * The rhs of a procedure call is a
602 * compound expression, denoted by
603 * TOK_COMPOUND. The compound
604 * expression is necessary to govern
605 * the context the arguments will
606 * eventually be stored in, so we do
607 * not collapse this even if its
610 dassert_exp(rhs, rhs->ex_Token ==
613 exp->ex_Rhs = rhs->ex_Lhs;