Exemplo n.º 1
0
        public static AstNode ParseTerm(TokenStream stream)
        {
            switch (stream.Current.Class) {
            case TokenClass.Identifier:
                return new NameExpression (stream.Location, stream.ReadToken ().Value);
            case TokenClass.IntLiteral:
                return new IntegerExpression (stream.Location, long.Parse (
                    stream.ReadToken ().Value));
            case TokenClass.FloatLiteral:
                return new FloatExpression (stream.Location, double.Parse (
                    stream.ReadToken ().Value));
            case TokenClass.InterpolatedStringLiteral:
                AstNode val = ParseString (stream.Location, stream.ReadToken ().Value);
                if (val == null) {
                    stream.MakeError ();
                    return new StringExpression (stream.Location, "");
                }
                return val;
            case TokenClass.StringLiteral:
                return new StringExpression (stream.Location, stream.ReadToken ().Value);
            case TokenClass.OpenBracket:
                return ParseList (stream);
            case TokenClass.OpenBrace:
                return ParseHash (stream);
            case TokenClass.OpenParan:
                stream.ReadToken ();
                AstNode expr = ParseExpression (stream);
                if (stream.Accept (TokenClass.Comma)) {
                    return ParseTuple (expr, stream);
                }
                stream.Expect (TokenClass.CloseParan);
                return expr;
            case TokenClass.Keyword:
                switch (stream.Current.Value) {
                case "self":
                    stream.ReadToken ();
                    return new SelfStatement (stream.Location);
                case "true":
                    stream.ReadToken ();
                    return new TrueExpression (stream.Location);
                case "false":
                    stream.ReadToken ();
                    return new FalseExpression (stream.Location);
                case "null":
                    stream.ReadToken ();
                    return new NullExpression (stream.Location);
                case "lambda":
                    return ParseLambda (stream);
                case "match":
                    return ParseMatch (stream);
                }
                break;
            }

            stream.MakeError ();
            return null;
        }
Exemplo n.º 2
0
 private static AstNode ParseStatement(TokenStream stream)
 {
     if (stream.Match (TokenClass.Keyword)) {
         switch (stream.Current.Value) {
         case "class":
             return ParseClass (stream);
         case "enum":
             return ParseEnum (stream);
         case "interface":
             return ParseInterface (stream);
         case "func":
             return ParseFunction (stream);
         case "if":
             return ParseIf (stream);
         case "given":
             return ParseGiven (stream);
         case "for":
             return ParseFor (stream);
         case "foreach":
             return ParseForeach (stream);
         case "with":
             return ParseWith (stream);
         case "while":
             return ParseWhile (stream);
         case "do":
             return ParseDoWhile (stream);
         case "use":
             return ParseUse (stream);
         case "return":
             return ParseReturn (stream);
         case "raise":
             return ParseRaise (stream);
         case "yield":
             return ParseYield (stream);
         case "try":
             return ParseTryExcept (stream);
         case "break":
             stream.Accept (TokenClass.Keyword);
             return new BreakStatement (stream.Location);
         case "continue":
             stream.Accept (TokenClass.Keyword);
             return new ContinueStatement (stream.Location);
         case "super":
             stream.ErrorLog.AddError (ErrorType.ParserError, stream.Location,
                 "super () constructor must be called first!");
             return ParseSuperCall (stream, new ClassDeclaration (stream.Location, "", null));
         }
     }
     if (stream.Match (TokenClass.OpenBrace)) {
         return ParseBlock (stream);
     } else if (stream.Accept (TokenClass.SemiColon)) {
         return new Statement (stream.Location);
     } else if (stream.Match (TokenClass.Operator, "@")) {
         return ParseFunction (stream);
     } else {
         AstNode node = ParseExpression (stream);
         if (node == null) {
             stream.MakeError ();
         }
         return new Expression (stream.Location, node);
     }
 }