Exemple #1
0
		private void ParseClass()											
		{
			uint classMask = ~(uint)Modifier.ClassMods;
			if ( ((uint)curmods & classMask) != (uint)Modifier.Empty)
				ReportError("Class contains illegal modifiers.");

			ClassNode node = new ClassNode(curtok);

            if (curAttributes.Count > 0)
            {
                node.Attributes = curAttributes;
                curAttributes = new NodeCollection<AttributeNode>();
            }

            node.IsPartial = nextIsPartial;
            nextIsPartial = false;

            ClassNode cl = typeStack.Count == 0 ? null : typeStack.Peek(); //retrieve the parent class if existing;

			node.Modifiers = curmods;
			curmods = Modifier.Empty;

            if ((node.Modifiers & Modifier.Unsafe) != Modifier.Empty)
            {
                //unsafe modifier -> unsafe type.
                isUnsafe++;
                node.IsUnsafeDeclared = true;
            }

            //the class is declared in an unsafe type ?
            node.IsUnsafe = isUnsafe > 0;

            if (cl != null)
            {
                CheckStaticClass(cl, node.Modifiers, false);
            }

            if ( node.IsStatic  )
            {
                if (( node.Modifiers  & (Modifier.Sealed | Modifier.Abstract)) != Modifier.Empty)
                {
                    ReportError("A class delared as 'static' can not be declared nor 'sealed', nor 'abstract'.");
                }
            }

			Advance(); // advance over Class token

            if (curtok.ID != TokenID.Ident)
            {
                string msg = "Error: Expected " + TokenID.Ident + " found: " + curtok.ID;

                ReportError(msg);

                //to stay coherent with the rest of the parser
                //it generate a random class name 

                node.Name = new IdentifierExpression("no_name_" + (no_name_index++).ToString(), node.RelatedToken );

            }
            else
            {
                node.Name = (IdentifierExpression)ParseIdentifierOrKeyword(false, false, false, true);
            }

            ParsePossibleTypeParameterNode(true, true, false);
            ApplyTypeParameters(node);

			if (curtok.ID == TokenID.Colon) // for base members
			{
                if (node.IsStatic)
                {
                    ReportError("Static class can not have nor base classes nor base interface.");
                }

				Advance();
				node.BaseClasses.Add(ParseType());
				while (curtok.ID == TokenID.Comma)
				{
					Advance();
					node.BaseClasses.Add(ParseType());
				}
			}

            ParsePossibleTypeParameterConstraintNode(node);
            ApplyTypeParameterConstraints(node);

            CheckTypeUnicityAndAdd(node);

            if (node.IsGeneric)
            {
                this.nameTable.AddIdentifier(new ClassName(node.Name.Identifier,
                    ToVisibilityRestriction(node.Modifiers),
                    node.Generic.TypeParameters.ConvertAll<string>(delegate(TypeParameterNode input) { return input.Identifier.Identifier; }).ToArray(),
                    this.currentContext));
            }
            else
            {
                this.nameTable.AddIdentifier(new ClassName(node.Name.Identifier, 
                    ToVisibilityRestriction(node.Modifiers), 
                    this.currentContext));
            }

            typeStack.Push(node);

            if (cl == null)
            {
                namespaceStack.Peek().Classes.Add(node);
            }
            else
            {
                cl.Classes.Add(node);
            }

			AssertAndAdvance(TokenID.LCurly);

            this.currentContext.Enter(node.Name.Identifier, false);

            while (curtok.ID != TokenID.RCurly && curtok.ID != TokenID.Eof) // guard for empty
			{
				ParseClassMember();
			}

			AssertAndAdvance(TokenID.RCurly);

            if ((node.Modifiers & Modifier.Unsafe) != Modifier.Empty)
            {
                isUnsafe--;
            }

            this.currentContext.Leave();
			typeStack.Pop();
		}
