Skip to content
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

[DO NOT MERGE YET] Added generic parser extensions #58

Draft
wants to merge 46 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
0049cc0
Fixed a minor bug
Unknown6656 Jan 15, 2017
0c42ce8
Another minor fix
Unknown6656 Jan 17, 2017
6f930d2
.
Unknown6656 Sep 18, 2017
e2401fd
Minor changes to improve debugging
Unknown6656 Oct 13, 2017
5df02d2
minor change due to VS warning
Unknown6656 Nov 14, 2017
54dd216
updated project
Unknown6656 Nov 14, 2017
61ff28a
minor refractoring
Unknown6656 Nov 16, 2017
9b27f94
Merge branch 'pr/1'
Unknown6656 Sep 29, 2019
b80a610
updated projects to .NET core 3.0
Unknown6656 Sep 29, 2019
acd3b6e
added wrapper interfaces for better generic support
Unknown6656 Sep 29, 2019
6c3f243
major refactorings
Unknown6656 Oct 13, 2019
91b4c95
minor fix-ups
Unknown6656 Oct 13, 2019
4a3875f
first step of major refactorings _without_ introducing the bugs of la…
Unknown6656 Oct 13, 2019
8985e93
second step of major refactorings _without_ introducing the bugs of l…
Unknown6656 Oct 13, 2019
d089488
more major refactorings
Unknown6656 Oct 13, 2019
0d42448
continued major refactorings
Unknown6656 Oct 14, 2019
6672cb4
began adding nullability annotations
Unknown6656 Oct 14, 2019
07cecf1
Merge branch 'master' into fix-bugs
Unknown6656 Oct 14, 2019
97efdf3
fixed post-merge-conflict conflicts
Unknown6656 Oct 14, 2019
180d253
Merge pull request #2 from Unknown6656/fix-bugs
Unknown6656 Oct 14, 2019
73d0bf3
Added better exception descriptions and possibilities to track all sy…
Unknown6656 Oct 19, 2019
13b590a
Various documentation updates
Unknown6656 Jun 6, 2020
1fa22dd
moved to .NET5
Unknown6656 Jun 6, 2020
de60bee
Merge branch 'master' into dervall-master
Unknown6656 Jun 6, 2020
edc43b3
Merge pull request #3 from Unknown6656/dervall-master
Unknown6656 Jun 6, 2020
a7c5e7b
Moved testing projects to .NET5
Unknown6656 Jun 6, 2020
8459b87
Added case-insensitivity support
Unknown6656 Jun 7, 2020
2799401
Added appveyor config
Unknown6656 Jun 7, 2020
7d1353e
Began updating readme
Unknown6656 Jun 7, 2020
da6ef59
Updated appveyor.yml
Unknown6656 Jun 7, 2020
8fa9f2a
Update appveyor.yml
Unknown6656 Jun 7, 2020
42a6eec
Moved back from .NET5, as it is currently not supported by appveyor CI
Unknown6656 Jun 7, 2020
237ba9a
[UNSTABLE] began moving away from T4-templates
Unknown6656 Jun 7, 2020
4f56ce6
Moved away from T4-templates
Unknown6656 Jun 7, 2020
d95dc0d
Fixed a few generator project bugs
Unknown6656 Jun 7, 2020
b057055
Added debug name to lexed token
Unknown6656 Jun 9, 2020
e955d9a
Added F# wrapper generator
Unknown6656 Jun 10, 2020
65ea885
fixed line-break error on windows in the test project
Unknown6656 Jun 10, 2020
7e8fe6f
Began adding more nullability annotations
Unknown6656 Jun 10, 2020
8d1009c
Continued adding more nullability annotations
Unknown6656 Jun 10, 2020
566002e
Continued adding nullability annotations. Also added doc comments.
Unknown6656 Jun 10, 2020
d4eea51
Continued adding XML doc comments.
Unknown6656 Jun 10, 2020
e194c2f
changed some demo code (only a private test)
Unknown6656 Jun 10, 2020
5dd01d0
Continued adding XML doc comments.
Unknown6656 Jun 11, 2020
ab4b059
Prepared F# for possible type extensions
Unknown6656 Jun 11, 2020
1c57077
Update README.md
Unknown6656 Jun 13, 2020
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ TestResults
*.dotCover
*.nupkg
build

.vs
util/
.vs/
Binary file added .vs/Piglet/v15/sqlite3/storage.ide
Binary file not shown.
1 change: 1 addition & 0 deletions Demo/Debug/CoreCompileInputs.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14c86c5107a4d04c0343e28e8f15d438958d165b
Binary file not shown.
151 changes: 132 additions & 19 deletions Demo/Demo.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,132 @@
using System;
using Piglet.Demo.Lexer;
using Piglet.Demo.Parser;

