comparison dmd/Lexer.d @ 51:b7d29f613539

StaticAssertStatement.syntaxCopy IfStatement.syntaxCopy CompoundDeclarationStatement.syntaxCopy VoidInitializer.syntaxCopy TypeAArray.syntaxCopy TypeTypeof.syntaxCopy TypeAArray.resolve TypeSArray.deduceType TypeAArray.deduceType TypeAArray.implicitConvTo TemplateDeclaration.leastAsSpecialized TemplateTypeParameter.dummyArg TypeIdentifier.deduceType TemplateTypeParameter.syntaxCopy Lexer.hexStringConstant Lexer.delimitedStringConstant GotoDefaultStatement.ctor CaseRangeStatement.ctor Type.castMod StorageClassDeclaration.syntaxCopy TemplateDeclaration.syntaxCopy
author korDen
date Sat, 21 Aug 2010 11:17:42 +0400
parents 0aa7d1437ada
children ef02e2e203c2
comparison
equal deleted inserted replaced
50:adf6f7f216ea 51:b7d29f613539
1620 } 1620 }
1621 1621
1622 assert(false); 1622 assert(false);
1623 } 1623 }
1624 1624
1625 /**************************************
1626 * Lex hex strings:
1627 * x"0A ae 34FE BD"
1628 */
1625 TOK hexStringConstant(Token* t) 1629 TOK hexStringConstant(Token* t)
1626 { 1630 {
1627 assert(false); 1631 uint c;
1632 Loc start = loc;
1633 uint n = 0;
1634 uint v;
1635
1636 p++;
1637 stringbuffer.reset();
1638 while (1)
1639 {
1640 c = *p++;
1641 switch (c)
1642 {
1643 case ' ':
1644 case '\t':
1645 case '\v':
1646 case '\f':
1647 continue; // skip white space
1648
1649 case '\r':
1650 if (*p == '\n')
1651 continue; // ignore
1652 // Treat isolated '\r' as if it were a '\n'
1653 case '\n':
1654 loc.linnum++;
1655 continue;
1656
1657 case 0:
1658 case 0x1A:
1659 error("unterminated string constant starting at %s", start.toChars());
1660 t.ustring = "".ptr;
1661 t.len = 0;
1662 t.postfix = 0;
1663 return TOKstring;
1664
1665 case '"':
1666 if (n & 1)
1667 {
1668 error("odd number (%d) of hex characters in hex string", n);
1669 stringbuffer.writeByte(v);
1670 }
1671 t.len = stringbuffer.offset;
1672 stringbuffer.writeByte(0);
1673 void* mem = malloc(stringbuffer.offset);
1674 memcpy(mem, stringbuffer.data, stringbuffer.offset);
1675 t.ustring = cast(const(char)*)mem;
1676 stringPostfix(t);
1677 return TOKstring;
1678
1679 default:
1680 if (c >= '0' && c <= '9')
1681 c -= '0';
1682 else if (c >= 'a' && c <= 'f')
1683 c -= 'a' - 10;
1684 else if (c >= 'A' && c <= 'F')
1685 c -= 'A' - 10;
1686 else if (c & 0x80)
1687 { p--;
1688 uint u = decodeUTF();
1689 p++;
1690 if (u == PS || u == LS)
1691 loc.linnum++;
1692 else
1693 error("non-hex character \\u%x", u);
1694 }
1695 else
1696 error("non-hex character '%c'", c);
1697 if (n & 1)
1698 { v = (v << 4) | c;
1699 stringbuffer.writeByte(v);
1700 }
1701 else
1702 v = c;
1703 n++;
1704 break;
1705 }
1706 }
1628 } 1707 }
1629 1708
1630 version (DMDV2) { 1709 version (DMDV2) {
1710 /**************************************
1711 * Lex delimited strings:
1712 * q"(foo(xxx))" // "foo(xxx)"
1713 * q"[foo(]" // "foo("
1714 * q"/foo]/" // "foo]"
1715 * q"HERE
1716 * foo
1717 * HERE" // "foo\n"
1718 * Input:
1719 * p is on the "
1720 */
1631 TOK delimitedStringConstant(Token* t) 1721 TOK delimitedStringConstant(Token* t)
1632 { 1722 {
1633 assert(false); 1723 uint c;
1724 Loc start = loc;
1725 uint delimleft = 0;
1726 uint delimright = 0;
1727 uint nest = 1;
1728 uint nestcount;
1729 Identifier hereid = null;
1730 uint blankrol = 0;
1731 uint startline = 0;
1732
1733 p++;
1734 stringbuffer.reset();
1735 while (1)
1736 {
1737 c = *p++;
1738 //printf("c = '%c'\n", c);
1739 switch (c)
1740 {
1741 case '\n':
1742 Lnextline:
1743 loc.linnum++;
1744 startline = 1;
1745 if (blankrol)
1746 { blankrol = 0;
1747 continue;
1748 }
1749 if (hereid)
1750 {
1751 stringbuffer.writeUTF8(c);
1752 continue;
1753 }
1754 break;
1755
1756 case '\r':
1757 if (*p == '\n')
1758 continue; // ignore
1759 c = '\n'; // treat EndOfLine as \n character
1760 goto Lnextline;
1761
1762 case 0:
1763 case 0x1A:
1764 goto Lerror;
1765
1766 default:
1767 if (c & 0x80)
1768 { p--;
1769 c = decodeUTF();
1770 p++;
1771 if (c == PS || c == LS)
1772 goto Lnextline;
1773 }
1774 break;
1775 }
1776 if (delimleft == 0)
1777 {
1778 delimleft = c;
1779 nest = 1;
1780 nestcount = 1;
1781 if (c == '(')
1782 delimright = ')';
1783 else if (c == '{')
1784 delimright = '}';
1785 else if (c == '[')
1786 delimright = ']';
1787 else if (c == '<')
1788 delimright = '>';
1789 else if (isalpha(c) || c == '_' || (c >= 0x80 && isUniAlpha(c)))
1790 {
1791 // Start of identifier; must be a heredoc
1792 Token t2;
1793 p--;
1794 scan(&t2); // read in heredoc identifier
1795 if (t2.value != TOKidentifier)
1796 {
1797 error("identifier expected for heredoc, not %s", t2.toChars());
1798 delimright = c;
1799 }
1800 else
1801 {
1802 hereid = t2.ident;
1803 //printf("hereid = '%s'\n", hereid.toChars());
1804 blankrol = 1;
1805 }
1806 nest = 0;
1807 }
1808 else
1809 {
1810 delimright = c;
1811 nest = 0;
1812 if (isspace(c))
1813 error("delimiter cannot be whitespace");
1814 }
1815 }
1816 else
1817 {
1818 if (blankrol)
1819 {
1820 error("heredoc rest of line should be blank");
1821 blankrol = 0;
1822 continue;
1823 }
1824 if (nest == 1)
1825 {
1826 if (c == delimleft)
1827 nestcount++;
1828 else if (c == delimright)
1829 { nestcount--;
1830 if (nestcount == 0)
1831 goto Ldone;
1832 }
1833 }
1834 else if (c == delimright)
1835 goto Ldone;
1836 if (startline && isalpha(c) && hereid)
1837 {
1838 Token t2;
1839 ubyte* psave = p;
1840 p--;
1841 scan(&t2); // read in possible heredoc identifier
1842 //printf("endid = '%s'\n", t2.ident.toChars());
1843 if (t2.value == TOKidentifier && t2.ident.equals(hereid))
1844 {
1845 /* should check that rest of line is blank
1846 */
1847 goto Ldone;
1848 }
1849 p = psave;
1850 }
1851 stringbuffer.writeUTF8(c);
1852 startline = 0;
1853 }
1854 }
1855
1856 Ldone:
1857 if (*p == '"')
1858 p++;
1859 else
1860 error("delimited string must end in %c\"", delimright);
1861 t.len = stringbuffer.offset;
1862 stringbuffer.writeByte(0);
1863 void* mem = malloc(stringbuffer.offset);
1864 memcpy(mem, stringbuffer.data, stringbuffer.offset);
1865 t.ustring = cast(const(char)*)mem;
1866 stringPostfix(t);
1867 return TOKstring;
1868
1869 Lerror:
1870 error("unterminated string constant starting at %s", start.toChars());
1871 t.ustring = "".ptr;
1872 t.len = 0;
1873 t.postfix = 0;
1874 return TOKstring;
1634 } 1875 }
1635 1876
1636 /************************************** 1877 /**************************************
1637 * Lex delimited strings: 1878 * Lex delimited strings:
1638 * q{ foo(xxx) } // " foo(xxx) " 1879 * q{ foo(xxx) } // " foo(xxx) "