Exemple #2
0
        private void BuilderType(InterfaceNode type,NamespaceNode nspace)
        {
            DDW.ClassNode cls = new ClassNode(new Token(TokenID.Public));
            cls.Modifiers = Modifier.Public;
            cls.BaseClasses.Add(new TypeNode(new IdentifierExpression("Peanut.Mappings.DataObject", new Token(TokenID.Typeof))));
            foreach (AttributeNode attr in type.Attributes)
            {
                cls.Attributes.Add(attr);
            }
            mTableName = GetTableName(type);
            mClassName = type.Name.Identifier.Substring(1, type.Name.Identifier.Length - 1);
            cls.Name = new IdentifierExpression(mClassName, new Token(TokenID.String|TokenID.Public));
            cls.IsPartial = true;
            nspace.Classes.Add(cls);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("///<summary>");
            sb.AppendLine("///Peanut Generator Copyright @ FanJianHan 2010-2013");
            sb.AppendLine("///website:http://www.ikende.com");
            if (!string.IsNullOrEmpty(type.Comment))
            {
                sb.AppendLine(type.Comment);
            }
            StringReader sr = new StringReader(type.DocComment);
            string value = sr.ReadLine();
            while (value != null)
            {
                if (value.IndexOf("summary>") == -1)
                {
                    sb.AppendLine(value);
                }
                value = sr.ReadLine();
            }
            sb.AppendLine("///</summary>");
            cls.DocComment = sb.ToString();

            foreach (InterfacePropertyNode property in type.Properties)
            {
                string propertyname = property.Names[0].GenericIdentifier;
                string fieldname= GetFieldName(property);
                FieldNode field = new FieldNode(new Token(TokenID.False));
                field.Modifiers = Modifier.Private;
                QualifiedIdentifierExpression name = new QualifiedIdentifierExpression(new Token(TokenID.String));
                name.Expressions.Add(new IdentifierExpression("m" + property.Names[0].GenericIdentifier, new Token(TokenID.String)));
                field.Names.Add(name);
                field.Type = property.Type;
                cls.Fields.Add(field);

                IType fieldtype=new TypeNode(new IdentifierExpression("Peanut.FieldInfo<"+((TypeNode)property.Type).GenericIdentifier+">", new Token(TokenID.Typeof)));
                field = new FieldNode(new Token(TokenID.False));
                field.Modifiers = Modifier.Public| Modifier.Static;
               
                NodeCollection<ArgumentNode> args = new NodeCollection<ArgumentNode>();
                args.Add(new ArgumentNode(new Token(TokenID.String)));
                args.Add(new ArgumentNode(new Token(TokenID.String)));
                args[0].Expression = new StringPrimitive(mTableName, new Token(TokenID.String));
                args[1].Expression = new StringPrimitive(fieldname, new Token(TokenID.String));

                name = new QualifiedIdentifierExpression(new Token(TokenID.String));
                name.Expressions.Add(new AssignmentExpression(TokenID.Equal,
                    new IdentifierExpression(propertyname.Substring(0, 1).ToLower() + propertyname.Substring(1, propertyname.Length - 1)
                    , new Token(TokenID.String)), new ObjectCreationExpression(fieldtype, args, new Token(TokenID.New))));
                field.Names.Add(name);
                field.Type = fieldtype;
               
                cls.Fields.Add(field);


                PropertyNode pn = new PropertyNode(new Token(TokenID.Newline));
                foreach (AttributeNode attr in property.Attributes)
                {
                    pn.Attributes.Add(attr);
                }
                pn.Names = property.Names;
                pn.Modifiers = Modifier.Public | Modifier.Virtual;
                pn.Type = property.Type;
                pn.Setter = new AccessorNode(true, new Token(TokenID.Newline));
                pn.Setter.Kind = "set";
                ExpressionStatement setvalue = new ExpressionStatement(
                    new AssignmentExpression(TokenID.Equal,
                    new IdentifierExpression("m" + property.Names[0].GenericIdentifier, new Token(TokenID.String))
                    , new IdentifierExpression("value", new Token(TokenID.String)))
                    );
                pn.Setter.StatementBlock.Statements.Add(setvalue);

                args = new NodeCollection<ArgumentNode>();
                args.Add(new ArgumentNode(new Token(TokenID.String)));
                args[0].Expression = new StringPrimitive(propertyname, new Token(TokenID.String));
                QualifiedIdentifierExpression invoke = new QualifiedIdentifierExpression(new Token(TokenID.String));
                invoke.Expressions.Add(new IdentifierExpression("EntityState", new Token(TokenID.String)));
                invoke.Expressions.Add(new InvocationExpression(new IdentifierExpression("FieldChange", new Token(TokenID.Default)), args));
                setvalue = new ExpressionStatement(invoke);
                pn.Setter.StatementBlock.Statements.Add(setvalue);

                pn.Getter = new AccessorNode(true, new Token(TokenID.Newline));
                pn.Getter.Kind = "get";
                ReturnStatement rs = new ReturnStatement(new Token(TokenID.Return));
                rs.ReturnValue = new IdentifierExpression("m" + property.Names[0].GenericIdentifier, new Token(TokenID.String));
                pn.Getter.StatementBlock.Statements.Add(rs);

                sb = new StringBuilder();
                sb.AppendLine("///<summary>");
                sb.AppendLine("///Type:" + ((TypeNode)property.Type).GenericIdentifier);
                if (!string.IsNullOrEmpty(property.Comment))
                {
                    sb.AppendLine(type.Comment);
                }
                sr = new StringReader(property.DocComment);
                value = sr.ReadLine();
                while (value != null)
                {
                    if (value.IndexOf("summary>") == -1)
                    {
                        sb.AppendLine(value);
                    }
                    value = sr.ReadLine();
                }
                sb.AppendLine("///</summary>");
                pn.DocComment = sb.ToString();
                

                cls.Properties.Add(pn);
                
            }
            //CodeTypeDeclaration entity = new CodeTypeDeclaration(mClassName);
            //CodeCommentStatement comm;
            //cns.Types.Add(entity);

           
            //comm = new CodeCommentStatement("<summary>");
            //comm.Comment.DocComment = true;
            //entity.Comments.Add(comm);
            //comm = new CodeCommentStatement("Peanut Generator Copyright © FanJianHan 2010-2013");
            //comm.Comment.DocComment = true;
            //entity.Comments.Add(comm);
            //comm = new CodeCommentStatement("website:http://www.ikende.com");
            //comm.Comment.DocComment = true;
            //entity.Comments.Add(comm);
            //if (!string.IsNullOrEmpty(type.Comment))
            //{
            //    comm = new CodeCommentStatement(type.Comment);
            //    comm.Comment.DocComment = true;
            //    entity.Comments.Add(comm);
            //}

            //StringReader sr = new StringReader(type.DocComment);
            //string value = sr.ReadLine();
            //while (value != null)
            //{
            //    if (value.IndexOf("summary>") == -1)
            //    {
            //        comm = new CodeCommentStatement(value.Replace("///", ""));
            //        comm.Comment.DocComment = true;
            //        entity.Comments.Add(comm);
            //    }
            //    value = sr.ReadLine();
            //}
            //comm = new CodeCommentStatement("</summary>");
            //comm.Comment.DocComment = true;
            //entity.Comments.Add(comm);
            //// }
            //entity.BaseTypes.Add(new CodeTypeReference("Peanut.Mappings.DataObject"));
            //entity.BaseTypes.Add(new CodeTypeReference(type.Name.Identifier));
            //entity.Attributes = MemberAttributes.Public;
            //entity.IsPartial = true;
            //entity.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable"));
            //entity.IsClass = true;
            //foreach (AttributeNode aitem in type.Attributes)
            //{
            //    CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(aitem.Name.GenericIdentifier);
            //    entity.CustomAttributes.Add(attribute);
            //    if (attribute.Name.ToLower() == "table")
            //    {
            //        if (aitem.Arguments.Count > 0)
            //        {

            //            DDW.StringPrimitive pe = (DDW.StringPrimitive)aitem.Arguments[0].Expression;
            //            if (pe != null)
            //            {
            //                mTableName = pe.Value.ToString();
            //            }
            //            else
            //            {
            //                mTableName = mClassName;

            //            }
            //            attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(mTableName)));
            //        }
            //        else
            //        {
            //            mTableName = mClassName;
            //        }
            //    }

            //}
            //foreach (InterfacePropertyNode mitem in type.Properties)
            //{


            //    BuilderProperty(entity, mitem);

            //}



        }
