예제 #1
0
파일: Parser.cs 프로젝트: strunberg/TEA
        public bool ParseClassDeclaration(IdentifierToken typeName, TokenReader reader, out TypeDeclaration type)
        {
            type = null;
            Token tok = reader.Peek();
            bool isPublic = false;
            bool isStatic = false;
            while (tok.Is(Keyword.Public) || tok.Is(Keyword.Static))
            {
                if (tok.Is(Keyword.Public))
                {
                    isPublic = true;
                }
                else
                {
                    isStatic = true;
                }

                reader.Read();
                tok = reader.Peek();
            }

            if (!this.Expect(reader, Keyword.Class))
            {
                return false;
            }

            string baseType = null;
            tok = reader.Peek();
            if (tok.Is(Keyword.LeftParen))
            {
                reader.Read();

                if (!this.ParseFullNameDeclaration(reader, out baseType))
                {
                    return false;
                }

                if (!this.Expect(reader, Keyword.RightParen))
                {
                    return false;
                }
            }

            ClassDeclaration classDecl = new ClassDeclaration(
                typeName,
                typeName.Value,
                baseType,
                isStatic,
                isPublic);

            tok = reader.Peek();
            if (tok.Is(Keyword.Public))
            {
                reader.Read();
                tok = reader.Peek();
                while (
                    tok.Is(Keyword.Procedure) ||
                    tok.Is(Keyword.Function) ||
                    tok.Is(Keyword.Static) ||
                    tok.Is(Keyword.Virtual) ||
                    tok.Is(Keyword.Abstract) ||
                    tok.Is(Keyword.Constructor) ||
                    tok.Is(Keyword.Destructor))
                {
                    MethodDeclaration methodDecl = null;
                    if (!this.ParseMethodDeclaration(reader, out methodDecl))
                    {
                        return false;
                    }

                    classDecl.AddPublicMethod(methodDecl);
                    if (!this.Expect(reader, Keyword.SemiColon))
                    {
                        return false;
                    }

                    tok = reader.Peek();
                }
            }

            tok = reader.Peek();
            if (tok.Is(Keyword.Protected))
            {
                reader.Read();
                tok = reader.Peek();
                while (
                    tok.Is(Keyword.Procedure) ||
                    tok.Is(Keyword.Function) ||
                    tok.Is(Keyword.Static) ||
                    tok.Is(Keyword.Virtual) ||
                    tok.Is(Keyword.Abstract) ||
                    tok.Is(Keyword.Constructor) ||
                    tok.Is(Keyword.Destructor))
                {
                    MethodDeclaration methodDecl = null;
                    if (!this.ParseMethodDeclaration(reader, out methodDecl))
                    {
                        return false;
                    }

                    classDecl.AddProtectedMethod(methodDecl);
                    if (!this.Expect(reader, Keyword.SemiColon))
                    {
                        return false;
                    }

                    tok = reader.Peek();
                }
            }

            tok = reader.Peek();
            if (tok.Is(Keyword.Private))
            {
                reader.Read();
                tok = reader.Peek();
                while (
                    tok.Is(Keyword.Procedure) ||
                    tok.Is(Keyword.Function) ||
                    tok.Is(Keyword.Static) ||
                    tok.Is(Keyword.Constructor) ||
                    tok.Is(Keyword.Destructor))
                {
                    MethodDeclaration methodDecl = null;
                    if (!this.ParseMethodDeclaration(reader, out methodDecl))
                    {
                        return false;
                    }

                    classDecl.AddPrivateMethod(methodDecl);
                    if (!this.Expect(reader, Keyword.SemiColon))
                    {
                        return false;
                    }

                    tok = reader.Peek();
                }
            }

            tok = reader.Peek();
            if (tok.Is(Keyword.Var))
            {
                VarBlock varBlock = null;
                if (!this.ParseVarBlock(reader, false, out varBlock))
                {
                    return false;
                }

                classDecl.Fields = varBlock;
            }

            if (!this.Expect(reader, Keyword.End))
            {
                return false;
            }

            type = classDecl;
            return true;
        }
예제 #2
0
파일: Parser.cs 프로젝트: strunberg/TEA
        private bool ParseEnumDeclaration(IdentifierToken typeName, TokenReader reader, out TypeDeclaration type)
        {
            EnumDeclaration enumDecl = new EnumDeclaration(typeName, typeName.Value);
            type = null;
            if (!this.Expect(reader, Keyword.LeftParen))
            {
                return false;
            }

            string name = null;
            if (!this.ExpectIdentifier(reader, out name))
            {
                return false;
            }

            enumDecl.AddValue(name);

            Token tok = reader.Peek();
            while (tok.Is(Keyword.Comma))
            {
                reader.Read();
                if (!this.ExpectIdentifier(reader, out name))
                {
                    return false;
                }

                enumDecl.AddValue(name);

                tok = reader.Peek();
            }

            if (!this.Expect(reader, Keyword.RightParen))
            {
                return false;
            }

            type = enumDecl;
            return true;
        }
예제 #3
0
 public void AddType(TypeDeclaration type)
 {
     types.Add(type.Name, type);
     typeList.Add(type);
 }