# HG changeset patch # User Aziz K?ksal # Date 1191164372 -7200 # Node ID 6057113f9a44a7216bc31152867f9d9517bb90f0 # Parent ad7977fe315affc4370358326401cbbfc85ab684 Refactored Parser.try_(). Also tidied code up a bit. diff -r ad7977fe315a -r 6057113f9a44 trunk/src/dil/Parser.d --- a/trunk/src/dil/Parser.d Sun Sep 30 15:20:35 2007 +0200 +++ b/trunk/src/dil/Parser.d Sun Sep 30 16:59:32 2007 +0200 @@ -55,16 +55,13 @@ ImportDeclaration[] imports; - this(char[] srcText, string fileName) + this(char[] srcText, string filePath) { - lx = new Lexer(srcText, fileName); + lx = new Lexer(srcText, filePath); } - debug char* prev; - private void init() { - debug prev = lx.text.ptr; nT(); prevToken = token; } @@ -76,51 +73,45 @@ { lx.nextToken(); token = lx.token; - -debug if (!trying) -{ - writef("\33[32m%s\33[0m", token.type); - writef("%s", prev[0 .. token.end - prev]); - prev = token.end; -} } while (token.isWhitespace) // Skip whitespace } - +/+ void skipToOnePast(TOK tok) { for (; token.type != tok && token.type != T.EOF; nT()) {} nT(); } - - int trying; - int errorCount; - - ReturnType try_(ReturnType)(lazy ReturnType parseMethod, out bool success) ++/ + uint trying; + uint errorCount; + + /++ + This method executes the delegate parseMethod and when an error occurred + the state of the lexer and parser are restored. + +/ + ReturnType try_(ReturnType)(ReturnType delegate() parseMethod, out bool success) { -debug writef("\33[31mtry_\33[0m"); ++trying; -// auto len = errors.length; - auto oldToken = token; - auto oldPrevToken = prevToken; - auto oldCount = errorCount; -// auto lexerState = lx.getState(); + auto oldToken = this.token; + auto oldPrevToken = this.prevToken; + auto oldCount = this.errorCount; + auto result = parseMethod(); - // If the length of the array changed we know an error occurred. + + // Check if an error occurred. if (errorCount != oldCount) { -// lexerState.restore(); // Restore state of the Lexer object. -// errors = errors[0..len]; // Remove errors that were added when parseMethod() was called. - token = oldToken; - prevToken = oldPrevToken; - lx.token = oldToken; + // Restore members. + token = oldToken; + prevToken = oldPrevToken; + lx.token = oldToken; errorCount = oldCount; success = false; } else success = true; --trying; -debug writef("\33[34m%s\33[0m", success); return result; } @@ -525,7 +516,7 @@ } bool success; - auto si = try_(parseStructInitializer(), success); + auto si = try_(&parseStructInitializer, success); if (success) { init = si; @@ -1632,7 +1623,7 @@ goto case T.Dot; case T.Dot, T.Typeof: bool success; - d = try_(parseDeclaration(), success); + d = try_({return parseDeclaration();}, success); if (success) goto case_DeclarationStatement; // Declaration else @@ -1816,7 +1807,7 @@ assert( delegate bool(){ bool success; - auto expression = try_(parseExpression(), success); + auto expression = try_(&parseExpression, success); return success; }() == false, "Any token that could start a valid expression must have been caught by a case statement in parseStatement()." ); @@ -2004,7 +1995,7 @@ return type; } bool success; - auto type = try_(parseDeclaratorAssign(), success); + auto type = try_(&parseDeclaratorAssign, success); if (success) { auto init = parseExpression(); @@ -3400,7 +3391,7 @@ return type; } bool success; - auto type = try_(parseType_(), success); + auto type = try_(&parseType_, success); if (success) { auto ident = requireId(); @@ -3880,7 +3871,7 @@ require(T.RBracket); return type; } - auto assocType = try_(parseAAType(), success); + auto assocType = try_(&parseAAType, success); if (success) t = new ArrayType(t, assocType); else @@ -4136,7 +4127,7 @@ return null; } bool success; - auto typeArgument = try_(parseType_(), success); + auto typeArgument = try_(&parseType_, success); if (success) // TemplateArgument: // Type