Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
unify exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lotes committed Apr 16, 2018
1 parent 6f16334 commit 5519f68
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
15 changes: 15 additions & 0 deletions Lexer/Automaton/AutomatonConstructionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lexer.Automaton
{
public class AutomatonConstructionException : Exception
{
public AutomatonConstructionException(string message) : base(message)
{
}
}
}
4 changes: 2 additions & 2 deletions Lexer/Automaton/Impl/AutomatonBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void AcceptState(int state)
public IAutomaton Build()
{
if(StateCounter == 0)
throw new InvalidOperationException("No states defined!");
throw new AutomatonConstructionException("No states defined!");
if(startState == -1)
throw new InvalidOperationException("No start defined!");
throw new AutomatonConstructionException("No start defined!");
return new Automaton(StateCounter, startState, acceptingStates,
transitions.ToDictionary(kv => kv.Key, kv => (ITransitionTargets)kv.Value));
}
Expand Down
12 changes: 12 additions & 0 deletions Lexer/LexerExecutionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Runtime.Serialization;

namespace Lexer
{
public class LexerExecutionException : Exception
{
public LexerExecutionException(string message) : base(message)
{
}
}
}
16 changes: 15 additions & 1 deletion Lexer/LexerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Lexer
{
Expand All @@ -15,7 +16,20 @@ public static IEnumerable<IToken> Read(this ILexer lexer, string input)
index += token.Value.Length;
}
if (index < input.Length)
throw new InvalidOperationException("EOF not reached!");
throw new LexerExecutionException("EOF not reached!");
}

public static string Times(this string str, int factor)
{
if (factor < 0)
throw new ArgumentException("Factor must be non-negative!", nameof(factor));
var builder = new StringBuilder(str.Length*factor);
while(factor > 0)
{
builder.Append(str);
factor--;
}
return builder.ToString();
}
}
}
12 changes: 6 additions & 6 deletions Lexer/RegexParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private IAutomaton PrimaryExpression()
private int Number()
{
if(Lookahead < '0' || Lookahead > '9')
throw new InvalidOperationException("Digit expected, but '"+Lookahead+"' found!");
throw new RegexParserException(index, "Digit expected, but '"+Lookahead+"' found!");
var num = "";
while (Lookahead >= '0' && Lookahead <= '9')
{
Expand Down Expand Up @@ -290,7 +290,7 @@ private bool MayConsume(string str)
private void Consumes(char c)
{
if(Lookahead != c)
throw new InvalidOperationException("'"+c+"' expected, but '"+Lookahead+"' found.");
throw new RegexParserException(index, "'"+c+"' expected, but '"+Lookahead+"' found.");
index++;
}

Expand Down Expand Up @@ -330,7 +330,7 @@ private ICharSet CharRange()
{
var last = Char();
if (last.Length != 1)
throw new InvalidOperationException("Invalid upper bound for character range!");
throw new RegexParserException(index, "Invalid upper bound for character range!");
result = new CharSet(new CharRange(first.First().First(), last.First().First()));
}
else
Expand All @@ -348,7 +348,7 @@ private ICharSet Char()
switch(asciiTable[Lookahead])
{
case CharType.Invalid:
throw new InvalidOperationException("Invalid character!");
throw new RegexParserException(index, "Invalid character: "+Lookahead+"!");
case CharType.Literal:
var first = Lookahead;
index++;
Expand Down Expand Up @@ -382,11 +382,11 @@ private ICharSet Char()
}
}
if (!found)
throw new InvalidOperationException("Invalid escape character: " + Lookahead);
throw new RegexParserException(index, "Invalid escape character: " + Lookahead);
}
}
else
throw new InvalidOperationException("Invalid character!");
throw new RegexParserException(index, "Invalid character: "+Lookahead+"!");
break;
}
}
Expand Down
17 changes: 17 additions & 0 deletions Lexer/RegexParserException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lexer
{
public class RegexParserException : Exception
{
public RegexParserException(int index, string message) : base(message)
{
Index = index;
}
public int Index { get; }
}
}
5 changes: 3 additions & 2 deletions Main/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public static void Main(string[] args)
var automaton = parser.Parse(input);
automaton.Print();
}
catch (Exception e)
catch (RegexParserException e)
{
Console.WriteLine(" "+(" ".Times(e.Index))+"^");
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
Console.WriteLine();
} while (true);
}
}
Expand Down

0 comments on commit 5519f68

Please sign in to comment.