|
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.Token; |
|
22 using SymBuildParsingLib.Utils; |
|
23 |
|
24 namespace SymBuildParsingLib.Lexer |
|
25 { |
|
26 public class SymLexerWorkerWord : SymLexerWorker |
|
27 { |
|
28 #region Constructors & destructor |
|
29 public SymLexerWorkerWord( SymLexer aLexer ) |
|
30 : base( aLexer ) |
|
31 { |
|
32 } |
|
33 #endregion |
|
34 |
|
35 #region API |
|
36 #endregion |
|
37 |
|
38 #region SymLexerWorker Members |
|
39 public override bool ProcessCharacter( char aCharacter ) |
|
40 { |
|
41 bool consumed = true; |
|
42 // |
|
43 if ( iCurrentWord.Length == 0 ) |
|
44 { |
|
45 // First letter of new word. Just take the character |
|
46 AddToWord( aCharacter, CharacterClassType( aCharacter ) ); |
|
47 } |
|
48 else if ( char.IsWhiteSpace( aCharacter ) ) |
|
49 { |
|
50 // Was the last character also white space? |
|
51 bool lastCharWasWhiteSpace = char.IsWhiteSpace( LastCharacter ); |
|
52 if ( lastCharWasWhiteSpace ) |
|
53 { |
|
54 // We don't want to make a new word for each white space |
|
55 // character, so begin to group them here. |
|
56 } |
|
57 else |
|
58 { |
|
59 // Last character wasn't whitespace, so we must have ended |
|
60 // a word |
|
61 MakeWord(); |
|
62 } |
|
63 |
|
64 AddToWord( aCharacter, SymToken.TClass.EClassWhiteSpace ); |
|
65 } |
|
66 else if ( iCurrentWord.Length > 0 ) |
|
67 { |
|
68 // We're already processing a word. |
|
69 bool thisCharIsLetterOrDigit = char.IsLetterOrDigit( aCharacter ); |
|
70 bool lastCharWasLetterOrDigit = char.IsLetterOrDigit( LastCharacter ); |
|
71 // |
|
72 if ( thisCharIsLetterOrDigit && lastCharWasLetterOrDigit ) |
|
73 { |
|
74 // In the middle of an ascii word, keep going... |
|
75 AddToWord( aCharacter, SymToken.TClass.EClassAlphaNumeric ); |
|
76 } |
|
77 else |
|
78 { |
|
79 // This char is text, but the last wasn't - make a new word |
|
80 // from what we have and use this character as the basis |
|
81 // for the next word. |
|
82 MakeWord(); |
|
83 AddToWord( aCharacter, CharacterClassType( aCharacter ) ); |
|
84 } |
|
85 } |
|
86 // |
|
87 return consumed; |
|
88 } |
|
89 |
|
90 public override void StartedNewLine( SymTextPosition aEOLPosition ) |
|
91 { |
|
92 MakeWord(); |
|
93 } |
|
94 #endregion |
|
95 |
|
96 #region Internal methods |
|
97 private static SymToken.TClass CharacterClassType( char aCharacter ) |
|
98 { |
|
99 SymToken.TClass ret = SymToken.TClass.EClassSymbol; |
|
100 // |
|
101 if ( char.IsWhiteSpace( aCharacter ) ) |
|
102 { |
|
103 ret = SymToken.TClass.EClassWhiteSpace; |
|
104 } |
|
105 else if ( char.IsLetterOrDigit( aCharacter ) ) |
|
106 { |
|
107 ret = SymToken.TClass.EClassAlphaNumeric; |
|
108 } |
|
109 // |
|
110 return ret; |
|
111 } |
|
112 |
|
113 private void AddToWord( char aCharacter, SymToken.TClass aClassType ) |
|
114 { |
|
115 iCurrentClass = aClassType; |
|
116 iCurrentWord.Append( aCharacter ); |
|
117 } |
|
118 |
|
119 private void MakeWord() |
|
120 { |
|
121 if ( iCurrentWord.Length > 0 ) |
|
122 { |
|
123 // Finished a word |
|
124 SymToken token = new SymToken( iCurrentWord.ToString(), iCurrentClass, Lexer.CurrentPosition ); |
|
125 Lexer.FlushToken( token ); |
|
126 |
|
127 // Reset |
|
128 iCurrentWord.Remove( 0, iCurrentWord.Length ); |
|
129 iCurrentClass = SymToken.TClass.EClassWhiteSpace; |
|
130 } |
|
131 } |
|
132 #endregion |
|
133 |
|
134 #region Internal properties |
|
135 private char LastCharacter |
|
136 { |
|
137 get |
|
138 { |
|
139 char ret = '\0'; |
|
140 int length = iCurrentWord.Length; |
|
141 // |
|
142 if ( length > 0 ) |
|
143 { |
|
144 ret = iCurrentWord[ length - 1 ]; |
|
145 } |
|
146 // |
|
147 return ret; |
|
148 } |
|
149 } |
|
150 #endregion |
|
151 |
|
152 #region Internal enumerations |
|
153 private enum TCharClass |
|
154 { |
|
155 ECharClassWhiteSpace = 0, |
|
156 ECharClassText, |
|
157 ECharClassDigit, |
|
158 ECharClassComma, |
|
159 ECharClassPeriod, |
|
160 ECharClassHash, |
|
161 ECharClassSlash, |
|
162 ECharClassColon, |
|
163 ECharClassSemiColon, |
|
164 ECharClassTilde, |
|
165 ECharClassMathematic |
|
166 }; |
|
167 #endregion |
|
168 |
|
169 #region Data members |
|
170 private StringBuilder iCurrentWord = new StringBuilder( 200 ); |
|
171 private SymToken.TClass iCurrentClass = SymToken.TClass.EClassWhiteSpace; |
|
172 #endregion |
|
173 } |
|
174 } |