namespace Piglet.Demo
{
public class Demo
{
public static void Main(string[] args)
{
// Simple demo runner
WordsAndNumbers.Run();
Movement.Run();
JsonParser.Run();
BlogFormatParser.RunFluent();
//Console.ReadKey();
}
}
}
using System;
using Piglet.Parser.Configuration.Generic;
using Piglet.Parser.Configuration;
using Piglet.Parser.Construction;
using Piglet.Demo.Parser;
using Piglet.Demo.Lexer;
using System.Linq;
using Piglet.Parser;

namespace Piglet.Demo
{
using f = Func<int, int>;

public class Demo
{
public static void Main(string[] args)
{
static int Exp(int a, int x) => Enumerable.Range(0, x).Aggregate(1, (acc, i) => acc * a);
var configurator = ParserFactory.Configure<int>();

INonTerminal<int> expr = configurator.CreateNonTerminal();
INonTerminal<int> term = configurator.CreateNonTerminal();
INonTerminal<int> factor = configurator.CreateNonTerminal();
INonTerminal<int> expexpr = configurator.CreateNonTerminal();
ITerminal<int> number = configurator.CreateTerminal("\\d+", t => int.Parse(t, System.Globalization.CultureInfo.InvariantCulture));
ITerminal<int> add = configurator.CreateTerminal("\\+");
ITerminal<int> sub = configurator.CreateTerminal("-");
ITerminal<int> mul = configurator.CreateTerminal("\\*");
ITerminal<int> div = configurator.CreateTerminal("/");
ITerminal<int> pow = configurator.CreateTerminal("[\\^]");

expr.AddProduction(expr, add, term).SetReduceFunction(s => s[0] + s[2]);
expr.AddProduction(expr, sub, term).SetReduceFunction(s => s[0] - s[2]);
expr.AddProduction(term).SetReduceFunction(s => s[0]);

term.AddProduction(term, mul, expexpr).SetReduceFunction(s => s[0] * s[2]);
term.AddProduction(term, div, expexpr).SetReduceFunction(s => s[0] / s[2]);
term.AddProduction(expexpr).SetReduceFunction(s => s[0]);

expexpr.AddProduction(expexpr, pow, factor).SetReduceFunction(s => Exp(s[0], s[2]));
expexpr.AddProduction(factor).SetReduceFunction(s => s[0]);

factor.AddProduction(number).SetReduceFunction(s => s[0]);
factor.AddProduction("(", expr, ")").SetReduceFunction(s => s[1]);

var parser = configurator.CreateParser();
var value = parser.Parse("3^4 + 1");














var s = @"x + -5 - 3 * -x * x";
var par = new test_lexer().CreateParser();
var f = par.Parse(s);

Console.WriteLine(s);
Console.WriteLine(f);

return;

// Simple demo runner
WordsAndNumbers.Run();
Movement.Run();
JsonParser.Run();
BlogFormatParser.RunFluent();
//Console.ReadKey();
}


private class test_lexer
: ParserConstructor<(string, f)>
{
/**/
protected override void Construct(NonTerminalWrapper<(string, f)> nt_func)
{
var nt_expr = CreateNonTerminal<(string s, f f)>();
var nt_unop = CreateNonTerminal<Func<(string s, f f), (string s, f f)>>();
var nt_binop = CreateNonTerminal<Func<(string s, f f), (string s, f f), (string s, f f)>>();

var t_lit = CreateTerminal(@"\d+", x => (i: int.Parse(x), x));
var t_var = CreateTerminal(@"x", x => x);
var t_add = CreateTerminal(@"\+", x => x);
var t_mul = CreateTerminal(@"\*", x => x);
var t_sub = CreateTerminal(@"-", x => x);
var t_div = CreateTerminal(@"/", x => x);
var t_mod = CreateTerminal(@"%", x => x);
var t_oparen = CreateTerminal(@"\(");
var t_cparen = CreateTerminal(@"\)");

SetPrecedenceList(
(AssociativityDirection.Left, new[] { t_add, t_sub }),
(AssociativityDirection.Left, new[] { t_mul, t_div, t_mod })
// (AssociativityDirection.Right, new[] { t_pow })
);

var prec_b = SetAssociativity(AssociativityDirection.Left);
var prec_u = SetAssociativity(AssociativityDirection.Right);

CreateProduction(nt_func, nt_expr);
CreateProduction(nt_unop, t_add, _ => f1 => ($"(+{f1.s})", f1.f));
CreateProduction(nt_unop, t_sub, _ => f1 => ($"(-{f1.s})", x => f1.f(x)));
CreateProduction(nt_binop, t_mod, _ => (f1, f2) => ($"({f1.s} % {f2.s})", x => f1.f(x) % f2.f(x)));
CreateProduction(nt_binop, t_div, _ => (f1, f2) => ($"({f1.s} / {f2.s})", x => f1.f(x) / f2.f(x)));
CreateProduction(nt_binop, t_mul, _ => (f1, f2) => ($"({f1.s} * {f2.s})", x => f1.f(x) * f2.f(x)));
CreateProduction(nt_binop, t_sub, _ => (f1, f2) => ($"({f1.s} - {f2.s})", x => f1.f(x) - f2.f(x)));
CreateProduction(nt_binop, t_add, _ => (f1, f2) => ($"({f1.s} + {f2.s})", x => f1.f(x) + f2.f(x)));
CreateProduction(nt_expr, t_var, l => ("X", x => x));
CreateProduction(nt_expr, t_lit, l => (l.x, _ => l.i));
CreateProduction(nt_expr, t_oparen, nt_expr, t_cparen, (x, y, z) => ($"({y.s})", u => y.f(u)));
CreateProduction(nt_expr, nt_unop, nt_expr, (x, y) => x(y)).SetPrecedence(prec_u);
CreateProduction(nt_expr, nt_expr, nt_binop, nt_expr, (x, y, z) => y(x, z)).SetPrecedence(prec_b);

Configurator.LexerSettings.Ignore = new[]
{
@"\s+",
@"/\*[^(\*/)]*\*/",
@"//[^\n]*\n"
};
}
}
}
}
5 changes: 3 additions & 2 deletions Demo/Lexer/Movement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Piglet.Lexer;
using Piglet.Lexer.Runtime;

