Esempio n. 1
0
        public static RootNode ParseProgram(List <TokenAnalyzer.Token> TokensList)
        {
            TokensArray = TokensList.ToArray();
            int      TokenNumber = 0;
            RootNode node        = new RootNode();

            while (true)
            {
                ClassDeclarationNode ClassDeclNode = ClassDeclarationParser.parse(TokenNumber);
                if (ClassDeclNode.BadParsed)
                {
                    ExitWithError(ClassDeclNode.ErrorLine);
                }
                else
                {
                    node.AddClassDeclaration(ClassDeclNode);
                    TokenNumber = ClassDeclNode.TokenNumber;
                }
                if (TokenNumber >= TokensArray.Length)
                {
                    break;
                }
            }
            return(node);
        }
        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);
                }
            }
        }
Esempio n. 3
0
 public void AddClassDeclaration(ClassDeclarationNode node)
 {
     ClassDeclarations.Add(node);
 }