private bool TryAutoAddAssemblyReference(Boo.Lang.Compiler.Ast.Import import) { Assembly asm = Parameters.FindAssembly(import.Namespace); if (asm != null) { return(false); //name resolution already failed earlier, don't try twice } asm = Parameters.LoadAssembly(import.Namespace, false); if (asm == null) { return(false); } if (asm != null) { try { NameResolutionService.OrganizeAssemblyTypes(asm); } catch (Exception /*ignored*/) { return(false); } Parameters.AddAssembly(asm); import.AssemblyReference = new ReferenceExpression(import.LexicalInfo, import.Namespace); import.AssemblyReference.Entity = new AssemblyReference(asm); return(true); } return(false); }
/// <summary> /// Add the namespaces to the module /// </summary> public override void OnModule(Module node) { foreach (string ns in namespaces) { Import import = new Import(node.LexicalInfo, ns); node.Imports.Add(import); } }
IEnumerable<Import> ImportTasks(Import import) { if (import.AssemblyReference == null) throw new InvalidOperationException("Cannot import tasks: 'from' assembly is not specified."); return from importer in importers from newImport in importer.BuildImportsFrom(import.AssemblyReference.Name) select newImport; }
public override void OnModule(Module node) { var imports = new[] { "UnityEngine", "System.Collections" }; foreach (var importName in imports) { var targetImport = new Import(LexicalInfo.Empty, importName); if (node.Imports.Contains(targetImport)) node.Imports.Add(targetImport); } }
public object VisitUsing(Using @using, object data) { B.Import import; if (@using.IsAlias) { import = new B.Import(@using.Alias.Type, null, new B.ReferenceExpression(@using.Name)); import.LexicalInfo = GetLexicalInfo(@using); } else { import = new B.Import(GetLexicalInfo(@using), @using.Name); } module.Imports.Add(import); return import; }
public override void OnImport(AST.Import p) { DefaultUsing u = new DefaultUsing(_cu.ProjectContent); if (p.Alias == null) { u.Usings.Add(p.Namespace); } else { u.AddAlias(p.Alias.Name, new GetClassReturnType(_cu.ProjectContent, p.Namespace, 0)); } _cu.UsingScope.Usings.Add(u); }
public object VisitUsing(Using @using, object data) { B.Import import; if (@using.IsAlias) { import = new B.Import(@using.Alias.Type, null, new B.ReferenceExpression(@using.Name)); import.LexicalInfo = GetLexicalInfo(@using); } else { import = new B.Import(GetLexicalInfo(@using), @using.Name); } module.Imports.Add(import); return(import); }
private bool HandledAsImportError(Import import, IEntity entity) { if (entity == null) { ImportError(import, CompilerErrorFactory.InvalidNamespace(import)); return true; } if (!IsValidImportTarget(entity)) { ImportError(import, CompilerErrorFactory.NotANamespace(import, entity)); return true; } return false; }
private bool TryAutoAddAssemblyReference(Boo.Lang.Compiler.Ast.Import import) { ICompileUnit asm = Parameters.FindAssembly(import.Namespace); if (asm != null) { return(false); //name resolution already failed earlier, don't try twice } asm = Parameters.LoadAssembly(import.Namespace, false); if (asm == null) { //try generalized namespaces string[] namespaces = import.Namespace.Split(new char[] { '.', }); if (namespaces.Length == 1) { return(false); } string ns; int level = namespaces.Length - 1; while (level > 0) { ns = string.Join(".", namespaces, 0, level); asm = Parameters.FindAssembly(ns); if (asm != null) { return(false); //name resolution already failed earlier, don't try twice } asm = Parameters.LoadAssembly(ns, false); if (asm != null) { break; } level--; } } if (asm != null) { Parameters.References.Add(asm); import.AssemblyReference = new ReferenceExpression(import.LexicalInfo, asm.FullName); import.AssemblyReference.Entity = asm; NameResolutionService.ClearResolutionCacheFor(asm.Name); return(true); } return(false); }
private bool HandledAsImportError(Import import, IEntity entity) { if (null == entity) { Errors.Add(CompilerErrorFactory.InvalidNamespace(import)); BindError(import); return true; } if (!IsValidNamespace(entity)) { Errors.Add(CompilerErrorFactory.NotANamespace(import, entity.FullName)); BindError(import); return true; } return false; }
public override void OnImport(Import import) { if (IsAlreadyBound(import)) return; if (import.AssemblyReference != null) { ImportFromAssemblyReference(import); return; } var entity = ResolveImport(import); if (HandledAsImportError(import, entity) || HandledAsDuplicatedNamespace(import)) return; Context.TraceInfo("{1}: import reference '{0}' bound to {2}.", import, import.LexicalInfo, entity); import.Entity = ImportedNamespaceFor(import, entity); }
public override void OnModule(Module node){ logger.get("comdiv.dsl").Debug("SetupDefaultNamespacesAndReferencesStep on {0} started", node.Name); Console.WriteLine("SetupDefaultNamespacesAndReferencesStep on {0} started", node.Name); if(null==node.Namespace){ node.Namespace = new NamespaceDeclaration(System.IO.Path.GetFileNameWithoutExtension(Context.Parameters.OutputAssembly)); } if (null != namespaces){ foreach (var ns in namespaces){ var import = new Import(); import.Namespace = ns; node.Imports.Add(import); logger.get("comdiv.dsl").Debug("SetupDefaultNamespacesAndReferencesStep on {0} - {1} ns added", node.Name, ns); Console.WriteLine("SetupDefaultNamespacesAndReferencesStep on {0} - {1} ns added", node.Name, ns); } } logger.get("comdiv.dsl").Debug("SetupDefaultNamespacesAndReferencesStep on {0} finished", node.Name); }
public override void OnImport(Boo.Lang.Compiler.Ast.Import import) { if (IsAlreadyBound(import)) { return; } if (null != import.AssemblyReference) { ImportFromAssemblyReference(import); return; } var entity = ResolveImport(import); if (HandledAsImportError(import, entity) || HandledAsDuplicatedNamespace(import, entity)) { return; } Context.TraceInfo("{1}: import reference '{0}' bound to {2}.", import, import.LexicalInfo, entity.FullName); import.Entity = ImportedNamespaceFor(import, entity); }
private IEntity ResolveImportOnParentNamespace(Import import) { var current = NameResolutionService.CurrentNamespace; try { INamespace parentNamespace = NameResolutionService.CurrentNamespace.ParentNamespace; if (parentNamespace != null) { NameResolutionService.EnterNamespace(parentNamespace); return NameResolutionService.ResolveQualifiedName(import.Namespace); } } finally { NameResolutionService.EnterNamespace(current); } return null; }
private bool TryAutoAddAssemblyReference(Import import) { var existingReference = Parameters.FindAssembly(import.Namespace); if (existingReference != null) return false; var asm = TryToLoadAssemblyContainingNamespace(import.Namespace); if (asm == null) return false; Parameters.References.Add(asm); import.AssemblyReference = new ReferenceExpression(import.LexicalInfo, asm.FullName).WithEntity(asm); NameResolutionService.ClearResolutionCacheFor(asm.Name); return true; }
private IEntity ResolveImport(Import import) { var entity = ResolveImportOnParentNamespace(import) ?? NameResolutionService.ResolveQualifiedName(import.Namespace); if (null != entity) return entity; //if 'import X', try 'import X from X' if (!TryAutoAddAssemblyReference(import)) return null; return NameResolutionService.ResolveQualifiedName(import.Namespace); }
private IEntity ResolveImportAgainstReferencedAssembly(Import import) { return NameResolutionService.ResolveQualifiedName(GetBoundReference(import.AssemblyReference).RootNamespace, import.Namespace); }
public static CompilerError InvalidNamespace(Import import) { return new CompilerError("BCE0021", import.LexicalInfo, import.Namespace); }
public static CompilerError InvalidNamespace(Import import) { if (import.AssemblyReference != null) return Instantiate("BCE0167", import, import.Namespace, import.AssemblyReference); return Instantiate("BCE0021", import, import.Namespace); }
private bool HandledAsDuplicatedNamespace(Import import) { var actualName = EffectiveNameForImportedNamespace(import); //only add unique namespaces Import cachedImport; if (!_namespaces.TryGetValue(actualName, out cachedImport)) { _namespaces[actualName] = import; return false; } //ignore for partial classes in separate files if (cachedImport.LexicalInfo.FileName == import.LexicalInfo.FileName) Warnings.Add(CompilerWarningFactory.DuplicateNamespace(import, import.Namespace)); BindError(import); return true; }
private static string EffectiveNameForImportedNamespace(Import import) { return null != import.Alias ? import.Alias.Name : import.Namespace; }
private void AddFileReference(Import node) { RemoveCurrentNode(); //we may need to preserve this, since it may be used in several compiler cycles. //which will set them to different things CompilerErrorCollection errors = Errors; AssemblyCollection references = Parameters.References; string url = GetFilePath(node); Assembly assembly; if (assemblyCache.TryGetValue(url, out assembly) == false) { assembly = CompileAssembly(node, url, errors); if (assembly == null) { throw new CompilationErrorsException(errors); } assemblyCache.Add(url, assembly); } references.Add(assembly); }
private void BindError(Import import) { Bind(import, Error.Default); }
protected Import import_directive_from_() //throws RecognitionException, TokenStreamException { Import returnValue; IToken from = null; Expression ns = null; ExpressionCollection names = null; returnValue = null; try { // for error handling from = LT(1); match(FROM); ns=identifier_expression(); match(IMPORT); if (0==inputState.guessing) { var mie = new MethodInvocationExpression(ns); names = mie.Arguments; returnValue = new Import(ToLexicalInfo(from), mie); } { if ((LA(1)==MULTIPLY) && (LA(2)==EOL||LA(2)==EOS)) { match(MULTIPLY); if (0==inputState.guessing) { returnValue.Expression = ns; } } else if ((tokenSet_24_.member(LA(1))) && (tokenSet_25_.member(LA(2)))) { expression_list(names); } else { throw new NoViableAltException(LT(1), getFilename()); } } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex, "import_directive_from_"); recover(ex,tokenSet_19_); } else { throw ex; } } return returnValue; }
private void AddNamespaceImports(Import node) { RemoveCurrentNode(); string url = GetFilePath(node); using(TextReader reader = urlResolver(url, baseDirectory)) { BooParsingStep parser = new BooParsingStep(); CompilerContext context = new CompilerContext(); StringInput input = new StringInput(node.AssemblyReference.Name, reader.ReadToEnd()); context.Parameters.Input.Add(input); parser.Initialize(context); parser.Run(); Module current = (Module) node.GetAncestor(NodeType.Module); foreach (Module module in context.CompileUnit.Modules) { foreach (Import import in module.Imports) { current.Imports.Add(import); } } } }
public override void OnImport(Import node) { if (node.LexicalInfo != null) results.MapParsedNode(new MappedImport(results, node)); base.OnImport(node); }
private static string GetFilePath(Import node) { // assume this is located relative to the current file if (node.LexicalInfo != null && File.Exists(node.LexicalInfo.FullPath)) { string directory = Path.GetDirectoryName(node.LexicalInfo.FullPath); return Path.Combine(directory, node.AssemblyReference.Name); } return node.AssemblyReference.Name .Replace("~", AppDomain.CurrentDomain.BaseDirectory); }
private IEntity ImportedNamespaceFor(Import import, IEntity entity) { var ns = entity as INamespace; if (ns == null) return entity; var selectiveImportSpec = import.Expression as MethodInvocationExpression; var imported = selectiveImportSpec != null ? SelectiveImportFor(ns, selectiveImportSpec) : ns; var actualNamespace = null != import.Alias ? AliasedNamespaceFor(imported, import) : imported; return new ImportedNamespace(import, actualNamespace); }
private static INamespace AliasedNamespaceFor(IEntity entity, Import import) { var aliasedNamespace = new AliasedNamespace(import.Alias.Name, entity); import.Alias.Entity = aliasedNamespace; return aliasedNamespace; }
private void ImportError(Import import, CompilerError error) { Errors.Add(error); BindError(import); }
private static bool IsAlreadyBound(Import import) { return import.Entity != null; }
private void ImportFromAssemblyReference(Import import) { var resolvedNamespace = ResolveImportAgainstReferencedAssembly(import); if (HandledAsImportError(import, resolvedNamespace)) return; if (HandledAsDuplicatedNamespace(import)) return; import.Entity = ImportedNamespaceFor(import, resolvedNamespace); }
protected Import import_directive_() //throws RecognitionException, TokenStreamException { Import returnValue; IToken imp = null; IToken dqs = null; IToken sqs = null; IToken alias = null; Expression ns = null; IToken id = null; returnValue = null; try { // for error handling imp = LT(1); match(IMPORT); ns=namespace_expression(); if (0==inputState.guessing) { if (ns != null) returnValue = new Import(ToLexicalInfo(imp), ns); } { switch ( LA(1) ) { case FROM: { match(FROM); { switch ( LA(1) ) { case THEN: case ID: { id=identifier(); break; } case DOUBLE_QUOTED_STRING: { dqs = LT(1); match(DOUBLE_QUOTED_STRING); if (0==inputState.guessing) { id=dqs; } break; } case SINGLE_QUOTED_STRING: { sqs = LT(1); match(SINGLE_QUOTED_STRING); if (0==inputState.guessing) { id=sqs; } break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } if (0==inputState.guessing) { returnValue.AssemblyReference = new ReferenceExpression(ToLexicalInfo(id), id.getText()); } break; } case EOL: case AS: case EOS: case QQ_END: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1) ) { case AS: { match(AS); alias = LT(1); match(ID); if (0==inputState.guessing) { returnValue.Alias = new ReferenceExpression(ToLexicalInfo(alias)); returnValue.Alias.Name = alias.getText(); } break; } case EOL: case EOS: case QQ_END: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex, "import_directive_"); recover(ex,tokenSet_23_); } else { throw ex; } } return returnValue; }
public override void OnImport(Boo.Lang.Compiler.Ast.Import import) { INamespace oldns = NameResolutionService.CurrentNamespace; IEntity entity = null; try { NameResolutionService.EnterNamespace(NameResolutionService.CurrentNamespace.ParentNamespace); entity = NameResolutionService.ResolveQualifiedName(import.Namespace); } finally { NameResolutionService.EnterNamespace(oldns); } if (null == entity) { entity = NameResolutionService.ResolveQualifiedName(import.Namespace); } //if 'import X', try 'import X from X' //comment out next if block if this is not wanted if (null == entity && null == import.AssemblyReference) { if (TryAutoAddAssemblyReference(import)) { entity = NameResolutionService.ResolveQualifiedName(import.Namespace); } } if (null == entity) { Errors.Add(CompilerErrorFactory.InvalidNamespace(import)); entity = TypeSystemServices.ErrorEntity; } else { if (!IsValidNamespace(entity)) { Errors.Add(CompilerErrorFactory.NotANamespace(import, entity.FullName)); entity = TypeSystemServices.ErrorEntity; } else { string name = entity.FullName; if (null != import.AssemblyReference) { NamespaceEntity nsInfo = entity as NamespaceEntity; if (null != nsInfo) { entity = new AssemblyQualifiedNamespaceEntity(GetBoundAssembly(import.AssemblyReference), nsInfo); } } if (null != import.Alias) { entity = new AliasedNamespace(import.Alias.Name, entity); import.Alias.Entity = entity; name = entity.Name; //use alias name instead of namespace name } //only add unique namespaces Import cachedImport = nameSpaces[name] as Import; if (cachedImport == null) { nameSpaces[name] = import; } else { //ignore for partial classes in separate files if (cachedImport.LexicalInfo.FileName == import.LexicalInfo.FileName) { Warnings.Add(CompilerWarningFactory.DuplicateNamespace( import, import.Namespace)); } RemoveCurrentNode(); return; } } } _context.TraceInfo("{1}: import reference '{0}' bound to {2}.", import, import.LexicalInfo, entity.FullName); import.Entity = entity; }
public override void LeaveImport(Import node) { if (null != node.Alias) CheckName(node,node.Alias.Name); }
public override void OnImport(Import node) { // this is a bit nasty - get all the members of the referenced namespace // then push them on the tree, so they're referencable var ns = (INamespace)TypeSystemServices.GetEntity(node); currentDocument.Imports[node.Namespace] = new ImportedNamespaceTreeNode(new EntitySourceOrigin((IEntity)ns)); base.OnImport(node); }