Skip to content

Commit 116c4a4

Browse files
authored
Removed Diagnostic enum generic type constraint. (#317)
* Chock it up as "seemed like a good idea at the time" * Increased complexity and decreased performance of `AntlParseErrorListenerAdapter`
1 parent 6e3fb4b commit 116c4a4

27 files changed

+132
-156
lines changed

src/Samples/Kaleidoscope/Chapter8/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.IO;
77

88
using Kaleidoscope.Grammar;
9-
using Kaleidoscope.Grammar.AST;
109

1110
using Ubiquity.NET.CommandLine;
1211
using Ubiquity.NET.Llvm;
@@ -60,7 +59,7 @@ public static int Main( string[] args )
6059
Console.WriteLine( "Compiling {0}", sourceFilePath );
6160

6261
// Create adapter to route parse messages for the Kaleidoscope language to the command line reporter for this app
63-
var errorLogger = new ParseErrorDiagnosticAdapter<DiagnosticCode>(reporter, "KLS", new Uri(sourceFilePath) );
62+
var errorLogger = new ParseErrorDiagnosticAdapter(reporter, "KLS", new Uri(sourceFilePath) );
6463

6564
// time the parse and code generation
6665
var timer = System.Diagnostics.Stopwatch.StartNew( );

src/Samples/Kaleidoscope/Chapter9/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.IO;
77

88
using Kaleidoscope.Grammar;
9-
using Kaleidoscope.Grammar.AST;
109

1110
using Ubiquity.NET.CommandLine;
1211
using Ubiquity.NET.Llvm;
@@ -60,7 +59,7 @@ public static int Main( string[] args )
6059
Console.WriteLine( "Compiling {0}", sourceFilePath );
6160

6261
// Create adapter to route parse messages for the Kaleidoscope language to the command line reporter for this app
63-
var errorLogger = new ParseErrorDiagnosticAdapter<DiagnosticCode>(reporter, "KLS", new Uri(sourceFilePath) );
62+
var errorLogger = new ParseErrorDiagnosticAdapter(reporter, "KLS", new Uri(sourceFilePath) );
6463

6564
// time the parse and code generation
6665
var timer = System.Diagnostics.Stopwatch.StartNew( );

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/ANTLR/KaleidoscopeLexer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ namespace Kaleidoscope.Grammar.ANTLR
1515
// on language features enabled.
1616
internal partial class KaleidoscopeLexer
1717
{
18-
public KaleidoscopeLexer( char[] input, LanguageLevel languageLevel, IParseErrorListener<DiagnosticCode> errorListener )
18+
public KaleidoscopeLexer( char[] input, LanguageLevel languageLevel, IParseErrorListener errorListener )
1919
: this( new AntlrInputStream( input.ThrowIfNull(), input.Length ) )
2020
{
2121
LanguageLevel = languageLevel;
22-
AddErrorListener( new AntlrParseErrorListenerAdapter<DiagnosticCode>( errorListener ) );
22+
AddErrorListener( new AntlrParseErrorListenerAdapter( errorListener ) );
2323
}
2424

2525
public LanguageLevel LanguageLevel { get; }

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/ANTLR/KaleidoscopeParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal partial class KaleidoscopeParser
2020
public KaleidoscopeParser(
2121
ITokenStream tokenStream,
2222
DynamicRuntimeState globalState,
23-
IParseErrorListener<DiagnosticCode>? errorListener,
23+
IParseErrorListener? errorListener,
2424
bool useDiagnosticListener = false
2525
)
2626
: this( tokenStream )
@@ -31,7 +31,7 @@ public KaleidoscopeParser(
3131
if(errorListener != null)
3232
{
3333
RemoveErrorListeners();
34-
AddErrorListener( new AntlrParseErrorListenerAdapter<DiagnosticCode>( errorListener ) );
34+
AddErrorListener( new AntlrParseErrorListenerAdapter( errorListener ) );
3535
}
3636

3737
if(globalState.LanguageLevel >= LanguageLevel.UserDefinedOperators)

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/AstBuilder.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public AstBuilder( DynamicRuntimeState globalState )
2626

2727
public override IAstNode VisitErrorNode( IErrorNode node )
2828
{
29-
return new ErrorNode<DiagnosticCode>( node.GetSourceRange(), DiagnosticCode.SyntaxError, $"Syntax Error: {node}" );
29+
return new ErrorNode( node.GetSourceRange(), (int)DiagnosticCode.SyntaxError, $"Syntax Error: {node}" );
3030
}
3131

3232
public override IAstNode VisitParenExpression( ParenExpressionContext context )
@@ -44,15 +44,15 @@ public override IAstNode VisitVariableExpression( VariableExpressionContext cont
4444
string varName = context.Name;
4545
return NamedValues.TryGetValue( varName, out IVariableDeclaration? declaration )
4646
? new VariableReferenceExpression( context.GetSourceRange(), declaration )
47-
: new ErrorNode<DiagnosticCode>( context.GetSourceRange(), DiagnosticCode.UnknownVariable, $"Unknown variable name: {varName}" );
47+
: new ErrorNode( context.GetSourceRange(), (int)DiagnosticCode.UnknownVariable, $"Unknown variable name: {varName}" );
4848
}
4949

5050
public override IAstNode VisitFunctionCallExpression( FunctionCallExpressionContext context )
5151
{
5252
Prototype? function = FindCallTarget( context.CaleeName );
5353
if(function is null)
5454
{
55-
return new ErrorNode<DiagnosticCode>( context.GetSourceRange(), DiagnosticCode.InvokeUnknownFunction, $"Call to unknown function '{context.CaleeName}'" );
55+
return new ErrorNode( context.GetSourceRange(), (int)DiagnosticCode.InvokeUnknownFunction, $"Call to unknown function '{context.CaleeName}'" );
5656
}
5757

5858
var argNodes = ( from expCtx in context.expression( )
@@ -98,7 +98,7 @@ public override IAstNode VisitExpression( ExpressionContext context )
9898
public override IAstNode VisitExternalDeclaration( ExternalDeclarationContext context )
9999
{
100100
var retVal = ( Prototype )context.Signature.Accept( this );
101-
var errors = retVal.CollectErrors<DiagnosticCode>( );
101+
var errors = retVal.CollectErrors( );
102102
if(errors.Length == 0)
103103
{
104104
RuntimeState.FunctionDeclarations.AddOrReplaceItem( retVal );
@@ -118,7 +118,7 @@ public override IAstNode VisitFunctionDefinition( FunctionDefinitionContext cont
118118
}
119119

120120
var body = context.BodyExpression.Accept( this );
121-
var errors = body.CollectErrors<DiagnosticCode>( );
121+
var errors = body.CollectErrors( );
122122

123123
// test for a non-expression (ErrorNode)
124124
if(body is not IExpression exp)
@@ -152,7 +152,7 @@ public override IAstNode VisitTopLevelExpression( TopLevelExpressionContext cont
152152
BeginFunctionDefinition();
153153
var sig = new Prototype( context.GetSourceRange( ), RuntimeState.GenerateAnonymousName( ), true );
154154
var bodyNode = context.expression( ).Accept( this );
155-
var errors = bodyNode.CollectErrors<DiagnosticCode>();
155+
var errors = bodyNode.CollectErrors();
156156

157157
// only add valid definitions to the runtime state.
158158
if(errors.Length > 0 || bodyNode is not IExpression bodyExp)
@@ -172,14 +172,14 @@ public override IAstNode VisitUnaryOpExpression( UnaryOpExpressionContext contex
172172
var opKind = RuntimeState.GetUnaryOperatorInfo( context.Op ).Kind;
173173
if(opKind == OperatorKind.None)
174174
{
175-
return new ErrorNode<DiagnosticCode>( context.GetSourceRange(), DiagnosticCode.InvalidUnaryOp, $"invalid unary operator {context.Op}" );
175+
return new ErrorNode( context.GetSourceRange(), (int)DiagnosticCode.InvalidUnaryOp, $"invalid unary operator {context.Op}" );
176176
}
177177

178178
string calleeName = CreateUnaryFunctionName( context.OpToken );
179179
var function = FindCallTarget( calleeName );
180180
if(function == null)
181181
{
182-
return new ErrorNode<DiagnosticCode>( context.GetSourceRange(), DiagnosticCode.InvalidUnaryOpRef, $"reference to unknown unary operator function {calleeName}" );
182+
return new ErrorNode( context.GetSourceRange(), (int)DiagnosticCode.InvalidUnaryOpRef, $"reference to unknown unary operator function {calleeName}" );
183183
}
184184

185185
var arg = context.Rhs.Accept( this );
@@ -355,13 +355,13 @@ private IAstNode CreateBinaryOperatorNode( IExpression lhs, BinaryopContext op,
355355
var opKind = RuntimeState.GetBinOperatorInfo( op.OpToken.Type ).Kind;
356356
if(opKind != OperatorKind.InfixLeftAssociative && opKind != OperatorKind.InfixRightAssociative)
357357
{
358-
return new ErrorNode<DiagnosticCode>( op.GetSourceRange(), DiagnosticCode.InvalidBinaryOp, $"Invalid binary operator '{op.OpToken.Text}'" );
358+
return new ErrorNode( op.GetSourceRange(), (int)DiagnosticCode.InvalidBinaryOp, $"Invalid binary operator '{op.OpToken.Text}'" );
359359
}
360360

361361
string calleeName = CreateBinaryFunctionName( op.OpToken );
362362
Prototype? callTarget = FindCallTarget( calleeName );
363363
return callTarget is null
364-
? new ErrorNode<DiagnosticCode>( op.GetSourceRange(), DiagnosticCode.UnaryOpNotFound, $"Unary operator function '{calleeName}' not found" )
364+
? new ErrorNode( op.GetSourceRange(), (int)DiagnosticCode.UnaryOpNotFound, $"Unary operator function '{calleeName}' not found" )
365365
: new FunctionCallExpression( op.GetSourceRange(), callTarget, lhs, rhs );
366366
}
367367
#endregion
@@ -397,11 +397,11 @@ private IAstNode BuildPrototype( PrototypeContext context, string name )
397397
{
398398
if(existingPrototype.Parameters.Count != retVal.Parameters.Count)
399399
{
400-
return new ErrorNode<DiagnosticCode>( context.GetSourceRange(), DiagnosticCode.IncompatibleRedclaration, "Declaration incompatible with previous declaration" );
400+
return new ErrorNode( context.GetSourceRange(), (int)DiagnosticCode.IncompatibleRedclaration, "Declaration incompatible with previous declaration" );
401401
}
402402
}
403403

404-
var errors = retVal.CollectErrors<DiagnosticCode>( );
404+
var errors = retVal.CollectErrors( );
405405
if(errors.Length == 0)
406406
{
407407
RuntimeState.FunctionDeclarations.AddOrReplaceItem( retVal );

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/IKaleidoscopeAstVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IKaleidoscopeAstVisitor<out TResult>
99
{
1010
TResult? Visit( RootNode root );
1111

12-
TResult? Visit( ErrorNode<DiagnosticCode> errorNode );
12+
TResult? Visit( ErrorNode errorNode );
1313

1414
TResult? Visit( Prototype prototype );
1515

@@ -43,7 +43,7 @@ public interface IKaleidoscopeAstVisitor<out TResult, TArg>
4343
{
4444
TResult? Visit( RootNode root, ref readonly TArg arg );
4545

46-
TResult? Visit( ErrorNode<DiagnosticCode> errorNode, ref readonly TArg arg );
46+
TResult? Visit( ErrorNode errorNode, ref readonly TArg arg );
4747

4848
TResult? Visit( Prototype prototype, ref readonly TArg arg );
4949

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/KaleidoscopeAstVisitorBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class KaleidoscopeAstVisitorBase<TResult>
1919
return node switch
2020
{
2121
RootNode n => Visit( n ),
22-
ErrorNode<DiagnosticCode> n => Visit( n ),
22+
ErrorNode n => Visit( n ),
2323
Prototype n => Visit( n ),
2424
FunctionDefinition n => Visit( n ),
2525
ConstantExpression n => Visit( n ),
@@ -37,7 +37,7 @@ public class KaleidoscopeAstVisitorBase<TResult>
3737

3838
public virtual TResult? Visit( RootNode root ) => VisitChildren( root );
3939

40-
public virtual TResult? Visit( ErrorNode<DiagnosticCode> errorNode ) => default;
40+
public virtual TResult? Visit( ErrorNode errorNode ) => default;
4141

4242
public virtual TResult? Visit( Prototype prototype ) => VisitChildren( prototype );
4343

@@ -102,7 +102,7 @@ public class KaleidoscopeAstVisitorBase<TResult, TArg>
102102
return node switch
103103
{
104104
RootNode n => Visit( n, in arg ),
105-
ErrorNode<DiagnosticCode> n => Visit( n, in arg ),
105+
ErrorNode n => Visit( n, in arg ),
106106
Prototype n => Visit( n, in arg ),
107107
FunctionDefinition n => Visit( n, in arg ),
108108
ConstantExpression n => Visit( n, in arg ),
@@ -120,7 +120,7 @@ public class KaleidoscopeAstVisitorBase<TResult, TArg>
120120

121121
public virtual TResult? Visit( RootNode root, ref readonly TArg arg ) => VisitChildren( root, in arg );
122122

123-
public virtual TResult? Visit( ErrorNode<DiagnosticCode> errorNode, ref readonly TArg arg ) => default;
123+
public virtual TResult? Visit( ErrorNode errorNode, ref readonly TArg arg ) => default;
124124

125125
public virtual TResult? Visit( Prototype prototype, ref readonly TArg arg ) => VisitChildren( prototype, in arg );
126126

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/Parser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal enum ParseMode
8484
{
8585
try
8686
{
87-
var errCollector = new ParseErrorCollector<DiagnosticCode>();
87+
var errCollector = new ParseErrorCollector();
8888

8989
(KaleidoscopeParser antlrParser, IParseTree parseTree) = CoreParse( input, mode, errCollector );
9090

@@ -131,11 +131,11 @@ internal enum ParseMode
131131
}
132132
catch(ParseCanceledException)
133133
{
134-
return new RootNode( default, new ErrorNode<DiagnosticCode>( default, DiagnosticCode.ParseCanceled, "Parse canceled" ) );
134+
return new RootNode( default, new ErrorNode( default, (int)DiagnosticCode.ParseCanceled, "Parse canceled" ) );
135135
}
136136
}
137137

138-
private (KaleidoscopeParser Op, IParseTree RHS) CoreParse( char[] input, ParseMode mode, ParseErrorCollector<DiagnosticCode> errCollector )
138+
private (KaleidoscopeParser Op, IParseTree RHS) CoreParse( char[] input, ParseMode mode, ParseErrorCollector errCollector )
139139
{
140140
var lexer = new KaleidoscopeLexer( input, GlobalState.LanguageLevel, errCollector );
141141

src/Samples/Kaleidoscope/Kaleidoscope.Runtime/KaleidoscopeReadEvaluatePrintLoopBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Kaleidoscope.Runtime
1616
{
1717
public abstract class KaleidoscopeReadEvaluatePrintLoopBase<T>
18-
: REPLBase<T, DiagnosticCode>
18+
: REPLBase<T>
1919
{
2020
public LanguageLevel LanguageFeatureLevel { get; }
2121

@@ -41,11 +41,11 @@ public sealed override void ShowPrompt( ReadyState state )
4141
}
4242

4343
protected KaleidoscopeReadEvaluatePrintLoopBase( LanguageLevel level )
44-
: this(level, new ParseErrorDiagnosticAdapter<DiagnosticCode>(new ColoredConsoleReporter(), "KLS"))
44+
: this(level, new ParseErrorDiagnosticAdapter(new ColoredConsoleReporter(), "KLS"))
4545
{
4646
}
4747

48-
protected KaleidoscopeReadEvaluatePrintLoopBase( LanguageLevel level, IParseErrorReporter<DiagnosticCode> logger )
48+
protected KaleidoscopeReadEvaluatePrintLoopBase( LanguageLevel level, IParseErrorReporter logger )
4949
: base( logger )
5050
{
5151
LanguageFeatureLevel = level;

src/Samples/Kaleidoscope/Kaleidoscope.Tests/BasicTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private async Task RunBasicReplLoop( LanguageLevel level
117117

118118
await foreach(IAstNode node in replSeq)
119119
{
120-
var errors = node.CollectErrors<DiagnosticCode>();
120+
var errors = node.CollectErrors();
121121
Assert.IsEmpty( errors );
122122

123123
var result = generator.Generate( node );

0 commit comments

Comments
 (0)