|
1 /* |
|
2 * Copyright (c) 2004-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 using System; |
|
19 using System.Text; |
|
20 using System.Threading; |
|
21 using System.Collections; |
|
22 using SymBuildParsingLib.Token; |
|
23 |
|
24 namespace SymBuildParsingLib.Grouper |
|
25 { |
|
26 public class SymGrouperMastermindCache : SymTokenContainer |
|
27 { |
|
28 #region Constructors & destructor |
|
29 public SymGrouperMastermindCache() |
|
30 { |
|
31 } |
|
32 #endregion |
|
33 |
|
34 #region API |
|
35 public int CountByType( SymToken aToken ) |
|
36 { |
|
37 int count = 0; |
|
38 // |
|
39 foreach( SymToken token in this ) |
|
40 { |
|
41 if ( aToken.Class == token.Class && |
|
42 aToken.Type == token.Type && |
|
43 aToken.Value == token.Value ) |
|
44 { |
|
45 ++count; |
|
46 } |
|
47 } |
|
48 // |
|
49 return count; |
|
50 } |
|
51 |
|
52 public bool CheckTokensAreOfClass( SymToken.TClass aClass ) |
|
53 { |
|
54 bool tokensAreAllTheSameClass = CheckTokensAreOfClass( aClass, 0 ); |
|
55 return tokensAreAllTheSameClass; |
|
56 } |
|
57 |
|
58 public bool CheckTokensAreOfClass( SymToken.TClass aClass, int aStartIndex ) |
|
59 { |
|
60 bool tokensAreAllTheSameClass = true; |
|
61 // |
|
62 int count = Count; |
|
63 for( int i=aStartIndex; i<count; i++ ) |
|
64 { |
|
65 SymToken token = this[ i ]; |
|
66 // |
|
67 if ( token.Class != aClass ) |
|
68 { |
|
69 tokensAreAllTheSameClass = false; |
|
70 break; |
|
71 } |
|
72 } |
|
73 // |
|
74 return tokensAreAllTheSameClass; |
|
75 } |
|
76 |
|
77 public bool CheckTokensAreOfEitherClass( SymToken.TClass aClass1, SymToken.TClass aClass2 ) |
|
78 { |
|
79 bool tokensAreAllTheSameClass = CheckTokensAreOfEitherClass( aClass1, aClass2, 0 ); |
|
80 return tokensAreAllTheSameClass; |
|
81 } |
|
82 |
|
83 public bool CheckTokensAreOfEitherClass( SymToken.TClass aClass1, SymToken.TClass aClass2, int aStartIndex ) |
|
84 { |
|
85 bool tokensAreAllTheSameClass = true; |
|
86 // |
|
87 int count = Count; |
|
88 for( int i=aStartIndex; i<count; i++ ) |
|
89 { |
|
90 SymToken token = this[ i ]; |
|
91 // |
|
92 if ( !(token.Class == aClass1 || token.Class == aClass2 ) ) |
|
93 { |
|
94 tokensAreAllTheSameClass = false; |
|
95 break; |
|
96 } |
|
97 } |
|
98 // |
|
99 return tokensAreAllTheSameClass; |
|
100 } |
|
101 |
|
102 public void MergeAllTokensWithinRange( int aStartIndex, int aEndIndex, bool aMergeInContinuations, bool aForceMerge ) |
|
103 { |
|
104 int count = Count; |
|
105 // |
|
106 System.Diagnostics.Debug.Assert( count > aStartIndex ); |
|
107 System.Diagnostics.Debug.Assert( aEndIndex < count ); |
|
108 |
|
109 // Have to do this in two passes to ensure token |
|
110 // text remains from left to right. |
|
111 SymToken startingToken = this[ aStartIndex++ ]; |
|
112 if ( aForceMerge == false ) |
|
113 { |
|
114 // Not force-merging, so need to find a valid combinable starting element |
|
115 while( startingToken.CombiningAllowed == false && aStartIndex < aEndIndex ) |
|
116 { |
|
117 startingToken = this[ ++aStartIndex ]; |
|
118 } |
|
119 } |
|
120 |
|
121 // First pass - join tokens |
|
122 for( int i=aStartIndex; i<=aEndIndex; i++ ) |
|
123 { |
|
124 SymToken thisToken = this[ i ]; |
|
125 |
|
126 // Ignore continuations during merging |
|
127 if ( thisToken.Class != SymToken.TClass.EClassContinuation || aMergeInContinuations ) |
|
128 { |
|
129 if ( aForceMerge == false ) |
|
130 { |
|
131 startingToken.Combine( thisToken ); |
|
132 } |
|
133 else |
|
134 { |
|
135 startingToken.ForceCombine( thisToken ); |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 // Second pass - discard merged tokens. |
|
141 for( int i=aEndIndex-1; i>=aStartIndex; i-- ) |
|
142 { |
|
143 Remove( i ); |
|
144 } |
|
145 |
|
146 //System.Diagnostics.Debug.WriteLine( "Merged: " + startingToken.Value ); |
|
147 } |
|
148 #endregion |
|
149 |
|
150 #region Data members |
|
151 #endregion |
|
152 } |
|
153 } |