private static void AddTokenFlavorNone(IList <FormattedSentence.Token> tokens, int endAt, ref FormattedSentence.SimpleToken.Builder tokenBuilder) { if (tokenBuilder.IsValid) { if (tokenBuilder.Flavor == FormattedSentence.SimpleToken.TokenFlavor.InCurlyBraces) { throw new FormatException("'{' is mismatched or not escaped"); } FormattedSentence.Sentence.AddToken(tokens, endAt, ref tokenBuilder); } }
private static bool AddToken(IList <FormattedSentence.Token> tokens, int endAt, ref FormattedSentence.SimpleToken.Builder tokenBuilder) { if (tokenBuilder.IsValid) { FormattedSentence.SimpleToken simpleToken = tokenBuilder.Create(endAt); if (simpleToken != null) { tokens.Add(simpleToken); } tokenBuilder.Invalidate(); return(simpleToken != null); } return(false); }
private static int Parse(string input, int startAt, out FormattedSentence.Sentence sentence) { FormattedSentence.SimpleToken.Builder builder = new FormattedSentence.SimpleToken.Builder(null, FormattedSentence.SimpleToken.TokenFlavor.None, -1); List <FormattedSentence.Token> list = new List <FormattedSentence.Token>(); int num = startAt - 1; bool flag = false; while (!flag) { num++; if (num > input.Length) { throw new FormatException("Unexpected end of escape sequence. If '' was intended for output, replace it with '\\'"); } if (num == input.Length) { break; } char c = input[num]; char c2 = c; switch (c2) { case '<': { FormattedSentence.Sentence.AddTokenFlavorNone(list, num - 1, ref builder); FormattedSentence.Sentence item; num = FormattedSentence.Sentence.Parse(input, num + 1, out item); if (num >= input.Length || input[num] != '>') { throw new FormatException("'<' is mismatched or not properly escaped"); } list.Add(item); continue; } case '=': break; case '>': flag = true; continue; default: switch (c2) { case '{': FormattedSentence.Sentence.AddTokenFlavorNone(list, num - 1, ref builder); builder = new FormattedSentence.SimpleToken.Builder(input, FormattedSentence.SimpleToken.TokenFlavor.InCurlyBraces, num + 1); continue; case '}': if (!builder.IsValid || builder.Flavor != FormattedSentence.SimpleToken.TokenFlavor.InCurlyBraces) { throw new FormatException("'}' is mismatched or not escaped"); } FormattedSentence.Sentence.AddToken(list, num - 1, ref builder); continue; } break; } if (!builder.IsValid) { builder = new FormattedSentence.SimpleToken.Builder(input, FormattedSentence.SimpleToken.TokenFlavor.None, num); } if (c == '\\') { num++; } } FormattedSentence.Sentence.AddTokenFlavorNone(list, num - 1, ref builder); sentence = new FormattedSentence.Sentence(list); return(num); }