public override void EnterConstructor(STEPParser.ConstructorContext context) { // Only cache an instance if it's an outer instance. if (context.Parent is STEPParser.InstanceContext) { instanceData.Add(currId, ParseConstructor(currId, context)); } }
private InstanceData ParseConstructor(int id, STEPParser.ConstructorContext context, Type ifcType = null) { //Console.WriteLine($"Parsing constructor {id}."); var typeName = context.TypeRef().GetText(); // If a type has been passed in, we're parsing a constructor // as a parameter of a constructor. if (ifcType == null) { if (!types.ContainsKey(typeName)) { throw new STEPUnknownTypeException(typeName); } ifcType = types[typeName]; } Type fromSTEP = null; if (types.ContainsKey(typeName)) { fromSTEP = types[typeName]; } // Use the constructor which includes all non-optional parameters. var ctorChain = new List <System.Reflection.ConstructorInfo>(); var ctor = GetConstructorForType(ifcType, ref ctorChain, fromSTEP); if (ctorChain.Any()) { var chain = string.Join("=>", ctorChain.Select(c => c.DeclaringType.Name)); } var ctorParams = ctor.GetParameters(); var constructorParams = new List <object>(); var cParams = context.parameter(); for (var i = 0; i < cParams.Count(); i++) { var p = cParams[i]; var pType = ctorParams[i].ParameterType; if (p.constructor() != null) { // If the parameter is a constructor, it may be a constructor // for a type that is used as a select. // In this case, we will need to find the select // for which the type is a parameter, and construct an instance // of the select instead. constructorParams.Add(ParseConstructor(-1, p.constructor(), pType)); } else if (p.collection() != null) { constructorParams.Add(ParseCollection(pType, p.collection())); } else if (p.Undefined() != null) { constructorParams.Add(null); } else if (p.StringLiteral() != null) { constructorParams.Add(ParseString(pType, p.StringLiteral().GetText())); } else if (p.Derived() != null) { constructorParams.Add(null); } else if (p.Enum() != null) { constructorParams.Add(ParseEnum(pType, p.Enum().GetText())); } else if (p.BoolLogical() != null) { constructorParams.Add(ParseBoolLogical(pType, p.BoolLogical().GetText())); } else if (p.RealLiteral() != null) { constructorParams.Add(ParseReal(pType, p.RealLiteral().GetText())); } else if (p.AnyString() != null) { constructorParams.Add(ParseString(pType, p.AnyString().GetText())); } else if (p.Id() != null) { constructorParams.Add(ParseId(p.Id().GetText())); } else if (p.IntegerLiteral() != null) { constructorParams.Add(ParseInt(pType, p.IntegerLiteral().GetText())); } } if (ctorParams.Count() != constructorParams.Count()) { throw new STEPParameterMismatchException(ifcType, ctorParams.Count(), constructorParams.Count()); } return(new InstanceData(id, ifcType, constructorParams, ctor, ctorChain)); }
private InstanceData ParseConstructor(int id, STEPParser.ConstructorContext context) { //Console.WriteLine($"Parsing context: {currId.Value} {context.GetText()}"); var typeName = context.TypeRef().GetText(); var ifcType = types.FirstOrDefault(t => t.Name.ToUpper() == typeName); if (ifcType == null) { throw new STEPUnknownTypeException(typeName); } // Use the constructor which includes all non-optional parameters. var ctor = GetMostLikelyConstructorForType(ifcType); var ctorParams = ctor.GetParameters(); var constructorParams = new List <object>(); var cParams = context.parameter(); for (var i = 0; i < cParams.Count(); i++) { var p = cParams[i]; var pType = ctorParams[i].ParameterType; if (p.constructor() != null) { constructorParams.Add(ParseConstructor(-1, p.constructor())); } else if (p.collection() != null) { constructorParams.Add(ParseCollection(pType, p.collection())); } else if (p.Undefined() != null) { constructorParams.Add(null); } else if (p.StringLiteral() != null) { constructorParams.Add(ParseString(pType, p.StringLiteral().GetText())); } else if (p.Derived() != null) { constructorParams.Add(null); } else if (p.Enum() != null) { constructorParams.Add(ParseEnum(pType, p.Enum().GetText())); } else if (p.BoolLogical() != null) { constructorParams.Add(ParseBoolLogical(pType, p.BoolLogical().GetText())); } else if (p.RealLiteral() != null) { constructorParams.Add(ParseReal(pType, p.RealLiteral().GetText())); } else if (p.AnyString() != null) { constructorParams.Add(ParseString(pType, p.AnyString().GetText())); } else if (p.Id() != null) { constructorParams.Add(ParseId(p.Id().GetText())); } else if (p.IntegerLiteral() != null) { constructorParams.Add(ParseInt(pType, p.IntegerLiteral().GetText())); } } if (ctorParams.Count() != constructorParams.Count()) { throw new STEPParameterMismatchException(ifcType, ctorParams.Count(), constructorParams.Count()); } return(new InstanceData(id, ifcType, constructorParams, ctor)); }
/// <summary> /// Exit a parse tree produced by <see cref="STEPParser.constructor"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitConstructor([NotNull] STEPParser.ConstructorContext context) { }