public MessageTemplate Parse(string messageTemplate) { if (messageTemplate == null) { throw new ArgumentNullException(nameof(messageTemplate)); } if (messageTemplate.Length == 0) { return(new MessageTemplate(Array.Empty <MessageTemplate.Token>())); } var chars = new CharsReadOnlyList(messageTemplate); var cursor = new ElementsCursor <char>(chars, EndOfInput); var tokens = new List <MessageTemplate.Token>(); while (!cursor.IsAtTheEnd()) { switch (cursor.Peek()) { case '{': if (cursor.Peek(1) == '{') { // Escaped opening bracket tokens.Add(ScanTextToken(cursor)); } else { // Argument tokens.Add(ScanArgumentToken(messageTemplate, cursor)); } break; case '}': if (cursor.Peek(1) == '}') { // Escaped closing bracket tokens.Add(ScanTextToken(cursor)); } else { throw new FormatException($"Unescaped closing bracket at symbol {cursor.Position}."); } break; default: tokens.Add(ScanTextToken(cursor)); break; } } return(new MessageTemplate(tokens.ToArray())); }
public IEnumerable <SyntaxToken> Lex(string commandLine, CancellationToken cancellation = default) { if (commandLine == null) { throw new ArgumentNullException(nameof(commandLine)); } cancellation.ThrowIfCancellationRequested(); var chars = new CharsReadOnlyList(commandLine); var cursor = new ElementsCursor <char>(chars, EndOfInput); var builder = new SyntaxTokenBuilder(); do { builder.LeadingTrivia = LexSyntaxTrivia(commandLine, cursor); LexSyntaxToken(builder, commandLine, cursor); builder.TrailingTrivia = LexSyntaxTrivia(commandLine, cursor); cancellation.ThrowIfCancellationRequested(); yield return(builder.Build()); } while (builder.Kind != SyntaxKind.EndOfInputToken); }