# HG changeset patch # User Aziz K?ksal # Date 1197852766 -3600 # Node ID 135e9e6933a74f39bb4376a52ce834047055e4ac # Parent d0ac6faeaf66a839d5f8edd6b3bae5b2b2f28a7d Tidied up version/debug parser functions. diff -r d0ac6faeaf66 -r 135e9e6933a7 trunk/src/dil/Messages.d --- a/trunk/src/dil/Messages.d Sun Dec 16 23:00:05 2007 +0100 +++ b/trunk/src/dil/Messages.d Mon Dec 17 01:52:46 2007 +0100 @@ -136,4 +136,5 @@ auto ExpectedTypeOrExpression = "expected a type or and expression not ')'"; auto ExpectedAliasTemplateParam = "expected name for alias template parameter, not '{}'"; auto ExpectedNameForThisTempParam = "expected name for 'this' template parameter, not '{}'"; + auto ExpectedIdentOrInt = "expected an identifier or an integer, not '{}'"; } diff -r d0ac6faeaf66 -r 135e9e6933a7 trunk/src/dil/Parser.d --- a/trunk/src/dil/Parser.d Sun Dec 16 23:00:05 2007 +0100 +++ b/trunk/src/dil/Parser.d Mon Dec 17 01:52:46 2007 +0100 @@ -1243,58 +1243,50 @@ return new UnittestDeclaration(funcBody); } + Token* parseIdentOrInt() + { + if (token.type == T.Int32 || + token.type == T.Identifier) + { + auto token = this.token; + nT(); + return token; + } + else + error(token, MSG.ExpectedIdentOrInt, token.srcText); + return null; + } + Declaration parseDebugDeclaration() { assert(token.type == T.Debug); - nT(); // Skip debug keyword. - Token* spec; // debug = Integer ; - // debug = Identifier ; - Token* cond; // debug ( Integer ) - // debug ( Identifier ) + Token* spec; + Token* cond; Declaration decls, elseDecls; - void parseIdentOrInt(ref Token* tok) - { - nT(); - if (token.type == T.Int32 || - token.type == T.Identifier) - { - tok = token; - nT(); - } - else - expected(T.Identifier); // TODO: better error msg - } - if (token.type == T.Assign) - { - parseIdentOrInt(spec); + { // debug = Integer ; + // debug = Identifier ; + nT(); + spec = parseIdentOrInt(); require(T.Semicolon); } else - { - // Condition: - // Integer - // Identifier - // ( Condition ) + { // ( Condition ) if (token.type == T.LParen) { - parseIdentOrInt(cond); + nT(); + cond = parseIdentOrInt(); require(T.RParen); } - // debug DeclarationsBlock // debug ( Condition ) DeclarationsBlock decls = parseDeclarationsBlockNoColon(); - // else DeclarationsBlock if (token.type == T.Else) - { - nT(); - elseDecls = parseDeclarationsBlockNoColon(); - } + nT(), (elseDecls = parseDeclarationsBlockNoColon()); } return new DebugDeclaration(spec, cond, decls, elseDecls); @@ -1303,53 +1295,29 @@ Declaration parseVersionDeclaration() { assert(token.type == T.Version); - nT(); // Skip version keyword. - Token* spec; // version = Integer ; - // version = Identifier ; - Token* cond; // version ( Integer ) - // version ( Identifier ) + Token* spec; + Token* cond; Declaration decls, elseDecls; - void parseIdentOrInt(ref Token* tok) - { - if (token.type == T.Int32 || - token.type == T.Identifier) - { - tok = token; - nT(); - } - else - expected(T.Identifier); // TODO: better error msg - } - if (token.type == T.Assign) - { + { // version = Integer ; + // version = Identifier ; nT(); - parseIdentOrInt(spec); + spec = parseIdentOrInt(); require(T.Semicolon); } else - { - // Condition: - // Integer - // Identifier - - // ( Condition ) + { // ( Condition ) require(T.LParen); - parseIdentOrInt(cond); + cond = parseIdentOrInt(); require(T.RParen); - // version ( Condition ) DeclarationsBlock decls = parseDeclarationsBlockNoColon(); - // else DeclarationsBlock if (token.type == T.Else) - { - nT(); - elseDecls = parseDeclarationsBlockNoColon(); - } + nT(), (elseDecls = parseDeclarationsBlockNoColon()); } return new VersionDeclaration(spec, cond, decls, elseDecls); @@ -2482,55 +2450,22 @@ assert(token.type == T.Debug); nT(); // Skip debug keyword. - Token* cond; // debug ( Integer ) - // debug ( Identifier ) + Token* cond; Statement debugBody, elseBody; - void parseIdentOrInt(ref Token* tok) + // ( Condition ) + if (token.type == T.LParen) { nT(); - if (token.type == T.Int32 || - token.type == T.Identifier) - { - tok = token; - nT(); - } - else - expected(T.Identifier); // TODO: better error msg + cond = parseIdentOrInt(); + require(T.RParen); } - -// if (token.type == T.Assign) -// { -// parseIdentOrInt(identSpec, levelSpec); -// require(T.Semicolon); -// } -// else - { - // Condition: - // Integer - // Identifier - - // ( Condition ) - if (token.type == T.LParen) - { - parseIdentOrInt(cond); - require(T.RParen); - } - - // debug Statement - // debug ( Condition ) Statement - debugBody = parseNoScopeStatement(); - - // else Statement - if (token.type == T.Else) - { - // debug without condition and else body makes no sense - //if (levelCond == -1 && identCond.length == 0) - // TODO: issue error msg - nT(); - elseBody = parseNoScopeStatement(); - } - } + // debug Statement + // debug ( Condition ) Statement + debugBody = parseNoScopeStatement(); + // else Statement + if (token.type == T.Else) + nT(), (elseBody = parseNoScopeStatement()); return new DebugStatement(cond, debugBody, elseBody); } @@ -2538,51 +2473,20 @@ Statement parseVersionStatement() { assert(token.type == T.Version); - nT(); // Skip version keyword. - Token* cond; // version ( Integer ) - // version ( Identifier ) + Token* cond; Statement versionBody, elseBody; - void parseIdentOrInt(ref Token* tok) - { - if (token.type == T.Int32 || - token.type == T.Identifier) - { - tok = token; - nT(); - } - else - expected(T.Identifier); // TODO: better error msg - } - -// if (token.type == T.Assign) -// { -// parseIdentOrInt(identSpec, levelSpec); -// require(T.Semicolon); -// } -// else - { - // Condition: - // Integer - // Identifier - - // ( Condition ) - require(T.LParen); - parseIdentOrInt(cond); - require(T.RParen); - - // version ( Condition ) Statement - versionBody = parseNoScopeStatement(); - - // else Statement - if (token.type == T.Else) - { - nT(); - elseBody = parseNoScopeStatement(); - } - } + // ( Condition ) + require(T.LParen); + cond = parseIdentOrInt(); + require(T.RParen); + // version ( Condition ) Statement + versionBody = parseNoScopeStatement(); + // else Statement + if (token.type == T.Else) + nT(), (elseBody = parseNoScopeStatement()); return new VersionStatement(cond, versionBody, elseBody); }