コード例 #1
0
        public Library ImportLibrary(Parser parser, Token throwToken, string name, List <Executable> executablesOut)
        {
            name = name.Split('.')[0];
            Library library = librariesAlreadyImportedIndexByName.ContainsKey(name)
                ? librariesAlreadyImported[librariesAlreadyImportedIndexByName[name]]
                : null;

            if (library == null)
            {
                string libraryManifestPath = this.GetSystemLibraryPath(name, parser.BuildContext.CrayonPath, parser.BuildContext.ProjectDirectory);

                if (libraryManifestPath == null)
                {
                    // No library found. Could just be a local namespace import.
                    // If this is a bogus import, it'll throw in the Resolver.
                    return(null);
                }

                string platformName = parser.BuildContext.Platform;
                Platform.AbstractPlatform platform = platformName == null || this.PlatformProvider == null ? null : this.PlatformProvider.GetPlatform(platformName);
                library = new Library(name, libraryManifestPath, platform);

                this.librariesAlreadyImportedIndexByName[name] = this.librariesAlreadyImported.Count;
                this.librariesAlreadyImported.Add(library);

                this.importedLibraries[name] = library;
                this.librariesByKey[name.ToLowerInvariant()] = library;

                string oldSystemLibrary = parser.CurrentSystemLibrary;
                parser.CurrentSystemLibrary = name;

                Dictionary <string, string> embeddedCode = library.GetEmbeddedCode();
                foreach (string embeddedFile in embeddedCode.Keys)
                {
                    string fakeName = "[" + embeddedFile + "]";
                    string code     = embeddedCode[embeddedFile];
                    parser.PushLocale(ENGLISH_LOCALE_FOR_LIBRARIES);
                    executablesOut.AddRange(parser.ParseInterpretedCode(fakeName, code, name));
                    parser.PopLocale();
                }

                parser.CurrentSystemLibrary = oldSystemLibrary;
            }

            // Even if already imported, still must check to see if this import is allowed here.
            if (!library.IsAllowedImport(parser.CurrentSystemLibrary))
            {
                throw new ParserException(throwToken, "This library cannot be imported from here.");
            }

            return(library);
        }
コード例 #2
0
        public Executable[] ImportLibrary(Parser parser, Token throwToken, string name)
        {
            name = name.Split('.')[0];
            Library library = alreadyImported.ContainsKey(name) ? alreadyImported[name] : null;

            Executable[] embedCode = EMPTY_EXECUTABLE;
            if (library == null)
            {
                string libraryManifestPath = this.GetSystemLibraryPath(name);

                if (libraryManifestPath == null)
                {
                    // TODO: show suggestions of similarly named libraries
                    throw new ParserException(throwToken, "Library not found.");
                }

                library = new Library(name, libraryManifestPath, parser.BuildContext.Platform);

                alreadyImported.Add(name, library);

                library.ExtractResources(parser.BuildContext.Platform, this.filesToCopy, this.contentToEmbed);

                this.importedLibraries[name] = library;
                this.librariesByKey[name.ToLowerInvariant()] = library;

                string oldSystemLibrary = parser.CurrentSystemLibrary;
                parser.CurrentSystemLibrary = name;

                List <Executable>           output       = new List <Executable>();
                Dictionary <string, string> embeddedCode = library.GetEmbeddedCode();
                foreach (string embeddedFile in embeddedCode.Keys)
                {
                    string fakeName = "[" + embeddedFile + "]";
                    string code     = embeddedCode[embeddedFile];
                    output.AddRange(parser.ParseInterpretedCode(fakeName, code, name));
                }

                parser.CurrentSystemLibrary = oldSystemLibrary;
                embedCode = output.ToArray();
            }

            // Even if already imported, still must check to see if this import is allowed here.
            if (!library.IsAllowedImport(parser.CurrentSystemLibrary))
            {
                throw new ParserException(throwToken, "This library cannot be imported from here.");
            }

            return(embedCode);
        }
