Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions src/Parlot/Fluent/Else.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
using Parlot.Compilation;
using Parlot.Rewriting;
using System.Linq.Expressions;

namespace Parlot.Fluent;

/// <summary>
/// Returns a default value if the previous parser failed.
/// </summary>
public sealed class Else<T> : Parser<T>, ICompilable, ISeekable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change

public sealed class Else<T> : Parser<T>, ICompilable
{
private readonly Parser<T> _parser;
private readonly T _value;

public bool CanSeek { get; }

public char[] ExpectedChars { get; } = [];

public bool SkipWhitespace { get; }

public Else(Parser<T> parser, T value)
{
_parser = parser;
_value = value;

if (_parser is ISeekable seekable)
{
CanSeek = seekable.CanSeek;
ExpectedChars = seekable.ExpectedChars;
SkipWhitespace = seekable.SkipWhitespace;
}
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down
15 changes: 1 addition & 14 deletions src/Parlot/Fluent/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,15 @@

namespace Parlot.Fluent;

public sealed class ElseError<T> : Parser<T>, ICompilable, ISeekable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a marker class, doesn't matter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's a public API it's a breaking-change :)

public sealed class ElseError<T> : Parser<T>, ICompilable
{
private readonly Parser<T> _parser;
private readonly string _message;

public bool CanSeek { get; }

public char[] ExpectedChars { get; } = [];

public bool SkipWhitespace { get; }

public ElseError(Parser<T> parser, string message)
{
_parser = parser ?? throw new ArgumentNullException(nameof(parser));
_message = message;

if (_parser is ISeekable seekable)
{
CanSeek = seekable.CanSeek;
ExpectedChars = seekable.ExpectedChars;
SkipWhitespace = seekable.SkipWhitespace;
}
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down
14 changes: 14 additions & 0 deletions test/Parlot.Tests/FluentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,4 +1172,18 @@ public void AnyOfShouldResetPositionWhenFalse()
.And(Literals.AnyOf("Z"))
.TryParse("aaaZZ", out _));
}

[Fact]
public void ElseErrorShouldNotBeSeekable()
{
Parser<char> a = Terms.Char('a');
Parser<char> b = Terms.Char('b');
Parser<object> c = a.Then<object>();

// Use two parsers to ensure OneOf tries to build a lookup table
var parser = OneOf(a.ElseError("Error"), b);

Assert.True(parser.TryParse("a", out _));
Assert.Throws<ParseException>(() => parser.Parse("b"));
}
}