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

Commit

Permalink
Finish parser
Browse files Browse the repository at this point in the history
Rate limit · GitHub

Whoa there!

You have triggered an abuse detection mechanism.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

sunsided committed Jan 24, 2015
2 parents 70cbb8c + 2158764 commit a7d8dda
Showing 6 changed files with 198 additions and 36 deletions.
45 changes: 10 additions & 35 deletions PDDL.Tests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using PDDL.Model.Pddl12;
using PDDL.Parser;
using PDDL.Parser.Pddl12;
using Sprache;

@@ -34,43 +37,15 @@ private static void Sprache()
string problemFileName = resources.FirstOrDefault(fileName => fileName.Contains("DWR-pb1.pddl"));
string problemDefinition = LoadNamedResourceString(assembly, problemFileName);

domainDefinition = RemoveAllComments(domainDefinition);
var domain = DefineGrammar.DefineDefinition.Parse(domainDefinition);
var parser = new PDDL12Parser();

problemDefinition = RemoveAllComments(problemDefinition);
var problem = DefineGrammar.DefineDefinition.Parse(problemDefinition);
}

/// <summary>
/// Removes all comments.
/// </summary>
/// <param name="source">The source.</param>
/// <returns>System.String.</returns>
private static string RemoveAllComments(string source)
{
var sb = new StringBuilder();
using (var reader = new StringReader(source))
{
string line;
while (null != (line = reader.ReadLine()))
{
// check for semicolons and, if none found, simply register the line
var index = line.IndexOf(';');
if (index < 0)
{
sb.AppendLine(line);
continue;
}
var combined = domainDefinition + Environment.NewLine + problemDefinition;
var definitions = parser.Parse(combined);

// if a semicolon was found, read until there and replace it with a single whitespace
// (as per language description)
sb.Append(line.Substring(0, index));
sb.AppendLine(" ");
}
}
return sb.ToString();
var domain = definitions.Single(item => item is IDomain);
var problem = definitions.Single(item => item is IProblem);
}

/// <summary>
/// Loads the named resource string.
/// </summary>
3 changes: 3 additions & 0 deletions PDDL/PDDL.csproj
Original file line number Diff line number Diff line change
@@ -122,6 +122,7 @@
<Compile Include="Model\Pddl12\Types\CustomType.cs" />
<Compile Include="Model\Pddl12\Types\DefaultType.cs" />
<Compile Include="Model\Pddl12\Constant.cs" />
<Compile Include="Parser\PDDL12Parser.cs" />
<Compile Include="Parser\Pddl12\AxiomGrammar.cs" />
<Compile Include="Parser\Pddl12\DefineGrammar.cs" />
<Compile Include="Parser\Pddl12\DomainVariableGrammar.cs" />
@@ -138,6 +139,8 @@
<Compile Include="Parser\Pddl12\TypedLists.cs" />
<Compile Include="Parser\Pddl12\VarsGrammar.cs" />
<Compile Include="Parser\Pddl12\SpracheExtensions.cs" />
<Compile Include="Parser\PDDLSyntaxException.cs" />
<Compile Include="Parser\PDDLParserException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
82 changes: 82 additions & 0 deletions PDDL/Parser/PDDL12Parser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using PDDL.Model.Pddl12;
using PDDL.Parser.Pddl12;
using Sprache;

namespace PDDL.Parser
{
/// <summary>
/// Class PDDL12Parser. This class cannot be inherited.
/// </summary>
public sealed class PDDL12Parser
{
/// <summary>
/// Parses the specified definition.
/// </summary>
/// <param name="definition">The definition.</param>
/// <returns>IReadOnlyList&lt;IDefinition&gt;.</returns>
/// <exception cref="PDDLSyntaxException">A syntax error or internal parser error occurred. </exception>
public IReadOnlyList<IDefinition> Parse(string definition)
{
// strip all comments
definition = RemoveAllComments(definition);

// run the actual parsers
try
{
var enumeration = DefineGrammar.MultiDefineDefinition.Parse(definition);
return enumeration.ToList();
}
catch (ParseException e)
{
throw new PDDLSyntaxException(e.Message, e);
}
}

/// <summary>
/// Removes all comments.
/// </summary>
/// <param name="source">The source.</param>
/// <returns>System.String.</returns>
private static string RemoveAllComments(string source)
{
using (var reader = new StringReader(source))
{
return RemoveAllComments(reader);
}
}

/// <summary>
/// Removes all comments.
/// </summary>
/// <param name="reader">The reader.</param>
/// <returns>System.String.</returns>
private static string RemoveAllComments([NotNull] StringReader reader)
{
var sb = new StringBuilder();

string line;
while (null != (line = reader.ReadLine()))
{
// check for semicolons and, if none found, simply register the line
var index = line.IndexOf(';');
if (index < 0)
{
sb.AppendLine(line);
continue;
}

// if a semicolon was found, read until there and replace it with a single whitespace
// (as per language description)
sb.Append(line.Substring(0, index));
sb.AppendLine(" ");
}

return sb.ToString();
}
}
}
47 changes: 47 additions & 0 deletions PDDL/Parser/PDDLParserException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Runtime.Serialization;

namespace PDDL.Parser
{
/// <summary>
/// Class PDDLParserException.
/// </summary>
[Serializable]
public class PDDLParserException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="PDDLParserException"/> class.
/// </summary>
public PDDLParserException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PDDLParserException" /> class with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public PDDLParserException(string message) : base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PDDLParserException"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="inner">The inner.</param>
public PDDLParserException(string message, Exception inner) : base(message, inner)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PDDLParserException" /> class with serialized data.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
protected PDDLParserException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
}
47 changes: 47 additions & 0 deletions PDDL/Parser/PDDLSyntaxException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Runtime.Serialization;

namespace PDDL.Parser
{
/// <summary>
/// Class PDDLSyntaxException.
/// </summary>
[Serializable]
public class PDDLSyntaxException : PDDLParserException
{
/// <summary>
/// Initializes a new instance of the <see cref="PDDLSyntaxException"/> class.
/// </summary>
public PDDLSyntaxException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PDDLSyntaxException" /> class with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public PDDLSyntaxException(string message) : base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PDDLSyntaxException"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="inner">The inner.</param>
public PDDLSyntaxException(string message, Exception inner) : base(message, inner)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PDDLSyntaxException" /> class with serialized data.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
protected PDDLSyntaxException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
}
10 changes: 9 additions & 1 deletion PDDL/Parser/Pddl12/DefineGrammar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System.Collections.Generic;
using JetBrains.Annotations;
using PDDL.Model.Pddl12;
using Sprache;

@@ -16,6 +17,13 @@ internal static class DefineGrammar
public static readonly Parser<IDefinition> DefineDefinition
= CreateDefineDefinition();

/// <summary>
/// Multiple defines
/// </summary>
[NotNull] public static readonly Parser<IEnumerable<IDefinition>> MultiDefineDefinition
= DefineDefinition.AtLeastOnce();


#region Factory Functions

/// <summary>

0 comments on commit a7d8dda

Please sign in to comment.