@@ -17,7 +17,7 @@ internal class TargetSelectorAliasAsAttribute : Attribute
1717 }
1818
1919 /// <summary>
20- /// TargetSelector 类型
20+ /// 目标选择器类型
2121 /// </summary>
2222 public enum TargetSelectorType
2323 {
@@ -70,17 +70,17 @@ private enum ParseStatus
7070 Rejected
7171 }
7272
73- private const char PrefixToken = '@' ;
73+ internal const char PrefixToken = '@' ;
7474 private const char ArgumentListStartToken = '[' ;
7575 private const char ArgumentListEndToken = ']' ;
7676 private const char ArgumentAssignmentToken = '=' ;
7777 private const char ArgumentSeparatorToken = ',' ;
7878
7979 private static readonly Dictionary < char , TargetSelectorType > TargetSelectorMap =
80- typeof ( TargetSelectorType ) . GetFields ( )
80+ typeof ( TargetSelectorType ) . GetFields ( BindingFlags . DeclaredOnly | BindingFlags . Public | BindingFlags . Static )
8181 . ToDictionary (
82- v => v . GetType ( ) . GetTypeInfo ( ) . GetCustomAttribute < TargetSelectorAliasAsAttribute > ( ) . Alias ,
83- v => ( TargetSelectorType ) v . GetValue ( null ) ) ;
82+ v => v . GetCustomAttribute < TargetSelectorAliasAsAttribute > ( ) . Alias ,
83+ v => ( TargetSelectorType ) v . GetRawConstantValue ( ) ) ;
8484
8585 /// <summary>
8686 /// Gets 指示选择了哪一类型的目标
@@ -91,11 +91,11 @@ private enum ParseStatus
9191
9292 /// <summary>
9393 /// Initializes a new instance of the <see cref="TargetSelectorArgument"/> class.<para />
94- /// 构造并分析一个 TargetSelector
94+ /// 构造并分析一个目标选择器
9595 /// </summary>
96- /// <param name="rawContent">作为 TargetSelector 的内容 </param>
96+ /// <param name="rawContent">作为目标选择器的内容 </param>
9797 /// <exception cref="ArgumentNullException"><paramref name="rawContent"/> 为 null</exception>
98- /// <exception cref="ArgumentException"><paramref name="rawContent"/> 无法作为 TargetSelector 解析 </exception>
98+ /// <exception cref="ArgumentException"><paramref name="rawContent"/> 无法作为目标选择器解析 </exception>
9999 public TargetSelectorArgument ( string rawContent )
100100 : base ( rawContent )
101101 {
@@ -108,13 +108,20 @@ public TargetSelectorArgument(string rawContent)
108108 switch ( status )
109109 {
110110 case ParseStatus . Prefix :
111- status = cur == PrefixToken ? ParseStatus . VariableTag : ParseStatus . Rejected ;
111+ if ( cur == PrefixToken )
112+ {
113+ status = ParseStatus . VariableTag ;
114+ }
115+ else
116+ {
117+ goto case ParseStatus . Rejected ;
118+ }
119+
112120 break ;
113121 case ParseStatus . VariableTag :
114122 if ( ! TargetSelectorMap . TryGetValue ( cur , out var type ) )
115123 {
116- status = ParseStatus . Rejected ;
117- break ;
124+ goto case ParseStatus . Rejected ;
118125 }
119126
120127 Type = type ;
@@ -123,11 +130,18 @@ public TargetSelectorArgument(string rawContent)
123130 case ParseStatus . OptionalArgumentListStart :
124131 if ( cur != ArgumentListStartToken )
125132 {
126- status = ParseStatus . Rejected ;
133+ goto case ParseStatus . Rejected ;
127134 }
128135
136+ status = ParseStatus . ArgumentElementName ;
129137 break ;
130138 case ParseStatus . ArgumentElementName :
139+ if ( char . IsWhiteSpace ( cur ) && tmpString . Length == 0 )
140+ {
141+ // 略过开头的空白字符,但是这不可能发生。。。
142+ continue ;
143+ }
144+
131145 if ( cur == ArgumentAssignmentToken )
132146 {
133147 argName = tmpString . ToString ( ) ;
@@ -144,7 +158,15 @@ public TargetSelectorArgument(string rawContent)
144158 Contract . Assert ( argName != null ) ;
145159 _arguments . Add ( argName , tmpString . ToString ( ) ) ;
146160 tmpString = new StringBuilder ( ) ;
147- status = cur == ArgumentSeparatorToken ? ParseStatus . ArgumentElementName : ParseStatus . ArgumentListEnd ;
161+ if ( cur == ArgumentSeparatorToken )
162+ {
163+ status = ParseStatus . ArgumentElementName ;
164+ }
165+ else
166+ {
167+ goto case ParseStatus . ArgumentListEnd ;
168+ }
169+
148170 break ;
149171 }
150172
@@ -154,7 +176,9 @@ public TargetSelectorArgument(string rawContent)
154176 status = ParseStatus . Accepted ;
155177 break ;
156178 case ParseStatus . Accepted :
157- return ;
179+ // 尾部有多余的字符
180+ status = ParseStatus . Rejected ;
181+ break ;
158182 case ParseStatus . Rejected :
159183 throw new ArgumentException ( $ "\" { rawContent } \" 不能被解析为合法的 TargetSelector", nameof ( rawContent ) ) ;
160184 default :
@@ -163,7 +187,7 @@ public TargetSelectorArgument(string rawContent)
163187 }
164188 }
165189
166- if ( status != ParseStatus . Accepted || status != ParseStatus . OptionalArgumentListStart )
190+ if ( status != ParseStatus . Accepted && status != ParseStatus . OptionalArgumentListStart )
167191 {
168192 throw new ArgumentException ( $ "在解析 \" { rawContent } \" 的过程中,解析被过早地中止") ;
169193 }
0 commit comments