# HG changeset patch # User Anders Johnsen # Date 1209733502 -7200 # Node ID 86aec21602211ce1ebec01fba4f84b3a8e1decb7 # Parent 192da4976daad5968d49c906a142d987c2049657 Parsing "int* x" diff -r 192da4976daa -r 86aec2160221 parser/Action.d --- a/parser/Action.d Fri May 02 13:19:23 2008 +0200 +++ b/parser/Action.d Fri May 02 15:05:02 2008 +0200 @@ -22,8 +22,14 @@ } -struct Id +class Id { + public static Id opCall(Token tok) + { + auto id = new Id(); + id.tok = tok; + return id; + } Token tok; char[] toString() @@ -32,6 +38,18 @@ } } +class PointerId : Id +{ + public static PointerId opCall(Id id) + { + auto p = new PointerId(); + p.id = id; + return p; + } + + Id id; +} + /** All methods are optional. diff -r 192da4976daa -r 86aec2160221 parser/Parser.d --- a/parser/Parser.d Fri May 02 13:19:23 2008 +0200 +++ b/parser/Parser.d Fri May 02 15:05:02 2008 +0200 @@ -39,7 +39,7 @@ if (t.isBasicType || t.isIdentifier) { - Id type = Id(lexer.next); + Id type = parseType;//Id(lexer.next); Id iden = Id(require(Tok.Identifier)); Token next = lexer.peek(); if (next.type == Tok.Seperator) @@ -180,11 +180,12 @@ Token n = lexer.peek(1); // Must be an decl, if we start with a basic type, or two // identifiers in a row - if (iden.isBasicType() && iden.isIdentifier() || n.isIdentifier()) + if (iden.isBasicType() || iden.isIdentifier()) { // manually hardcoded to only support "type id [= exp];" // as that is the only thing the codegen understands - Id type = Id(lexer.next); + Id type = parseType; + n = lexer.peek(); Id id = Id(lexer.next); Exp init; if (skip(Tok.Assign)) @@ -288,15 +289,31 @@ .tok(tok); } + /** + Parse a type - this includes pointer and array(at some point) types. + */ Id parseType() { Token type = lexer.next; - if (type.isBasicType || type.type == Tok.Identifier) - return Id(type); + Id currentType; + + if ( !(type.isBasicType || type.type == Tok.Identifier) ) + throw error(__LINE__, "Unexpected token in Type parsing. Got %0") + .arg(type.getType) + .tok(type); - char[] c = type.getType; - error(__LINE__, "Unexpected token in Type parsing. Got %0").arg(c); + currentType = Id(type); + type = lexer.peek; + + while(type.type == Tok.Star) + { + currentType = PointerId(currentType); + lexer.next; + type = lexer.peek; + } + + return currentType; } private: