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

Refactoring #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions InfHelper/src/Exceptions/NoTokenRecognizedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Runtime.Serialization;

namespace InfHelper.Exceptions
{
public class NoTokenRecognizedException : InfParserException
{
public NoTokenRecognizedException()
{
}

public NoTokenRecognizedException(string message) : base(message)
{
}

public NoTokenRecognizedException(string message, Exception innerException) : base(message, innerException)
{
}

protected NoTokenRecognizedException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
24 changes: 0 additions & 24 deletions InfHelper/src/Exceptions/NoneTokenRecognizedException.cs

This file was deleted.

11 changes: 0 additions & 11 deletions InfHelper/src/Models/Tokens/CategoryClosingToken.cs

This file was deleted.

8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/CategoryOpeningToken.cs

This file was deleted.

8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/EqualityToken.cs

This file was deleted.

8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/InlineCommentToken.cs

This file was deleted.

31 changes: 0 additions & 31 deletions InfHelper/src/Models/Tokens/LetterToken.cs

This file was deleted.

8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/LineConcatenatorToken.cs

This file was deleted.

8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/NewLineToken.cs

This file was deleted.

8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/SpaceToken.cs

This file was deleted.

13 changes: 13 additions & 0 deletions InfHelper/src/Models/Tokens/Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace InfHelper.Models.Tokens
{
public class Token
{
public Token(TokenType tokenType)
{
Type = tokenType;
}

public char Symbol { get; set; }
public TokenType Type { get; }
}
}
15 changes: 0 additions & 15 deletions InfHelper/src/Models/Tokens/TokenBase.cs

This file was deleted.

139 changes: 116 additions & 23 deletions InfHelper/src/Models/Tokens/TokenType.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,122 @@
namespace InfHelper.Models.Tokens
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace InfHelper.Models.Tokens
{

internal abstract class PredicateBasedTokenTypeAttribute: Attribute
{
protected PredicateBasedTokenTypeAttribute(Predicate<char> predicate)
{
Predicate = predicate;
}

internal readonly Predicate<char> Predicate;

}

internal class SymbolBasedTokenTypeAttribute: Attribute
{
internal SymbolBasedTokenTypeAttribute(string symbols)
{
Symbols = symbols.ToCharArray();
}

internal SymbolBasedTokenTypeAttribute(char symbol)
{
Symbols = new []{symbol};
}

public char[] Symbols { get; }
}

internal class LetterTokenTypeAttribute: PredicateBasedTokenTypeAttribute
{
public LetterTokenTypeAttribute() : base(c => !char.IsControl(c) && !char.IsWhiteSpace(c))
{
}
}

internal class WhiteSpaceTokenTypeAttribute: PredicateBasedTokenTypeAttribute
{
public WhiteSpaceTokenTypeAttribute() : base(char.IsWhiteSpace)
{
}

}

public enum TokenType
{
// a-z A-Z
Letter,
// =
EQ,
// [
CategoryOpening,
// ]
CategoryClosing,
// \r space etc
WhiteSpace,
// \n
NewLine,
// \
LineConcatenator,
// ,
ValueSeparator,
// "
ValueMarker,
// ;
InlineComment,
[LetterTokenType] Letter,
[SymbolBasedTokenType('=')] Equality,
[SymbolBasedTokenType('[')] CategoryOpening,
[SymbolBasedTokenType(']')] CategoryClosing,
[WhiteSpaceTokenType] WhiteSpace,
[SymbolBasedTokenType("\n\r")] NewLine,
[SymbolBasedTokenType('\\')] LineConcatenator,
[SymbolBasedTokenType(',')] ValueSeparator,
[SymbolBasedTokenType('"')] ValueMarker,
[SymbolBasedTokenType(';')] InlineComment,
[SymbolBasedTokenType(' ')] Space
}

public static class TokenTypes
{

private static readonly Dictionary<TokenType, Predicate<char>> Predicates = Initialize();

private static Dictionary<TokenType, Predicate<char>> Initialize()
{
var predicates = new Dictionary<TokenType, Predicate<char>>();
foreach (var tokenType in Enum.GetValues(typeof(TokenType)).Cast<TokenType>())
{
switch (GetAttr(tokenType))
{
case PredicateBasedTokenTypeAttribute attribute:
predicates.Add(tokenType, attribute.Predicate);
break;
case SymbolBasedTokenTypeAttribute attribute:
predicates.Add(tokenType, c => attribute.Symbols.Contains(c));
break;
default:
throw new Exception("Misconfigured TokenType: " + tokenType);
}
}
return predicates;
}

public static bool IsToken(TokenType tokenType, char c)
{
return Predicates[tokenType].Invoke(c);
}

// just space (not a tab or newline)
Space
public static Token CreateToken(TokenType tokenType, char c)
{
return new Token(tokenType)
{
Symbol = c
};
}

private static Attribute GetAttr(TokenType tokenType)
{
var memberInfo = ForValue(tokenType);
if (memberInfo == null)
{
throw new Exception("Misconfigured TokenType: " + tokenType);
}

return Attribute.GetCustomAttribute(memberInfo, typeof(Attribute));
}

private static MemberInfo ForValue(TokenType tokenType)
{
var name = Enum.GetName(typeof(TokenType), tokenType);
return name != null ? typeof(TokenType).GetField(name) : null;
}

}

}
8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/ValueMarkerToken.cs

This file was deleted.

8 changes: 0 additions & 8 deletions InfHelper/src/Models/Tokens/ValueSeparatorToken.cs

This file was deleted.

12 changes: 0 additions & 12 deletions InfHelper/src/Models/Tokens/WhiteSpaceToken.cs

This file was deleted.

Loading