Skip to content

Commit 0fa7a9c

Browse files
authored
Merge pull request #560 from frg2089/enum
refactor(sly): add Enum constraint to token type
2 parents 5c461e5 + 910ab7a commit 0fa7a9c

File tree

78 files changed

+164
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+164
-116
lines changed

src/sly/EnumConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public static T ConvertIntToEnum<T>(int value) where T : struct
1717

1818

1919

20-
public static IN ConvertStringToEnum<IN>(string name) where IN : struct
20+
public static IN ConvertStringToEnum<IN>(string name) where IN : struct, Enum
2121
{
2222
Enum.TryParse(name, out IN token);
2323
return token;
2424
}
2525

26-
public static bool IsEnumValue<IN>(string name) where IN : struct
26+
public static bool IsEnumValue<IN>(string name) where IN : struct, Enum
2727
{
2828
return Enum.TryParse(name, out IN token);
2929
}

src/sly/lexer/CallBacksBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace sly.lexer
88
public static class CallBacksBuilder
99
{
1010

11-
public static void BuildCallbacks<IN>(GenericLexer<IN> lexer) where IN : struct
11+
public static void BuildCallbacks<IN>(GenericLexer<IN> lexer) where IN : struct, Enum
1212
{
1313
var attributes =
1414
(CallBacksAttribute[]) typeof(IN).GetCustomAttributes(typeof(CallBacksAttribute), true);
@@ -20,7 +20,7 @@ public static void BuildCallbacks<IN>(GenericLexer<IN> lexer) where IN : struct
2020

2121
}
2222

23-
public static void ExtractCallBacks<IN>(Type callbackClass, GenericLexer<IN> lexer) where IN : struct
23+
public static void ExtractCallBacks<IN>(Type callbackClass, GenericLexer<IN> lexer) where IN : struct, Enum
2424
{
2525
var methods = callbackClass.GetMethods().ToList();
2626
methods = methods.Where<MethodInfo>(m =>
@@ -37,13 +37,13 @@ public static void ExtractCallBacks<IN>(Type callbackClass, GenericLexer<IN> lex
3737
}
3838
}
3939

40-
public static void AddCallback<IN>(GenericLexer<IN> lexer, MethodInfo method, IN token) where IN : struct
40+
public static void AddCallback<IN>(GenericLexer<IN> lexer, MethodInfo method, IN token) where IN : struct, Enum
4141
{
4242
var callbackDelegate = (Func<Token<IN>,Token<IN>>)Delegate.CreateDelegate(typeof(Func<Token<IN>,Token<IN>>), method);
4343
lexer.AddCallBack(token,callbackDelegate);
4444
}
4545

46-
public static List<(IN tokenId,Func<Token<IN>, Token<IN>> callback)> GetCallbacks<IN>(GenericLexer<IN> lexer) where IN : struct
46+
public static List<(IN tokenId,Func<Token<IN>, Token<IN>> callback)> GetCallbacks<IN>(GenericLexer<IN> lexer) where IN : struct, Enum
4747
{
4848
List<(IN,Func<Token<IN>, Token<IN>>)> callbacks = new List<(IN,Func<Token<IN>, Token<IN>>)>();
4949
var classAttributes =

src/sly/lexer/GenericLexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace sly.lexer
1111
{
12-
public class GenericLexer<IN> : ILexer<IN> where IN : struct
12+
public class GenericLexer<IN> : ILexer<IN> where IN : struct, Enum
1313
{
1414
public class Config
1515
{

src/sly/lexer/ILexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace sly.lexer
77
{
8-
public interface ILexer<T> where T : struct
8+
public interface ILexer<T> where T : struct, Enum
99
{
1010

1111
Dictionary<T, Dictionary<string, string>> LexemeLabels { get; set; }

src/sly/lexer/Lexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace sly.lexer
1010
/// T is the token type
1111
/// </summary>
1212
/// <typeparam name="T">T is the enum Token type</typeparam>
13-
public class Lexer<T> : ILexer<T> where T : struct
13+
public class Lexer<T> : ILexer<T> where T : struct, Enum
1414
{
1515
public Dictionary<T, Dictionary<string, string>> LexemeLabels { get; set; }
1616
public string I18n { get; set; }

src/sly/lexer/LexerAttribute.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace sly.lexer
44
{
5+
56
[AttributeUsage(AttributeTargets.Enum)]
67
public class LexerAttribute : Attribute
78
{
8-
private static readonly GenericLexer<int>.Config Defaults = new GenericLexer<int>.Config();
9+
private enum DefaultEnum : int
10+
{
11+
}
12+
private static readonly GenericLexer<DefaultEnum>.Config Defaults = new GenericLexer<DefaultEnum>.Config();
913

1014
private bool? ignoreWS;
1115

src/sly/lexer/LexerBuilder.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public static T[] GetAttributesOfType<T>(this Enum enumVal) where T : Attribute
4545

4646
public delegate (List<string> modes, bool isModePopper, string pushTarget) ModesForLexemeGetter<IN>(
4747
KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)> attribute)
48-
where IN : struct;
48+
where IN : struct, Enum;
4949

5050
public static class LexerBuilder
5151
{
5252

5353
public static Dictionary<IN, (List<LexemeAttribute>,List<LexemeLabelAttribute>)> GetLexemesWithReflection<IN>(BuildResult<ILexer<IN>> result, string lang)
54-
where IN : struct
54+
where IN : struct, Enum
5555
{
5656
var attributes = new Dictionary<IN, (List<LexemeAttribute>,List<LexemeLabelAttribute>)>();
5757

@@ -101,7 +101,7 @@ public static class LexerBuilder
101101

102102
public static BuildResult<ILexer<IN>> BuildLexer<IN>(
103103
Action<IN, LexemeAttribute, GenericLexer<IN>> extensionBuilder = null,
104-
LexerPostProcess<IN> lexerPostProcess = null) where IN : struct
104+
LexerPostProcess<IN> lexerPostProcess = null) where IN : struct, Enum
105105
{
106106
return BuildLexer<IN>(new BuildResult<ILexer<IN>>(), extensionBuilder, lexerPostProcess: lexerPostProcess);
107107
}
@@ -115,7 +115,7 @@ public static BuildResult<ILexer<IN>> BuildLexer<IN>(BuildResult<ILexer<IN>> res
115115
Dictionary<IN,List<CommentAttribute>> comments = null,
116116
Func<KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)>,(List<string> modes, bool isModePopper, string pushTarget)> modesGetter = null,
117117
Func<List<(IN tokenId, Func<Token<IN>, Token<IN>> callback)>> callbacksGetter = null)
118-
where IN : struct
118+
where IN : struct, Enum
119119
{
120120
attributes = attributes ?? GetLexemesWithReflection<IN>(result, lang);
121121
lexerAttribute = lexerAttribute ?? typeof(IN).GetCustomAttribute<LexerAttribute>();
@@ -143,7 +143,7 @@ public static BuildResult<ILexer<IN>> BuildLexer<IN>(BuildResult<ILexer<IN>> res
143143
return result;
144144
}
145145

146-
private static List<Token<IN>> LabelTokens<IN>(string lang, List<Token<IN>> tokens, Dictionary<IN, Dictionary<string, string>> labels) where IN : struct
146+
private static List<Token<IN>> LabelTokens<IN>(string lang, List<Token<IN>> tokens, Dictionary<IN, Dictionary<string, string>> labels) where IN : struct, Enum
147147
{
148148
List<Token<IN>> labeledTokens;
149149
labeledTokens = tokens.Select(token =>
@@ -175,7 +175,7 @@ private static BuildResult<ILexer<IN>> Build<IN>(
175175
string lang = null,
176176
IList<string> explicitTokens = null, Dictionary<IN, List<CommentAttribute>> comments = null,
177177
Func<KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)>, (List<string>
178-
modes, bool isModePopper, string pushTarget)> modesGetter = null, Func<List<(IN tokenId, Func<Token<IN>, Token<IN>> callback)>> callbacksGetter = null) where IN : struct
178+
modes, bool isModePopper, string pushTarget)> modesGetter = null, Func<List<(IN tokenId, Func<Token<IN>, Token<IN>> callback)>> callbacksGetter = null) where IN : struct, Enum
179179
{
180180
var hasRegexLexemes = IsRegexLexer<IN>(attributes);
181181
var hasGenericLexemes = IsGenericLexer<IN>(attributes);
@@ -214,7 +214,7 @@ private static BuildResult<ILexer<IN>> Build<IN>(
214214

215215
private static BuildResult<ILexer<IN>> SetLabels<IN>(
216216
Dictionary<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)> attributes,
217-
BuildResult<ILexer<IN>> result) where IN : struct
217+
BuildResult<ILexer<IN>> result) where IN : struct, Enum
218218
{
219219
if (result.IsOk && result.Result != null)
220220
{
@@ -269,7 +269,7 @@ private static bool IsGenericLexer<IN>(Dictionary<IN, (List<LexemeAttribute> lex
269269
private static BuildResult<ILexer<IN>> BuildRegexLexer<IN>(
270270
Dictionary<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)> attributes,
271271
string lang,
272-
BuildResult<ILexer<IN>> result) where IN : struct
272+
BuildResult<ILexer<IN>> result) where IN : struct, Enum
273273
{
274274
var lexer = new Lexer<IN>()
275275
{
@@ -314,7 +314,7 @@ private static BuildResult<ILexer<IN>> BuildRegexLexer<IN>(
314314

315315

316316
private static Dictionary<string, IDictionary<IN, List<LexemeAttribute>>> GetSubLexers<IN>(
317-
IDictionary<IN, (List<LexemeAttribute> lexemes,List<LexemeLabelAttribute> labels)> attributes, Func<KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)>,(List<string> modes, bool isModePopper, string pushTarget)> modesGetter = null) where IN : struct
317+
IDictionary<IN, (List<LexemeAttribute> lexemes,List<LexemeLabelAttribute> labels)> attributes, Func<KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)>,(List<string> modes, bool isModePopper, string pushTarget)> modesGetter = null) where IN : struct, Enum
318318
{
319319
if (modesGetter == null)
320320
{
@@ -354,7 +354,7 @@ private static Dictionary<string, IDictionary<IN, List<LexemeAttribute>>> GetSub
354354
return subLexers;
355355
}
356356

357-
private static (List<string> modes,bool isModePopper, string pushTarget) GetModesForLexeme<IN>(KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)> attribute) where IN : struct
357+
private static (List<string> modes,bool isModePopper, string pushTarget) GetModesForLexeme<IN>(KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)> attribute) where IN : struct, Enum
358358
{
359359
(List<string> modes, bool isModePopper, string pushTarget) result = (new List<string>(), false, null);
360360
if (attribute.Key is Enum enumValue)
@@ -403,7 +403,7 @@ private static (List<string> modes,bool isModePopper, string pushTarget) GetMode
403403

404404
private static (GenericLexer<IN>.Config, GenericToken[]) GetConfigAndGenericTokens<IN>(
405405
IDictionary<IN, List<LexemeAttribute>> attributes, LexerAttribute lexerAttribute = null)
406-
where IN : struct
406+
where IN : struct, Enum
407407
{
408408
var config = new GenericLexer<IN>.Config();
409409
if (lexerAttribute != null)
@@ -460,7 +460,7 @@ private static IEnumerable<char[]> ParseIdentifierPattern(string pattern)
460460
}
461461

462462
private static NodeCallback<GenericToken> GetCallbackSingle<IN>(IN token, int channel)
463-
where IN : struct
463+
where IN : struct, Enum
464464
{
465465
NodeCallback<GenericToken> callback = match =>
466466
{
@@ -474,7 +474,7 @@ private static NodeCallback<GenericToken> GetCallbackSingle<IN>(IN token, int ch
474474
}
475475

476476
private static NodeCallback<GenericToken> GetCallbackMulti<IN>(IN token, int channel)
477-
where IN : struct
477+
where IN : struct, Enum
478478
{
479479
NodeCallback<GenericToken> callbackMulti = match =>
480480
{
@@ -494,7 +494,7 @@ public static BuildResult<ILexer<IN>> BuildGenericSubLexers<IN>(
494494
IList<string> explicitTokens = null, LexerAttribute lexerAttribute = null,
495495
Dictionary<IN, List<CommentAttribute>> comments = null,
496496
Func<KeyValuePair<IN, (List<LexemeAttribute> lexemes, List<LexemeLabelAttribute> labels)>, (List<string>
497-
modes, bool isModePopper, string pushTarget)> modesGetter = null, Func<List<(IN tokenId, Func<Token<IN>, Token<IN>> callback) >> callbacksGetter = null) where IN : struct
497+
modes, bool isModePopper, string pushTarget)> modesGetter = null, Func<List<(IN tokenId, Func<Token<IN>, Token<IN>> callback) >> callbacksGetter = null) where IN : struct, Enum
498498
{
499499
GenericLexer<IN> genLexer = null;
500500
var subLexers = GetSubLexers(attributes, modesGetter);
@@ -534,7 +534,7 @@ public static BuildResult<ILexer<IN>> BuildGenericSubLexers<IN>(
534534
private static BuildResult<ILexer<IN>> BuildGenericLexer<IN>(IDictionary<IN, List<LexemeAttribute>> attributes,
535535
Action<IN, LexemeAttribute, GenericLexer<IN>> extensionBuilder, BuildResult<ILexer<IN>> result, string lang,
536536
LexerAttribute lexerAttribute = null, Dictionary<IN, List<CommentAttribute>> comments = null,
537-
IList<string> explicitTokens = null) where IN : struct
537+
IList<string> explicitTokens = null) where IN : struct, Enum
538538
{
539539
result = CheckStringAndCharTokens<IN>(attributes, result, lang);
540540
var (config, tokens) = GetConfigAndGenericTokens<IN>(attributes, lexerAttribute);
@@ -783,7 +783,7 @@ private static (string delimiter, string escape, bool doEscape) GetDelimiters(Le
783783

784784
private static BuildResult<ILexer<IN>> CheckStringAndCharTokens<IN>(
785785
IDictionary<IN, List<LexemeAttribute>> attributes, BuildResult<ILexer<IN>> result, string lang)
786-
where IN : struct
786+
where IN : struct, Enum
787787
{
788788
var allLexemes = attributes.Values.SelectMany<List<LexemeAttribute>, LexemeAttribute>(a => a);
789789

@@ -809,7 +809,7 @@ private static BuildResult<ILexer<IN>> CheckStringAndCharTokens<IN>(
809809

810810

811811
private static Dictionary<IN, List<CommentAttribute>> GetCommentsAttribute<IN>(BuildResult<ILexer<IN>> result,
812-
string lang) where IN : struct
812+
string lang) where IN : struct, Enum
813813
{
814814
var attributes = new Dictionary<IN, List<CommentAttribute>>();
815815

@@ -861,7 +861,7 @@ private static Dictionary<IN, List<CommentAttribute>> GetCommentsAttribute<IN>(B
861861
}
862862

863863
private static void AddExtensions<IN>(Dictionary<IN, LexemeAttribute> extensions,
864-
Action<IN, LexemeAttribute, GenericLexer<IN>> extensionBuilder, GenericLexer<IN> lexer) where IN : struct
864+
Action<IN, LexemeAttribute, GenericLexer<IN>> extensionBuilder, GenericLexer<IN> lexer) where IN : struct, Enum
865865
{
866866
if (extensionBuilder != null)
867867
{

src/sly/lexer/LexerResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Diagnostics.CodeAnalysis;
34

45
namespace sly.lexer
56
{
6-
public class LexerResult<IN> where IN : struct
7+
public class LexerResult<IN> where IN : struct, Enum
78
{
89
public bool IsError { get; set; }
910

src/sly/lexer/Token.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public enum CommentType
1515
}
1616

1717
[DebuggerDisplay("{TokenID} : {Value} - {IsExplicit}")]
18-
public class Token<T> where T:struct
18+
public class Token<T> where T:struct, Enum
1919
{
2020

2121

src/sly/lexer/TokenChannel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using System.Text;
33
using System.Diagnostics.CodeAnalysis;
44
using System.Linq;
5+
using System;
56

67
namespace sly.lexer
78
{
8-
public class TokenChannel<IN> where IN : struct
9+
public class TokenChannel<IN> where IN : struct, Enum
910
{
1011
public readonly List<Token<IN>> Tokens;
1112

0 commit comments

Comments
 (0)