@@ -62,15 +62,15 @@ private TokenValidationError ValidateToken(int lineNumber, TokenType tokenType,
62
62
63
63
private TokenValidationError ValidateTokenPattern ( int lineNumber , TokenPattern tokenPattern , Stack < TokenType > preceeding , Queue < TokenType > succeeding )
64
64
{
65
- if ( tokenPattern . Preceeding . Length > 0 )
65
+ if ( tokenPattern . Preceeding . Count > 0 )
66
66
{
67
67
if ( ! PatternMatch ( tokenPattern . Preceeding , preceeding ) )
68
68
{
69
69
return new TokenValidationError ( lineNumber , $ "Preceeding tokens did not match the pattern for token { tokenPattern . Token } ", tokenPattern . Preceeding , preceeding ) ;
70
70
}
71
71
}
72
72
73
- if ( tokenPattern . Succeeding . Length > 0 )
73
+ if ( tokenPattern . Succeeding . Count > 0 )
74
74
{
75
75
if ( ! PatternMatch ( tokenPattern . Succeeding , succeeding ) )
76
76
{
@@ -81,25 +81,32 @@ private TokenValidationError ValidateTokenPattern(int lineNumber, TokenPattern t
81
81
return null ;
82
82
}
83
83
84
- private bool PatternMatch ( IEnumerable < TokenType > initialTokens , IEnumerable < TokenType > comparisonTokens )
84
+ private bool PatternMatch ( IReadOnlyCollection < TokenType > initialTokens , IReadOnlyCollection < TokenType > comparisonTokens )
85
85
{
86
86
//Check number of tokens match (though ignore required new lines)
87
87
//This effectively allows "NewLine" tokens to be optional when there are no proceeding tokens (eg. at the start of the file)
88
- if ( initialTokens . Count ( t => t != TokenType . NewLine ) > comparisonTokens . Count ( ) )
88
+ if ( initialTokens . Count ( t => t != TokenType . NewLine ) > comparisonTokens . Count )
89
89
{
90
90
return false ;
91
91
}
92
92
93
- var sliceSize = Math . Min ( initialTokens . Count ( ) , comparisonTokens . Count ( ) ) ;
94
- var slicedTokensArray = comparisonTokens . Take ( sliceSize ) . ToArray ( ) ;
93
+ var index = 0 ;
94
+ var length = Math . Min ( initialTokens . Count , comparisonTokens . Count ) ;
95
95
var initialTokensArray = ( TokenType [ ] ) initialTokens ;
96
96
97
- for ( int i = 0 , l = sliceSize ; i < l ; i ++ )
97
+ foreach ( var comparisonToken in comparisonTokens )
98
98
{
99
- if ( initialTokensArray [ i ] != slicedTokensArray [ i ] )
99
+ if ( index >= length )
100
+ {
101
+ break ;
102
+ }
103
+
104
+ if ( initialTokensArray [ index ] != comparisonToken )
100
105
{
101
106
return false ;
102
107
}
108
+
109
+ index ++ ;
103
110
}
104
111
105
112
return true ;
0 commit comments