Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Superpower v2 #23

Merged
merged 4 commits into from
Jun 25, 2018
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.user
*.userosscache
*.sln.docstates
.idea

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Serilog.Events;
using Serilog.Filters.Expressions.Ast;
using Serilog.Filters.Expressions.Compilation;
using System;
using System.Linq;
Expand All @@ -19,9 +18,7 @@ public static class FilterLanguage
/// <returns>A function that evaluates the expression in the context of the log event.</returns>
public static Func<LogEvent, object> CreateFilter(string expression)
{
Func<LogEvent, object> filter;
string error;
if (!TryCreateFilter(expression, out filter, out error))
if (!TryCreateFilter(expression, out var filter, out var error))
throw new ArgumentException(error);

return filter;
Expand All @@ -36,8 +33,7 @@ public static Func<LogEvent, object> CreateFilter(string expression)
/// <returns>True if the filter could be created; otherwise, false.</returns>
public static bool TryCreateFilter(string expression, out Func<LogEvent, object> filter, out string error)
{
FilterExpression root;
if (!FilterExpressionParser.TryParse(expression, out root, out error))
if (!FilterExpressionParser.TryParse(expression, out var root, out error))
{
filter = null;
return false;
Expand All @@ -54,6 +50,7 @@ public static bool TryCreateFilter(string expression, out Func<LogEvent, object>
/// <param name="text">The text to escape.</param>
/// <returns>The text with any special values escaped. Will need to be passed through
/// <see cref="EscapeStringContent(string)"/> if it is being embedded directly into a filter expression.</returns>
// ReSharper disable once UnusedMember.Global
public static string EscapeLikeExpressionContent(string text)
{
if (text == null) throw new ArgumentNullException(nameof(text));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using Serilog.Filters.Expressions.Ast;
using Superpower;

namespace Serilog.Filters.Expressions.Parsing
{
static class FilterExpressionParser
{
public static FilterExpression Parse(string filterExpression)
{
FilterExpression root;
string error;
if (!TryParse(filterExpression, out root, out error))
if (!TryParse(filterExpression, out var root, out var error))
throw new ArgumentException(error);

return root;
Expand All @@ -20,17 +17,15 @@ public static bool TryParse(string filterExpression, out FilterExpression root,
{
if (filterExpression == null) throw new ArgumentNullException(nameof(filterExpression));

var tokenizer = new FilterExpressionTokenizer();
var tokenList = tokenizer.TryTokenize(filterExpression);

var tokenList = FilterExpressionTokenizer.Instance.TryTokenize(filterExpression);
if (!tokenList.HasValue)
{
error = tokenList.ToString();
root = null;
return false;
}

var result = FilterExpressionTokenParsers.Expr.AtEnd().TryParse(tokenList.Value);
var result = FilterExpressionTokenParsers.TryParse(tokenList.Value);
if (!result.HasValue)
{
error = result.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
using System;
using System.Globalization;
using System.Linq;
using Superpower.Model;

namespace Serilog.Filters.Expressions.Parsing
{
static class FilterExpressionTokenParsers
{
public static TokenListParserResult<FilterExpressionToken, FilterExpression> TryParse(
TokenList<FilterExpressionToken> input)
{
return Expr.AtEnd().TryParse(input);
}

static readonly TokenListParser<FilterExpressionToken, string> Add = Token.EqualTo(FilterExpressionToken.Plus).Value(Operators.OpAdd);
static readonly TokenListParser<FilterExpressionToken, string> Subtract = Token.EqualTo(FilterExpressionToken.Minus).Value(Operators.OpSubtract);
static readonly TokenListParser<FilterExpressionToken, string> Multiply = Token.EqualTo(FilterExpressionToken.Asterisk).Value(Operators.OpMultiply);
Expand Down Expand Up @@ -130,7 +137,7 @@ from factor in Factor

static readonly TokenListParser<FilterExpressionToken, FilterExpression> Disjunction = Parse.Chain(Or, Conjunction, MakeBinary);

public static readonly TokenListParser<FilterExpressionToken, FilterExpression> Expr = Disjunction;
static readonly TokenListParser<FilterExpressionToken, FilterExpression> Expr = Disjunction;

static FilterExpression MakeBinary(string operatorName, FilterExpression leftOperand, FilterExpression rightOperand)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FilterExpressionTokenizer : Tokenizer<FilterExpressionToken>
FilterExpressionToken.Is
};

static readonly FilterExpressionKeyword[] SqlKeywords =
static readonly FilterExpressionKeyword[] Keywords =
{
new FilterExpressionKeyword("and", FilterExpressionToken.And),
new FilterExpressionKeyword("in", FilterExpressionToken.In),
Expand Down Expand Up @@ -61,7 +61,9 @@ static FilterExpressionTokenizer()
SimpleOps['?'] = FilterExpressionToken.QuestionMark;
}

protected override IEnumerable<Result<FilterExpressionToken>> Tokenize(TextSpan stringSpan)
protected override IEnumerable<Result<FilterExpressionToken>> Tokenize(
TextSpan stringSpan,
TokenizationState<FilterExpressionToken> tokenizationState)
{
var next = SkipWhiteSpace(stringSpan);
if (!next.HasValue)
Expand All @@ -88,7 +90,7 @@ protected override IEnumerable<Result<FilterExpressionToken>> Tokenize(TextSpan
next = real.Remainder.ConsumeChar();
}

if (next.HasValue && !char.IsPunctuation(next.Value) && !char.IsWhiteSpace(next.Value))
if (!IsDelimiter(next))
{
yield return Result.Empty<FilterExpressionToken>(next.Location, new[] { "digit" });
}
Expand Down Expand Up @@ -141,7 +143,9 @@ protected override IEnumerable<Result<FilterExpressionToken>> Tokenize(TextSpan
yield return Result.Value(FilterExpressionToken.Identifier, beginIdentifier, next.Location);
}
}
else if (next.Value == '/' && (!Previous.HasValue || PreRegexTokens.Contains(Previous.Kind)))
else if (next.Value == '/' &&
(!tokenizationState.Previous.HasValue ||
PreRegexTokens.Contains(tokenizationState.Previous.Value.Kind)))
{
var regex = FilterExpressionTextParsers.RegularExpression(next.Location);
if (!regex.HasValue)
Expand Down Expand Up @@ -174,9 +178,16 @@ protected override IEnumerable<Result<FilterExpressionToken>> Tokenize(TextSpan
} while (next.HasValue);
}

static bool IsDelimiter(Result<char> next)
{
return !next.HasValue ||
char.IsWhiteSpace(next.Value) ||
next.Value < SimpleOps.Length && SimpleOps[next.Value] != FilterExpressionToken.None;
}

static bool TryGetKeyword(TextSpan span, out FilterExpressionToken keyword)
{
foreach (var kw in SqlKeywords)
foreach (var kw in Keywords)
{
if (span.EqualsValueIgnoreCase(kw.Text))
{
Expand All @@ -188,5 +199,7 @@ static bool TryGetKeyword(TextSpan span, out FilterExpressionToken keyword)
keyword = FilterExpressionToken.None;
return false;
}

public static FilterExpressionTokenizer Instance { get; } = new FilterExpressionTokenizer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<Description>Expression-based event filtering for Serilog.</Description>
<VersionPrefix>1.1.1</VersionPrefix>
<VersionPrefix>2.0.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard1.5</TargetFrameworks>
<TargetFrameworks>net4.5;netstandard1.5</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Filters.Expressions</AssemblyName>
Expand All @@ -14,18 +14,19 @@
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageId>Serilog.Filters.Expressions</PackageId>
<PackageTags>serilog</PackageTags>
<PackageIconUrl>http://serilog.net/images/serilog-extension-nuget.png</PackageIconUrl>
<PackageIconUrl>https://serilog.net/images/serilog-extension-nuget.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/serilog/serilog-filters-expressions</PackageProjectUrl>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.5' ">1.6.0</NetStandardImplicitPackageVersion>
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' == 'net4.5' ">True</DisableImplicitFrameworkReferences>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.5' ">1.6.1</NetStandardImplicitPackageVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.3.0" />
<PackageReference Include="Superpower" Version="1.0.1" />
<PackageReference Include="Serilog" Version="2.7.1" />
<PackageReference Include="Superpower" Version="2.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net4.5' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net462</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net4.6.2</TargetFrameworks>
<AssemblyName>Serilog.Filters.Expressions.PerformanceTests</AssemblyName>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand All @@ -18,7 +18,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.10.9" />
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class FilterExpressionParserTests
[InlineData("@EventType = 0xC0ffee", "Equal(@EventType,12648430)")]
[InlineData("@Level in ['Error', 'Warning']", "_Internal_In(@Level,[@\"Error\",@\"Warning\"])")]
[InlineData("5 not in [1, 2]", "_Internal_NotIn(5,[1,2])")]
[InlineData("1+1", "Add(1,1)")]
public void ValidSyntaxIsAccepted(string input, string expected = null)
{
var roundTrip = FilterExpressionParser.Parse(input).ToString();
Expand All @@ -59,5 +60,16 @@ public void InvalidSyntaxIsRejected(string input)
{
Assert.Throws<ArgumentException>(() => FilterExpressionParser.Parse(input));
}

[Theory]
[InlineData("A = 'b", "Syntax error: unexpected end of input, expected `'`.")]
[InlineData("A or B) and C", "Syntax error (line 1, column 7): unexpected `)`.")]
[InlineData("A lik3 C", "Syntax error (line 1, column 3): unexpected identifier `lik3`.")]
[InlineData("A > 1234f", "Syntax error (line 1, column 9): unexpected `f`, expected digit.")]
public void PreciseErrorsAreReported(string input, string expectedMessage)
{
var ex = Assert.Throws<ArgumentException>(() => FilterExpressionParser.Parse(input));
Assert.Equal(expectedMessage, ex.Message);
}
}
}
2 changes: 1 addition & 1 deletion test/Serilog.Filters.Expressions.Tests/PackagingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class PackagingTests
public void AssemblyVersionIsSet()
{
var version = typeof(LoggerFilterConfigurationExtensions).GetTypeInfo().Assembly.GetName().Version;
Assert.Equal("1", version.ToString(1));
Assert.Equal("2", version.ToString(1));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp1.0;net452</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net4.6.2</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AssemblyName>Serilog.Filters.Expressions.Tests</AssemblyName>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
Expand All @@ -10,28 +9,19 @@
<PackageId>Serilog.Filters.Expressions.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<None Include="App.config" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Filters.Expressions\Serilog.Filters.Expressions.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
</Project>
16 changes: 0 additions & 16 deletions test/Serilog.Filters.Expressions.Tests/app.config

This file was deleted.