Skip to content

Intern the parts of Superpower used by this assembly #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 25, 2021
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,8 @@ var myFunctions = new StaticMemberNameResolver(typeof(MyFunctions));
var expr = SerilogExpression.Compile("IsHello(User.Name)", new[] { myFunctions });
// Filter events based on whether `User.Name` is `'Hello'` :-)
```

## Acknowledgements

Includes the parser combinator implementation from [Superpower](https://github.com/datalust/superpower), copyright Datalust,
Superpower Contributors, and Sprache Contributors; licensed under the Apache License, 2.0.
1 change: 1 addition & 0 deletions serilog-expressions.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Existentials/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=formattable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nblumhardt/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ogham/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Reorderable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subproperties/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override string ToString()
case IFormattable formattable:
return formattable.ToString(null, CultureInfo.InvariantCulture);
default:
return (sv.Value ?? "null").ToString();
return (sv.Value ?? "null").ToString() ?? "<ToString() returned null>";
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.Expressions/Expressions/Parsing/Combinators.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Superpower;
using Superpower.Model;
using Serilog.ParserConstruction;
using Serilog.ParserConstruction.Model;

namespace Serilog.Expressions.Parsing
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Superpower;
using Superpower.Model;
using Superpower.Parsers;
using Serilog.ParserConstruction;
using Serilog.ParserConstruction.Model;
using Serilog.ParserConstruction.Parsers;

namespace Serilog.Expressions.Parsing
{
Expand Down Expand Up @@ -31,6 +31,6 @@ static class ExpressionTextParsers
public static readonly TextParser<TextSpan> Real =
Numerics.Integer
.Then(n => Character.EqualTo('.').IgnoreThen(Numerics.Integer).OptionalOrDefault()
.Select(f => f == TextSpan.None ? n : new TextSpan(n.Source, n.Position, n.Length + f.Length + 1)));
.Select(f => f == TextSpan.None ? n : new TextSpan(n.Source!, n.Position, n.Length + f.Length + 1)));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Superpower.Display;
using Serilog.ParserConstruction.Display;

namespace Serilog.Expressions.Parsing
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Linq;
using Serilog.Events;
using Serilog.Expressions.Ast;
using Superpower;
using Superpower.Model;
using Superpower.Parsers;
using Serilog.ParserConstruction;
using Serilog.ParserConstruction.Model;
using Serilog.ParserConstruction.Parsers;

namespace Serilog.Expressions.Parsing
{
Expand Down Expand Up @@ -65,23 +65,23 @@ public static TokenListParserResult<ExpressionToken, Expression> TryPartialParse

static readonly TokenListParser<ExpressionToken, Func<Expression, Expression>> PropertyPathIndexerStep =
from open in Token.EqualTo(ExpressionToken.LBracket)
from indexer in Wildcard.Or(Parse.Ref(() => Expr))
from indexer in Wildcard.Or(Parse.Ref(() => Expr!))
from close in Token.EqualTo(ExpressionToken.RBracket)
select new Func<Expression, Expression>(r => new IndexerExpression(r, indexer));

static readonly TokenListParser<ExpressionToken, Expression> Function =
(from name in Token.EqualTo(ExpressionToken.Identifier)
from lparen in Token.EqualTo(ExpressionToken.LParen)
from expr in Parse.Ref(() => Expr).ManyDelimitedBy(Token.EqualTo(ExpressionToken.Comma))
from expr in Parse.Ref(() => Expr!).ManyDelimitedBy(Token.EqualTo(ExpressionToken.Comma))
from rparen in Token.EqualTo(ExpressionToken.RParen)
from ci in Token.EqualTo(ExpressionToken.CI).Value(true).OptionalOrDefault()
select (Expression)new CallExpression(ci, name.ToStringValue(), expr)).Named("function");

static readonly TokenListParser<ExpressionToken, Element> ArrayElement =
Token.EqualTo(ExpressionToken.Spread)
.IgnoreThen(Parse.Ref(() => Expr))
.IgnoreThen(Parse.Ref(() => Expr!))
.Select(content => (Element)new SpreadElement(content))
.Or(Parse.Ref(() => Expr).Select(item => (Element) new ItemElement(item)));
.Or(Parse.Ref(() => Expr!).Select(item => (Element) new ItemElement(item)));

static readonly TokenListParser<ExpressionToken, Expression> ArrayLiteral =
(from lbracket in Token.EqualTo(ExpressionToken.LBracket)
Expand All @@ -92,7 +92,7 @@ from rbracket in Token.EqualTo(ExpressionToken.RBracket)
static readonly TokenListParser<ExpressionToken, Member> IdentifierMember =
from key in Token.EqualTo(ExpressionToken.Identifier).Or(Token.EqualTo(ExpressionToken.BuiltInIdentifier))
from value in Token.EqualTo(ExpressionToken.Colon)
.IgnoreThen(Parse.Ref(() => Expr))
.IgnoreThen(Parse.Ref(() => Expr!))
.Cast<ExpressionToken, Expression, Expression?>()
.OptionalOrDefault()
select (Member) new PropertyMember(
Expand All @@ -104,12 +104,12 @@ from value in Token.EqualTo(ExpressionToken.Colon)
static readonly TokenListParser<ExpressionToken, Member> StringMember =
from key in Token.EqualTo(ExpressionToken.String).Apply(ExpressionTextParsers.String)
from colon in Token.EqualTo(ExpressionToken.Colon)
from value in Parse.Ref(() => Expr)
from value in Parse.Ref(() => Expr!)
select (Member)new PropertyMember(key, value);

static readonly TokenListParser<ExpressionToken, Member> SpreadMember =
from spread in Token.EqualTo(ExpressionToken.Spread)
from content in Parse.Ref(() => Expr)
from content in Parse.Ref(() => Expr!)
select (Member) new SpreadMember(content);

static readonly TokenListParser<ExpressionToken, Member> ObjectMember =
Expand Down Expand Up @@ -146,11 +146,11 @@ from rbrace in Token.EqualTo(ExpressionToken.RBrace)

static readonly TokenListParser<ExpressionToken, Expression> Conditional =
from _ in Token.EqualTo(ExpressionToken.If)
from condition in Parse.Ref(() => Expr)
from condition in Parse.Ref(() => Expr!)
from __ in Token.EqualTo(ExpressionToken.Then)
from consequent in Parse.Ref(() => Expr)
from consequent in Parse.Ref(() => Expr!)
from ___ in Token.EqualTo(ExpressionToken.Else)
from alternative in Parse.Ref(() => Expr)
from alternative in Parse.Ref(() => Expr!)
select (Expression)new CallExpression(false, Operators.RuntimeOpIfThenElse, condition, consequent, alternative);

static readonly TokenListParser<ExpressionToken, Expression> Literal =
Expand All @@ -172,7 +172,7 @@ from alternative in Parse.Ref(() => Expr)

static readonly TokenListParser<ExpressionToken, Expression> Factor =
(from lparen in Token.EqualTo(ExpressionToken.LParen)
from expr in Parse.Ref(() => Expr)
from expr in Parse.Ref(() => Expr!)
from rparen in Token.EqualTo(ExpressionToken.RParen)
select expr)
.Or(Item);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Superpower;
using Superpower.Model;
using Serilog.ParserConstruction;
using Serilog.ParserConstruction.Model;

namespace Serilog.Expressions.Parsing
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Superpower;
using Superpower.Model;
using Serilog.ParserConstruction;
using Serilog.ParserConstruction.Model;

namespace Serilog.Expressions.Parsing
{
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Expressions/Expressions/Runtime/Coerce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static bool String(LogEventPropertyValue? value, [MaybeNullWhen(false)] o

if (sv.Value?.GetType().IsEnum ?? false)
{
str = sv.Value.ToString();
str = sv.Value.ToString()!;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,14 @@ public static LogEventPropertyValue _Internal_IsNotNull(LogEventPropertyValue? v

public static LogEventPropertyValue? ToString(LogEventPropertyValue? value, LogEventPropertyValue? format)
{
if (!(value is ScalarValue sv) ||
if (value is not ScalarValue sv ||
sv.Value == null ||
!(Coerce.String(format, out var fmt) || format == null || format is ScalarValue { Value: null }))
!(Coerce.String(format, out var fmt) || format is null or ScalarValue { Value: null }))
{
return null;
}

string toString;
string? toString;
if (sv.Value is IFormattable formattable)
{
// TODO #19: formatting is culture-specific.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -27,9 +28,9 @@ public StaticMemberNameResolver(Type type)
}

/// <inheritdoc />
public override bool TryResolveFunctionName(string name, out MethodInfo implementation)
public override bool TryResolveFunctionName(string name, [MaybeNullWhen(false)] out MethodInfo implementation)
{
return _methods.TryGetValue(name, out implementation);
}
}
}
}
Loading