using System.Collections; using System.Collections.Generic; using System.Linq; namespace Parser.Internal { public class DiagnosticsBag : IEnumerable { internal DiagnosticsBag() { _diagnostics = new List(); } public DiagnosticsBag(IEnumerable diagnostics) { _diagnostics = diagnostics.ToList(); } private readonly List _diagnostics; public IReadOnlyCollection Diagnostics => _diagnostics.AsReadOnly(); private void Report(TextSpan span, string message) { var diagnostic = new Diagnostic(span, message); _diagnostics.Add(diagnostic); } private void Report(string message) { var diagnostic = new Diagnostic(message); _diagnostics.Add(diagnostic); } internal void ReportUnexpectedEndOfFile(TextSpan span) { Report(span, "Unexpected end of file."); } internal void ReportUnexpectedCharacterWhileParsingNumber(TextSpan span, char c) { Report(span, $"Unexpected character '{c}' while parsing a number."); } internal void ReportUnexpectedEOLWhileParsingString(TextSpan span) { Report(span, "Unexpected end of line while parsing a string literal."); } internal void ReportUnknownSymbol(TextSpan span, char c) { Report(span, $"Unknown symbol '{c}'."); } internal void ReportUnexpectedToken(TokenKind expected, TokenKind actual) { Report($"Unexpected token '{actual}', expected '{expected}'."); } public IEnumerator GetEnumerator() { return _diagnostics.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }