|
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 SymBuildParsingLib.Tree; |
|
20 using SymbianTree; |
|
21 |
|
22 namespace SymBuildParsingLib.Token |
|
23 { |
|
24 public class SymTokenUtils |
|
25 { |
|
26 #region Trim functions |
|
27 public static SymNode TrimEntireTree( SymNode aNode ) |
|
28 { |
|
29 return Trim( aNode, true ); |
|
30 } |
|
31 |
|
32 public static SymNode Trim( SymNode aNode ) |
|
33 { |
|
34 return Trim( aNode, false ); |
|
35 } |
|
36 |
|
37 public static void RemoveWhiteSpace( SymNode aNode, bool aRecurse ) |
|
38 { |
|
39 int count = aNode.ChildCount; |
|
40 for( int i=count-1; i>=0; i-- ) |
|
41 { |
|
42 SymNode basicNode = aNode[ i ]; |
|
43 |
|
44 // If the node is whitespace, then remove it |
|
45 if ( basicNode is SymNodeToken ) |
|
46 { |
|
47 SymNodeToken tokenNode = (SymNodeToken) basicNode; |
|
48 bool isWhiteSpace = ( tokenNode.Token.Class == SymToken.TClass.EClassWhiteSpace ); |
|
49 // |
|
50 if ( isWhiteSpace ) |
|
51 { |
|
52 System.Diagnostics.Debug.Assert( basicNode.HasChildren == false ); |
|
53 basicNode.Remove(); |
|
54 } |
|
55 } |
|
56 |
|
57 // Remove whitespace from this node's children |
|
58 if ( basicNode.HasChildren && aRecurse ) |
|
59 { |
|
60 RemoveWhiteSpace( basicNode, aRecurse ); |
|
61 } |
|
62 } |
|
63 } |
|
64 #endregion |
|
65 |
|
66 #region Internal methods |
|
67 public static SymNode Trim( SymNode aNode, bool aRecurse ) |
|
68 { |
|
69 // Forward pass |
|
70 while( aNode.HasChildren ) |
|
71 { |
|
72 SymNode n = aNode.FirstChild; |
|
73 |
|
74 if ( n is SymNodeToken ) |
|
75 { |
|
76 SymNodeToken nodeToken = (SymNodeToken) n; |
|
77 bool isWhiteSpace = ( nodeToken.Token.Class == SymToken.TClass.EClassWhiteSpace ); |
|
78 // |
|
79 if ( isWhiteSpace ) |
|
80 { |
|
81 System.Diagnostics.Debug.Assert( n.HasChildren == false ); |
|
82 nodeToken.Remove(); |
|
83 } |
|
84 else |
|
85 { |
|
86 break; |
|
87 } |
|
88 } |
|
89 else |
|
90 { |
|
91 break; |
|
92 } |
|
93 } |
|
94 |
|
95 // Backward pass |
|
96 while( aNode.HasChildren ) |
|
97 { |
|
98 SymNode n = aNode.LastChild; |
|
99 |
|
100 if ( n is SymNodeToken ) |
|
101 { |
|
102 SymNodeToken nodeToken = (SymNodeToken) n; |
|
103 bool isWhiteSpace = ( nodeToken.Token.Class == SymToken.TClass.EClassWhiteSpace ); |
|
104 // |
|
105 if ( isWhiteSpace ) |
|
106 { |
|
107 System.Diagnostics.Debug.Assert( n.HasChildren == false ); |
|
108 nodeToken.Remove(); |
|
109 } |
|
110 else |
|
111 { |
|
112 break; |
|
113 } |
|
114 } |
|
115 else |
|
116 { |
|
117 break; |
|
118 } |
|
119 } |
|
120 |
|
121 // Child pass |
|
122 if ( aRecurse ) |
|
123 { |
|
124 foreach( SymNode n in aNode ) |
|
125 { |
|
126 if ( n.HasChildren ) |
|
127 { |
|
128 Trim( n, aRecurse ); |
|
129 } |
|
130 } |
|
131 } |
|
132 |
|
133 return aNode; |
|
134 } |
|
135 #endregion |
|
136 } |
|
137 } |