Permalink
Browse files

Some refactoring

  • Loading branch information...
1 parent 9719f7e commit 2949d6410c18d8d5123e18f3e20724c178afaf40 @Dervall committed Mar 12, 2012
View
1 .gitignore
@@ -7,3 +7,4 @@ obj
TestResults
*.vsmdi
*.dotCover
+*.nupkg
View
22 Piglet.sln
@@ -19,21 +19,43 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
+ Debug|Mixed Platforms = Debug|Mixed Platforms
+ Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
+ Release|Mixed Platforms = Release|Mixed Platforms
+ Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Debug|x86.ActiveCfg = Debug|Any CPU
{1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Release|Any CPU.Build.0 = Release|Any CPU
+ {1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {1E23F251-94E1-4504-81E8-2618F5C9FEA3}.Release|x86.ActiveCfg = Release|Any CPU
{911347A0-30E7-497F-99B9-59DBEEF90A83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{911347A0-30E7-497F-99B9-59DBEEF90A83}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {911347A0-30E7-497F-99B9-59DBEEF90A83}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {911347A0-30E7-497F-99B9-59DBEEF90A83}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {911347A0-30E7-497F-99B9-59DBEEF90A83}.Debug|x86.ActiveCfg = Debug|Any CPU
{911347A0-30E7-497F-99B9-59DBEEF90A83}.Release|Any CPU.ActiveCfg = Release|Any CPU
{911347A0-30E7-497F-99B9-59DBEEF90A83}.Release|Any CPU.Build.0 = Release|Any CPU
+ {911347A0-30E7-497F-99B9-59DBEEF90A83}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {911347A0-30E7-497F-99B9-59DBEEF90A83}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {911347A0-30E7-497F-99B9-59DBEEF90A83}.Release|x86.ActiveCfg = Release|Any CPU
{B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Debug|x86.ActiveCfg = Debug|Any CPU
{B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Release|Any CPU.Build.0 = Release|Any CPU
+ {B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {B23B66AD-EA0F-4254-BDE6-C4EF883BD5B0}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
2 Piglet/Common/ITable2D.cs
@@ -1,6 +1,6 @@
namespace Piglet.Common
{
- internal interface ITable2D
+ public interface ITable2D
{
int this[int state, int input] { get; }
}
View
1 Piglet/Lexer/Construction/RegExLexer.cs
@@ -149,6 +149,7 @@ public RegExToken NextToken()
case '\\':
case '{':
case '}':
+ case ' ':
return new RegExToken { Type = RegExToken.TokenType.Accept, Characters = new[] { c } };
default:
View
5 Piglet/Parser/Construction/IParseTable.cs
@@ -2,10 +2,11 @@
namespace Piglet.Parser.Construction
{
- internal interface IParseTable<T>
+ public interface IParseTable<T>
{
ITable2D Action { get; }
ITable2D Goto { get; }
- ReductionRule<T>[] ReductionRules { get; set; }
+ IReductionRule<T>[] ReductionRules { get; set; }
+ int StateCount { get; }
}
}
View
11 Piglet/Parser/Construction/IReductionRule.cs
@@ -0,0 +1,11 @@
+using System;
+
+namespace Piglet.Parser.Construction
+{
+ public interface IReductionRule<T>
+ {
+ int NumTokensToPop { get; }
+ int TokenToPush { get; }
+ Func<ParseException, T[], T> OnReduce { get; }
+ }
+}
View
4 Piglet/Parser/Construction/LRParseTable.cs
@@ -7,7 +7,9 @@ internal class LRParseTable<T> : IParseTable<T>
{
public ITable2D Action { get; internal set; }
public ITable2D Goto { get; internal set; }
- public ReductionRule<T>[] ReductionRules { get; set; }
+ public IReductionRule<T>[] ReductionRules { get; set; }
+
+ public int StateCount { get; set; }
public static short Shift(int stateToChangeTo)
{
View
1 Piglet/Parser/Construction/ParserBuilder.cs
@@ -275,6 +275,7 @@ private LRParseTable<T> CreateParseTable(List<Lr1ItemSet<T>> itemSets, List<Goto
table.ReductionRules = reductionRules.Select(f => f.Item2).ToArray();
table.Action = new CompressedTable(uncompressedActionTable);
table.Goto = new GotoTable(gotos);
+ table.StateCount = itemSets.Count;
// Useful point to look at the table, and everything the builder has generated, since after this point the grammar is pretty much destroyed.
//string gotoGraph = gotoSetTransitions.AsDotNotation(itemSets);
View
2 Piglet/Parser/Construction/ReductionRule.cs
@@ -2,7 +2,7 @@
namespace Piglet.Parser.Construction
{
- internal class ReductionRule<T>
+ internal class ReductionRule<T> : IReductionRule<T>
{
public int NumTokensToPop { get; set; }
public int TokenToPush { get; set; }
View
5 Piglet/Parser/IParser.cs
@@ -1,5 +1,6 @@
using System.IO;
using Piglet.Lexer;
+using Piglet.Parser.Construction;
namespace Piglet.Parser
{
@@ -13,7 +14,9 @@ public interface IParser<T>
/// Gets or sets the lexer associated with the parser.
/// </summary>
ILexer<T> Lexer { get; set; }
-
+
+ IParseTable<T> ParseTable { get; }
+
/// <summary>
/// Parse an input string, returning the resulting semantic value type that is left on the parse
/// stack.
View
7 Piglet/Parser/LRParser.cs
@@ -29,10 +29,7 @@ internal LRParser(IParseTable<T> parseTable, int errorTokenNumber, int endOfInpu
this.terminalDebugNames = terminalDebugNames;
}
- /// <summary>
- /// This is accessible for test and debug reasons
- /// </summary>
- internal IParseTable<T> Table { get { return parseTable; } }
+ public IParseTable<T> ParseTable { get { return parseTable; } }
public ILexer<T> Lexer { get; set; }
@@ -128,7 +125,7 @@ private T Parse()
else
{
// Get the right reduction rule to apply
- ReductionRule<T> reductionRule = parseTable.ReductionRules[-(action + 1)];
+ var reductionRule = parseTable.ReductionRules[-(action + 1)];
for (int i = 0; i < reductionRule.NumTokensToPop*2; ++i)
{
parseStack.Pop();
View
1 Piglet/Piglet.csproj
@@ -81,6 +81,7 @@
<Compile Include="Parser\Configuration\NonTerminal.cs" />
<Compile Include="Parser\Configuration\ParserConfigurationException.cs" />
<Compile Include="Parser\Configuration\ParserConfigurator.cs" />
+ <Compile Include="Parser\Construction\IReductionRule.cs" />
<Compile Include="Parser\Construction\Lr1Item.cs" />
<Compile Include="Parser\Construction\Lr1ItemSet.cs" />
<Compile Include="Parser\Configuration\Fluent\IFluentParserConfigurator.cs" />
View
4 Tests/Parser/Construction/Debug/TestDebugStrings.cs
@@ -30,9 +30,9 @@ public void TestParseTableToString()
c.AddProduction("c");
var grammar = (IGrammar<int>)configurator;
- var parser = (LRParser<int>)configurator.CreateParser();
+ var parser = configurator.CreateParser();
- string debugString = parser.Table.ToDebugString(grammar, 3);
+ string debugString = parser.ParseTable.ToDebugString(grammar, 3);
Assert.IsNotNull(debugString);
}

0 comments on commit 2949d64

Please sign in to comment.