diff parser/Parser.d @ 92:771ac63898e2 new_gen

A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
author Anders Johnsen <skabet@gmail.com>
date Mon, 05 May 2008 18:44:20 +0200
parents a49bb982a7b0
children 48bb2287c035
line wrap: on
line diff
--- a/parser/Parser.d	Mon May 05 17:07:16 2008 +0200
+++ b/parser/Parser.d	Mon May 05 18:44:20 2008 +0200
@@ -48,25 +48,41 @@
 
         if (t.isBasicType || t.isIdentifier)
         {
-            Id type = Id(lexer.next);
-            Id iden = Id(require(Tok.Identifier));
-            Token next = lexer.peek();
-            if (next.type == Tok.Seperator)
-            {
-                Token sep = lexer.next();
-                return action.actOnDeclarator(type, iden, null);
-            }
-            else if (next.type == Tok.Assign)
+            Id type;
+            Id iden;
+            int len = peekParseType;
+            if(lexer.peek(len).type == Tok.Identifier && len != 0)
             {
-                Token assign = lexer.next();
-                Exp exp = parseExpression();
-                require(Tok.Seperator);
-                return action.actOnDeclarator(type, iden, exp);
+                type = parseType;
+parseDeclAfterInvalidType:
+                iden = Id(require(Tok.Identifier));
+                Token next = lexer.peek();
+                if (next.type == Tok.Seperator)
+                {
+                    Token sep = lexer.next();
+                    return action.actOnDeclarator(type, iden, null);
+                }
+                else if (next.type == Tok.Assign)
+                {
+                    Token assign = lexer.next();
+                    Exp exp = parseExpression();
+                    require(Tok.Seperator);
+                    return action.actOnDeclarator(type, iden, exp);
+                }
+                else if (next.type == Tok.OpenParentheses)
+                    return parseFunc(type, iden);
+                else
+                    messages.report(UnexpectedTok, next.location).arg(next.getType);
             }
-            else if (next.type == Tok.OpenParentheses)
-                return parseFunc(type, iden);
-            else
-                messages.report(UnexpectedTok, next.location).arg(next.getType);
+            t = lexer.peek(len);
+            messages.report(InvalidDeclType, t.location)
+                .arg(sm.getText(t.asRange));
+            while(len--)
+                lexer.next;
+            while(lexer.peek.type != Tok.Identifier)
+                lexer.next;
+            type = Id(lexer.peek);
+            goto parseDeclAfterInvalidType;
         }
         else if (t.type == Tok.Struct)
         {
@@ -75,7 +91,10 @@
             
             return parseStruct(type, iden);
         }
-        messages.report(UnexpectedTok, t.location).arg(t.getType);
+        messages.report(UnexpectedTok, t.location)
+            .arg(t.getType)
+            .arg(Tok.Identifier)
+            .fatal(ExitLevel.Parser);
     }
 
     /**
@@ -330,8 +349,7 @@
         Id currentType;
 
         if ( !(type.isBasicType || type.type == Tok.Identifier) )
-            messages.report(UnexpectedTokSingle, type.location)
-                .arg(type.getType);
+            messages.report(InvalidType, type.location);
 
         currentType = Id(type);
         type = lexer.peek;
@@ -346,7 +364,8 @@
             else
             {
                 lexer.next;
-                currentType = ArrayId(currentType, action.actOnNumericConstant(require(Tok.Integer)));
+                if(lexer.peek.type == Tok.Integer)
+                    currentType = ArrayId(currentType, action.actOnNumericConstant(require(Tok.Integer)));
                 require(Tok.CloseBracket);
                 
             }
@@ -379,10 +398,15 @@
             {
                 if(lexer.peek(i++).type != Tok.OpenBracket)
                     return 0;
-                if(lexer.peek(i++).type != Tok.Integer)
-                    return 0;
-                if(lexer.peek(i++).type != Tok.CloseBracket)
-                    return 0;
+                if(lexer.peek(i).type == Tok.Integer)
+                {
+                    i++;
+                    if(lexer.peek(i++).type != Tok.CloseBracket)    
+                        return 0;
+                }
+                else
+                    if(lexer.peek(i++).type != Tok.CloseBracket)
+                        return 0;
                 
             }
             type = lexer.peek(i);
@@ -475,8 +499,8 @@
         else if (next.type == Tok.Integer)
             return action.actOnNumericConstant(next);
 
-        messages.report(ExpectedExp, next.location, true);
-//        assert(0, "Should not happen");
+        messages.report(ExpectedExp, next.location)
+            .fatal(ExitLevel.Parser);
         return null;
     }