private void WriteEntryToStream(BibEntry value, TextWriter stream) { stream.Write("@"); stream.Write(value.Type); stream.Write('{'); stream.Write(value.Key); stream.Write(","); stream.Write(this.config.NewLine); var pad = 0; if (this.config.Align) { pad = value.Max(v => v.Key.Length); } foreach (var tag in value) { if (!string.IsNullOrWhiteSpace(tag.Key) && !string.IsNullOrWhiteSpace(tag.Value)) { stream.Write(this.config.Indent); stream.Write(tag.Key.PadRight(pad)); stream.Write(" = {"); stream.Write(tag.Value); stream.Write("},"); stream.Write(this.config.NewLine); } } stream.Write("}"); stream.Flush(); }
private IEnumerable <BibEntry> Parser() { var curState = ParserState.Begin; var nextState = ParserState.Begin; BibEntry bib = null; var tagValueBuilder = new StringBuilder(); var tagName = ""; // Fetch token from Tokenizer and build BibEntry foreach (var token in Tokenizer()) { // Transfer state if (StateMap[curState].ContainsKey(token.Type)) { nextState = StateMap[curState][token.Type].Item1; } else { var expected = from pair in StateMap[curState] select pair.Key; throw new UnexpectedTokenException(_lineCount, _colCount, token.Type, expected.ToArray()); } // Build BibEntry switch (StateMap[curState][token.Type].Item2) { case BibBuilderState.Create: bib = new BibEntry(); break; case BibBuilderState.SetType: Debug.Assert(bib != null, "bib != null"); bib.Type = token.Value; break; case BibBuilderState.SetKey: Debug.Assert(bib != null, "bib != null"); bib.Key = token.Value; break; case BibBuilderState.SetTagName: tagName = token.Value; break; case BibBuilderState.SetTagValue: tagValueBuilder.Append(token.Value); break; case BibBuilderState.SetTag: Debug.Assert(bib != null, "bib != null"); bib[tagName] = tagValueBuilder.ToString(); tagValueBuilder.Clear(); tagName = string.Empty; break; case BibBuilderState.Build: if (tagName != string.Empty) { Debug.Assert(bib != null, "bib != null"); bib[tagName] = tagValueBuilder.ToString(); tagValueBuilder.Clear(); tagName = string.Empty; } yield return(bib); break; } curState = nextState; } if (curState != ParserState.OutEntry) { var expected = from pair in StateMap[curState] select pair.Key; throw new UnexpectedTokenException(_lineCount, _colCount, TokenType.EOF, expected.ToArray()); } }