コード例 #3
0
ファイル: LibraryManager.cs プロジェクト: TimJSwan89/crayon
        public Library ImportLibrary(Parser parser, Token throwToken, string name)
        {
            name = name.Split('.')[0];
            string          key             = parser.CurrentLocale.ID + ":" + name;
            LibraryMetadata libraryMetadata = this.GetLibraryMetadataFromAnyPossibleKey(key);

            if (libraryMetadata == null)
            {
                // check for default locale
                libraryMetadata = this.GetLibraryMetadataFromAnyPossibleKey(name);
                if (libraryMetadata == null)
                {
                    // No library found. Could just be a local namespace import.
                    // If this is a bogus import, it'll throw in the Resolver.
                    return(null);
                }

                if (libraryMetadata.SupportedLocales.Contains(parser.CurrentLocale))
                {
                    // If you import something by its default name from a supported locale, then it doesn't count.
                    // Don't throw an error. A user should be able to define a namespace that happens to have the
                    // same name as a library in some locale they aren't using.
                    return(null);
                }
            }

            Library library = librariesAlreadyImportedIndexByKey.ContainsKey(libraryMetadata.CanonicalKey)
                ? librariesAlreadyImported[librariesAlreadyImportedIndexByKey[libraryMetadata.CanonicalKey]]
                : null;

            if (library == null)
            {
                string platformName = parser.BuildContext.Platform;
                Platform.AbstractPlatform platform = platformName == null || this.PlatformProvider == null ? null : this.PlatformProvider.GetPlatform(platformName);
                library = new Library(libraryMetadata, platform);
                CompilationScope scope = new CompilationScope(parser.BuildContext, library);
                library.Scope = scope;
                library.AddLocaleAccess(parser.CurrentLocale);

                this.librariesAlreadyImportedIndexByKey[libraryMetadata.CanonicalKey] = this.librariesAlreadyImported.Count;
                this.librariesAlreadyImported.Add(library);

                this.importedLibraries[name] = library;
                this.librariesByKey[name.ToLowerInvariant()] = library;

                parser.PushScope(scope);
                Dictionary <string, string> embeddedCode = library.GetEmbeddedCode();
                foreach (string embeddedFile in embeddedCode.Keys)
                {
                    string fakeName = "[" + embeddedFile + "]";
                    string code     = embeddedCode[embeddedFile];
                    parser.ParseInterpretedCode(fakeName, code);
                }
                parser.PopScope();
            }

            // Even if already imported, still must check to see if this import is allowed here.
            if (!library.IsAllowedImport(parser.CurrentLibrary))
            {
                throw new ParserException(throwToken, "This library cannot be imported from here.");
            }

            return(library);
        }
コード例 #4
0
		public Executable[] ImportLibrary(Parser parser, Token throwToken, string name)
		{
			name = name.Split('.')[0];
			if (alreadyImported.Contains(name))
			{
				return EMPTY_EXECUTABLE;
			}

			alreadyImported.Add(name);

			string dllPath = this.GetSystemLibraryPath(name);

			if (dllPath == null)
			{
				return EMPTY_EXECUTABLE;
			}

			System.Reflection.Assembly assembly = null;
			try
			{
				assembly = System.Reflection.Assembly.LoadFrom(dllPath);
			}
			catch (Exception)
			{
				throw new ParserException(throwToken, "Could not import library: " + name);
			}

			ILibraryConfig libraryConfig = assembly.CreateInstance(name + ".LibraryConfig") as ILibraryConfig;
			if (libraryConfig == null)
			{
				throw new ParserException(throwToken, "Error creating LibraryConfig instance in Library '" + name + "'");
			}

			this.importedLibraries[name] = libraryConfig;
			this.librariesByKey[name.ToLowerInvariant()] = libraryConfig;

			string oldSystemLibrary = parser.CurrentSystemLibrary;
			parser.CurrentSystemLibrary = name;

			string libraryCode = libraryConfig.GetEmbeddedCode();
			Executable[] libraryParseTree = parser.ParseInterpretedCode("[" + name + "]", libraryCode, name);

			parser.CurrentSystemLibrary = oldSystemLibrary;
			return libraryParseTree;
		}