コード例 #1
0
        public static VariableDeclarationNode parse(int TokenNumber)
        {
            VariableDeclarationNode node = new VariableDeclarationNode();

            if (!(Parser.NeededToken(TokenNumber, "value", "var", 0) &&
                  Parser.NeededToken(TokenNumber + 1, "type", "name", 0) &&
                  Parser.NeededToken(TokenNumber + 2, "value", ":", 0)))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            node.SetName(Parser.TokensArray[TokenNumber + 1].Value);
            TokenNumber += 3;
            ExpressionNode ExprNode = ExpressionParser.parse(TokenNumber);

            if (ExprNode.BadParsed)
            {
                node.BadParsed = true;
                node.ErrorLine = ExprNode.ErrorLine;
                return(node);
            }
            node.SetExpression(ExprNode);
            node.TokenNumber = ExprNode.TokenNumber;
            return(node);
        }
コード例 #2
0
        public static ClassDeclarationNode parse(int TokenNumber)
        {
            ClassDeclarationNode node = new ClassDeclarationNode();

            if (!(Parser.NeededToken(TokenNumber, "value", "class", 0) && Parser.NeededToken(TokenNumber + 1, "type", "name", 0)))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            node.SetName(Parser.TokensArray[TokenNumber + 1].Value);
            TokenNumber += 2;
            if (Parser.NeededToken(TokenNumber, "value", "extends", 0) && Parser.NeededToken(TokenNumber + 1, "type", "name", 0))
            {
                node.SetSuperClass(Parser.TokensArray[TokenNumber + 1].Value);
                TokenNumber += 2;
            }
            if (!Parser.NeededToken(TokenNumber, "value", "is", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            while (true)
            {
                if (Parser.NeededToken(TokenNumber, "value", "end", 0))
                {
                    TokenNumber++;
                    node.TokenNumber = TokenNumber;
                    return(node);
                }
                VariableDeclarationNode VariableNode = VariableDeclarationParser.parse(TokenNumber);
                if (!VariableNode.BadParsed)
                {
                    TokenNumber = VariableNode.TokenNumber;
                    node.AddVariableDeclaration(VariableNode);
                    continue;
                }
                MethodDeclarationNode MethodNode = MethodDeclarationParser.parse(TokenNumber);
                if (!MethodNode.BadParsed)
                {
                    TokenNumber = MethodNode.TokenNumber;
                    node.AddMethodDeclaration(MethodNode);
                    continue;
                }
                ConstructorDeclarationNode ConstructorNode = ConstructorDeclarationParser.parse(TokenNumber);
                if (ConstructorNode.BadParsed)
                {
                    node.BadParsed = true;
                    node.ErrorLine = Math.Max(VariableNode.ErrorLine, Math.Max(MethodNode.ErrorLine, ConstructorNode.ErrorLine));
                    return(node);
                }
                TokenNumber = ConstructorNode.TokenNumber;
                node.AddConstructorDeclaraton(ConstructorNode);
                if (TokenNumber >= Parser.TokensArray.Length)
                {
                    node.BadParsed = true;
                    node.ErrorLine = Parser.TokensArray[Parser.TokensArray.Length - 1].Line;
                    return(node);
                }
            }
        }
コード例 #3
0
        public static List <Node> parse(int TokenNumber)
        {
            List <Node> BodyNodes = new List <Node>();

            while (true)
            {
                if (!(Parser.NeededToken(TokenNumber, "type", "name", 0) ||
                      Parser.NeededToken(TokenNumber, "value", "while", 0) ||
                      Parser.NeededToken(TokenNumber, "value", "if", 0) ||
                      Parser.NeededToken(TokenNumber, "value", "return", 0) ||
                      Parser.NeededToken(TokenNumber, "value", "var", 0) ||
                      Parser.NeededToken(TokenNumber, "value", "this", 0)))
                {
                    return(BodyNodes);
                }
                VariableDeclarationNode VariableNode = VariableDeclarationParser.parse(TokenNumber);
                if (!VariableNode.BadParsed)
                {
                    BodyNodes.Add(VariableNode);
                    TokenNumber = VariableNode.TokenNumber;
                    continue;
                }
                AssignmentNode anode = AssignmentParser.parse(TokenNumber);
                if (!anode.BadParsed)
                {
                    BodyNodes.Add(anode);
                    TokenNumber = anode.TokenNumber;
                    continue;
                }
                WhileLoopNode LoopNode = WhileLoopParser.parse(TokenNumber);
                if (!LoopNode.BadParsed)
                {
                    BodyNodes.Add(LoopNode);
                    TokenNumber = LoopNode.TokenNumber;
                    continue;
                }
                IfStatementNode IfNode = IfStatementParser.parse(TokenNumber);
                if (!IfNode.BadParsed)
                {
                    BodyNodes.Add(IfNode);
                    TokenNumber = IfNode.TokenNumber;
                    continue;
                }
                ReturnStatementNode ReturnNode = ReturnStatementParser.parse(TokenNumber);
                if (!ReturnNode.BadParsed)
                {
                    BodyNodes.Add(ReturnNode);
                    TokenNumber = ReturnNode.TokenNumber;
                    continue;
                }
                CallNode cnode = CallParser.parse(TokenNumber);
                if (cnode.BadParsed)
                {
                    Node node = new Node();
                    node.BadParsed = true;
                    node.ErrorLine = Math.Max(Math.Max(VariableNode.ErrorLine, anode.ErrorLine),
                                              Math.Max(LoopNode.ErrorLine, IfNode.ErrorLine));
                    node.ErrorLine = Math.Max(Math.Max(ReturnNode.ErrorLine, cnode.ErrorLine), node.ErrorLine); //deepest parsing error
                    BodyNodes.Add(node);
                    return(BodyNodes);
                }
                BodyNodes.Add(cnode);
                TokenNumber = cnode.TokenNumber;
                if (TokenNumber >= Parser.TokensArray.Length)  //if tokens ended
                {
                    Node node = new Node();
                    node.BadParsed = true;
                    node.ErrorLine = Parser.TokensArray[Parser.TokensArray.Length - 1].Line;
                    return(BodyNodes);
                }
            }
        }
コード例 #4
0
 public void AddVariableDeclaration(VariableDeclarationNode node)
 {
     VariableDeclarations.Add(node);
 }