diff parser/Parser.d @ 89:a49bb982a7b0 new_gen

Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
author Anders Johnsen <skabet@gmail.com>
date Sun, 04 May 2008 20:27:01 +0200
parents eb5b2c719a39
children 771ac63898e2
line wrap: on
line diff
--- a/parser/Parser.d	Sun May 04 18:13:46 2008 +0200
+++ b/parser/Parser.d	Sun May 04 20:27:01 2008 +0200
@@ -5,7 +5,7 @@
 
 import parser.Action;
 
-import misc.Error;
+import basic.Message;
 
 import basic.SmallArray,
        basic.SourceManager;
@@ -16,11 +16,18 @@
 class Parser
 {
     Action action;
+    MessageHandler messages;
     alias Object Exp;
     alias Object Stmt;
     alias Object Decl;
 
 public:
+
+    this(MessageHandler messages)
+    {
+        this.messages = messages;
+    }
+
     Decl[] parse(SourceManager sm, Lexer lexer, Action act)
     {
         this.sm = sm;
@@ -59,9 +66,7 @@
             else if (next.type == Tok.OpenParentheses)
                 return parseFunc(type, iden);
             else
-                throw error(__LINE__, PE.UnexpectedTok)
-                    .tok(next)
-                    .arg(next.getType);
+                messages.report(UnexpectedTok, next.location).arg(next.getType);
         }
         else if (t.type == Tok.Struct)
         {
@@ -70,8 +75,7 @@
             
             return parseStruct(type, iden);
         }
-        char[] c = t.getType;
-        throw error(__LINE__, PE.UnexpectedTok).tok(t).arg(c);
+        messages.report(UnexpectedTok, t.location).arg(t.getType);
     }
 
     /**
@@ -102,8 +106,7 @@
                 action.actOnStructMember(decl, var_type, var_iden, exp);
                 continue;
             }
-            throw error(__LINE__, PE.UnexpectedTok)
-                .tok(next).arg(next.getType);
+            messages.report(UnexpectedTok, next.location).arg(next.getType);
         }
 
         require(Tok.CloseBrace);
@@ -187,7 +190,7 @@
                     if ( n.type == Tok.Star || n.type == Tok.OpenBracket)
                     {
                         int len = peekParseType;
-                        if(lexer.peek(len).type == Tok.Identifier || len == 0)
+                        if(lexer.peek(len).type == Tok.Identifier && len != 0)
                             return action.actOnDeclStmt(parseVarDecl());
 
                         Exp exp = parseExpression();
@@ -206,7 +209,7 @@
                 }
 
             case Tok.Switch:
-                throw error(__LINE__, ":(").tok(lexer.peek);
+                messages.report(UnexpectedTok, lexer.peek.location).arg(lexer.next.getType);
                 return null;
 
             default:
@@ -218,10 +221,9 @@
                     require(Tok.Seperator);
                     return action.actOnExprStmt(exp);
                 }
-                    
-                throw error(__LINE__, "Unexpexted begining of statement.").tok(lexer.peek);
+                messages.report(UnexpectedBeginStmt, lexer.peek.location).arg(lexer.next.getType);
         }
-        throw error(__LINE__, "").tok(t);
+        messages.report(UnexpectedTok, t.location);
         return null;
     }
 
@@ -313,10 +315,9 @@
         if (tok.type is Tok.Identifier)
             return Id(tok);
 
-        throw error(__LINE__, PE.UnexpectedTokSingle)
+        messages.report(UnexpectedTokSingle, tok.location)
             .arg(tok.getType)
-            .arg(Tok.Identifier)
-            .tok(tok);
+            .arg(Tok.Identifier);
     }
 
     /**
@@ -329,9 +330,8 @@
         Id currentType;
 
         if ( !(type.isBasicType || type.type == Tok.Identifier) )
-            throw error(__LINE__, "Unexpected token in Type parsing. Got %0")
-                .arg(type.getType)
-                .tok(type);
+            messages.report(UnexpectedTokSingle, type.location)
+                .arg(type.getType);
 
         currentType = Id(type);
         type = lexer.peek;
@@ -364,7 +364,7 @@
         Id currentType;
 
         if ( !(type.isBasicType || type.type == Tok.Identifier) )
-            return i;
+            return 0;
 
         currentType = Id(type);
         type = lexer.peek(++i);
@@ -407,7 +407,7 @@
                         return parsePostfixExp(exp);
                     default:
                         Token t = lexer.peek(1);
-                        throw error(__LINE__, "Expected identifier after '.'").tok(t);
+                        messages.report(ExpectedIdAfterDot, t.location);
                 }
             case Tok.OpenBracket:
                 Token open = lexer.next;
@@ -475,10 +475,9 @@
         else if (next.type == Tok.Integer)
             return action.actOnNumericConstant(next);
 
-        throw error(__LINE__, "Expected expression, not '%0'")
-            .tok(next)
-            .arg(next.getType);
-        assert(0, "Should not happen");
+        messages.report(ExpectedExp, next.location, true);
+//        assert(0, "Should not happen");
+        return null;
     }
 
     Exp parseCast(ref Token _cast)
@@ -486,9 +485,7 @@
         require(Tok.OpenParentheses);
         auto next = lexer.next;
         if(!next.isBasicType && !next.isIdentifier)
-            throw error(__LINE__, "Expected cast type, not %0")
-                .tok(next)
-                .arg(next.getType);
+            messages.report(ExpectedCastType, next.location);
         
         require(Tok.CloseParentheses);
         auto exp = P();
@@ -554,10 +551,9 @@
     Token require(Tok t)
     {
         if (lexer.peek().type != t)
-            throw error(__LINE__, PE.UnexpectedTokSingle)
+            messages.report(UnexpectedTokSingle, lexer.peek.location)
                 .arg(lexer.peek.getType)
-                .arg(t)
-                .tok(lexer.peek);
+                .arg(t);
         return lexer.next();
     }
 
@@ -569,26 +565,6 @@
         return true;
     }
 
-    Error error(uint line, char[] errMsg)
-    {
-        SLoc loc = lexer.peek.location;
-        auto e =
-            new Error("Parser.d(" ~ Integer.toString(line) ~ "): " ~errMsg);
-            //e.loc(loc);
-            return e;
-    }
-
-    struct PE
-    {
-        static char[]
-            UnexpectedTokMulti  = "Unexpected token, got %0 expected one of %1",
-            UnexpectedTokSingle = "Unexpected token, got %0 expected %1",
-            UnexpectedTok       = "Unexpected token %0";
-
-        static char[]
-            CaseValueMustBeInt  = "Cases can only be integer literals";
-    }
-
     Lexer lexer;
     SourceManager sm;
 }