/// <summary> /// Checks for errors before applying a rule. /// </summary> /// <param name="node">The node instance to check.</param> /// <param name="dataList">Optional data collected during inspection of sources.</param> /// <param name="data">Private data to give to Apply() upon return.</param> /// <returns>True if an error occured.</returns> public override bool CheckConsistency(IFunctionType node, IDictionary <ISourceTemplate, object> dataList, out object data) { bool Success = true; data = null; // This is ensured because the root node is valid. Debug.Assert(node.OverloadList.Count > 0); IFunctionType ResolvedType = node.ResolvedType.Item as IFunctionType; Debug.Assert(node.OverloadList.Count == ResolvedType.OverloadList.Count); IList <IQueryOverloadType> OverloadTypeList = ResolvedType.OverloadList; IResultType CommonResults = Feature.CommonResultType(OverloadTypeList); IErrorList CheckErrorList = new ErrorList(); for (int i = 0; i < CommonResults.Count; i++) { Success &= Feature.JoinedResultCheck(OverloadTypeList, i, CommonResults.At(i).ValueType, node, CheckErrorList); } if (!Success) { Debug.Assert(!CheckErrorList.IsEmpty); AddSourceErrorList(CheckErrorList); } if (Success) { data = CommonResults; } return(Success); }
/// <summary> /// Checks for errors before applying a rule. /// </summary> /// <param name="node">The node instance to check.</param> /// <param name="dataList">Optional data collected during inspection of sources.</param> /// <param name="data">Private data to give to Apply() upon return.</param> /// <returns>True if an error occured.</returns> public override bool CheckConsistency(IKeywordAnchoredType node, IDictionary <ISourceTemplate, object> dataList, out object data) { bool Success = true; data = null; if (node.Anchor == BaseNode.Keyword.Current) { IClass EmbeddingClass = node.EmbeddingClass; IErrorList CheckErrorList = new ErrorList(); // 'Current' is always available. Success = KeywordExpression.IsKeywordAvailable(node.Anchor, node, CheckErrorList, out ITypeName ResultTypeName, out ICompiledType ResultType); Debug.Assert(Success); data = new Tuple <ITypeName, ICompiledType>(ResultTypeName, ResultType); } else { data = new Tuple <ITypeName, ICompiledType>(Class.ClassAny.ResolvedClassTypeName.Item, Class.ClassAny.ResolvedClassType.Item); } return(Success); }
/// <summary> /// Compiles the file. The file must contain a serialized Easly Root object. /// </summary> /// <param name="fileName">The file to compile.</param> public virtual void Compile(string fileName) { FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); ErrorList.ClearErrors(); if (File.Exists(FileName)) { try { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { Compile(fs); } } catch (Exception e) { ErrorList.AddError(new ErrorInputFileInvalid(e)); } } else { ErrorList.AddError(new ErrorInputFileNotFound(FileName)); } }
/// <summary> /// Translates nodes from the compiler to the target language. /// </summary> public override void Translate() { ErrorList.ClearErrors(); if (!CreateClasses(out IDictionary <IClass, ICSharpClass> ClassTable)) { return; } if (!CreateFeatures(ClassTable, out IDictionary <ICompiledFeature, ICSharpFeature> FeatureTable)) { return; } ICSharpContext Context = new CSharpContext(ClassTable, FeatureTable); foreach (KeyValuePair <IClass, ICSharpClass> ClassEntry in ClassTable) { ICSharpClass Class = ClassEntry.Value; IList <ICSharpFeature> ClassFeatureList = new List <ICSharpFeature>(); IList <ICSharpFeature> InheritedFeatureList = new List <ICSharpFeature>(); foreach (KeyValuePair <ICompiledFeature, ICSharpFeature> FeatureEntry in FeatureTable) { ICSharpFeature Feature = FeatureEntry.Value; IFeatureInstance Instance = Feature.Instance; if (Instance.IsDiscontinued) { continue; } if (FeatureEntry.Value.Owner == Class) { ClassFeatureList.Add(Feature); } else if (!IsDirectOrNotMainParentFeature(Instance, Class)) { InheritedFeatureList.Add(Feature); } } Class.SetFeatureList(Context, ClassFeatureList, InheritedFeatureList); } foreach (KeyValuePair <ICompiledFeature, ICSharpFeature> Entry in FeatureTable) { ICSharpFeature Feature = Entry.Value; Feature.InitOverloadsAndBodies(Context); } foreach (KeyValuePair <ICompiledFeature, ICSharpFeature> Entry in FeatureTable) { ICSharpFeature Feature = Entry.Value; Feature.InitHierarchy(Context); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; CheckSharedName(Class, ClassTable); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckOverrides(); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckOverrides(); } bool Continue; do { Continue = false; foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckForcedReadWrite(FeatureTable, ref Continue); } }while (Continue); foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckSideBySideAttributes(); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckInheritSideBySideAttributes(FeatureTable); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CreateDelegates(); } ICSharpFeature SingledClassFeature = null; if (SingledGuid != Guid.Empty || SingledGuid == Guid.Empty) { foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; if (Class.Source.ClassGuid == SingledGuid || SingledGuid == Guid.Empty || SingledGuid != Guid.Empty) { foreach (ICSharpFeature Feature in Class.FeatureList) { if (Feature is ICSharpFeatureWithName AsWithName && AsWithName.Name == SingledName) { SingledClassFeature = Feature; break; } } } if (SingledClassFeature != null) { break; } } } if (SingledClassFeature == null) { foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.SetWriteDown(); foreach (ICSharpFeature Feature in Class.FeatureList) { Feature.SetWriteDown(); } foreach (ICSharpAssertion Invariant in Class.InvariantList) { Invariant.SetWriteDown(); } } } else { SingledClassFeature.SetWriteDown(); } if (!Directory.Exists(OutputRootFolder)) { Directory.CreateDirectory(OutputRootFolder); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; if (!CSharpClass.IsLanguageClass(Class.Source) && !IsClassFromLibrary(Class.Source)) { if (Class.WriteDown) { Class.Write(OutputRootFolder, Namespace, SourceFileName, SingledClassFeature); } } } }
private bool CheckRename(KeyValuePair <IIdentifier, IIdentifier> entry, IDictionaryIndex <IFeatureName>[] renamedItemTables, ISealableDictionary <string, string> sourceIdentifierTable, ISealableDictionary <string, string> destinationIdentifierTable, Func <IFeatureName, string> key2String, Func <string, IFeatureName> string2Key) { IIdentifier SourceIdentifier = entry.Key; IIdentifier DestinationIdentifier = entry.Value; OnceReference <ISealableDictionary> SourceTable = new OnceReference <ISealableDictionary>(); OnceReference <IFeatureName> SourceKey = new OnceReference <IFeatureName>(); OnceReference <object> SourceItem = new OnceReference <object>(); foreach (ISealableDictionary Table in renamedItemTables) { foreach (System.Collections.DictionaryEntry SourceEntry in (System.Collections.IDictionary)Table) { IFeatureName EntryKey = SourceEntry.Key as IFeatureName; object EntryValue = SourceEntry.Value; string ValidName = key2String(EntryKey); if (ValidName == SourceIdentifier.Text) { SourceTable.Item = Table; SourceKey.Item = EntryKey; SourceItem.Item = EntryValue; break; } } if (SourceTable.IsAssigned) { break; } } if (!SourceTable.IsAssigned) { ErrorList.AddError(new ErrorUnknownIdentifier(SourceIdentifier, SourceIdentifier.Text)); return(false); } foreach (ISealableDictionary Table in renamedItemTables) { foreach (System.Collections.DictionaryEntry SourceEntry in (System.Collections.IDictionary)Table) { IFeatureName EntryKey = SourceEntry.Key as IFeatureName; string ValidName = key2String(EntryKey); if (ValidName == DestinationIdentifier.Text) { ErrorList.AddError(new ErrorIdentifierAlreadyListed(DestinationIdentifier, DestinationIdentifier.Text)); return(false); } } } sourceIdentifierTable.Add(SourceIdentifier.Text, DestinationIdentifier.Text); destinationIdentifierTable.Add(DestinationIdentifier.Text, SourceIdentifier.Text); ((System.Collections.IDictionary)SourceTable.Item).Remove(SourceKey.Item); ((System.Collections.IDictionary)SourceTable.Item).Add(string2Key(DestinationIdentifier.Text), SourceItem.Item); return(true); }