comparison trunk/src/dil/Parser.d @ 508:943ecc9c133a

Added isIntegralType() to Token and refactored code.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 12 Dec 2007 03:25:07 +0100
parents 996041463028
children baa7c4c0be78
comparison
equal deleted inserted replaced
507:996041463028 508:943ecc9c133a
343 nT(); 343 nT();
344 decl = new EmptyDeclaration(); 344 decl = new EmptyDeclaration();
345 break; 345 break;
346 // Declaration 346 // Declaration
347 case T.Identifier, T.Dot, T.Typeof: 347 case T.Identifier, T.Dot, T.Typeof:
348 // IntegralType
349 case T.Char, T.Wchar, T.Dchar, T.Bool,
350 T.Byte, T.Ubyte, T.Short, T.Ushort,
351 T.Int, T.Uint, T.Long, T.Ulong,
352 T.Float, T.Double, T.Real,
353 T.Ifloat, T.Idouble, T.Ireal,
354 T.Cfloat, T.Cdouble, T.Creal, T.Void:
355 case_Declaration: 348 case_Declaration:
356 return parseVariableOrFunction(this.storageClass, this.protection, this.linkageType); 349 return parseVariableOrFunction(this.storageClass, this.protection, this.linkageType);
357 /+case T.Module: 350 /+case T.Module:
358 // TODO: Error: module is optional and can appear only once at the top of the source file. 351 // TODO: Error: module is optional and can appear only once at the top of the source file.
359 break;+/ 352 break;+/
360 default: 353 default:
354 if (token.isIntegralType)
355 goto case_Declaration;
356
361 decl = new IllegalDeclaration(); 357 decl = new IllegalDeclaration();
362 // Skip to next valid token. 358 // Skip to next valid token.
363 do 359 do
364 nT(); 360 nT();
365 while (!token.isDeclDefStart && 361 while (!token.isDeclDefStart &&
1657 Statement parseStatement() 1653 Statement parseStatement()
1658 { 1654 {
1659 auto begin = token; 1655 auto begin = token;
1660 Statement s; 1656 Statement s;
1661 Declaration d; 1657 Declaration d;
1658
1659 if (token.isIntegralType)
1660 {
1661 d = parseVariableOrFunction();
1662 goto LreturnDeclarationStatement;
1663 }
1664
1662 switch (token.type) 1665 switch (token.type)
1663 { 1666 {
1664 case T.Align: 1667 case T.Align:
1665 uint size = parseAlignAttribute(); 1668 uint size = parseAlignAttribute();
1666 // Restrict align attribute to structs in parsing phase. 1669 // Restrict align attribute to structs in parsing phase.
1675 else 1678 else
1676 expected(T.Struct); 1679 expected(T.Struct);
1677 1680
1678 d = new AlignDeclaration(size, structDecl ? cast(Declaration)structDecl : new Declarations); 1681 d = new AlignDeclaration(size, structDecl ? cast(Declaration)structDecl : new Declarations);
1679 goto LreturnDeclarationStatement; 1682 goto LreturnDeclarationStatement;
1680 /+ Not applicable for statements. 1683 /+ Not applicable for statements.
1681 // T.Private, 1684 T.Private, T.Package, T.Protected, T.Public, T.Export,
1682 // T.Package, 1685 T.Deprecated, T.Override, T.Abstract,+/
1683 // T.Protected,
1684 // T.Public,
1685 // T.Export,
1686 // T.Deprecated,
1687 // T.Override,
1688 // T.Abstract,
1689 +/
1690 case T.Extern, 1686 case T.Extern,
1691 T.Final, 1687 T.Final,
1692 T.Const, 1688 T.Const,
1693 T.Auto: 1689 T.Auto:
1694 //T.Scope 1690 //T.Scope
1715 ); 1711 );
1716 if (success) 1712 if (success)
1717 goto LreturnDeclarationStatement; // Declaration 1713 goto LreturnDeclarationStatement; // Declaration
1718 else 1714 else
1719 goto case_parseExpressionStatement; // Expression 1715 goto case_parseExpressionStatement; // Expression
1720 // IntegralType 1716
1721 case T.Char, T.Wchar, T.Dchar, T.Bool,
1722 T.Byte, T.Ubyte, T.Short, T.Ushort,
1723 T.Int, T.Uint, T.Long, T.Ulong,
1724 T.Float, T.Double, T.Real,
1725 T.Ifloat, T.Idouble, T.Ireal,
1726 T.Cfloat, T.Cdouble, T.Creal, T.Void:
1727 case_parseDeclaration:
1728 d = parseVariableOrFunction();
1729 goto LreturnDeclarationStatement;
1730 case T.If: 1717 case T.If:
1731 s = parseIfStatement(); 1718 s = parseIfStatement();
1732 break; 1719 break;
1733 case T.While: 1720 case T.While:
1734 s = parseWhileStatement(); 1721 s = parseWhileStatement();
1862 // case T.Mixin: 1849 // case T.Mixin:
1863 case T.Import: 1850 case T.Import:
1864 case T.Typeid: 1851 case T.Typeid:
1865 case T.Is: 1852 case T.Is:
1866 case T.LParen: 1853 case T.LParen:
1867 /+ // IntegralType
1868 case T.Char, T.Wchar, T.Dchar, T.Bool,
1869 T.Byte, T.Ubyte, T.Short, T.Ushort,
1870 T.Int, T.Uint, T.Long, T.Ulong,
1871 T.Float, T.Double, T.Real,
1872 T.Ifloat, T.Idouble, T.Ireal,
1873 T.Cfloat, T.Cdouble, T.Creal, T.Void:+/
1874 case T.Traits: // D2.0 1854 case T.Traits: // D2.0
1875 // Tokens that can start a UnaryExpression: 1855 // Tokens that can start a UnaryExpression:
1876 case T.AndBinary, 1856 case T.AndBinary, T.PlusPlus, T.MinusMinus, T.Mul, T.Minus,
1877 T.PlusPlus, 1857 T.Plus, T.Not, T.Tilde, T.New, T.Delete, T.Cast:
1878 T.MinusMinus,
1879 T.Mul,
1880 T.Minus,
1881 T.Plus,
1882 T.Not,
1883 T.Tilde,
1884 T.New,
1885 T.Delete,
1886 T.Cast:
1887 case_parseExpressionStatement: 1858 case_parseExpressionStatement:
1888 s = new ExpressionStatement(parseExpression()); 1859 s = new ExpressionStatement(parseExpression());
1889 require(T.Semicolon); 1860 require(T.Semicolon);
1890 break; 1861 break;
1891 default: 1862 default:
3691 e = parseExpression(); 3662 e = parseExpression();
3692 require(T.RParen); 3663 require(T.RParen);
3693 // TODO: create ParenExpression? 3664 // TODO: create ParenExpression?
3694 } 3665 }
3695 break; 3666 break;
3696 // IntegralType . Identifier
3697 case T.Char, T.Wchar, T.Dchar, T.Bool,
3698 T.Byte, T.Ubyte, T.Short, T.Ushort,
3699 T.Int, T.Uint, T.Long, T.Ulong,
3700 T.Float, T.Double, T.Real,
3701 T.Ifloat, T.Idouble, T.Ireal,
3702 T.Cfloat, T.Cdouble, T.Creal, T.Void:
3703 auto type = new IntegralType(token.type);
3704 nT();
3705 set(type, begin);
3706 require(T.Dot);
3707 auto ident = requireIdentifier(MSG.ExpectedIdAfterTypeDot);
3708
3709 e = new TypeDotIdExpression(type, ident);
3710 break;
3711 version(D2) 3667 version(D2)
3712 { 3668 {
3713 case T.Traits: 3669 case T.Traits:
3714 nT(); 3670 nT();
3715 require(T.LParen); 3671 require(T.LParen);
3721 require(T.RParen); 3677 require(T.RParen);
3722 e = new TraitsExpression(id, args); 3678 e = new TraitsExpression(id, args);
3723 break; 3679 break;
3724 } 3680 }
3725 default: 3681 default:
3726 if (token.isSpecialToken) 3682 if (token.isIntegralType)
3683 { // IntegralType . Identifier
3684 auto type = new IntegralType(token.type);
3685 nT();
3686 set(type, begin);
3687 require(T.Dot);
3688 auto ident = requireIdentifier(MSG.ExpectedIdAfterTypeDot);
3689 e = new TypeDotIdExpression(type, ident);
3690 }
3691 else if (token.isSpecialToken)
3727 { 3692 {
3728 e = new SpecialTokenExpression(token); 3693 e = new SpecialTokenExpression(token);
3729 nT(); 3694 nT();
3730 break; 3695 }
3731 } 3696 else
3732 3697 {
3733 error(MID.ExpectedButFound, "Expression", token.srcText); 3698 error(MID.ExpectedButFound, "Expression", token.srcText);
3734 e = new EmptyExpression(); 3699 e = new EmptyExpression();
3735 if (!trying) 3700 if (!trying)
3736 { 3701 {
3737 // Insert a dummy token and don't consume current one. 3702 // Insert a dummy token and don't consume current one.
3738 begin = lx.insertEmptyTokenBefore(token); 3703 begin = lx.insertEmptyTokenBefore(token);
3739 this.prevToken = begin; 3704 this.prevToken = begin;
3705 }
3740 } 3706 }
3741 } 3707 }
3742 set(e, begin); 3708 set(e, begin);
3743 return e; 3709 return e;
3744 } 3710 }
3788 3754
3789 Type parseBasicType() 3755 Type parseBasicType()
3790 { 3756 {
3791 auto begin = token; 3757 auto begin = token;
3792 Type t; 3758 Type t;
3793 // IdentifierType tident; 3759
3794 3760 if (token.isIntegralType)
3761 {
3762 t = new IntegralType(token.type);
3763 nT();
3764 }
3765 else
3795 switch (token.type) 3766 switch (token.type)
3796 { 3767 {
3797 case T.Char, T.Wchar, T.Dchar, T.Bool,
3798 T.Byte, T.Ubyte, T.Short, T.Ushort,
3799 T.Int, T.Uint, T.Long, T.Ulong,
3800 T.Float, T.Double, T.Real,
3801 T.Ifloat, T.Idouble, T.Ireal,
3802 T.Cfloat, T.Cdouble, T.Creal, T.Void:
3803 t = new IntegralType(token.type);
3804 nT();
3805 set(t, begin);
3806 break;
3807 case T.Identifier, T.Typeof, T.Dot: 3768 case T.Identifier, T.Typeof, T.Dot:
3808 t = parseDotListType(); 3769 t = parseDotListType();
3809 assert(!isNodeSet(t)); 3770 assert(!isNodeSet(t));
3810 set(t, begin);
3811 break; 3771 break;
3812 version(D2) 3772 version(D2)
3813 { 3773 {
3814 case T.Const: 3774 case T.Const:
3815 // const ( Type ) 3775 // const ( Type )
3816 nT(); 3776 nT();
3817 require(T.LParen); 3777 require(T.LParen);
3818 t = parseType(); 3778 t = parseType();
3819 require(T.RParen); 3779 require(T.RParen);
3820 t = new ConstType(t); 3780 t = new ConstType(t);
3821 set(t, begin);
3822 break; 3781 break;
3823 case T.Invariant: 3782 case T.Invariant:
3824 // invariant ( Type ) 3783 // invariant ( Type )
3825 nT(); 3784 nT();
3826 require(T.LParen); 3785 require(T.LParen);
3827 t = parseType(); 3786 t = parseType();
3828 require(T.RParen); 3787 require(T.RParen);
3829 t = new InvariantType(t); 3788 t = new InvariantType(t);
3830 set(t, begin);
3831 break; 3789 break;
3832 } // version(D2) 3790 } // version(D2)
3833 default: 3791 default:
3834 error(MID.ExpectedButFound, "BasicType", token.srcText); 3792 error(MID.ExpectedButFound, "BasicType", token.srcText);
3835 t = new UndefinedType(); 3793 t = new UndefinedType();
3836 nT(); 3794 nT();
3837 set(t, begin); 3795 }
3838 } 3796 return set(t, begin);
3839 return t;
3840 } 3797 }
3841 3798
3842 Type parseBasicType2(Type t) 3799 Type parseBasicType2(Type t)
3843 { 3800 {
3844 typeof(token) begin; 3801 typeof(token) begin;