-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from datalust/dev
1.1.0 Release
- Loading branch information
Showing
23 changed files
with
370 additions
and
246 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Superpower\Superpower.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Superpower; | ||
using Superpower.Model; | ||
using Superpower.Parsers; | ||
|
||
namespace DateTimeTextParser | ||
{ | ||
static public class DateTimeTextParser | ||
{ | ||
static TextParser<char[]> Repeat(this TextParser<char> parser, int count) | ||
{ | ||
return input => | ||
{ | ||
List<char> result = new List<char>(); | ||
|
||
Result<char> next = input.ConsumeChar(); | ||
var beginning = next.Location; | ||
|
||
for (int i = 0; i < count; i++) | ||
{ | ||
var parserResult = parser.Invoke(next.Location); | ||
if (parserResult.HasValue) | ||
{ | ||
result.Add(parserResult.Value); | ||
next = next.Remainder.ConsumeChar(); | ||
} | ||
else | ||
return Result.Empty<char[]>(input); | ||
} | ||
|
||
return Result.Value(result.ToArray(), beginning, next.Location); | ||
}; | ||
} | ||
|
||
|
||
static TextParser<string> TwoDigits = | ||
Character.Digit.Repeat(2).Select(chs => new String(chs)); | ||
|
||
static TextParser<string> YearOfDate = | ||
Character.Digit.Repeat(4).Select(chs => new String(chs)); | ||
|
||
static TextParser<string> MonthOfDate = | ||
TwoDigits; | ||
|
||
static TextParser<string> DayOfDate = | ||
TwoDigits; | ||
|
||
static TextParser<DateTime> Date = | ||
from year in YearOfDate.Select(Int32.Parse) | ||
from sep1 in Character.EqualTo('-') | ||
from mon in MonthOfDate.Select(Int32.Parse) | ||
from sep2 in Character.EqualTo('-') | ||
from day in DayOfDate.Select(Int32.Parse) | ||
select new DateTime(year, mon, day); | ||
|
||
static TextParser<int> secondWithSep = | ||
from sep in Character.EqualTo(':') | ||
from second in TwoDigits.Select(Int32.Parse) | ||
select second; | ||
|
||
static TextParser<TimeSpan> Time = | ||
from hour in TwoDigits.Select(Int32.Parse) | ||
from sep1 in Character.EqualTo(':') | ||
from minute in TwoDigits.Select(Int32.Parse) | ||
from second in secondWithSep.OptionalOrDefault() | ||
select new TimeSpan(hour, minute, second); | ||
|
||
public static TextParser<DateTime> DateTime = | ||
from q1 in Character.EqualTo('"').Optional() | ||
from date in (from date in Date | ||
from s in Character.In('T', ' ') | ||
from time in Time | ||
select date + time).Try() | ||
.Or(from time in Time | ||
select System.DateTime.Now.Date + time).Try() | ||
.Or(Date) | ||
from q2 in Character.EqualTo('"').Optional().AtEnd() | ||
where (q1 == null && q2 == null) || (q1 != null && q2 != null) | ||
select date; | ||
} | ||
} |
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,38 @@ | ||
using System; | ||
using Superpower; | ||
|
||
namespace DateTimeTextParser | ||
{ | ||
class Program | ||
{ | ||
static void ParseAndPrint(string input) | ||
{ | ||
try | ||
{ | ||
var dt = DateTimeTextParser.DateTime.Parse(input); | ||
Console.WriteLine("Input: '{0}', ParsedValue: '{1}'", input, dt.ToString("o")); | ||
} | ||
catch (System.Exception ex) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Red; | ||
Console.WriteLine("Input: '{0}'", input); | ||
Console.WriteLine(ex.ToString()); | ||
Console.ForegroundColor = ConsoleColor.White; | ||
} | ||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
ParseAndPrint("12:38"); | ||
ParseAndPrint("12:38:10"); | ||
ParseAndPrint("2017-01-01"); | ||
ParseAndPrint("2017-01-01 05:28:10"); | ||
ParseAndPrint("2017-01-01 05:28"); | ||
ParseAndPrint("\"2017-01-01\""); | ||
ParseAndPrint("\"2017-01-01"); | ||
ParseAndPrint("2017-01-01 05:x8:10"); | ||
ParseAndPrint("2017-01-01T05:28:10"); | ||
ParseAndPrint("2017-01-01T05:28"); | ||
} | ||
} | ||
} |
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,58 @@ | ||
# Superpower sample / `DateTimeTextParser` | ||
|
||
This example should shows how to build a simple text parser with Superpower. | ||
It uses a simple and well known requirement: parsing date and time values | ||
according to ISO-8601 format. | ||
|
||
## The requirement | ||
|
||
In a simple, custom query language a user enters multiple conditions where a | ||
property is compared to a static value. The property identifier the | ||
comparison operator and the static value had to be separated with spaces. | ||
There are text properties and datetime properties to query. | ||
|
||
The static text values had to be entered enclosed with `'"'` (double qoutes), | ||
datetime static values can be entered as date, time or date and time. For | ||
better usability not only text can be enclosed with `'"'`, also the date time | ||
static values can be surrounded with double qoutes. When only time is entered | ||
it should be considered as todays time. When only date is entered midnight is | ||
the default time value. | ||
|
||
For example: | ||
- `2017-01-01 12:10` | ||
- `"2017-01-01 12:10"` | ||
- `12:10` | ||
- `2017-01-01` | ||
|
||
## The code | ||
|
||
The `DateTimeTextParser` class contains the `DateTime` Superpower `TextParser`, which | ||
covers all the requirements to parse a static datetime value. | ||
|
||
```cs | ||
public static TextParser<DateTime> DateTime = | ||
from q1 in Character.EqualTo('"').Optional() | ||
// ^- First we looking for a optional double quote | ||
from date in (from date in Date | ||
from s in Character.In('T', ' ') | ||
from time in Time | ||
select date + time).Try() | ||
// ^- here we're looking for date and time values | ||
.Or(from time in Time | ||
select System.DateTime.Now.Date + time).Try() | ||
// ^- when the first parser failed check for time only | ||
// and add it to the current date | ||
.Or(Date) | ||
// ^- else it must be a date value | ||
from q2 in Character.EqualTo('"').Optional().AtEnd() | ||
// ^- then we check for a optional double quote at the end of the input | ||
where (q1 == null && q2 == null) || (q1 != null && q2 != null) | ||
// ^- now we're checking if both quote are either set or not set | ||
select date; | ||
``` |
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net46;netcoreapp1.0</TargetFrameworks> | ||
<AssemblyName>IntCalc</AssemblyName> | ||
<OutputType>Exe</OutputType> | ||
<PackageId>IntCalc</PackageId> | ||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">$(PackageTargetFallback);dnxcore50;portable-net45+win8</PackageTargetFallback> | ||
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">1.0.4</RuntimeFrameworkVersion> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Superpower\Superpower.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' "> | ||
<Reference Include="System" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.