@@ -14,6 +14,7 @@ public sealed class DialectRegistry
1414 private readonly Dictionary < string , AttributeDefinition > attributesByParserName = new Dictionary < string , AttributeDefinition > ( StringComparer . Ordinal ) ;
1515 private readonly Dictionary < string , AttributeConstraintDefinition > attributeConstraintsByName = new Dictionary < string , AttributeConstraintDefinition > ( StringComparer . Ordinal ) ;
1616 private readonly Dictionary < string , TypeDefinition > typesByName = new Dictionary < string , TypeDefinition > ( StringComparer . Ordinal ) ;
17+ private readonly Dictionary < string , TypeConstraintDefinition > typeConstraintsByName = new Dictionary < string , TypeConstraintDefinition > ( StringComparer . Ordinal ) ;
1718
1819 /// <summary>
1920 /// Gets the dialects currently registered in the registry.
@@ -90,6 +91,25 @@ private void RegisterDialect(Dialect dialect, bool isDependency)
9091 {
9192 throw new ArgumentException ( $ "The type '{ type . Name } ' is already registered.", nameof ( dialect ) ) ;
9293 }
94+
95+ if ( typeConstraintsByName . ContainsKey ( type . Name ) )
96+ {
97+ throw new ArgumentException ( $ "The type constraint '{ type . Name } ' is already registered.", nameof ( dialect ) ) ;
98+ }
99+ }
100+
101+ foreach ( var typeConstraint in dialect . TypeConstraints )
102+ {
103+ var constraintName = typeConstraint . Name ;
104+ if ( constraintName != null && typeConstraintsByName . ContainsKey ( constraintName ) )
105+ {
106+ throw new ArgumentException ( $ "The type constraint '{ constraintName } ' is already registered.", nameof ( dialect ) ) ;
107+ }
108+
109+ if ( constraintName != null && typesByName . ContainsKey ( constraintName ) )
110+ {
111+ throw new ArgumentException ( $ "The type '{ constraintName } ' is already registered.", nameof ( dialect ) ) ;
112+ }
93113 }
94114
95115 dialectsByName . Add ( dialect . Name , dialect ) ;
@@ -120,6 +140,16 @@ private void RegisterDialect(Dialect dialect, bool isDependency)
120140 foreach ( var type in dialect . Types )
121141 {
122142 typesByName . Add ( type . Name , type ) ;
143+ typeConstraintsByName . Add ( type . Name , type ) ;
144+ }
145+
146+ foreach ( var typeConstraint in dialect . TypeConstraints )
147+ {
148+ var constraintName = typeConstraint . Name ;
149+ if ( constraintName != null )
150+ {
151+ typeConstraintsByName . Add ( constraintName , typeConstraint ) ;
152+ }
123153 }
124154 }
125155
@@ -201,4 +231,12 @@ public bool TryGetType(string name, out TypeDefinition type)
201231 {
202232 return typesByName . TryGetValue ( name , out type ! ) ;
203233 }
234+
235+ /// <summary>
236+ /// Tries to resolve a type constraint definition by name.
237+ /// </summary>
238+ public bool TryResolveTypeConstraint ( string name , out TypeConstraintDefinition typeConstraint )
239+ {
240+ return typeConstraintsByName . TryGetValue ( name , out typeConstraint ! ) ;
241+ }
204242}
0 commit comments