1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/*
* tclCompExpr.c --
*
* This file contains the code to parse and compile Tcl expressions and
* implementations of the Tcl commands corresponding to expression
* operators, such as the command ::tcl::mathop::+ .
*
* Contributions from Don Porter, NIST, 2006-2007. (not subject to US copyright)
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclCompExpr.c,v 1.103 2010/02/24 10:45:04 dkf Exp $
*/
#include "tclInt.h"
#include "tclCompile.h" /* CompileEnv */
/*
* Expression parsing takes place in the routine ParseExpr(). It takes a
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/*
* tclCompExpr.c --
*
* This file contains the code to parse and compile Tcl expressions and
* implementations of the Tcl commands corresponding to expression
* operators, such as the command ::tcl::mathop::+ .
*
* Contributions from Don Porter, NIST, 2006-2007. (not subject to US copyright)
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclCompExpr.c,v 1.104 2010/03/05 14:34:04 dkf Exp $
*/
#include "tclInt.h"
#include "tclCompile.h" /* CompileEnv */
/*
* Expression parsing takes place in the routine ParseExpr(). It takes a
|
| ︙ | | | ︙ | |
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
|
/* No tokens and no characters for the OT_EMPTY leaf. */
break;
case OT_LITERAL:
/* Skip any white space that comes before the literal */
scanned = TclParseAllWhiteSpace(start, numBytes);
start +=scanned;
numBytes -= scanned;
/*
* Reparse the literal to get pointers into source string.
*/
scanned = ParseLexeme(start, numBytes, &lexeme, NULL);
TclGrowParseTokenArray(parsePtr, 2);
subExprTokenPtr = parsePtr->tokenPtr + parsePtr->numTokens;
subExprTokenPtr->type = TCL_TOKEN_SUB_EXPR;
subExprTokenPtr->start = start;
subExprTokenPtr->size = scanned;
subExprTokenPtr->numComponents = 1;
subExprTokenPtr[1].type = TCL_TOKEN_TEXT;
subExprTokenPtr[1].start = start;
subExprTokenPtr[1].size = scanned;
subExprTokenPtr[1].numComponents = 0;
parsePtr->numTokens += 2;
start +=scanned;
numBytes -= scanned;
break;
case OT_TOKENS: {
/*
* tokenPtr points to a token sequence that came from parsing a
* Tcl word. A Tcl word is made up of a sequence of one or more
|
|
|
|
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
|
/* No tokens and no characters for the OT_EMPTY leaf. */
break;
case OT_LITERAL:
/* Skip any white space that comes before the literal */
scanned = TclParseAllWhiteSpace(start, numBytes);
start += scanned;
numBytes -= scanned;
/*
* Reparse the literal to get pointers into source string.
*/
scanned = ParseLexeme(start, numBytes, &lexeme, NULL);
TclGrowParseTokenArray(parsePtr, 2);
subExprTokenPtr = parsePtr->tokenPtr + parsePtr->numTokens;
subExprTokenPtr->type = TCL_TOKEN_SUB_EXPR;
subExprTokenPtr->start = start;
subExprTokenPtr->size = scanned;
subExprTokenPtr->numComponents = 1;
subExprTokenPtr[1].type = TCL_TOKEN_TEXT;
subExprTokenPtr[1].start = start;
subExprTokenPtr[1].size = scanned;
subExprTokenPtr[1].numComponents = 0;
parsePtr->numTokens += 2;
start += scanned;
numBytes -= scanned;
break;
case OT_TOKENS: {
/*
* tokenPtr points to a token sequence that came from parsing a
* Tcl word. A Tcl word is made up of a sequence of one or more
|
| ︙ | | | ︙ | |
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
|
subExprTokenPtr++;
memcpy(subExprTokenPtr, tokenPtr,
(size_t) toCopy * sizeof(Tcl_Token));
parsePtr->numTokens += toCopy + 1;
}
scanned = tokenPtr->start + tokenPtr->size - start;
start +=scanned;
numBytes -= scanned;
tokenPtr += toCopy;
break;
}
default:
/* Advance to the child node, which is an operator. */
nodePtr = nodes + next;
/*
* Skip any white space that comes before the subexpression.
*/
scanned = TclParseAllWhiteSpace(start, numBytes);
start +=scanned;
numBytes -= scanned;
/*
* Generate tokens for the operator / subexpression...
*/
switch (nodePtr->lexeme) {
|
|
|
|
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
|
subExprTokenPtr++;
memcpy(subExprTokenPtr, tokenPtr,
(size_t) toCopy * sizeof(Tcl_Token));
parsePtr->numTokens += toCopy + 1;
}
scanned = tokenPtr->start + tokenPtr->size - start;
start += scanned;
numBytes -= scanned;
tokenPtr += toCopy;
break;
}
default:
/* Advance to the child node, which is an operator. */
nodePtr = nodes + next;
/*
* Skip any white space that comes before the subexpression.
*/
scanned = TclParseAllWhiteSpace(start, numBytes);
start += scanned;
numBytes -= scanned;
/*
* Generate tokens for the operator / subexpression...
*/
switch (nodePtr->lexeme) {
|
| ︙ | | | ︙ | |
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
|
break;
case MARK_RIGHT:
next = nodePtr->right;
/* Skip any white space that comes before the operator */
scanned = TclParseAllWhiteSpace(start, numBytes);
start +=scanned;
numBytes -= scanned;
/*
* Here we scan from the string the operator corresponding to
* nodePtr->lexeme.
*/
|
|
|
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
|
break;
case MARK_RIGHT:
next = nodePtr->right;
/* Skip any white space that comes before the operator */
scanned = TclParseAllWhiteSpace(start, numBytes);
start += scanned;
numBytes -= scanned;
/*
* Here we scan from the string the operator corresponding to
* nodePtr->lexeme.
*/
|
| ︙ | | | ︙ | |
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
|
subExprTokenPtr = parsePtr->tokenPtr + subExprTokenIdx;
subExprTokenPtr[1].start = start;
subExprTokenPtr[1].size = scanned;
break;
}
start +=scanned;
numBytes -= scanned;
break;
case MARK_PARENT:
switch (nodePtr->lexeme) {
case START:
/* When we get back to the START node, we're done. */
return;
case COMMA:
case COLON:
/* No tokens for these lexemes -> nothing to do. */
break;
case OPEN_PAREN:
/* Skip past matching close paren. */
scanned = TclParseAllWhiteSpace(start, numBytes);
start +=scanned;
numBytes -= scanned;
scanned = ParseLexeme(start, numBytes, &lexeme, NULL);
start +=scanned;
numBytes -= scanned;
break;
default: {
/*
* Before we leave this node/operator/subexpression for the
|
|
|
|
|
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
|
subExprTokenPtr = parsePtr->tokenPtr + subExprTokenIdx;
subExprTokenPtr[1].start = start;
subExprTokenPtr[1].size = scanned;
break;
}
start += scanned;
numBytes -= scanned;
break;
case MARK_PARENT:
switch (nodePtr->lexeme) {
case START:
/* When we get back to the START node, we're done. */
return;
case COMMA:
case COLON:
/* No tokens for these lexemes -> nothing to do. */
break;
case OPEN_PAREN:
/* Skip past matching close paren. */
scanned = TclParseAllWhiteSpace(start, numBytes);
start += scanned;
numBytes -= scanned;
scanned = ParseLexeme(start, numBytes, &lexeme, NULL);
start += scanned;
numBytes -= scanned;
break;
default: {
/*
* Before we leave this node/operator/subexpression for the
|
| ︙ | | | ︙ | |