namespace Piglet.Demo.Lexer
{
Expand Down Expand Up @@ -37,9 +38,9 @@ public static void Run()
configurator.Ignore(@"\s+");
});

foreach (var token in lexer.Tokenize("up down left right right north west left north up"))
foreach ((int number, LexedToken<string> token) in lexer.Tokenize("up down left right right north west left north up"))
{
Console.WriteLine("{0} Current position is {1},{2}", token.Item2, positionX, positionY);
Console.WriteLine("{0} Current position is {1},{2}", token.SymbolValue, positionX, positionY);
}

Console.WriteLine(System.DateTime.Now.Ticks - ticks);
Expand Down
15 changes: 4 additions & 11 deletions Demo/Lexer/WordsAndNumbers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Piglet.Lexer;
using Piglet.Lexer.Runtime;

namespace Piglet.Demo.Lexer
{
Expand All @@ -22,17 +23,9 @@ public static void Run()

// Run the lexer
string input = "10 piglets 5 boars 1 big sow";
foreach (var token in lexer.Tokenize(input))
{
if (token.Item2 is int)
{
Console.WriteLine("Lexer found an integer {0}", token.Item2);
}
else
{
Console.WriteLine("Lexer found a string {0}", token.Item2);
}
}

foreach ((int number, LexedToken<object> token) in lexer.Tokenize(input))
Console.WriteLine($"Lexer found {(token.SymbolValue is int ? "an integer" : "a string")} {token.SymbolValue}");
}
}
}
79 changes: 14 additions & 65 deletions Demo/Piglet.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,67 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Piglet.Demo</RootNamespace>
<AssemblyName>Piglet.Demo</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Piglet.Demo.Demo</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Demo.cs" />
<Compile Include="Lexer\Movement.cs" />
<Compile Include="Lexer\WordsAndNumbers.cs" />
<Compile Include="Parser\BlogFormatParser.cs" />
<Compile Include="Parser\JsonParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Piglet\Piglet.csproj">
<Project>{1E23F251-94E1-4504-81E8-2618F5C9FEA3}</Project>
<Name>Piglet</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<OutputType>Exe</OutputType>
<RootNamespace>Piglet.Demo</RootNamespace>
<AssemblyName>Piglet.Demo</AssemblyName>
<OutputPath>bin\Debug\</OutputPath>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Piglet\Piglet.csproj"/>
</ItemGroup>
</Project>
25 changes: 0 additions & 25 deletions Demo/Properties/AssemblyInfo.cs

This file was deleted.

3 changes: 3 additions & 0 deletions Demo/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/></startup></configuration>
Loading