|
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.Collections; |
|
21 using SymBuildParsingLib.Utils; |
|
22 |
|
23 namespace SymBuildParsingLib.Token |
|
24 { |
|
25 public class SymToken |
|
26 { |
|
27 #region Enumerations |
|
28 public enum TClass |
|
29 { |
|
30 // The lexer only deals at this level of abstraction |
|
31 EClassNull = 0, |
|
32 EClassWhiteSpace, |
|
33 EClassAlphaNumeric, |
|
34 EClassSymbol, |
|
35 EClassNewLine, |
|
36 |
|
37 // The grouper also can identify these types |
|
38 EClassComment, |
|
39 EClassQuotation, |
|
40 EClassContinuation, |
|
41 EClassPreProcessor, |
|
42 }; |
|
43 |
|
44 public enum TType |
|
45 { |
|
46 ETypeUnidentified = 0, |
|
47 ETypeCommentFullLine, |
|
48 ETypeCommentBlock, |
|
49 ETypeQuotationSingle, |
|
50 ETypeQuotationDouble, |
|
51 ETypeAlphaNumericNormal, |
|
52 ETypeAlphaNumericUnderscore, |
|
53 } |
|
54 #endregion |
|
55 |
|
56 #region Constructors & destructor |
|
57 public static SymToken NullToken() |
|
58 { |
|
59 return new SymToken( "", TClass.EClassNull, new SymTextPosition() ); |
|
60 } |
|
61 |
|
62 public SymToken( string aValue, TClass aClass, TType aType ) |
|
63 { |
|
64 iValue = aValue; |
|
65 iClass = aClass; |
|
66 iType = aType; |
|
67 } |
|
68 |
|
69 public SymToken( string aValue, TClass aClass, SymTextPosition aPosition ) |
|
70 { |
|
71 iValue = aValue; |
|
72 iClass = aClass; |
|
73 iPosition = aPosition; |
|
74 } |
|
75 |
|
76 public SymToken( SymToken aToken ) |
|
77 { |
|
78 iValue = aToken.Value; |
|
79 iClass = aToken.Class; |
|
80 iType = aToken.Type; |
|
81 iPosition = aToken.Position; |
|
82 iTag = aToken.Tag; |
|
83 } |
|
84 #endregion |
|
85 |
|
86 #region API |
|
87 public void RefineTokenClass() |
|
88 { |
|
89 if ( Class == SymToken.TClass.EClassSymbol ) |
|
90 { |
|
91 if ( Value.Length == 1 ) |
|
92 { |
|
93 if ( Value == "\"" ) |
|
94 { |
|
95 Class = SymToken.TClass.EClassQuotation; |
|
96 } |
|
97 else if ( Value == "\'" ) |
|
98 { |
|
99 Class = SymToken.TClass.EClassQuotation; |
|
100 } |
|
101 else if ( Value == "_" ) |
|
102 { |
|
103 Class = SymToken.TClass.EClassAlphaNumeric; |
|
104 } |
|
105 else if ( Value == "#" ) |
|
106 { |
|
107 Class = SymToken.TClass.EClassPreProcessor; |
|
108 } |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 public void RefineTokenType() |
|
114 { |
|
115 if ( Type == SymToken.TType.ETypeUnidentified ) |
|
116 { |
|
117 if ( Class == SymToken.TClass.EClassQuotation ) |
|
118 { |
|
119 if ( Value.Length == 1 ) |
|
120 { |
|
121 if ( Value == "\"" ) |
|
122 { |
|
123 Type = SymToken.TType.ETypeQuotationDouble; |
|
124 } |
|
125 else if ( Value == "\'" ) |
|
126 { |
|
127 Type = SymToken.TType.ETypeQuotationSingle; |
|
128 } |
|
129 } |
|
130 } |
|
131 else if ( Class == SymToken.TClass.EClassAlphaNumeric ) |
|
132 { |
|
133 if ( Value.Length == 1 && Value == "_" ) |
|
134 { |
|
135 Type = SymToken.TType.ETypeAlphaNumericUnderscore; |
|
136 } |
|
137 else |
|
138 { |
|
139 Type = SymToken.TType.ETypeAlphaNumericNormal; |
|
140 } |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 public void Combine( SymToken aToken ) |
|
146 { |
|
147 if ( aToken.Value == ")" || Value == ")" ) |
|
148 { |
|
149 int x = 9; |
|
150 } |
|
151 |
|
152 System.Diagnostics.Debug.Assert( Class != SymToken.TClass.EClassNull ); |
|
153 System.Diagnostics.Debug.Assert( aToken.CombiningAllowed == true ); |
|
154 System.Diagnostics.Debug.Assert( CombiningAllowed == true ); |
|
155 // |
|
156 ForceCombine( aToken ); |
|
157 } |
|
158 |
|
159 public void ForceCombine( SymToken aToken ) |
|
160 { |
|
161 System.Diagnostics.Debug.Assert( Class != SymToken.TClass.EClassNull ); |
|
162 // |
|
163 string newValue = iValue + aToken.Value; |
|
164 iValue = newValue; |
|
165 } |
|
166 #endregion |
|
167 |
|
168 #region Properties |
|
169 public bool CombiningAllowed |
|
170 { |
|
171 get |
|
172 { |
|
173 // We don't permit combining for new lines at all. |
|
174 // However, other combining is permitted (irrespective of class type). |
|
175 bool okayToCombine = ( Class != TClass.EClassNewLine ); |
|
176 // |
|
177 if ( Value.Length == 1 ) |
|
178 { |
|
179 // Check its not a disallowed character |
|
180 int index = KDisallowedCombiningCharacters.IndexOf( Value[ 0 ] ); |
|
181 okayToCombine = ( index < 0 ); |
|
182 } |
|
183 // |
|
184 return okayToCombine; |
|
185 } |
|
186 } |
|
187 |
|
188 public string Value |
|
189 { |
|
190 get { return iValue; } |
|
191 } |
|
192 |
|
193 public TClass Class |
|
194 { |
|
195 get { return iClass; } |
|
196 set |
|
197 { |
|
198 iClass = value; |
|
199 |
|
200 // When changing from some other type to a continuation, then |
|
201 // we also ensure we preseve the new line value |
|
202 if ( iClass == SymToken.TClass.EClassContinuation ) |
|
203 { |
|
204 iValue = @"\" + System.Environment.NewLine; |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 public TType Type |
|
210 { |
|
211 get { return iType; } |
|
212 set { iType = value; } |
|
213 } |
|
214 |
|
215 public SymTextPosition Position |
|
216 { |
|
217 get { return iPosition; } |
|
218 } |
|
219 |
|
220 public object Tag |
|
221 { |
|
222 get { return iTag; } |
|
223 set { iTag = value; } |
|
224 } |
|
225 #endregion |
|
226 |
|
227 #region From System.Object |
|
228 public override string ToString() |
|
229 { |
|
230 return Value; |
|
231 } |
|
232 |
|
233 public override bool Equals( object aObject ) |
|
234 { |
|
235 bool same = false; |
|
236 // |
|
237 if ( aObject is SymToken ) |
|
238 { |
|
239 SymToken otherToken = (SymToken) aObject; |
|
240 // |
|
241 if ( otherToken.Class == Class ) |
|
242 { |
|
243 if ( otherToken.Type == Type ) |
|
244 { |
|
245 same = ( otherToken.Value == Value ); |
|
246 } |
|
247 } |
|
248 } |
|
249 // |
|
250 return same; |
|
251 } |
|
252 #endregion |
|
253 |
|
254 #region Internal Constants |
|
255 private string KDisallowedCombiningCharacters = "!()\'\";"; |
|
256 #endregion |
|
257 |
|
258 #region Data members |
|
259 private string iValue; |
|
260 private TClass iClass = TClass.EClassNull; |
|
261 private TType iType = TType.ETypeUnidentified; |
|
262 private object iTag; |
|
263 private SymTextPosition iPosition = new SymTextPosition(); |
|
264 #endregion |
|
265 } |
|
266 } |