Singleton for each language.
Exemple #1
0
        internal void AddLanguage(string languageTypeName, string displayName, IList <string> names, IList <string> fileExtensions,
                                  IDictionary <string, object> options, string paramName)
        {
            ContractUtils.Requires(!_frozen, "Configuration cannot be modified once the runtime is initialized");
            ContractUtils.Requires(
                names.TrueForAll((id) => !String.IsNullOrEmpty(id) && !_languageNames.ContainsKey(id)),
                paramName ?? "names",
                "Language name should not be null, empty or duplicated between languages"
                );
            ContractUtils.Requires(
                fileExtensions.TrueForAll((ext) => !String.IsNullOrEmpty(ext) && !_languageExtensions.ContainsKey(ext)),
                paramName ?? "fileExtensions",
                "File extension should not be null, empty or duplicated between languages"
                );
            ContractUtils.RequiresNotNull(displayName, paramName ?? "displayName");

            if (string.IsNullOrEmpty(displayName))
            {
                ContractUtils.Requires(names.Count > 0, paramName ?? "displayName", "Must have a non-empty display name or a a non-empty list of language names");
                displayName = names[0];
            }

            var aqtn = AssemblyQualifiedTypeName.ParseArgument(languageTypeName, paramName ?? "languageTypeName");

            if (_languageConfigurations.ContainsKey(aqtn))
            {
                throw new ArgumentException(string.Format("Duplicate language with type name '{0}'", aqtn), "languageTypeName");
            }

            // Add global language options first, they can be rewritten by language specific ones:
            var mergedOptions = new Dictionary <string, object>(_options);

            // Replace global options with language-specific options
            foreach (var option in options)
            {
                mergedOptions[option.Key] = option.Value;
            }

            var config = new LanguageConfiguration(aqtn, displayName, mergedOptions);

            _languageConfigurations.Add(aqtn, config);

            // allow duplicate ids in identifiers and extensions lists:
            foreach (var name in names)
            {
                _languageNames[name] = config;
            }

            foreach (var ext in fileExtensions)
            {
                _languageExtensions[NormalizeExtension(ext)] = config;
            }
        }
Exemple #2
0
        internal string[] GetFileExtensions(LanguageConfiguration config)
        {
            var result = new List <string>();

            foreach (var entry in _languageExtensions)
            {
                if (entry.Value == config)
                {
                    result.Add(entry.Key);
                }
            }

            return(result.ToArray());
        }
Exemple #3
0
        private LanguageContext LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config)
        {
            var language = config.LoadLanguageContext(manager, out bool alreadyLoaded);

            if (!alreadyLoaded)
            {
                // Checks whether a single language is not registered under two different AQTNs.
                // We can only do it now because there is no way how to ensure that two AQTNs don't refer to the same type w/o loading the type.
                // The check takes place after config.LoadLanguageContext is called to avoid calling user code while holding a lock.
                lock (_loadedProviderTypes) {
                    Type type = language.GetType();
                    if (_loadedProviderTypes.TryGetValue(type, out LanguageConfiguration existingConfig))
                    {
                        throw new InvalidOperationException(
                                  $"Language implemented by type '{config.ProviderName}' has already been loaded using name '{existingConfig.ProviderName}'");
                    }

                    _loadedProviderTypes.Add(type, config);
                }
            }
            return(language);
        }
        internal string[] GetFileExtensions(LanguageConfiguration config) {
            var result = new List<string>();
            foreach (var entry in _languageExtensions) {
                if (entry.Value == config) {
                    result.Add(entry.Key);
                }
            }

            return result.ToArray();
        }
        private LanguageContext LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config) {
            bool alreadyLoaded;
            var language = config.LoadLanguageContext(manager, out alreadyLoaded);

            if (!alreadyLoaded) {
                // Checks whether a single language is not registered under two different AQTNs.
                // We can only do it now because there is no way how to ensure that two AQTNs don't refer to the same type w/o loading the type.
                // The check takes place after config.LoadLanguageContext is called to avoid calling user code while holding a lock.
                lock (_loadedProviderTypes) {
                    LanguageConfiguration existingConfig;
                    Type type = language.GetType();
                    if (_loadedProviderTypes.TryGetValue(type, out existingConfig)) {
                        throw new InvalidOperationException(String.Format("Language implemented by type '{0}' has already been loaded using name '{1}'",
                            config.ProviderName, existingConfig.ProviderName));
                    }

                    _loadedProviderTypes.Add(type, config);
                }
            }
            return language;
        }
        internal void AddLanguage(string languageTypeName, string displayName, IList<string> names, IList<string> fileExtensions, 
            IDictionary<string, object> options, string paramName) {
            ContractUtils.Requires(!_frozen, "Configuration cannot be modified once the runtime is initialized");
            ContractUtils.Requires(
                names.TrueForAll((id) => !String.IsNullOrEmpty(id) && !_languageNames.ContainsKey(id)),
                paramName ?? "names",
                "Language name should not be null, empty or duplicated between languages"
            );
            ContractUtils.Requires(
                fileExtensions.TrueForAll((ext) => !String.IsNullOrEmpty(ext) && !_languageExtensions.ContainsKey(ext)),
                paramName ?? "fileExtensions",
                "File extension should not be null, empty or duplicated between languages"
            );
            ContractUtils.RequiresNotNull(displayName, paramName ?? "displayName");

            if (string.IsNullOrEmpty(displayName)) {
                ContractUtils.Requires(names.Count > 0, paramName ?? "displayName", "Must have a non-empty display name or a a non-empty list of language names");
                displayName = names[0];
            }

            var aqtn = AssemblyQualifiedTypeName.ParseArgument(languageTypeName, paramName ?? "languageTypeName");
            if (_languageConfigurations.ContainsKey(aqtn)) {
                throw new ArgumentException(string.Format("Duplicate language with type name '{0}'", aqtn), "languageTypeName");
            }

            // Add global language options first, they can be rewritten by language specific ones:
            var mergedOptions = new Dictionary<string, object>(_options);

            // Replace global options with language-specific options
            foreach (var option in options) {
                mergedOptions[option.Key] = option.Value;
            }

            var config = new LanguageConfiguration(aqtn, displayName, mergedOptions);

            _languageConfigurations.Add(aqtn, config);

            // allow duplicate ids in identifiers and extensions lists:
            foreach (var name in names) {
                _languageNames[name] = config;
            }

            foreach (var ext in fileExtensions) {
                _languageExtensions[NormalizeExtension(ext)] = config;
            }
        }