-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Else and ThenElse implementation to replace Optional (#113)
Fixes #59
- Loading branch information
1 parent
a3b3276
commit bc9680c
Showing
12 changed files
with
229 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Parlot.Compilation; | ||
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 | ||
{ | ||
private readonly Parser<T> _parser; | ||
private readonly T _value; | ||
|
||
public Else(Parser<T> parser, T value) | ||
{ | ||
_parser = parser; | ||
_value = value; | ||
} | ||
|
||
public override bool Parse(ParseContext context, ref ParseResult<T> result) | ||
{ | ||
context.EnterParser(this); | ||
|
||
if (!_parser.Parse(context, ref result)) | ||
{ | ||
result.Set(result.Start, result.End, _value); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public CompilationResult Compile(CompilationContext context) | ||
{ | ||
var result = new CompilationResult(); | ||
|
||
var success = context.DeclareSuccessVariable(result, true); | ||
var value = context.DeclareValueVariable(result, Expression.Default(typeof(T)), typeof(T)); | ||
|
||
var parserCompileResult = _parser.Build(context); | ||
|
||
// success = true; | ||
// | ||
// parser instructions | ||
// | ||
// if (parser.success) | ||
// { | ||
// value = parser.Value | ||
// } | ||
// else | ||
// { | ||
// value = defaultValue | ||
// } | ||
|
||
result.Body.Add( | ||
Expression.Block( | ||
parserCompileResult.Variables, | ||
Expression.Block(parserCompileResult.Body), | ||
context.DiscardResult | ||
? Expression.Empty() | ||
: Expression.IfThenElse( | ||
parserCompileResult.Success, | ||
Expression.Assign(value, parserCompileResult.Value), | ||
Expression.Assign(value, Expression.Constant(_value, typeof(T))) | ||
) | ||
) | ||
); | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.