Extract interfaces of Emitter and Parser.

This commit is contained in:
Antoine Aubry 2013-08-09 18:05:23 +01:00
Родитель b56d008557
Коммит 60002d0cde
23 изменённых файлов: 102 добавлений и 64 удалений

Просмотреть файл

@ -33,7 +33,7 @@ namespace Dumper
using (TextReader input = File.OpenText(args[0]))
{
int indent = 0;
Parser parser = new Parser(input);
var parser = new Parser(input);
while(parser.MoveNext())
{
if (parser.Current is StreamEnd || parser.Current is DocumentEnd || parser.Current is SequenceEnd || parser.Current is SequenceEnd || parser.Current is MappingEnd)

Просмотреть файл

@ -36,7 +36,7 @@ namespace YamlDotNet.Core
/// <summary>
/// Emits YAML streams.
/// </summary>
public class Emitter
public class Emitter : IEmitter
{
private readonly TextWriter output;
@ -108,7 +108,7 @@ namespace YamlDotNet.Core
private const int MaxBestIndent = 9;
/// <summary>
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
public Emitter(TextWriter output)
@ -117,7 +117,7 @@ namespace YamlDotNet.Core
}
/// <summary>
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
/// <param name="bestIndent">The preferred indentation.</param>
@ -127,7 +127,7 @@ namespace YamlDotNet.Core
}
/// <summary>
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
/// <param name="bestIndent">The preferred indentation.</param>
@ -139,7 +139,7 @@ namespace YamlDotNet.Core
/// <summary>
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
/// <param name="bestIndent">The preferred indentation.</param>

Просмотреть файл

@ -33,14 +33,14 @@ namespace YamlDotNet.Core
/// </summary>
public class EventReader
{
private readonly Parser parser;
private readonly IParser parser;
private bool endOfStream;
/// <summary>
/// Initializes a new instance of the <see cref="EventReader"/> class.
/// </summary>
/// <param name="parser">The parser that provides the events.</param>
public EventReader(Parser parser)
public EventReader(IParser parser)
{
this.parser = parser;
MoveNext();
@ -50,7 +50,7 @@ namespace YamlDotNet.Core
/// Gets the underlying parser.
/// </summary>
/// <value>The parser.</value>
public Parser Parser
public IParser Parser
{
get
{

Просмотреть файл

@ -0,0 +1,15 @@
using YamlDotNet.Core.Events;
namespace YamlDotNet.Core
{
/// <summary>
/// Represents a YAML stream emitter.
/// </summary>
public interface IEmitter
{
/// <summary>
/// Emits an event.
/// </summary>
void Emit(IParsingEvent @event);
}
}

Просмотреть файл

@ -0,0 +1,21 @@
using YamlDotNet.Core.Events;
namespace YamlDotNet.Core
{
/// <summary>
/// Represents a YAML stream paser.
/// </summary>
public interface IParser
{
/// <summary>
/// Gets the current event.
/// </summary>
ParsingEvent Current { get; }
/// <summary>
/// Moves to the next event.
/// </summary>
/// <returns>Returns true if there are more events available, otherwise returns false.</returns>
bool MoveNext();
}
}

Просмотреть файл

@ -33,7 +33,7 @@ namespace YamlDotNet.Core
/// <summary>
/// Parses YAML streams.
/// </summary>
public class Parser
public class Parser : IParser
{
private readonly Stack<ParserState> states = new Stack<ParserState>();
private readonly TagDirectiveCollection tagDirectives = new TagDirectiveCollection();
@ -57,7 +57,7 @@ namespace YamlDotNet.Core
}
/// <summary>
/// Initializes a new instance of the <see cref="Parser"/> class.
/// Initializes a new instance of the <see cref="IParser"/> class.
/// </summary>
/// <param name="input">The input where the YAML stream is to be read.</param>
public Parser(TextReader input)

Просмотреть файл

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -101,8 +101,10 @@
<Compile Include="Events\StreamEnd.cs" />
<Compile Include="Events\StreamStart.cs" />
<Compile Include="FakeList.cs" />
<Compile Include="IEmitter.cs" />
<Compile Include="ILookAheadBuffer.cs" />
<Compile Include="InsertionQueue.cs" />
<Compile Include="IParser.cs" />
<Compile Include="LookAheadBuffer.cs" />
<Compile Include="Mark.cs" />
<Compile Include="Parser.cs" />

Просмотреть файл

@ -7,10 +7,10 @@ namespace YamlDotNet.RepresentationModel.Serialization
{
public sealed class CustomSerializationObjectGraphVisitor : ChainedObjectGraphVisitor
{
private readonly Emitter emitter;
private readonly IEmitter emitter;
private readonly IEnumerable<IYamlTypeConverter> typeConverters;
public CustomSerializationObjectGraphVisitor(Emitter emitter, IObjectGraphVisitor nextVisitor, IEnumerable<IYamlTypeConverter> typeConverters)
public CustomSerializationObjectGraphVisitor(IEmitter emitter, IObjectGraphVisitor nextVisitor, IEnumerable<IYamlTypeConverter> typeConverters)
: base(nextVisitor)
{
this.emitter = emitter;

Просмотреть файл

@ -31,12 +31,12 @@ namespace YamlDotNet.RepresentationModel
{
/// <summary>
/// Reads this object's state from a YAML parser.
/// </summary>
void ReadYaml(Parser parser);
/// </summary>
void ReadYaml(IParser parser);
/// <summary>
/// Writes this object's state to a YAML emitter.
/// </summary>
void WriteYaml(Emitter emitter);
void WriteYaml(IEmitter emitter);
}
}

Просмотреть файл

@ -37,11 +37,11 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// <summary>
/// Reads an object's state from a YAML parser.
/// </summary>
object ReadYaml(Parser parser, Type type);
object ReadYaml(IParser parser, Type type);
/// <summary>
/// Writes the specified object's state to a YAML emitter.
/// </summary>
void WriteYaml(Emitter emitter, object value, Type type);
void WriteYaml(IEmitter emitter, object value, Type type);
}
}

Просмотреть файл

@ -122,9 +122,9 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// <summary>
/// Serializes the specified object.
/// </summary>
/// <param name="emitter">The <see cref="Emitter" /> where to serialize the object.</param>
/// <param name="emitter">The <see cref="IEmitter" /> where to serialize the object.</param>
/// <param name="graph">The object to serialize.</param>
public void Serialize(Emitter emitter, object graph)
public void Serialize(IEmitter emitter, object graph)
{
Serialize(emitter, graph, graph != null ? graph.GetType() : typeof(object));
}
@ -132,10 +132,10 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// <summary>
/// Serializes the specified object.
/// </summary>
/// <param name="emitter">The <see cref="Emitter" /> where to serialize the object.</param>
/// <param name="emitter">The <see cref="IEmitter" /> where to serialize the object.</param>
/// <param name="graph">The object to serialize.</param>
/// <param name="type">The static type of the object to serialize.</param>
public void Serialize(Emitter emitter, object graph, Type type)
public void Serialize(IEmitter emitter, object graph, Type type)
{
if (emitter == null)
{
@ -153,7 +153,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
EmitDocument(emitter, traversalStrategy, emittingVisitor, graph, type);
}
private void EmitDocument(Emitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IObjectGraphVisitor emittingVisitor, object graph, Type type)
private void EmitDocument(IEmitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IObjectGraphVisitor emittingVisitor, object graph, Type type)
{
emitter.Emit(new StreamStart());
emitter.Emit(new DocumentStart());
@ -164,7 +164,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
emitter.Emit(new StreamEnd());
}
private IObjectGraphVisitor CreateEmittingVisitor(Emitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IEventEmitter eventEmitter, object graph, Type type)
private IObjectGraphVisitor CreateEmittingVisitor(IEmitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IEventEmitter eventEmitter, object graph, Type type)
{
IObjectGraphVisitor emittingVisitor = new EmittingObjectGraphVisitor(eventEmitter);
@ -186,7 +186,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
return emittingVisitor;
}
private IEventEmitter CreateEventEmitter(Emitter emitter)
private IEventEmitter CreateEventEmitter(IEmitter emitter)
{
var writer = new WriterEventEmitter(emitter);

Просмотреть файл

@ -51,7 +51,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// Reads this object's state from a YAML parser.
/// </summary>
/// <param name="parser"></param>
void IYamlSerializable.ReadYaml(Parser parser)
void IYamlSerializable.ReadYaml(IParser parser)
{
events.Clear();
@ -74,7 +74,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// Writes this object's state to a YAML emitter.
/// </summary>
/// <param name="emitter"></param>
void IYamlSerializable.WriteYaml(Emitter emitter)
void IYamlSerializable.WriteYaml(IEmitter emitter)
{
foreach (var item in events)
{

Просмотреть файл

@ -5,9 +5,9 @@ namespace YamlDotNet.RepresentationModel.Serialization
{
public sealed class WriterEventEmitter : IEventEmitter
{
private readonly Emitter emitter;
private readonly IEmitter emitter;
public WriterEventEmitter(Emitter emitter)
public WriterEventEmitter(IEmitter emitter)
{
this.emitter = emitter;
}

Просмотреть файл

@ -53,7 +53,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
/// <param name="emitter">The emitter where the node is to be saved.</param>
/// <param name="state">The state.</param>
internal override void Emit(Emitter emitter, EmitterState state)
internal override void Emit(IEmitter emitter, EmitterState state)
{
throw new NotSupportedException("A YamlAliasNode is an implementation detail and should never be saved.");
}

Просмотреть файл

@ -168,7 +168,7 @@ namespace YamlDotNet.RepresentationModel
visitor.AssignAnchors(this);
}
internal void Save(Emitter emitter)
internal void Save(IEmitter emitter)
{
AssignAnchors();

Просмотреть файл

@ -251,7 +251,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
/// <param name="emitter">The emitter where the node is to be saved.</param>
/// <param name="state">The state.</param>
internal override void Emit(Emitter emitter, EmitterState state)
internal override void Emit(IEmitter emitter, EmitterState state)
{
emitter.Emit(new MappingStart(Anchor, Tag, true, Style));
foreach (var entry in children)

Просмотреть файл

@ -113,7 +113,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
/// <param name="emitter">The emitter where the node is to be saved.</param>
/// <param name="state">The state.</param>
internal void Save(Emitter emitter, EmitterState state)
internal void Save(IEmitter emitter, EmitterState state)
{
if (!string.IsNullOrEmpty(Anchor) && !state.EmittedAnchors.Add(Anchor))
{
@ -130,7 +130,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
/// <param name="emitter">The emitter where the node is to be saved.</param>
/// <param name="state">The state.</param>
internal abstract void Emit(Emitter emitter, EmitterState state);
internal abstract void Emit(IEmitter emitter, EmitterState state);
/// <summary>
/// Accepts the specified visitor by calling the appropriate Visit method on it.

Просмотреть файл

@ -89,7 +89,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
/// <param name="emitter">The emitter where the node is to be saved.</param>
/// <param name="state">The state.</param>
internal override void Emit(Emitter emitter, EmitterState state)
internal override void Emit(IEmitter emitter, EmitterState state)
{
emitter.Emit(new Scalar(Anchor, Tag, Value, Style, true, false));
}

Просмотреть файл

@ -158,7 +158,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
/// <param name="emitter">The emitter where the node is to be saved.</param>
/// <param name="state">The state.</param>
internal override void Emit(Emitter emitter, EmitterState state)
internal override void Emit(IEmitter emitter, EmitterState state)
{
emitter.Emit(new SequenceStart(Anchor, Tag, true, Style));
foreach (var node in children)

Просмотреть файл

@ -88,7 +88,7 @@ namespace YamlDotNet.RepresentationModel
{
documents.Clear();
Parser parser = new Parser(input);
var parser = new Parser(input);
EventReader events = new EventReader(parser);
events.Expect<StreamStart>();
@ -106,7 +106,7 @@ namespace YamlDotNet.RepresentationModel
/// <param name="output">The output.</param>
public void Save(TextWriter output)
{
Emitter emitter = new Emitter(output);
IEmitter emitter = new Emitter(output);
emitter.Emit(new StreamStart());
foreach (var document in documents)

Просмотреть файл

@ -36,9 +36,9 @@ namespace YamlDotNet.UnitTests
private void ParseAndEmit(string name) {
string testText = YamlFile(name).ReadToEnd();
Parser parser = new Parser(new StringReader(testText));
IParser parser = new Parser(new StringReader(testText));
using(StringWriter output = new StringWriter()) {
Emitter emitter = new Emitter(output, 2, int.MaxValue, false);
IEmitter emitter = new Emitter(output, 2, int.MaxValue, false);
while(parser.MoveNext()) {
//Console.WriteLine(parser.Current.GetType().Name);
Console.Error.WriteLine(parser.Current);

Просмотреть файл

@ -33,19 +33,19 @@ namespace YamlDotNet.UnitTests
{
public class ParserTests : YamlTest
{
private static Parser CreateParser(string name) {
private static IParser CreateParser(string name) {
return new Parser(YamlFile(name));
}
private void AssertHasNext(Parser parser) {
private void AssertHasNext(IParser parser) {
Assert.True(parser.MoveNext());
}
private void AssertDoesNotHaveNext(Parser parser) {
private void AssertDoesNotHaveNext(IParser parser) {
Assert.False(parser.MoveNext());
}
private void AssertCurrent(Parser parser, ParsingEvent expected) {
private void AssertCurrent(IParser parser, ParsingEvent expected) {
Console.WriteLine(expected.GetType().Name);
Assert.True(expected.GetType().IsAssignableFrom(parser.Current.GetType()), string.Format("The event is not of the expected type. Exprected: {0}, Actual: {1}", expected.GetType().Name, parser.Current.GetType().Name));
@ -86,7 +86,7 @@ namespace YamlDotNet.UnitTests
}
}
private void AssertNext(Parser parser, ParsingEvent expected) {
private void AssertNext(IParser parser, ParsingEvent expected) {
AssertHasNext(parser);
AssertCurrent(parser, expected);
}
@ -94,7 +94,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void EmptyDocument()
{
Parser parser = CreateParser("empty.yaml");
IParser parser = CreateParser("empty.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new StreamEnd());
@ -109,7 +109,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyEventsOnExample1()
{
Parser parser = CreateParser("test1.yaml");
IParser parser = CreateParser("test1.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(new VersionDirective(new Core.Version(1, 1)), new TagDirectiveCollection(new TagDirective[] {
@ -126,7 +126,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample2()
{
Parser parser = CreateParser("test2.yaml");
IParser parser = CreateParser("test2.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -139,7 +139,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample3()
{
Parser parser = CreateParser("test3.yaml");
IParser parser = CreateParser("test3.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, false));
@ -152,7 +152,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample4()
{
Parser parser = CreateParser("test4.yaml");
IParser parser = CreateParser("test4.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -171,7 +171,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample5()
{
Parser parser = CreateParser("test5.yaml");
IParser parser = CreateParser("test5.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -186,7 +186,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample6()
{
Parser parser = CreateParser("test6.yaml");
IParser parser = CreateParser("test6.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -199,7 +199,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample7()
{
Parser parser = CreateParser("test7.yaml");
IParser parser = CreateParser("test7.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, false));
@ -227,7 +227,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample8()
{
Parser parser = CreateParser("test8.yaml");
IParser parser = CreateParser("test8.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -244,7 +244,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample9()
{
Parser parser = CreateParser("test9.yaml");
IParser parser = CreateParser("test9.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -262,7 +262,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample10()
{
Parser parser = CreateParser("test10.yaml");
IParser parser = CreateParser("test10.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -288,7 +288,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample11()
{
Parser parser = CreateParser("test11.yaml");
IParser parser = CreateParser("test11.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -318,7 +318,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample12()
{
Parser parser = CreateParser("test12.yaml");
IParser parser = CreateParser("test12.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -346,7 +346,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample13()
{
Parser parser = CreateParser("test13.yaml");
IParser parser = CreateParser("test13.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -372,7 +372,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokensOnExample14()
{
Parser parser = CreateParser("test14.yaml");
IParser parser = CreateParser("test14.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, true));
@ -391,7 +391,7 @@ namespace YamlDotNet.UnitTests
[Fact]
public void VerifyTokenWithLocalTags()
{
Parser parser = CreateParser("local-tags.yaml");
IParser parser = CreateParser("local-tags.yaml");
AssertNext(parser, new StreamStart());
AssertNext(parser, new DocumentStart(null, defaultDirectives, false));

Просмотреть файл

@ -614,14 +614,14 @@ namespace YamlDotNet.UnitTests.RepresentationModel
{
public bool Accepts(Type type) { return type == typeof(SomeCustomeType); }
public object ReadYaml(Parser parser, Type type)
public object ReadYaml(IParser parser, Type type)
{
var value = ((Scalar)parser.Current).Value;
parser.MoveNext();
return new SomeCustomeType(value);
}
public void WriteYaml(Emitter emitter, object value, Type type)
public void WriteYaml(IEmitter emitter, object value, Type type)
{
emitter.Emit(new Scalar(((SomeCustomeType)value).Value));
}