# HG changeset patch # User aziz # Date 1183747023 0 # Node ID 0eb4c8a5b32b35173cc4998fb4c4c0625f9669a2 # Parent 0fe650a7a8d1f415e4eb5d318f66383eb1652fd7 - Added TOK.Invalid. - Moved STC as StorageClass to module Type. - Added In, Out, Ref, Lazy and Variadic to StorageClass. - Added member 'next' to class Type to represent non-basic types like pointers, arrays etc. - Added class PointerType and ArrayType. - Added class Argument to represent function parameters. - Implemented most of parseBasicType2() and parseParameters(). diff -r 0fe650a7a8d1 -r 0eb4c8a5b32b trunk/src/Parser.d --- a/trunk/src/Parser.d Fri Jul 06 15:23:04 2007 +0000 +++ b/trunk/src/Parser.d Fri Jul 06 18:37:03 2007 +0000 @@ -10,19 +10,31 @@ import Expressions; import Types; -enum STC + +class Argument { - Abstract, - Auto, - Const, - Deprecated, - Extern, - Final, - Invariant, - Override, - Scope, - Static, - Synchronized + StorageClass stc; + Type type; + string ident; + Expression assignExpr; + + this(StorageClass stc, Type type, string ident, Expression assignExpr) + { + this.stc = stc; + this.type = type; + this.ident = ident; + this.assignExpr = assignExpr; + } + + bool isVariadic() + { + return !!(stc & StorageClass.Variadic); + } + + bool isOnlyVariadic() + { + return stc == StorageClass.Variadic; + } } private alias TOK T; @@ -632,6 +644,102 @@ return t; } + Type parseBasicType2(Type t) + { + while (1) + { + switch (token.type) + { + case T.Mul: + t = new PointerType(t); + break; + case T.LBracket: + nT(); + if (token.type == T.RBracket) + { + t = new ArrayType(t, null); + nT(); + } + else + { + t = new ArrayType(t, parseExpression()); + require(T.RBracket); + } + continue; + case T.Function, T.Delegate: + TOK tok = token.type; + nT(); + auto args = parseParameters(); +// if (tok == T.Function) +// t = new FunctionType(); +// else +// t = new DelegateType(); + default: + return t; + } + nT(); + } + assert(0); + } + + Argument[] parseParameters() + { + require(T.LParen); + + if (token.type == T.RParen) + { + nT(); + return null; + } + + Argument[] args; + StorageClass stc; + + while (1) + { + stc = StorageClass.In; + switch (token.type) + { + case T.In: stc = StorageClass.In; nT(); goto default; + case T.Out: stc = StorageClass.Out; nT(); goto default; + case T.Ref: stc = StorageClass.Ref; nT(); goto default; + case T.Lazy: stc = StorageClass.Lazy; nT(); goto default; + case T.Ellipses: + args ~= new Argument(StorageClass.Variadic, null, null, null); + break; + default: + Type type = parseBasicType(); + string ident; + // parseDeclarator() + Expression assignExpr; + if (token.type == T.Assign) + { + nT(); + assignExpr = parseAssignExpression(); + } + + if (token.type == T.Ellipses) + { + stc |= StorageClass.Variadic; + args ~= new Argument(stc, type, ident, assignExpr); + nT(); + break; + } + + args ~= new Argument(stc, type, ident, assignExpr); + if (token.type == T.Comma) + { + nT(); + continue; + } + break; + } + break; + } + require(T.RParen); + return args; + } + void errorIfNot(TOK tok) { if (token.type != tok) diff -r 0fe650a7a8d1 -r 0eb4c8a5b32b trunk/src/Token.d --- a/trunk/src/Token.d Fri Jul 06 15:23:04 2007 +0000 +++ b/trunk/src/Token.d Fri Jul 06 18:37:03 2007 +0000 @@ -12,6 +12,8 @@ enum TOK { + Invalid, + Identifier, Comment, String, diff -r 0fe650a7a8d1 -r 0eb4c8a5b32b trunk/src/Types.d --- a/trunk/src/Types.d Fri Jul 06 15:23:04 2007 +0000 +++ b/trunk/src/Types.d Fri Jul 06 18:37:03 2007 +0000 @@ -6,12 +6,39 @@ import Token; import Expressions; +enum StorageClass +{ + None = 0, + Abstract = 1, + Auto = 1<<2, + Const = 1<<3, + Deprecated = 1<<4, + Extern = 1<<5, + Final = 1<<6, + Invariant = 1<<7, + Override = 1<<8, + Scope = 1<<9, + Static = 1<<10, + Synchronized = 1<<11, + In = 1<<12, + Out = 1<<13, + Ref = 1<<14, + Lazy = 1<<15, + Variadic = 1<<16, +} + class Type { TOK type; + Type next; + this(TOK type) + { this(type, null); } + + this(TOK type, Type next) { this.type = type; + this.next = next; } } @@ -21,13 +48,13 @@ this(string[] idents) { - super(TOK.Identifier); + super(TOK.Identifier, null); this.idents = idents; } this(TOK type) { - super(type); + super(type, null); } void opCatAssign(string ident) @@ -45,3 +72,21 @@ this.e = e; } } + +class PointerType : Type +{ + this(Type t) + { + super(TOK.Mul, t); + } +} + +class ArrayType : Type +{ + Expression e; + this(Type t, Expression e) + { + super(TOK.Invalid, t); + this.e = e; + } +}