Debian Codegraph @

pTk/mTk/tclGeneric/tclParseExpr.c

raw symbols SCIP
1 /*
2 * tclParseExpr.c --
3 *
4 * This file contains procedures that parse Tcl expressions. They
5 * do so in a general-purpose fashion that can be used for many
6 * different purposes, including compilation, direct execution,
7 * code analysis, etc.
8 *
9 * Copyright (c) 1997 Sun Microsystems, Inc.
10 * Copyright (c) 1998-2000 by Scriptics Corporation.
11 * Contributions from Don Porter, NIST, 2002. (not subject to US copyright)
12 *
13 * See the file "license.terms" for information on usage and redistribution
14 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 *
16 * RCS: @(#) $Id: tclParseExpr.c,v 1.17 2003/02/16 01:36:32 msofer Exp $
17 */
18
19 #include "tclInt.h"
20
21 /*
22 * The stuff below is a bit of a hack so that this file can be used in
23 * environments that include no UNIX, i.e. no errno: just arrange to use
24 * the errno from tclExecute.c here.
25 */
26
27 #ifndef TCL_GENERIC_ONLY
28 #include "tclPort.h"
29 #else
30 #define NO_ERRNO_H
31 #endif
32
33 #ifdef NO_ERRNO_H
34 extern int errno; /* Use errno from tclExecute.c. */
35 #define ERANGE 34
36 #endif
37
38 /*
39 * Boolean variable that controls whether expression parse tracing
40 * is enabled.
41 */
42
43 #ifdef TCL_COMPILE_DEBUG
44 static int traceParseExpr = 0;
45 #endif /* TCL_COMPILE_DEBUG */
46
47 /*
48 * The ParseInfo structure holds state while parsing an expression.
49 * A pointer to an ParseInfo record is passed among the routines in
50 * this module.
51 */
52
53 typedef struct ParseInfo {
54 Tcl_Parse *parsePtr; /* Points to structure to fill in with
55 * information about the expression. */
56 int lexeme; /* Type of last lexeme scanned in expr.
57 * See below for definitions. Corresponds to
58 * size characters beginning at start. */
59 CONST char *start; /* First character in lexeme. */
60 int size; /* Number of bytes in lexeme. */
61 CONST char *next; /* Position of the next character to be
62 * scanned in the expression string. */
63 CONST char *prevEnd; /* Points to the character just after the
64 * last one in the previous lexeme. Used to
65 * compute size of subexpression tokens. */
66 CONST char *originalExpr; /* Points to the start of the expression
67 * originally passed to Tcl_ParseExpr. */
68 CONST char *lastChar; /* Points just after last byte of expr. */
69 } ParseInfo;
70
71 /*
72 * Definitions of the different lexemes that appear in expressions. The
73 * order of these must match the corresponding entries in the
74 * operatorStrings array below.
75 *
76 * Basic lexemes:
77 */
78
79 #define LITERAL 0
80 #define FUNC_NAME 1
81 #define OPEN_BRACKET 2
82 #define OPEN_BRACE 3
83 #define OPEN_PAREN 4
84 #define CLOSE_PAREN 5
85 #define DOLLAR 6
86 #define QUOTE 7
87 #define COMMA 8
88 #define END 9
89 #define UNKNOWN 10
90 #define UNKNOWN_CHAR 11
91
92 /*
93 * Binary numeric operators:
94 */
95
96 #define MULT 12
97 #define DIVIDE 13
98 #define MOD 14
99 #define PLUS 15
100 #define MINUS 16
101 #define LEFT_SHIFT 17
102 #define RIGHT_SHIFT 18
103 #define LESS 19
104 #define GREATER 20
105 #define LEQ 21
106 #define GEQ 22
107 #define EQUAL 23
108 #define NEQ 24
109 #define BIT_AND 25
110 #define BIT_XOR 26
111 #define BIT_OR 27
112 #define AND 28
113 #define OR 29
114 #define QUESTY 30
115 #define COLON 31
116
117 /*
118 * Unary operators. Unary minus and plus are represented by the (binary)
119 * lexemes MINUS and PLUS.
120 */
121
122 #define NOT 32
123 #define BIT_NOT 33
124
125 /*
126 * Binary string operators:
127 */
128
129 #define STREQ 34
130 #define STRNEQ 35
131
132 /*
133 * Mapping from lexemes to strings; used for debugging messages. These
134 * entries must match the order and number of the lexeme definitions above.
135 */
136
137 static char *lexemeStrings[] = {
138 "LITERAL", "FUNCNAME",
139 "[", "{", "(", ")", "$", "\"", ",", "END", "UNKNOWN", "UNKNOWN_CHAR",
140 "*", "/", "%", "+", "-",
141 "<<", ">>", "<", ">", "<=", ">=", "==", "!=",
142 "&", "^", "|", "&&", "||", "?", ":",
143 "!", "~", "eq", "ne",
144 };
145
146 /*
147 * Declarations for local procedures to this file:
148 */
149
150 static int GetLexeme _ANSI_ARGS_((ParseInfo *infoPtr));
151 static void LogSyntaxError _ANSI_ARGS_((ParseInfo *infoPtr,
152 CONST char *extraInfo));
153 static int ParseAddExpr _ANSI_ARGS_((ParseInfo *infoPtr));
154 static int ParseBitAndExpr _ANSI_ARGS_((ParseInfo *infoPtr));
155 static int ParseBitOrExpr _ANSI_ARGS_((ParseInfo *infoPtr));
156 static int ParseBitXorExpr _ANSI_ARGS_((ParseInfo *infoPtr));
157 static int ParseCondExpr _ANSI_ARGS_((ParseInfo *infoPtr));
158 static int ParseEqualityExpr _ANSI_ARGS_((ParseInfo *infoPtr));
159 static int ParseLandExpr _ANSI_ARGS_((ParseInfo *infoPtr));
160 static int ParseLorExpr _ANSI_ARGS_((ParseInfo *infoPtr));
161 static int ParseMaxDoubleLength _ANSI_ARGS_((CONST char *string,
162 CONST char *end));
163 static int ParseMultiplyExpr _ANSI_ARGS_((ParseInfo *infoPtr));
164 static int ParsePrimaryExpr _ANSI_ARGS_((ParseInfo *infoPtr));
165 static int ParseRelationalExpr _ANSI_ARGS_((ParseInfo *infoPtr));
166 static int ParseShiftExpr _ANSI_ARGS_((ParseInfo *infoPtr));
167 static int ParseUnaryExpr _ANSI_ARGS_((ParseInfo *infoPtr));
168 static void PrependSubExprTokens _ANSI_ARGS_((CONST char *op,
169 int opBytes, CONST char *src, int srcBytes,
170 int firstIndex, ParseInfo *infoPtr));
171
172 /*
173 * Macro used to debug the execution of the recursive descent parser used
174 * to parse expressions.
175 */
176
177 #ifdef TCL_COMPILE_DEBUG
178 #define HERE(production, level) \
179 if (traceParseExpr) { \
180 fprintf(stderr, "%*s%s: lexeme=%s, next=\"%.20s\"\n", \
181 (level), " ", (production), \
182 lexemeStrings[infoPtr->lexeme], infoPtr->next); \
183 }
184 #else
185 #define HERE(production, level)
186 #endif /* TCL_COMPILE_DEBUG */
187
188 /*
189 *----------------------------------------------------------------------
190 *
191 * Tcl_ParseExpr --
192 *
193 * Given a string, this procedure parses the first Tcl expression
194 * in the string and returns information about the structure of
195 * the expression. This procedure is the top-level interface to the
196 * the expression parsing module. No more that numBytes bytes will
197 * be scanned.
198 *
199 * Results:
200 * The return value is TCL_OK if the command was parsed successfully
201 * and TCL_ERROR otherwise. If an error occurs and interp isn't NULL
202 * then an error message is left in its result. On a successful return,
203 * parsePtr is filled in with information about the expression that
204 * was parsed.
205 *
206 * Side effects:
207 * If there is insufficient space in parsePtr to hold all the
208 * information about the expression, then additional space is
209 * malloc-ed. If the procedure returns TCL_OK then the caller must
210 * eventually invoke Tcl_FreeParse to release any additional space
211 * that was allocated.
212 *
213 *----------------------------------------------------------------------
214 */
215
216 int
217 Tcl_ParseExpr(interp, string, numBytes, parsePtr)
218 Tcl_Interp *interp; /* Used for error reporting. */
219 CONST char *string; /* The source string to parse. */
220 int numBytes; /* Number of bytes in string. If < 0, the
221 * string consists of all bytes up to the
222 * first null character. */
223 Tcl_Parse *parsePtr; /* Structure to fill with information about
224 * the parsed expression; any previous
225 * information in the structure is
226 * ignored. */
227 {
228 ParseInfo info;
229 int code;
230
231 if (numBytes < 0) {
232 numBytes = (string? strlen(string) : 0);
233 }
234 #ifdef TCL_COMPILE_DEBUG
235 if (traceParseExpr) {
236 fprintf(stderr, "Tcl_ParseExpr: string=\"%.*s\"\n",
237 numBytes, string);
238 }
239 #endif /* TCL_COMPILE_DEBUG */
240
241 parsePtr->commentStart = NULL;
242 parsePtr->commentSize = 0;
243 parsePtr->commandStart = NULL;
244 parsePtr->commandSize = 0;
245 parsePtr->numWords = 0;
246 parsePtr->tokenPtr = parsePtr->staticTokens;
247 parsePtr->numTokens = 0;
248 parsePtr->tokensAvailable = NUM_STATIC_TOKENS;
249 parsePtr->string = string;
250 parsePtr->end = (string + numBytes);
251 parsePtr->interp = interp;
252 parsePtr->term = string;
253 parsePtr->incomplete = 0;
254
255 /*
256 * Initialize the ParseInfo structure that holds state while parsing
257 * the expression.
258 */
259
260 info.parsePtr = parsePtr;
261 info.lexeme = UNKNOWN;
262 info.start = NULL;
263 info.size = 0;
264 info.next = string;
265 info.prevEnd = string;
266 info.originalExpr = string;
267 info.lastChar = (string + numBytes); /* just after last char of expr */
268
269 /*
270 * Get the first lexeme then parse the expression.
271 */
272
273 code = GetLexeme(&info);
274 if (code != TCL_OK) {
275 goto error;
276 }
277 code = ParseCondExpr(&info);
278 if (code != TCL_OK) {
279 goto error;
280 }
281 if (info.lexeme != END) {
282 LogSyntaxError(&info, "extra tokens at end of expression");
283 goto error;
284 }
285 return TCL_OK;
286
287 error:
288 if (parsePtr->tokenPtr != parsePtr->staticTokens) {
289 ckfree((char *) parsePtr->tokenPtr);
290 }
291 return TCL_ERROR;
292 }
293
294 /*
295 *----------------------------------------------------------------------
296 *
297 * ParseCondExpr --
298 *
299 * This procedure parses a Tcl conditional expression:
300 * condExpr ::= lorExpr ['?' condExpr ':' condExpr]
301 *
302 * Note that this is the topmost recursive-descent parsing routine used
303 * by Tcl_ParseExpr to parse expressions. This avoids an extra procedure
304 * call since such a procedure would only return the result of calling
305 * ParseCondExpr. Other recursive-descent procedures that need to parse
306 * complete expressions also call ParseCondExpr.
307 *
308 * Results:
309 * The return value is TCL_OK on a successful parse and TCL_ERROR
310 * on failure. If TCL_ERROR is returned, then the interpreter's result
311 * contains an error message.
312 *
313 * Side effects:
314 * If there is insufficient space in parsePtr to hold all the
315 * information about the subexpression, then additional space is
316 * malloc-ed.
317 *
318 *----------------------------------------------------------------------
319 */
320
321 static int
322 ParseCondExpr(infoPtr)
323 ParseInfo *infoPtr; /* Holds the parse state for the
324 * expression being parsed. */
325 {
326 Tcl_Parse *parsePtr = infoPtr->parsePtr;
327 Tcl_Token *tokenPtr, *firstTokenPtr, *condTokenPtr;
328 int firstIndex, numToMove, code;
329 CONST char *srcStart;
330
331 HERE("condExpr", 1);
332 srcStart = infoPtr->start;
333 firstIndex = parsePtr->numTokens;
334
335 code = ParseLorExpr(infoPtr);
336 if (code != TCL_OK) {
337 return code;
338 }
339
340 if (infoPtr->lexeme == QUESTY) {
341 /*
342 * Emit two tokens: one TCL_TOKEN_SUB_EXPR token for the entire
343 * conditional expression, and a TCL_TOKEN_OPERATOR token for
344 * the "?" operator. Note that these two tokens must be inserted
345 * before the LOR operand tokens generated above.
346 */
347
348 if ((parsePtr->numTokens + 1) >= parsePtr->tokensAvailable) {
349 TclExpandTokenArray(parsePtr);
350 }
351 firstTokenPtr = &parsePtr->tokenPtr[firstIndex];
352 tokenPtr = (firstTokenPtr + 2);
353 numToMove = (parsePtr->numTokens - firstIndex);
354 memmove((VOID *) tokenPtr, (VOID *) firstTokenPtr,
355 (size_t) (numToMove * sizeof(Tcl_Token)));
356 parsePtr->numTokens += 2;
357
358 tokenPtr = firstTokenPtr;
359 tokenPtr->type = TCL_TOKEN_SUB_EXPR;
360 tokenPtr->start = srcStart;
361
362 tokenPtr++;
363 tokenPtr->type = TCL_TOKEN_OPERATOR;
364 tokenPtr->start = infoPtr->start;
365 tokenPtr->size = 1;
366 tokenPtr->numComponents = 0;
367
368 /*
369 * Skip over the '?'.
370 */
371
372 code = GetLexeme(infoPtr);
373 if (code != TCL_OK) {
374 return code;
375 }
376
377 /*
378 * Parse the "then" expression.
379 */
380
381 code = ParseCondExpr(infoPtr);
382 if (code != TCL_OK) {
383 return code;
384 }
385 if (infoPtr->lexeme != COLON) {
386 LogSyntaxError(infoPtr, "missing colon from ternary conditional");
387 return TCL_ERROR;
388 }
389 code = GetLexeme(infoPtr); /* skip over the ':' */
390 if (code != TCL_OK) {
391 return code;
392 }
393
394 /*
395 * Parse the "else" expression.
396 */
397
398 code = ParseCondExpr(infoPtr);
399 if (code != TCL_OK) {
400 return code;
401 }
402
403 /*
404 * Now set the size-related fields in the '?' subexpression token.
405 */
406
407 condTokenPtr = &parsePtr->tokenPtr[firstIndex];
408 condTokenPtr->size = (infoPtr->prevEnd - srcStart);
409 condTokenPtr->numComponents = parsePtr->numTokens - (firstIndex+1);
410 }
411 return TCL_OK;
412 }
413
414 /*
415 *----------------------------------------------------------------------
416 *
417 * ParseLorExpr --
418 *
419 * This procedure parses a Tcl logical or expression:
420 * lorExpr ::= landExpr {'||' landExpr}
421 *
422 * Results:
423 * The return value is TCL_OK on a successful parse and TCL_ERROR
424 * on failure. If TCL_ERROR is returned, then the interpreter's result
425 * contains an error message.
426 *
427 * Side effects:
428 * If there is insufficient space in parsePtr to hold all the
429 * information about the subexpression, then additional space is
430 * malloc-ed.
431 *
432 *----------------------------------------------------------------------
433 */
434
435 static int
436 ParseLorExpr(infoPtr)
437 ParseInfo *infoPtr; /* Holds the parse state for the
438 * expression being parsed. */
439 {
440 Tcl_Parse *parsePtr = infoPtr->parsePtr;
441 int firstIndex, code;
442 CONST char *srcStart, *operator;
443
444 HERE("lorExpr", 2);
445 srcStart = infoPtr->start;
446 firstIndex = parsePtr->numTokens;
447
448 code = ParseLandExpr(infoPtr);
449 if (code != TCL_OK) {
450 return code;
451 }
452
453 while (infoPtr->lexeme == OR) {
454 operator = infoPtr->start;
455 code = GetLexeme(infoPtr); /* skip over the '||' */
456 if (code != TCL_OK) {
457 return code;
458 }
459 code = ParseLandExpr(infoPtr);
460 if (code != TCL_OK) {
461 return code;
462 }
463
464 /*
465 * Generate tokens for the LOR subexpression and the '||' operator.
466 */
467
468 PrependSubExprTokens(operator, 2, srcStart,
469 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
470 }
471 return TCL_OK;
472 }
473
474 /*
475 *----------------------------------------------------------------------
476 *
477 * ParseLandExpr --
478 *
479 * This procedure parses a Tcl logical and expression:
480 * landExpr ::= bitOrExpr {'&&' bitOrExpr}
481 *
482 * Results:
483 * The return value is TCL_OK on a successful parse and TCL_ERROR
484 * on failure. If TCL_ERROR is returned, then the interpreter's result
485 * contains an error message.
486 *
487 * Side effects:
488 * If there is insufficient space in parsePtr to hold all the
489 * information about the subexpression, then additional space is
490 * malloc-ed.
491 *
492 *----------------------------------------------------------------------
493 */
494
495 static int
496 ParseLandExpr(infoPtr)
497 ParseInfo *infoPtr; /* Holds the parse state for the
498 * expression being parsed. */
499 {
500 Tcl_Parse *parsePtr = infoPtr->parsePtr;
501 int firstIndex, code;
502 CONST char *srcStart, *operator;
503
504 HERE("landExpr", 3);
505 srcStart = infoPtr->start;
506 firstIndex = parsePtr->numTokens;
507
508 code = ParseBitOrExpr(infoPtr);
509 if (code != TCL_OK) {
510 return code;
511 }
512
513 while (infoPtr->lexeme == AND) {
514 operator = infoPtr->start;
515 code = GetLexeme(infoPtr); /* skip over the '&&' */
516 if (code != TCL_OK) {
517 return code;
518 }
519 code = ParseBitOrExpr(infoPtr);
520 if (code != TCL_OK) {
521 return code;
522 }
523
524 /*
525 * Generate tokens for the LAND subexpression and the '&&' operator.
526 */
527
528 PrependSubExprTokens(operator, 2, srcStart,
529 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
530 }
531 return TCL_OK;
532 }
533
534 /*
535 *----------------------------------------------------------------------
536 *
537 * ParseBitOrExpr --
538 *
539 * This procedure parses a Tcl bitwise or expression:
540 * bitOrExpr ::= bitXorExpr {'|' bitXorExpr}
541 *
542 * Results:
543 * The return value is TCL_OK on a successful parse and TCL_ERROR
544 * on failure. If TCL_ERROR is returned, then the interpreter's result
545 * contains an error message.
546 *
547 * Side effects:
548 * If there is insufficient space in parsePtr to hold all the
549 * information about the subexpression, then additional space is
550 * malloc-ed.
551 *
552 *----------------------------------------------------------------------
553 */
554
555 static int
556 ParseBitOrExpr(infoPtr)
557 ParseInfo *infoPtr; /* Holds the parse state for the
558 * expression being parsed. */
559 {
560 Tcl_Parse *parsePtr = infoPtr->parsePtr;
561 int firstIndex, code;
562 CONST char *srcStart, *operator;
563
564 HERE("bitOrExpr", 4);
565 srcStart = infoPtr->start;
566 firstIndex = parsePtr->numTokens;
567
568 code = ParseBitXorExpr(infoPtr);
569 if (code != TCL_OK) {
570 return code;
571 }
572
573 while (infoPtr->lexeme == BIT_OR) {
574 operator = infoPtr->start;
575 code = GetLexeme(infoPtr); /* skip over the '|' */
576 if (code != TCL_OK) {
577 return code;
578 }
579
580 code = ParseBitXorExpr(infoPtr);
581 if (code != TCL_OK) {
582 return code;
583 }
584
585 /*
586 * Generate tokens for the BITOR subexpression and the '|' operator.
587 */
588
589 PrependSubExprTokens(operator, 1, srcStart,
590 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
591 }
592 return TCL_OK;
593 }
594
595 /*
596 *----------------------------------------------------------------------
597 *
598 * ParseBitXorExpr --
599 *
600 * This procedure parses a Tcl bitwise exclusive or expression:
601 * bitXorExpr ::= bitAndExpr {'^' bitAndExpr}
602 *
603 * Results:
604 * The return value is TCL_OK on a successful parse and TCL_ERROR
605 * on failure. If TCL_ERROR is returned, then the interpreter's result
606 * contains an error message.
607 *
608 * Side effects:
609 * If there is insufficient space in parsePtr to hold all the
610 * information about the subexpression, then additional space is
611 * malloc-ed.
612 *
613 *----------------------------------------------------------------------
614 */
615
616 static int
617 ParseBitXorExpr(infoPtr)
618 ParseInfo *infoPtr; /* Holds the parse state for the
619 * expression being parsed. */
620 {
621 Tcl_Parse *parsePtr = infoPtr->parsePtr;
622 int firstIndex, code;
623 CONST char *srcStart, *operator;
624
625 HERE("bitXorExpr", 5);
626 srcStart = infoPtr->start;
627 firstIndex = parsePtr->numTokens;
628
629 code = ParseBitAndExpr(infoPtr);
630 if (code != TCL_OK) {
631 return code;
632 }
633
634 while (infoPtr->lexeme == BIT_XOR) {
635 operator = infoPtr->start;
636 code = GetLexeme(infoPtr); /* skip over the '^' */
637 if (code != TCL_OK) {
638 return code;
639 }
640
641 code = ParseBitAndExpr(infoPtr);
642 if (code != TCL_OK) {
643 return code;
644 }
645
646 /*
647 * Generate tokens for the XOR subexpression and the '^' operator.
648 */
649
650 PrependSubExprTokens(operator, 1, srcStart,
651 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
652 }
653 return TCL_OK;
654 }
655
656 /*
657 *----------------------------------------------------------------------
658 *
659 * ParseBitAndExpr --
660 *
661 * This procedure parses a Tcl bitwise and expression:
662 * bitAndExpr ::= equalityExpr {'&' equalityExpr}
663 *
664 * Results:
665 * The return value is TCL_OK on a successful parse and TCL_ERROR
666 * on failure. If TCL_ERROR is returned, then the interpreter's result
667 * contains an error message.
668 *
669 * Side effects:
670 * If there is insufficient space in parsePtr to hold all the
671 * information about the subexpression, then additional space is
672 * malloc-ed.
673 *
674 *----------------------------------------------------------------------
675 */
676
677 static int
678 ParseBitAndExpr(infoPtr)
679 ParseInfo *infoPtr; /* Holds the parse state for the
680 * expression being parsed. */
681 {
682 Tcl_Parse *parsePtr = infoPtr->parsePtr;
683 int firstIndex, code;
684 CONST char *srcStart, *operator;
685
686 HERE("bitAndExpr", 6);
687 srcStart = infoPtr->start;
688 firstIndex = parsePtr->numTokens;
689
690 code = ParseEqualityExpr(infoPtr);
691 if (code != TCL_OK) {
692 return code;
693 }
694
695 while (infoPtr->lexeme == BIT_AND) {
696 operator = infoPtr->start;
697 code = GetLexeme(infoPtr); /* skip over the '&' */
698 if (code != TCL_OK) {
699 return code;
700 }
701 code = ParseEqualityExpr(infoPtr);
702 if (code != TCL_OK) {
703 return code;
704 }
705
706 /*
707 * Generate tokens for the BITAND subexpression and '&' operator.
708 */
709
710 PrependSubExprTokens(operator, 1, srcStart,
711 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
712 }
713 return TCL_OK;
714 }
715
716 /*
717 *----------------------------------------------------------------------
718 *
719 * ParseEqualityExpr --
720 *
721 * This procedure parses a Tcl equality (inequality) expression:
722 * equalityExpr ::= relationalExpr
723 * {('==' | '!=' | 'ne' | 'eq') relationalExpr}
724 *
725 * Results:
726 * The return value is TCL_OK on a successful parse and TCL_ERROR
727 * on failure. If TCL_ERROR is returned, then the interpreter's result
728 * contains an error message.
729 *
730 * Side effects:
731 * If there is insufficient space in parsePtr to hold all the
732 * information about the subexpression, then additional space is
733 * malloc-ed.
734 *
735 *----------------------------------------------------------------------
736 */
737
738 static int
739 ParseEqualityExpr(infoPtr)
740 ParseInfo *infoPtr; /* Holds the parse state for the
741 * expression being parsed. */
742 {
743 Tcl_Parse *parsePtr = infoPtr->parsePtr;
744 int firstIndex, lexeme, code;
745 CONST char *srcStart, *operator;
746
747 HERE("equalityExpr", 7);
748 srcStart = infoPtr->start;
749 firstIndex = parsePtr->numTokens;
750
751 code = ParseRelationalExpr(infoPtr);
752 if (code != TCL_OK) {
753 return code;
754 }
755
756 lexeme = infoPtr->lexeme;
757 while ((lexeme == EQUAL) || (lexeme == NEQ)
758 || (lexeme == STREQ) || (lexeme == STRNEQ)) {
759 operator = infoPtr->start;
760 code = GetLexeme(infoPtr); /* skip over ==, !=, 'eq' or 'ne' */
761 if (code != TCL_OK) {
762 return code;
763 }
764 code = ParseRelationalExpr(infoPtr);
765 if (code != TCL_OK) {
766 return code;
767 }
768
769 /*
770 * Generate tokens for the subexpression and '==', '!=', 'eq' or 'ne'
771 * operator.
772 */
773
774 PrependSubExprTokens(operator, 2, srcStart,
775 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
776 lexeme = infoPtr->lexeme;
777 }
778 return TCL_OK;
779 }
780
781 /*
782 *----------------------------------------------------------------------
783 *
784 * ParseRelationalExpr --
785 *
786 * This procedure parses a Tcl relational expression:
787 * relationalExpr ::= shiftExpr {('<' | '>' | '<=' | '>=') shiftExpr}
788 *
789 * Results:
790 * The return value is TCL_OK on a successful parse and TCL_ERROR
791 * on failure. If TCL_ERROR is returned, then the interpreter's result
792 * contains an error message.
793 *
794 * Side effects:
795 * If there is insufficient space in parsePtr to hold all the
796 * information about the subexpression, then additional space is
797 * malloc-ed.
798 *
799 *----------------------------------------------------------------------
800 */
801
802 static int
803 ParseRelationalExpr(infoPtr)
804 ParseInfo *infoPtr; /* Holds the parse state for the
805 * expression being parsed. */
806 {
807 Tcl_Parse *parsePtr = infoPtr->parsePtr;
808 int firstIndex, lexeme, operatorSize, code;
809 CONST char *srcStart, *operator;
810
811 HERE("relationalExpr", 8);
812 srcStart = infoPtr->start;
813 firstIndex = parsePtr->numTokens;
814
815 code = ParseShiftExpr(infoPtr);
816 if (code != TCL_OK) {
817 return code;
818 }
819
820 lexeme = infoPtr->lexeme;
821 while ((lexeme == LESS) || (lexeme == GREATER) || (lexeme == LEQ)
822 || (lexeme == GEQ)) {
823 operator = infoPtr->start;
824 if ((lexeme == LEQ) || (lexeme == GEQ)) {
825 operatorSize = 2;
826 } else {
827 operatorSize = 1;
828 }
829 code = GetLexeme(infoPtr); /* skip over the operator */
830 if (code != TCL_OK) {
831 return code;
832 }
833 code = ParseShiftExpr(infoPtr);
834 if (code != TCL_OK) {
835 return code;
836 }
837
838 /*
839 * Generate tokens for the subexpression and the operator.
840 */
841
842 PrependSubExprTokens(operator, operatorSize, srcStart,
843 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
844 lexeme = infoPtr->lexeme;
845 }
846 return TCL_OK;
847 }
848
849 /*
850 *----------------------------------------------------------------------
851 *
852 * ParseShiftExpr --
853 *
854 * This procedure parses a Tcl shift expression:
855 * shiftExpr ::= addExpr {('<<' | '>>') addExpr}
856 *
857 * Results:
858 * The return value is TCL_OK on a successful parse and TCL_ERROR
859 * on failure. If TCL_ERROR is returned, then the interpreter's result
860 * contains an error message.
861 *
862 * Side effects:
863 * If there is insufficient space in parsePtr to hold all the
864 * information about the subexpression, then additional space is
865 * malloc-ed.
866 *
867 *----------------------------------------------------------------------
868 */
869
870 static int
871 ParseShiftExpr(infoPtr)
872 ParseInfo *infoPtr; /* Holds the parse state for the
873 * expression being parsed. */
874 {
875 Tcl_Parse *parsePtr = infoPtr->parsePtr;
876 int firstIndex, lexeme, code;
877 CONST char *srcStart, *operator;
878
879 HERE("shiftExpr", 9);
880 srcStart = infoPtr->start;
881 firstIndex = parsePtr->numTokens;
882
883 code = ParseAddExpr(infoPtr);
884 if (code != TCL_OK) {
885 return code;
886 }
887
888 lexeme = infoPtr->lexeme;
889 while ((lexeme == LEFT_SHIFT) || (lexeme == RIGHT_SHIFT)) {
890 operator = infoPtr->start;
891 code = GetLexeme(infoPtr); /* skip over << or >> */
892 if (code != TCL_OK) {
893 return code;
894 }
895 code = ParseAddExpr(infoPtr);
896 if (code != TCL_OK) {
897 return code;
898 }
899
900 /*
901 * Generate tokens for the subexpression and '<<' or '>>' operator.
902 */
903
904 PrependSubExprTokens(operator, 2, srcStart,
905 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
906 lexeme = infoPtr->lexeme;
907 }
908 return TCL_OK;
909 }
910
911 /*
912 *----------------------------------------------------------------------
913 *
914 * ParseAddExpr --
915 *
916 * This procedure parses a Tcl addition expression:
917 * addExpr ::= multiplyExpr {('+' | '-') multiplyExpr}
918 *
919 * Results:
920 * The return value is TCL_OK on a successful parse and TCL_ERROR
921 * on failure. If TCL_ERROR is returned, then the interpreter's result
922 * contains an error message.
923 *
924 * Side effects:
925 * If there is insufficient space in parsePtr to hold all the
926 * information about the subexpression, then additional space is
927 * malloc-ed.
928 *
929 *----------------------------------------------------------------------
930 */
931
932 static int
933 ParseAddExpr(infoPtr)
934 ParseInfo *infoPtr; /* Holds the parse state for the
935 * expression being parsed. */
936 {
937 Tcl_Parse *parsePtr = infoPtr->parsePtr;
938 int firstIndex, lexeme, code;
939 CONST char *srcStart, *operator;
940
941 HERE("addExpr", 10);
942 srcStart = infoPtr->start;
943 firstIndex = parsePtr->numTokens;
944
945 code = ParseMultiplyExpr(infoPtr);
946 if (code != TCL_OK) {
947 return code;
948 }
949
950 lexeme = infoPtr->lexeme;
951 while ((lexeme == PLUS) || (lexeme == MINUS)) {
952 operator = infoPtr->start;
953 code = GetLexeme(infoPtr); /* skip over + or - */
954 if (code != TCL_OK) {
955 return code;
956 }
957 code = ParseMultiplyExpr(infoPtr);
958 if (code != TCL_OK) {
959 return code;
960 }
961
962 /*
963 * Generate tokens for the subexpression and '+' or '-' operator.
964 */
965
966 PrependSubExprTokens(operator, 1, srcStart,
967 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
968 lexeme = infoPtr->lexeme;
969 }
970 return TCL_OK;
971 }
972
973 /*
974 *----------------------------------------------------------------------
975 *
976 * ParseMultiplyExpr --
977 *
978 * This procedure parses a Tcl multiply expression:
979 * multiplyExpr ::= unaryExpr {('*' | '/' | '%') unaryExpr}
980 *
981 * Results:
982 * The return value is TCL_OK on a successful parse and TCL_ERROR
983 * on failure. If TCL_ERROR is returned, then the interpreter's result
984 * contains an error message.
985 *
986 * Side effects:
987 * If there is insufficient space in parsePtr to hold all the
988 * information about the subexpression, then additional space is
989 * malloc-ed.
990 *
991 *----------------------------------------------------------------------
992 */
993
994 static int
995 ParseMultiplyExpr(infoPtr)
996 ParseInfo *infoPtr; /* Holds the parse state for the
997 * expression being parsed. */
998 {
999 Tcl_Parse *parsePtr = infoPtr->parsePtr;
1000 int firstIndex, lexeme, code;
1001 CONST char *srcStart, *operator;
1002
1003 HERE("multiplyExpr", 11);
1004 srcStart = infoPtr->start;
1005 firstIndex = parsePtr->numTokens;
1006
1007 code = ParseUnaryExpr(infoPtr);
1008 if (code != TCL_OK) {
1009 return code;
1010 }
1011
1012 lexeme = infoPtr->lexeme;
1013 while ((lexeme == MULT) || (lexeme == DIVIDE) || (lexeme == MOD)) {
1014 operator = infoPtr->start;
1015 code = GetLexeme(infoPtr); /* skip over * or / or % */
1016 if (code != TCL_OK) {
1017 return code;
1018 }
1019 code = ParseUnaryExpr(infoPtr);
1020 if (code != TCL_OK) {
1021 return code;
1022 }
1023
1024 /*
1025 * Generate tokens for the subexpression and * or / or % operator.
1026 */
1027
1028 PrependSubExprTokens(operator, 1, srcStart,
1029 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
1030 lexeme = infoPtr->lexeme;
1031 }
1032 return TCL_OK;
1033 }
1034
1035 /*
1036 *----------------------------------------------------------------------
1037 *
1038 * ParseUnaryExpr --
1039 *
1040 * This procedure parses a Tcl unary expression:
1041 * unaryExpr ::= ('+' | '-' | '~' | '!') unaryExpr | primaryExpr
1042 *
1043 * Results:
1044 * The return value is TCL_OK on a successful parse and TCL_ERROR
1045 * on failure. If TCL_ERROR is returned, then the interpreter's result
1046 * contains an error message.
1047 *
1048 * Side effects:
1049 * If there is insufficient space in parsePtr to hold all the
1050 * information about the subexpression, then additional space is
1051 * malloc-ed.
1052 *
1053 *----------------------------------------------------------------------
1054 */
1055
1056 static int
1057 ParseUnaryExpr(infoPtr)
1058 ParseInfo *infoPtr; /* Holds the parse state for the
1059 * expression being parsed. */
1060 {
1061 Tcl_Parse *parsePtr = infoPtr->parsePtr;
1062 int firstIndex, lexeme, code;
1063 CONST char *srcStart, *operator;
1064
1065 HERE("unaryExpr", 12);
1066 srcStart = infoPtr->start;
1067 firstIndex = parsePtr->numTokens;
1068
1069 lexeme = infoPtr->lexeme;
1070 if ((lexeme == PLUS) || (lexeme == MINUS) || (lexeme == BIT_NOT)
1071 || (lexeme == NOT)) {
1072 operator = infoPtr->start;
1073 code = GetLexeme(infoPtr); /* skip over the unary operator */
1074 if (code != TCL_OK) {
1075 return code;
1076 }
1077 code = ParseUnaryExpr(infoPtr);
1078 if (code != TCL_OK) {
1079 return code;
1080 }
1081
1082 /*
1083 * Generate tokens for the subexpression and the operator.
1084 */
1085
1086 PrependSubExprTokens(operator, 1, srcStart,
1087 (infoPtr->prevEnd - srcStart), firstIndex, infoPtr);
1088 } else { /* must be a primaryExpr */
1089 code = ParsePrimaryExpr(infoPtr);
1090 if (code != TCL_OK) {
1091 return code;
1092 }
1093 }
1094 return TCL_OK;
1095 }
1096
1097 /*
1098 *----------------------------------------------------------------------
1099 *
1100 * ParsePrimaryExpr --
1101 *
1102 * This procedure parses a Tcl primary expression:
1103 * primaryExpr ::= literal | varReference | quotedString |
1104 * '[' command ']' | mathFuncCall | '(' condExpr ')'
1105 *
1106 * Results:
1107 * The return value is TCL_OK on a successful parse and TCL_ERROR
1108 * on failure. If TCL_ERROR is returned, then the interpreter's result
1109 * contains an error message.
1110 *
1111 * Side effects:
1112 * If there is insufficient space in parsePtr to hold all the
1113 * information about the subexpression, then additional space is
1114 * malloc-ed.
1115 *
1116 *----------------------------------------------------------------------
1117 */
1118
1119 static int
1120 ParsePrimaryExpr(infoPtr)
1121 ParseInfo *infoPtr; /* Holds the parse state for the
1122 * expression being parsed. */
1123 {
1124 Tcl_Parse *parsePtr = infoPtr->parsePtr;
1125 Tcl_Interp *interp = parsePtr->interp;
1126 Tcl_Token *tokenPtr, *exprTokenPtr;
1127 Tcl_Parse nested;
1128 CONST char *dollarPtr, *stringStart, *termPtr, *src;
1129 int lexeme, exprIndex, firstIndex, numToMove, code;
1130
1131 /*
1132 * We simply recurse on parenthesized subexpressions.
1133 */
1134
1135 HERE("primaryExpr", 13);
1136 lexeme = infoPtr->lexeme;
1137 if (lexeme == OPEN_PAREN) {
1138 code = GetLexeme(infoPtr); /* skip over the '(' */
1139 if (code != TCL_OK) {
1140 return code;
1141 }
1142 code = ParseCondExpr(infoPtr);
1143 if (code != TCL_OK) {
1144 return code;
1145 }
1146 if (infoPtr->lexeme != CLOSE_PAREN) {
1147 LogSyntaxError(infoPtr, "looking for close parenthesis");
1148 return TCL_ERROR;
1149 }
1150 code = GetLexeme(infoPtr); /* skip over the ')' */
1151 if (code != TCL_OK) {
1152 return code;
1153 }
1154 return TCL_OK;
1155 }
1156
1157 /*
1158 * Start a TCL_TOKEN_SUB_EXPR token for the primary.
1159 */
1160
1161 if (parsePtr->numTokens == parsePtr->tokensAvailable) {
1162 TclExpandTokenArray(parsePtr);
1163 }
1164 exprIndex = parsePtr->numTokens;
1165 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1166 exprTokenPtr->type = TCL_TOKEN_SUB_EXPR;
1167 exprTokenPtr->start = infoPtr->start;
1168 parsePtr->numTokens++;
1169
1170 /*
1171 * Process the primary then finish setting the fields of the
1172 * TCL_TOKEN_SUB_EXPR token. Note that we can't use the pointer now
1173 * stored in "exprTokenPtr" in the code below since the token array
1174 * might be reallocated.
1175 */
1176
1177 firstIndex = parsePtr->numTokens;
1178 switch (lexeme) {
1179 case LITERAL:
1180 /*
1181 * Int or double number.
1182 */
1183
1184 if (parsePtr->numTokens == parsePtr->tokensAvailable) {
1185 TclExpandTokenArray(parsePtr);
1186 }
1187 tokenPtr = &parsePtr->tokenPtr[parsePtr->numTokens];
1188 tokenPtr->type = TCL_TOKEN_TEXT;
1189 tokenPtr->start = infoPtr->start;
1190 tokenPtr->size = infoPtr->size;
1191 tokenPtr->numComponents = 0;
1192 parsePtr->numTokens++;
1193
1194 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1195 exprTokenPtr->size = infoPtr->size;
1196 exprTokenPtr->numComponents = 1;
1197 break;
1198
1199 case DOLLAR:
1200 /*
1201 * $var variable reference.
1202 */
1203
1204 dollarPtr = (infoPtr->next - 1);
1205 code = Tcl_ParseVarName(interp, dollarPtr,
1206 (infoPtr->lastChar - dollarPtr), parsePtr, 1);
1207 if (code != TCL_OK) {
1208 return code;
1209 }
1210 infoPtr->next = dollarPtr + parsePtr->tokenPtr[firstIndex].size;
1211
1212 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1213 exprTokenPtr->size = parsePtr->tokenPtr[firstIndex].size;
1214 exprTokenPtr->numComponents =
1215 (parsePtr->tokenPtr[firstIndex].numComponents + 1);
1216 break;
1217
1218 case QUOTE:
1219 /*
1220 * '"' string '"'
1221 */
1222
1223 stringStart = infoPtr->next;
1224 code = Tcl_ParseQuotedString(interp, infoPtr->start,
1225 (infoPtr->lastChar - stringStart), parsePtr, 1, &termPtr);
1226 if (code != TCL_OK) {
1227 return code;
1228 }
1229 infoPtr->next = termPtr;
1230
1231 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1232 exprTokenPtr->size = (termPtr - exprTokenPtr->start);
1233 exprTokenPtr->numComponents = parsePtr->numTokens - firstIndex;
1234
1235 /*
1236 * If parsing the quoted string resulted in more than one token,
1237 * insert a TCL_TOKEN_WORD token before them. This indicates that
1238 * the quoted string represents a concatenation of multiple tokens.
1239 */
1240
1241 if (exprTokenPtr->numComponents > 1) {
1242 if (parsePtr->numTokens >= parsePtr->tokensAvailable) {
1243 TclExpandTokenArray(parsePtr);
1244 }
1245 tokenPtr = &parsePtr->tokenPtr[firstIndex];
1246 numToMove = (parsePtr->numTokens - firstIndex);
1247 memmove((VOID *) (tokenPtr + 1), (VOID *) tokenPtr,
1248 (size_t) (numToMove * sizeof(Tcl_Token)));
1249 parsePtr->numTokens++;
1250
1251 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1252 exprTokenPtr->numComponents++;
1253
1254 tokenPtr->type = TCL_TOKEN_WORD;
1255 tokenPtr->start = exprTokenPtr->start;
1256 tokenPtr->size = exprTokenPtr->size;
1257 tokenPtr->numComponents = (exprTokenPtr->numComponents - 1);
1258 }
1259 break;
1260
1261 case OPEN_BRACKET:
1262 /*
1263 * '[' command {command} ']'
1264 */
1265
1266 if (parsePtr->numTokens == parsePtr->tokensAvailable) {
1267 TclExpandTokenArray(parsePtr);
1268 }
1269 tokenPtr = &parsePtr->tokenPtr[parsePtr->numTokens];
1270 tokenPtr->type = TCL_TOKEN_COMMAND;
1271 tokenPtr->start = infoPtr->start;
1272 tokenPtr->numComponents = 0;
1273 parsePtr->numTokens++;
1274
1275 /*
1276 * Call Tcl_ParseCommand repeatedly to parse the nested command(s)
1277 * to find their end, then throw away that parse information.
1278 */
1279
1280 src = infoPtr->next;
1281 while (1) {
1282 if (Tcl_ParseCommand(interp, src, (parsePtr->end - src), 1,
1283 &nested) != TCL_OK) {
1284 parsePtr->term = nested.term;
1285 parsePtr->errorType = nested.errorType;
1286 parsePtr->incomplete = nested.incomplete;
1287 return TCL_ERROR;
1288 }
1289 src = (nested.commandStart + nested.commandSize);
1290
1291 /*
1292 * This is equivalent to Tcl_FreeParse(&nested), but
1293 * presumably inlined here for sake of runtime optimization
1294 */
1295
1296 if (nested.tokenPtr != nested.staticTokens) {
1297 ckfree((char *) nested.tokenPtr);
1298 }
1299
1300 /*
1301 * Check for the closing ']' that ends the command substitution.
1302 * It must have been the last character of the parsed command.
1303 */
1304
1305 if ((nested.term < parsePtr->end) && (*nested.term == ']')
1306 && !nested.incomplete) {
1307 break;
1308 }
1309 if (src == parsePtr->end) {
1310 if (parsePtr->interp != NULL) {
1311 Tcl_SetResult(interp, "missing close-bracket",
1312 TCL_STATIC);
1313 }
1314 parsePtr->term = tokenPtr->start;
1315 parsePtr->errorType = TCL_PARSE_MISSING_BRACKET;
1316 parsePtr->incomplete = 1;
1317 return TCL_ERROR;
1318 }
1319 }
1320 tokenPtr->size = (src - tokenPtr->start);
1321 infoPtr->next = src;
1322
1323 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1324 exprTokenPtr->size = (src - tokenPtr->start);
1325 exprTokenPtr->numComponents = 1;
1326 break;
1327
1328 case OPEN_BRACE:
1329 /*
1330 * '{' string '}'
1331 */
1332
1333 code = Tcl_ParseBraces(interp, infoPtr->start,
1334 (infoPtr->lastChar - infoPtr->start), parsePtr, 1,
1335 &termPtr);
1336 if (code != TCL_OK) {
1337 return code;
1338 }
1339 infoPtr->next = termPtr;
1340
1341 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1342 exprTokenPtr->size = (termPtr - infoPtr->start);
1343 exprTokenPtr->numComponents = parsePtr->numTokens - firstIndex;
1344
1345 /*
1346 * If parsing the braced string resulted in more than one token,
1347 * insert a TCL_TOKEN_WORD token before them. This indicates that
1348 * the braced string represents a concatenation of multiple tokens.
1349 */
1350
1351 if (exprTokenPtr->numComponents > 1) {
1352 if (parsePtr->numTokens >= parsePtr->tokensAvailable) {
1353 TclExpandTokenArray(parsePtr);
1354 }
1355 tokenPtr = &parsePtr->tokenPtr[firstIndex];
1356 numToMove = (parsePtr->numTokens - firstIndex);
1357 memmove((VOID *) (tokenPtr + 1), (VOID *) tokenPtr,
1358 (size_t) (numToMove * sizeof(Tcl_Token)));
1359 parsePtr->numTokens++;
1360
1361 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1362 exprTokenPtr->numComponents++;
1363
1364 tokenPtr->type = TCL_TOKEN_WORD;
1365 tokenPtr->start = exprTokenPtr->start;
1366 tokenPtr->size = exprTokenPtr->size;
1367 tokenPtr->numComponents = exprTokenPtr->numComponents-1;
1368 }
1369 break;
1370
1371 case FUNC_NAME:
1372 /*
1373 * math_func '(' expr {',' expr} ')'
1374 */
1375
1376 if (parsePtr->numTokens == parsePtr->tokensAvailable) {
1377 TclExpandTokenArray(parsePtr);
1378 }
1379 tokenPtr = &parsePtr->tokenPtr[parsePtr->numTokens];
1380 tokenPtr->type = TCL_TOKEN_OPERATOR;
1381 tokenPtr->start = infoPtr->start;
1382 tokenPtr->size = infoPtr->size;
1383 tokenPtr->numComponents = 0;
1384 parsePtr->numTokens++;
1385
1386 code = GetLexeme(infoPtr); /* skip over function name */
1387 if (code != TCL_OK) {
1388 return code;
1389 }
1390 if (infoPtr->lexeme != OPEN_PAREN) {
1391 /*
1392 * Guess what kind of error we have by trying to tell
1393 * whether we have a function or variable name here.
1394 * Alas, this makes the parser more tightly bound with the
1395 * rest of the interpreter, but that is the only way to
1396 * give a sensible message here. Still, it is not too
1397 * serious as this is only done when generating an error.
1398 */
1399 Interp *iPtr = (Interp *) infoPtr->parsePtr->interp;
1400 Tcl_DString functionName;
1401 Tcl_HashEntry *hPtr;
1402
1403 /*
1404 * Look up the name as a function name. We need a writable
1405 * copy (DString) so we can terminate it with a NULL for
1406 * the benefit of Tcl_FindHashEntry which operates on
1407 * NULL-terminated string keys.
1408 */
1409 Tcl_DStringInit(&functionName);
1410 hPtr = Tcl_FindHashEntry(&iPtr->mathFuncTable,
1411 Tcl_DStringAppend(&functionName, tokenPtr->start,
1412 tokenPtr->size));
1413 Tcl_DStringFree(&functionName);
1414
1415 /*
1416 * Assume that we have an attempted variable reference
1417 * unless we've got a function name, as the set of
1418 * potential function names is typically much smaller.
1419 */
1420 if (hPtr != NULL) {
1421 LogSyntaxError(infoPtr,
1422 "expected parenthesis enclosing function arguments");
1423 } else {
1424 LogSyntaxError(infoPtr,
1425 "variable references require preceding $");
1426 }
1427 return TCL_ERROR;
1428 }
1429 code = GetLexeme(infoPtr); /* skip over '(' */
1430 if (code != TCL_OK) {
1431 return code;
1432 }
1433
1434 while (infoPtr->lexeme != CLOSE_PAREN) {
1435 code = ParseCondExpr(infoPtr);
1436 if (code != TCL_OK) {
1437 return code;
1438 }
1439
1440 if (infoPtr->lexeme == COMMA) {
1441 code = GetLexeme(infoPtr); /* skip over , */
1442 if (code != TCL_OK) {
1443 return code;
1444 }
1445 } else if (infoPtr->lexeme != CLOSE_PAREN) {
1446 LogSyntaxError(infoPtr,
1447 "missing close parenthesis at end of function call");
1448 return TCL_ERROR;
1449 }
1450 }
1451
1452 exprTokenPtr = &parsePtr->tokenPtr[exprIndex];
1453 exprTokenPtr->size = (infoPtr->next - exprTokenPtr->start);
1454 exprTokenPtr->numComponents = parsePtr->numTokens - firstIndex;
1455 break;
1456
1457 case COMMA:
1458 LogSyntaxError(infoPtr,
1459 "commas can only separate function arguments");
1460 return TCL_ERROR;
1461 case END:
1462 LogSyntaxError(infoPtr, "premature end of expression");
1463 return TCL_ERROR;
1464 case UNKNOWN:
1465 LogSyntaxError(infoPtr, "single equality character not legal in expressions");
1466 return TCL_ERROR;
1467 case UNKNOWN_CHAR:
1468 LogSyntaxError(infoPtr, "character not legal in expressions");
1469 return TCL_ERROR;
1470 case QUESTY:
1471 LogSyntaxError(infoPtr, "unexpected ternary 'then' separator");
1472 return TCL_ERROR;
1473 case COLON:
1474 LogSyntaxError(infoPtr, "unexpected ternary 'else' separator");
1475 return TCL_ERROR;
1476 case CLOSE_PAREN:
1477 LogSyntaxError(infoPtr, "unexpected close parenthesis");
1478 return TCL_ERROR;
1479
1480 default: {
1481 char buf[64];
1482
1483 sprintf(buf, "unexpected operator %s", lexemeStrings[lexeme]);
1484 LogSyntaxError(infoPtr, buf);
1485 return TCL_ERROR;
1486 }
1487 }
1488
1489 /*
1490 * Advance to the next lexeme before returning.
1491 */
1492
1493 code = GetLexeme(infoPtr);
1494 if (code != TCL_OK) {
1495 return code;
1496 }
1497 parsePtr->term = infoPtr->next;
1498 return TCL_OK;
1499 }
1500
1501 /*
1502 *----------------------------------------------------------------------
1503 *
1504 * GetLexeme --
1505 *
1506 * Lexical scanner for Tcl expressions: scans a single operator or
1507 * other syntactic element from an expression string.
1508 *
1509 * Results:
1510 * TCL_OK is returned unless an error occurred. In that case a standard
1511 * Tcl error code is returned and, if infoPtr->parsePtr->interp is
1512 * non-NULL, the interpreter's result is set to hold an error
1513 * message. TCL_ERROR is returned if an integer overflow, or a
1514 * floating-point overflow or underflow occurred while reading in a
1515 * number. If the lexical analysis is successful, infoPtr->lexeme
1516 * refers to the next symbol in the expression string, and
1517 * infoPtr->next is advanced past the lexeme. Also, if the lexeme is a
1518 * LITERAL or FUNC_NAME, then infoPtr->start is set to the first
1519 * character of the lexeme; otherwise it is set NULL.
1520 *
1521 * Side effects:
1522 * If there is insufficient space in parsePtr to hold all the
1523 * information about the subexpression, then additional space is
1524 * malloc-ed..
1525 *
1526 *----------------------------------------------------------------------
1527 */
1528
1529 static int
1530 GetLexeme(infoPtr)
1531 ParseInfo *infoPtr; /* Holds state needed to parse the expr,
1532 * including the resulting lexeme. */
1533 {
1534 register CONST char *src; /* Points to current source char. */
1535 char c;
1536 int offset, length, numBytes;
1537 Tcl_Parse *parsePtr = infoPtr->parsePtr;
1538 Tcl_Interp *interp = parsePtr->interp;
1539 Tcl_UniChar ch;
1540
1541 /*
1542 * Record where the previous lexeme ended. Since we always read one
1543 * lexeme ahead during parsing, this helps us know the source length of
1544 * subexpression tokens.
1545 */
1546
1547 infoPtr->prevEnd = infoPtr->next;
1548
1549 /*
1550 * Scan over leading white space at the start of a lexeme.
1551 */
1552
1553 src = infoPtr->next;
1554 numBytes = parsePtr->end - src;
1555 do {
1556 char type;
1557 int scanned = TclParseWhiteSpace(src, numBytes, parsePtr, &type);
1558 src += scanned; numBytes -= scanned;
1559 } while (numBytes && (*src == '\n') && (src++,numBytes--));
1560 parsePtr->term = src;
1561 if (numBytes == 0) {
1562 infoPtr->lexeme = END;
1563 infoPtr->next = src;
1564 return TCL_OK;
1565 }
1566
1567 /*
1568 * Try to parse the lexeme first as an integer or floating-point
1569 * number. Don't check for a number if the first character c is
1570 * "+" or "-". If we did, we might treat a binary operator as unary
1571 * by mistake, which would eventually cause a syntax error.
1572 */
1573
1574 c = *src;
1575 if ((c != '+') && (c != '-')) {
1576 CONST char *end = infoPtr->lastChar;
1577 if ((length = TclParseInteger(src, (end - src)))) {
1578 /*
1579 * First length bytes look like an integer. Verify by
1580 * attempting the conversion to the largest integer we have.
1581 */
1582 int code;
1583 Tcl_WideInt wide;
1584 Tcl_Obj *value = Tcl_NewStringObj(src, length);
1585
1586 Tcl_IncrRefCount(value);
1587 code = Tcl_GetWideIntFromObj(interp, value, &wide);
1588 Tcl_DecrRefCount(value);
1589 if (code == TCL_ERROR) {
1590 parsePtr->errorType = TCL_PARSE_BAD_NUMBER;
1591 return TCL_ERROR;
1592 }
1593 infoPtr->lexeme = LITERAL;
1594 infoPtr->start = src;
1595 infoPtr->size = length;
1596 infoPtr->next = (src + length);
1597 parsePtr->term = infoPtr->next;
1598 return TCL_OK;
1599 } else if ((length = ParseMaxDoubleLength(src, end))) {
1600 /*
1601 * There are length characters that could be a double.
1602 * Let strtod() tells us for sure. Need a writable copy
1603 * so we can set an terminating NULL to keep strtod from
1604 * scanning too far.
1605 */
1606 char *startPtr, *termPtr;
1607 double doubleValue;
1608 Tcl_DString toParse;
1609
1610 errno = 0;
1611 Tcl_DStringInit(&toParse);
1612 startPtr = Tcl_DStringAppend(&toParse, src, length);
1613 doubleValue = strtod(startPtr, &termPtr);
1614 Tcl_DStringFree(&toParse);
1615 if (termPtr != startPtr) {
1616 if (errno != 0) {
1617 if (interp != NULL) {
1618 TclExprFloatError(interp, doubleValue);
1619 }
1620 parsePtr->errorType = TCL_PARSE_BAD_NUMBER;
1621 return TCL_ERROR;
1622 }
1623
1624 /*
1625 * startPtr was the start of a valid double, copied
1626 * from src.
1627 */
1628
1629 infoPtr->lexeme = LITERAL;
1630 infoPtr->start = src;
1631 if ((termPtr - startPtr) > length) {
1632 infoPtr->size = length;
1633 } else {
1634 infoPtr->size = (termPtr - startPtr);
1635 }
1636 infoPtr->next = src + infoPtr->size;
1637 parsePtr->term = infoPtr->next;
1638 return TCL_OK;
1639 }
1640 }
1641 }
1642
1643 /*
1644 * Not an integer or double literal. Initialize the lexeme's fields
1645 * assuming the common case of a single character lexeme.
1646 */
1647
1648 infoPtr->start = src;
1649 infoPtr->size = 1;
1650 infoPtr->next = src+1;
1651 parsePtr->term = infoPtr->next;
1652
1653 switch (*src) {
1654 case '[':
1655 infoPtr->lexeme = OPEN_BRACKET;
1656 return TCL_OK;
1657
1658 case '{':
1659 infoPtr->lexeme = OPEN_BRACE;
1660 return TCL_OK;
1661
1662 case '(':
1663 infoPtr->lexeme = OPEN_PAREN;
1664 return TCL_OK;
1665
1666 case ')':
1667 infoPtr->lexeme = CLOSE_PAREN;
1668 return TCL_OK;
1669
1670 case '$':
1671 infoPtr->lexeme = DOLLAR;
1672 return TCL_OK;
1673
1674 case '\"':
1675 infoPtr->lexeme = QUOTE;
1676 return TCL_OK;
1677
1678 case ',':
1679 infoPtr->lexeme = COMMA;
1680 return TCL_OK;
1681
1682 case '*':
1683 infoPtr->lexeme = MULT;
1684 return TCL_OK;
1685
1686 case '/':
1687 infoPtr->lexeme = DIVIDE;
1688 return TCL_OK;
1689
1690 case '%':
1691 infoPtr->lexeme = MOD;
1692 return TCL_OK;
1693
1694 case '+':
1695 infoPtr->lexeme = PLUS;
1696 return TCL_OK;
1697
1698 case '-':
1699 infoPtr->lexeme = MINUS;
1700 return TCL_OK;
1701
1702 case '?':
1703 infoPtr->lexeme = QUESTY;
1704 return TCL_OK;
1705
1706 case ':':
1707 infoPtr->lexeme = COLON;
1708 return TCL_OK;
1709
1710 case '<':
1711 infoPtr->lexeme = LESS;
1712 if ((infoPtr->lastChar - src) > 1) {
1713 switch (src[1]) {
1714 case '<':
1715 infoPtr->lexeme = LEFT_SHIFT;
1716 infoPtr->size = 2;
1717 infoPtr->next = src+2;
1718 break;
1719 case '=':
1720 infoPtr->lexeme = LEQ;
1721 infoPtr->size = 2;
1722 infoPtr->next = src+2;
1723 break;
1724 }
1725 }
1726 parsePtr->term = infoPtr->next;
1727 return TCL_OK;
1728
1729 case '>':
1730 infoPtr->lexeme = GREATER;
1731 if ((infoPtr->lastChar - src) > 1) {
1732 switch (src[1]) {
1733 case '>':
1734 infoPtr->lexeme = RIGHT_SHIFT;
1735 infoPtr->size = 2;
1736 infoPtr->next = src+2;
1737 break;
1738 case '=':
1739 infoPtr->lexeme = GEQ;
1740 infoPtr->size = 2;
1741 infoPtr->next = src+2;
1742 break;
1743 }
1744 }
1745 parsePtr->term = infoPtr->next;
1746 return TCL_OK;
1747
1748 case '=':
1749 infoPtr->lexeme = UNKNOWN;
1750 if ((src[1] == '=') && ((infoPtr->lastChar - src) > 1)) {
1751 infoPtr->lexeme = EQUAL;
1752 infoPtr->size = 2;
1753 infoPtr->next = src+2;
1754 }
1755 parsePtr->term = infoPtr->next;
1756 return TCL_OK;
1757
1758 case '!':
1759 infoPtr->lexeme = NOT;
1760 if ((src[1] == '=') && ((infoPtr->lastChar - src) > 1)) {
1761 infoPtr->lexeme = NEQ;
1762 infoPtr->size = 2;
1763 infoPtr->next = src+2;
1764 }
1765 parsePtr->term = infoPtr->next;
1766 return TCL_OK;
1767
1768 case '&':
1769 infoPtr->lexeme = BIT_AND;
1770 if ((src[1] == '&') && ((infoPtr->lastChar - src) > 1)) {
1771 infoPtr->lexeme = AND;
1772 infoPtr->size = 2;
1773 infoPtr->next = src+2;
1774 }
1775 parsePtr->term = infoPtr->next;
1776 return TCL_OK;
1777
1778 case '^':
1779 infoPtr->lexeme = BIT_XOR;
1780 return TCL_OK;
1781
1782 case '|':
1783 infoPtr->lexeme = BIT_OR;
1784 if ((src[1] == '|') && ((infoPtr->lastChar - src) > 1)) {
1785 infoPtr->lexeme = OR;
1786 infoPtr->size = 2;
1787 infoPtr->next = src+2;
1788 }
1789 parsePtr->term = infoPtr->next;
1790 return TCL_OK;
1791
1792 case '~':
1793 infoPtr->lexeme = BIT_NOT;
1794 return TCL_OK;
1795
1796 case 'e':
1797 if ((src[1] == 'q') && ((infoPtr->lastChar - src) > 1)) {
1798 infoPtr->lexeme = STREQ;
1799 infoPtr->size = 2;
1800 infoPtr->next = src+2;
1801 parsePtr->term = infoPtr->next;
1802 return TCL_OK;
1803 } else {
1804 goto checkFuncName;
1805 }
1806
1807 case 'n':
1808 if ((src[1] == 'e') && ((infoPtr->lastChar - src) > 1)) {
1809 infoPtr->lexeme = STRNEQ;
1810 infoPtr->size = 2;
1811 infoPtr->next = src+2;
1812 parsePtr->term = infoPtr->next;
1813 return TCL_OK;
1814 } else {
1815 goto checkFuncName;
1816 }
1817
1818 default:
1819 checkFuncName:
1820 length = (infoPtr->lastChar - src);
1821 if (Tcl_UtfCharComplete(src, length)) {
1822 offset = Tcl_UtfToUniChar(src, &ch);
1823 } else {
1824 char utfBytes[TCL_UTF_MAX];
1825 memcpy(utfBytes, src, (size_t) length);
1826 utfBytes[length] = '\0';
1827 offset = Tcl_UtfToUniChar(utfBytes, &ch);
1828 }
1829 c = UCHAR(ch);
1830 if (isalpha(UCHAR(c))) { /* INTL: ISO only. */
1831 infoPtr->lexeme = FUNC_NAME;
1832 while (isalnum(UCHAR(c)) || (c == '_')) { /* INTL: ISO only. */
1833 src += offset; length -= offset;
1834 if (Tcl_UtfCharComplete(src, length)) {
1835 offset = Tcl_UtfToUniChar(src, &ch);
1836 } else {
1837 char utfBytes[TCL_UTF_MAX];
1838 memcpy(utfBytes, src, (size_t) length);
1839 utfBytes[length] = '\0';
1840 offset = Tcl_UtfToUniChar(utfBytes, &ch);
1841 }
1842 c = UCHAR(ch);
1843 }
1844 infoPtr->size = (src - infoPtr->start);
1845 infoPtr->next = src;
1846 parsePtr->term = infoPtr->next;
1847 /*
1848 * Check for boolean literals (true, false, yes, no, on, off)
1849 */
1850 switch (infoPtr->start[0]) {
1851 case 'f':
1852 if (infoPtr->size == 5 &&
1853 strncmp("false", infoPtr->start, 5) == 0) {
1854 infoPtr->lexeme = LITERAL;
1855 return TCL_OK;
1856 }
1857 break;
1858 case 'n':
1859 if (infoPtr->size == 2 &&
1860 strncmp("no", infoPtr->start, 2) == 0) {
1861 infoPtr->lexeme = LITERAL;
1862 return TCL_OK;
1863 }
1864 break;
1865 case 'o':
1866 if (infoPtr->size == 3 &&
1867 strncmp("off", infoPtr->start, 3) == 0) {
1868 infoPtr->lexeme = LITERAL;
1869 return TCL_OK;
1870 } else if (infoPtr->size == 2 &&
1871 strncmp("on", infoPtr->start, 2) == 0) {
1872 infoPtr->lexeme = LITERAL;
1873 return TCL_OK;
1874 }
1875 break;
1876 case 't':
1877 if (infoPtr->size == 4 &&
1878 strncmp("true", infoPtr->start, 4) == 0) {
1879 infoPtr->lexeme = LITERAL;
1880 return TCL_OK;
1881 }
1882 break;
1883 case 'y':
1884 if (infoPtr->size == 3 &&
1885 strncmp("yes", infoPtr->start, 3) == 0) {
1886 infoPtr->lexeme = LITERAL;
1887 return TCL_OK;
1888 }
1889 break;
1890 }
1891 return TCL_OK;
1892 }
1893 infoPtr->lexeme = UNKNOWN_CHAR;
1894 return TCL_OK;
1895 }
1896 }
1897
1898 /*
1899 *----------------------------------------------------------------------
1900 *
1901 * TclParseInteger --
1902 *
1903 * Scans up to numBytes bytes starting at src, and checks whether
1904 * the leading bytes look like an integer's string representation.
1905 *
1906 * Results:
1907 * Returns 0 if the leading bytes do not look like an integer.
1908 * Otherwise, returns the number of bytes examined that look
1909 * like an integer. This may be less than numBytes if the integer
1910 * is only the leading part of the string.
1911 *
1912 * Side effects:
1913 * None.
1914 *
1915 *----------------------------------------------------------------------
1916 */
1917
1918 int
1919 TclParseInteger(string, numBytes)
1920 register CONST char *string;/* The string to examine. */
1921 register int numBytes; /* Max number of bytes to scan. */
1922 {
1923 register CONST char *p = string;
1924
1925 /* Take care of introductory "0x" */
1926 if ((numBytes > 1) && (p[0] == '0') && ((p[1] == 'x') || (p[1] == 'X'))) {
1927 int scanned;
1928 Tcl_UniChar ch;
1929 p+=2; numBytes -= 2;
1930 scanned = TclParseHex(p, numBytes, &ch);
1931 if (scanned) {
1932 return scanned + 2;
1933 }
1934
1935 /* Recognize the 0 as valid integer, but x is left behind */
1936 return 1;
1937 }
1938 while (numBytes && isdigit(UCHAR(*p))) { /* INTL: digit */
1939 numBytes--; p++;
1940 }
1941 if (numBytes == 0) {
1942 return (p - string);
1943 }
1944 if ((*p != '.') && (*p != 'e') && (*p != 'E')) {
1945 return (p - string);
1946 }
1947 return 0;
1948 }
1949
1950 /*
1951 *----------------------------------------------------------------------
1952 *
1953 * ParseMaxDoubleLength --
1954 *
1955 * Scans a sequence of bytes checking that the characters could
1956 * be in a string rep of a double.
1957 *
1958 * Results:
1959 * Returns the number of bytes starting with string, runing to, but
1960 * not including end, all of which could be part of a string rep.
1961 * of a double. Only character identity is used, no actual
1962 * parsing is done.
1963 *
1964 * The legal bytes are '0' - '9', 'A' - 'F', 'a' - 'f',
1965 * '.', '+', '-', 'i', 'I', 'n', 'N', 'p', 'P', 'x', and 'X'.
1966 * This covers the values "Inf" and "Nan" as well as the
1967 * decimal and hexadecimal representations recognized by a
1968 * C99-compliant strtod().
1969 *
1970 * Side effects:
1971 * None.
1972 *
1973 *----------------------------------------------------------------------
1974 */
1975
1976 static int
1977 ParseMaxDoubleLength(string, end)
1978 register CONST char *string;/* The string to examine. */
1979 CONST char *end; /* Point to the first character past the end
1980 * of the string we are examining. */
1981 {
1982 CONST char *p = string;
1983 while (p < end) {
1984 switch (*p) {
1985 case '0': case '1': case '2': case '3': case '4': case '5':
1986 case '6': case '7': case '8': case '9': case 'A': case 'B':
1987 case 'C': case 'D': case 'E': case 'F': case 'I': case 'N':
1988 case 'P': case 'X': case 'a': case 'b': case 'c': case 'd':
1989 case 'e': case 'f': case 'i': case 'n': case 'p': case 'x':
1990 case '.': case '+': case '-':
1991 p++;
1992 break;
1993 default:
1994 goto done;
1995 }
1996 }
1997 done:
1998 return (p - string);
1999 }
2000
2001 /*
2002 *----------------------------------------------------------------------
2003 *
2004 * PrependSubExprTokens --
2005 *
2006 * This procedure is called after the operands of an subexpression have
2007 * been parsed. It generates two tokens: a TCL_TOKEN_SUB_EXPR token for
2008 * the subexpression, and a TCL_TOKEN_OPERATOR token for its operator.
2009 * These two tokens are inserted before the operand tokens.
2010 *
2011 * Results:
2012 * None.
2013 *
2014 * Side effects:
2015 * If there is insufficient space in parsePtr to hold the new tokens,
2016 * additional space is malloc-ed.
2017 *
2018 *----------------------------------------------------------------------
2019 */
2020
2021 static void
2022 PrependSubExprTokens(op, opBytes, src, srcBytes, firstIndex, infoPtr)
2023 CONST char *op; /* Points to first byte of the operator
2024 * in the source script. */
2025 int opBytes; /* Number of bytes in the operator. */
2026 CONST char *src; /* Points to first byte of the subexpression
2027 * in the source script. */
2028 int srcBytes; /* Number of bytes in subexpression's
2029 * source. */
2030 int firstIndex; /* Index of first token already emitted for
2031 * operator's first (or only) operand. */
2032 ParseInfo *infoPtr; /* Holds the parse state for the
2033 * expression being parsed. */
2034 {
2035 Tcl_Parse *parsePtr = infoPtr->parsePtr;
2036 Tcl_Token *tokenPtr, *firstTokenPtr;
2037 int numToMove;
2038
2039 if ((parsePtr->numTokens + 1) >= parsePtr->tokensAvailable) {
2040 TclExpandTokenArray(parsePtr);
2041 }
2042 firstTokenPtr = &parsePtr->tokenPtr[firstIndex];
2043 tokenPtr = (firstTokenPtr + 2);
2044 numToMove = (parsePtr->numTokens - firstIndex);
2045 memmove((VOID *) tokenPtr, (VOID *) firstTokenPtr,
2046 (size_t) (numToMove * sizeof(Tcl_Token)));
2047 parsePtr->numTokens += 2;
2048
2049 tokenPtr = firstTokenPtr;
2050 tokenPtr->type = TCL_TOKEN_SUB_EXPR;
2051 tokenPtr->start = src;
2052 tokenPtr->size = srcBytes;
2053 tokenPtr->numComponents = parsePtr->numTokens - (firstIndex + 1);
2054
2055 tokenPtr++;
2056 tokenPtr->type = TCL_TOKEN_OPERATOR;
2057 tokenPtr->start = op;
2058 tokenPtr->size = opBytes;
2059 tokenPtr->numComponents = 0;
2060 }
2061
2062 /*
2063 *----------------------------------------------------------------------
2064 *
2065 * LogSyntaxError --
2066 *
2067 * This procedure is invoked after an error occurs when parsing an
2068 * expression. It sets the interpreter result to an error message
2069 * describing the error.
2070 *
2071 * Results:
2072 * None.
2073 *
2074 * Side effects:
2075 * Sets the interpreter result to an error message describing the
2076 * expression that was being parsed when the error occurred, and why
2077 * the parser considers that to be a syntax error at all.
2078 *
2079 *----------------------------------------------------------------------
2080 */
2081
2082 static void
2083 LogSyntaxError(infoPtr, extraInfo)
2084 ParseInfo *infoPtr; /* Holds the parse state for the
2085 * expression being parsed. */
2086 CONST char *extraInfo; /* String to provide extra information
2087 * about the syntax error. */
2088 {
2089 int numBytes = (infoPtr->lastChar - infoPtr->originalExpr);
2090 char buffer[100];
2091
2092 if (numBytes > 60) {
2093 sprintf(buffer, "syntax error in expression \"%.60s...\"",
2094 infoPtr->originalExpr);
2095 } else {
2096 sprintf(buffer, "syntax error in expression \"%.*s\"",
2097 numBytes, infoPtr->originalExpr);
2098 }
2099 Tcl_ResetResult(infoPtr->parsePtr->interp);
2100 Tcl_AppendStringsToObj(Tcl_GetObjResult(infoPtr->parsePtr->interp),
2101 buffer, ": ", extraInfo, (char *) NULL);
2102 infoPtr->parsePtr->errorType = TCL_PARSE_SYNTAX;
2103 infoPtr->parsePtr->term = infoPtr->start;
2104 }