/
EvilBeaver
/
OneScript
Обзор
Документация
Войти
/
EvilBeaver
/
OneScript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/OneScript.Core/Compilation/Binding/SymbolsCollection.cs
88 строк
3 KB
EvilBeaver
Поправил мелкие замечания сонара
24 апр 2024, 13:50
24 апр 2024, 13:50
ebfc286
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System; using System.Collections; using System.Collections.Generic; using OneScript.Commons; using OneScript.Language; using OneScript.Language.SyntaxAnalysis; namespace OneScript.Compilation.Binding { public class SymbolsCollection<T> : IReadOnlyList<T> where T : ISymbol { private readonly IndexedNameValueCollection<T> _storage = new IndexedNameValueCollection<T>(); public int Add(T symbol) { CodeError SelectMessage(T item, string text) { return item switch { IVariableSymbol _ => LocalizedErrors.DuplicateVarDefinition(text), IMethodSymbol _ => LocalizedErrors.DuplicateMethodDefinition(text), _ => throw new InvalidOperationException() }; } var index = AddItem(symbol); if (index == -1) throw new BindingException(SelectMessage(symbol, symbol.Name)); if (!AddAlias(index, symbol)) throw new BindingException(SelectMessage(symbol, symbol.Alias)); return index; } public IEnumerator<T> GetEnumerator() => _storage.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public int Count => _storage.Count; public T this[int index] => _storage[index]; public T this[string index] => _storage[index]; public int IndexOf(string name) => _storage.IndexOf(name); public bool IsDefined(string name) => _storage.IndexOf(name) >= 0; private int AddItem(T item) { try { return _storage.Add(item, item.Name); } catch (InvalidOperationException) { return -1; } } private bool AddAlias(int index, T item) { try { if (string.IsNullOrWhiteSpace(item.Alias)) return true; _storage.AddName(index, item.Alias); return true; } catch (InvalidOperationException) { return false; } } } }