crashanalysercmd/PerfToolsSharedLibraries/Engine/SymBuildParsingLib/Parser/Framework/Workers/SymParserWorkerCondition.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     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 SymBuildParsingLib.Token;
       
    21 using SymBuildParsingLib.Parser.Framework.Nodes;
       
    22 using SymBuildParsingLib.Utils;
       
    23 using SymbianTree;
       
    24 
       
    25 namespace SymBuildParsingLib.Parser.Framework.Workers
       
    26 {
       
    27 	public class SymParserWorkerCondition : SymParserWorkerConsumer
       
    28 	{
       
    29 		#region Constructors & destructor
       
    30 		public SymParserWorkerCondition( SymParserWorkerContext aContext )
       
    31 			: this( aContext, new SymNodeCondition( aContext.CurrentToken.Value ) )
       
    32 		{
       
    33 		}
       
    34 
       
    35 		public SymParserWorkerCondition( SymParserWorkerContext aContext, SymNodeCondition aNodeCondition )
       
    36 			: base( aContext, SymToken.TClass.EClassNewLine )
       
    37 		{
       
    38 			System.Diagnostics.Debug.Assert( aContext.Document.CurrentNode is SymNodeConditionalExpression );
       
    39 
       
    40 			// Make a new child node for the conditions. 
       
    41 			iConditionNode = aNodeCondition;
       
    42 			aContext.Document.CurrentNode.Add( iConditionNode );
       
    43 			aContext.Document.CurrentNode = iConditionNode;
       
    44 
       
    45 			// Set up the token balancer event handler
       
    46 			iTokenBalancer.EventLevelFinished += new SymBuildParsingLib.Token.SymTokenBalancer.LevelChangeEventHandler( TokenBalancer_EventLevelFinished );
       
    47 			iTokenBalancer.EventLevelsImbalanced += new SymBuildParsingLib.Token.SymTokenBalancer.LevelsImbalancedEventHandler( TokenBalancer_EventLevelsImbalanced );
       
    48 
       
    49 			// If we're handling an ifdef or ifndef expression, we can tell
       
    50 			// the token balancer to discard all brackets, irrespective of their level.
       
    51 			// For normal if statements, we want the brackets.
       
    52 			bool emitLevelZeroBrackets = !( iConditionNode.Type == SymNodeCondition.TType.ETypeIfdef || iConditionNode.Type == SymNodeCondition.TType.ETypeIfndef );
       
    53 			iTokenBalancer.RegisterBalancerTokens( emitLevelZeroBrackets );
       
    54 		}
       
    55 		#endregion
       
    56 
       
    57 		#region From SymParserWorker
       
    58 		public override SymParserWorker.TTokenConsumptionType OfferToken( SymToken aToken )
       
    59 		{
       
    60 			System.Diagnostics.Debug.Assert( WorkerContext.Document.CurrentNode is SymNodeCondition );
       
    61 			//
       
    62 			TTokenConsumptionType ret = TTokenConsumptionType.ETokenNotConsumed;
       
    63 			if	( aToken.Class != SymToken.TClass.EClassNewLine )
       
    64 			{
       
    65 				if	( iTokenBalancer.OfferToken( aToken ) )
       
    66 				{
       
    67 					ret = TTokenConsumptionType.ETokenConsumed;
       
    68 				}
       
    69 			}
       
    70 
       
    71 			// Try offering to base class....
       
    72 			// Base class will dequeue us once we reach the new line
       
    73 			if	( ret != TTokenConsumptionType.ETokenConsumed )
       
    74 			{
       
    75 				ret = base.OfferToken( aToken );
       
    76 			}
       
    77 
       
    78 			return ret;
       
    79 		}
       
    80 		#endregion
       
    81 
       
    82 		#region From SymParserWorkerConsumer
       
    83 		protected override void HandleTerminatingConditionMatch( SymToken aToken )
       
    84 		{
       
    85 			// Give the condition node ownership of the raw parsed tokens
       
    86 			iConditionNode.AssignArgumentTokens( iTokenBalancer.DocumentTree );
       
    87 
       
    88 			// We only need to evaluate the conditional arguments if we have not yet
       
    89 			// found a positive branch.
       
    90 			SymNodeConditionalExpression condExpNode = (SymNodeConditionalExpression) iConditionNode.Parent;
       
    91 			if	( condExpNode.HasPositiveBranch == false )
       
    92 			{
       
    93 				iConditionNode.Evaluate( WorkerContext );
       
    94 			}
       
    95 
       
    96 			// We remain as the current node, even after we die. This allows
       
    97 			// the body of the condition to be added as our child.
       
    98 		}
       
    99 		#endregion
       
   100 
       
   101 		#region Properties
       
   102 		public SymNodeCondition ConditionNode
       
   103 		{
       
   104 			get { return iConditionNode; }
       
   105 		}
       
   106 		#endregion
       
   107 
       
   108 		#region Token balancer event handlers
       
   109 		private void TokenBalancer_EventLevelFinished( int aLevelCount, SymNode aOldLevel, SymNode aNewLevel )
       
   110 		{
       
   111 		}
       
   112 
       
   113 		private void TokenBalancer_EventLevelsImbalanced()
       
   114 		{
       
   115 			throw new Exception( "Levels not balanced during condition parsing" );
       
   116 		}
       
   117 		#endregion
       
   118 
       
   119 		#region Data members
       
   120 		private SymTokenBalancer iTokenBalancer = new SymTokenBalancer();
       
   121 		private readonly SymNodeCondition iConditionNode;
       
   122 		#endregion
       
   123 	}
       
   124 }