Exemple #1
0
 public override void ExitEntityDecl(ExpressParser.EntityDeclContext context)
 {
     currTypeData = null;
 }
Exemple #2
0
        // ENTITY
        public override void EnterEntityDecl(ExpressParser.EntityDeclContext context)
        {
            Entity entity;
            var    entityName = context.entityHead().entityDef().SimpleId().GetText();

            if (typeData.ContainsKey(entityName))
            {
                // TypeData was created previously possible as a reference
                // to a sub or super type.
                entity = (Entity)typeData[entityName];
            }
            else
            {
                entity = new Entity(entityName, generator);
                typeData.Add(entityName, entity);
            }

            var subSuper = context.entityHead().subSuper();

            // SUPERTYPE
            if (subSuper.supertypeDecl() != null)
            {
                var super = subSuper.supertypeDecl();
                entity.IsAbstract = super.ABSTRACT() != null;
                var factor = super.supertypeExpr().supertypeFactor();

                // IFC: Use choice only.
                if (factor[0].choice() != null)
                {
                    foreach (var superRef in factor[0].choice().supertypeExpr())
                    {
                        var    superName = superRef.supertypeFactor()[0].entityRef().SimpleId().GetText();
                        Entity sup;
                        if (typeData.ContainsKey(superName))
                        {
                            sup = (Entity)typeData[superName];
                        }
                        else
                        {
                            sup = new Entity(superName, generator);
                            typeData.Add(superName, sup);
                        }
                        entity.Supers.Add(sup);
                    }
                }
            }

            // SUBTYPE
            if (subSuper.subtypeDecl() != null)
            {
                foreach (var subRef in subSuper.subtypeDecl().entityRef())
                {
                    var    subName = subRef.SimpleId().GetText();
                    Entity sub;
                    if (typeData.ContainsKey(subName))
                    {
                        sub = (Entity)typeData[subName];
                    }
                    else
                    {
                        sub = new Entity(subName, generator);
                        typeData.Add(subName, sub);
                    }
                    entity.Subs.Add(sub);
                }
            }

            if (context.entityBody().attributes() != null)
            {
                var attrs = context.entityBody().attributes();
                foreach (var expl in attrs.explicitClause())
                {
                    if (expl.explDef() != null)
                    {
                        var optional = expl.explDef().OPTIONAL() != null;
                        foreach (var attrDef in expl.explDef().attrDef())
                        {
                            var rank         = 0;
                            var isCollection = false;
                            var name         = "";
                            var isGeneric    = false;
                            if (attrDef.SimpleId() != null)
                            {
                                name = attrDef.SimpleId().GetText();
                            }
                            else if (attrDef.Path() != null)
                            {
                                name = attrDef.Path().GetText();
                            }
                            var type = ParseCollectionTypeSel(expl.explDef().collectionTypeSel(), ref rank, ref isCollection, ref isGeneric);
                            var ad   = new AttributeData(generator, name, type, rank, isCollection, isGeneric, false, optional, false);
                            entity.Attributes.Add(ad);

                            if (ad.Type == null)
                            {
                                throw new Exception($"The Type of attribute data, {ad.Name}, is null.");
                            }
                        }
                    }
                    else if (expl.explRedef() != null)
                    {
                        var rank         = 0;
                        var isCollection = false;
                        var name         = "";
                        var attrRef      = expl.explRedef().attrRef();
                        var isGeneric    = false;
                        if (attrRef.SimpleId() != null)
                        {
                            name = attrRef.SimpleId().GetText();
                        }
                        else if (attrRef.Path() != null)
                        {
                            name = attrRef.Path().GetText();
                        }
                        var optional = expl.explRedef().OPTIONAL() != null;
                        var type     = ParseCollectionTypeSel(expl.explRedef().collectionTypeSel(), ref rank, ref isCollection, ref isGeneric);
                        var ad       = new AttributeData(generator, name, type, rank, isCollection, isGeneric, false, optional, false);
                        entity.Attributes.Add(ad);

                        if (ad.Type == null)
                        {
                            throw new Exception($"The Type of attribute data, {ad.Name}, is null.");
                        }
                    }
                }

                // DERIVE
                foreach (var der in attrs.deriveClause())
                {
                    foreach (var derAttr in der.derivedAttr())
                    {
                        var  name         = "";
                        var  rank         = 0;
                        bool isCollection = false;
                        bool isGeneric    = false;
                        if (derAttr.deriveDef() != null)
                        {
                            if (derAttr.deriveDef().attrDef().SimpleId() != null)
                            {
                                name = derAttr.deriveDef().attrDef().SimpleId().GetText();
                            }
                            else if (derAttr.deriveDef().attrDef().Path() != null)
                            {
                                name = derAttr.deriveDef().attrDef().Path().GetText();
                            }
                            var type = ParseCollectionTypeSel(derAttr.deriveDef().collectionTypeSel(), ref rank, ref isCollection, ref isGeneric);
                            var ad   = new AttributeData(generator, name, type, rank, isCollection, isGeneric, true, false, false);
                            entity.Attributes.Add(ad);
                        }
                        else if (derAttr.derivedRedef() != null)
                        {
                            if (derAttr.derivedRedef().attrRef().SimpleId() != null)
                            {
                                name = derAttr.derivedRedef().attrRef().SimpleId().GetText();
                            }
                            else if (derAttr.derivedRedef().attrRef().Path() != null)
                            {
                                name = derAttr.derivedRedef().attrRef().Path().GetText();
                            }
                            var type = ParseCollectionTypeSel(derAttr.derivedRedef().collectionTypeSel(), ref rank, ref isCollection, ref isGeneric);
                            var ad   = new AttributeData(generator, name, type, rank, isCollection, isGeneric, true, false, false);
                            entity.Attributes.Add(ad);
                        }
                    }
                }

                // INVERSE
                foreach (var inv in attrs.inverseClause())
                {
                    foreach (var invAttr in inv.inverseAttr())
                    {
                        var  name         = "";
                        var  rank         = 0;
                        bool isCollection = false;
                        bool optional     = false;
                        bool inverse      = true;

                        if (invAttr.inverseDef() != null)
                        {
                            if (invAttr.inverseDef().attrDef().SimpleId() != null)
                            {
                                name = invAttr.inverseDef().attrDef().SimpleId().GetText();
                            }
                            else if (invAttr.inverseDef().attrDef().Path() != null)
                            {
                                name = invAttr.inverseDef().attrDef().Path().GetText();
                            }
                            var type = ParseInverseType(invAttr.inverseDef().inverseType(), ref isCollection, ref rank);
                            var ad   = new AttributeData(generator, name, type, rank, isCollection, false, false, optional, inverse);
                            entity.Attributes.Add(ad);
                        }
                        else if (invAttr.inverseRedef() != null)
                        {
                            if (invAttr.inverseRedef().attrRef()[0].SimpleId() != null)
                            {
                                name = invAttr.inverseRedef().attrRef()[0].SimpleId().GetText();
                            }
                            else if (invAttr.inverseRedef().attrRef()[0].Path() != null)
                            {
                                name = invAttr.inverseRedef().attrRef()[0].Path().GetText();
                            }
                            var type = ParseInverseType(invAttr.inverseRedef().inverseType(), ref isCollection, ref rank);
                            var ad   = new AttributeData(generator, name, type, rank, isCollection, false, false, optional, inverse);
                            entity.Attributes.Add(ad);
                        }
                    }
                }
            }

            if (entity.Attributes.Any(a => a.IsCollection && a.Rank == 0))
            {
                throw new Exception("I found an attribute with IsCollection=true, but a rank of 0.");
            }

            if (entity.Attributes.Any(a => a.IsInverse && a.IsOptional))
            {
                throw new Exception("I found an attribute with IsInverse=true, but marked as not optional.");
            }
        }