Exemple #3
0
        private void CheckStaticClass( ClassNode cl, Modifier nodeModifier, bool checkStaticModifier )
        {
            if (cl.IsStatic)
            {
                if (checkStaticModifier
                    && ((nodeModifier & Modifier.Static) == Modifier.Empty))
                {
                    ReportError("Static class accepts only static members.");
                }

                if (((nodeModifier & (Modifier.Protected | Modifier.Internal)) == (Modifier.Protected | Modifier.Internal)))
                {
                    ReportError("Static class does not accept protected internal member.");
                }
                else
                {
                    //do it in the else condition of the previous if or the message
                    //will be duplicate
                    if (((nodeModifier & Modifier.Protected) == Modifier.Protected))
                    {
                        ReportError("Static class does not accept protected member.");
                    }
                }

            }

        }
        public virtual object VisitClassDeclaration(ClassNode classDeclaration, object data)
        {
            stackMap.Push(classDeclaration);
            classDeclaration.Attributes.AcceptVisitor(this, data);

            classDeclaration.BaseClasses.AcceptVisitor(this, data);

            classDeclaration.Classes.AcceptVisitor(this, data);

            classDeclaration.Constants.AcceptVisitor(this, data);

            classDeclaration.Constructors.AcceptVisitor(this, data);

            classDeclaration.Delegates.AcceptVisitor(this, data);

            classDeclaration.Destructors.AcceptVisitor(this, data);

            classDeclaration.Enums.AcceptVisitor(this, data);

            classDeclaration.Events.AcceptVisitor(this, data);

            classDeclaration.Fields.AcceptVisitor(this, data);

            classDeclaration.FixedBuffers.AcceptVisitor(this, data);

            if (classDeclaration.Generic != null)
            {
                classDeclaration.Generic.AcceptVisitor(this, data);
            }

            classDeclaration.Indexers.AcceptVisitor(this, data);

            classDeclaration.Interfaces.AcceptVisitor(this, data);

            classDeclaration.Methods.AcceptVisitor(this, data);

            classDeclaration.Operators.AcceptVisitor(this, data);

            if (classDeclaration.Partials != null)
            {
                classDeclaration.Partials.AcceptVisitor(this, data);
            }

            classDeclaration.Properties.AcceptVisitor(this, data);

            classDeclaration.Structs.AcceptVisitor(this, data);

            stackMap.Pop();
            return null;

        }