Merge pull request #7 from mrvux/errorhandling

[Parser error handling] Exception with text location
This commit is contained in:
David Molnar 2015-11-26 02:26:26 -08:00
Родитель 9cc618dedb 057d974a4d
Коммит 6f558109d3
3 изменённых файлов: 55 добавлений и 3 удалений

Просмотреть файл

@ -0,0 +1,48 @@
using Antlr4.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PreposeGestures
{
public class PreposeParserException : Exception
{
private readonly int startLineNumber;
private readonly int startColumnNumber;
private readonly int endLineNumber;
private readonly int endColumnNumber;
public int StartLineNumber
{
get { return this.startLineNumber; }
}
public int StartColumnNumber
{
get { return this.startColumnNumber; }
}
public int EndLineNumber
{
get { return this.endLineNumber; }
}
public int EndColumnNumber
{
get { return this.endColumnNumber; }
}
public PreposeParserException(string message, ParserRuleContext currentContext)
: base(message)
{
this.startLineNumber = currentContext.start.Line;
this.startColumnNumber = currentContext.start.Column;
this.endLineNumber = currentContext.stop.Line;
this.endColumnNumber = currentContext.stop.Column;
}
}
}

Просмотреть файл

@ -155,7 +155,10 @@ namespace PreposeGestures.Parser
{
Contract.Assert(s != null);
var w = this.Visit(s);
Contract.Assert(w != null);
if (w == null)
{
throw new PreposeParserException("Failed to parse statement", s);
}
var statement = w.GetValue();
if (statement != null)
{
@ -171,14 +174,14 @@ namespace PreposeGestures.Parser
continue;
}
throw new ArgumentException("Wrong return type");
throw new PreposeParserException("Invalid return type", s);
}
}
var pose = new Pose(context.ID().GetText(), bt, br);
if (this.Poses.ContainsKey(pose.Name))
{
throw new ArgumentException("Pose " + pose.Name + " has been previosly seen.");
throw new PreposeParserException("Pose " + pose.Name + " has been previously seen.", context);
}
this.Poses.Add(pose.Name, pose);

Просмотреть файл

@ -117,6 +117,7 @@
<Compile Include="Parser\PreposeGesturesLexer.cs" />
<Compile Include="Parser\PreposeGesturesListener.cs" />
<Compile Include="Parser\PreposeGesturesParser.cs" />
<Compile Include="Parser\PreposeParserException.cs" />
<Compile Include="Parser\PreposeGesturesVisitor.cs" />
<Compile Include="Gestures\Pose.cs" />
<Compile Include="Parser\Visitor.cs" />