crashanalysercmd/PerfToolsSharedLibraries/Engine/SymBuildParsingLib/Parser/Framework/Utils/SymFunctionParser.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 System.Text;
       
    20 using System.Collections;
       
    21 using SymBuildParsingLib.Token;
       
    22 using SymBuildParsingLib.Tree;
       
    23 using SymBuildParsingLib.Common.Objects;
       
    24 using SymbianTree;
       
    25 
       
    26 namespace SymBuildParsingLib.Parser.Framework.Utils
       
    27 {
       
    28 	public class SymFunctionParser : SymTokenBalancer
       
    29 	{
       
    30 		#region Observer interface
       
    31 		public delegate void ArgumentAvailable( SymArgument aArgument, SymToken aDelimitingToken );
       
    32 		#endregion
       
    33 
       
    34 		#region Events
       
    35 		public event ArgumentAvailable EventArgumentAvailableHandler;
       
    36 		#endregion
       
    37 
       
    38 		#region Constructors & destructor
       
    39 		public SymFunctionParser()
       
    40 		{
       
    41 		}
       
    42 		#endregion
       
    43 
       
    44 		#region API
       
    45 		public virtual void RegisterFunctionParserTokens()
       
    46 		{
       
    47 			// Base class registration
       
    48 			RegisterBalancerTokens();
       
    49 
       
    50 			// These are the important tokens relating to function arguments
       
    51 			SymToken bracketTokenOpening = new SymToken( "(", SymToken.TClass.EClassSymbol, SymToken.TType.ETypeUnidentified );
       
    52 			SymToken bracketTokenClosing = new SymToken( ")", SymToken.TClass.EClassSymbol, SymToken.TType.ETypeUnidentified );
       
    53 			SymToken squareBracketTokenOpening = new SymToken( "[", SymToken.TClass.EClassSymbol, SymToken.TType.ETypeUnidentified );
       
    54 			SymToken squareBracketTokenClosing = new SymToken( "]", SymToken.TClass.EClassSymbol, SymToken.TType.ETypeUnidentified );
       
    55 			SymToken templateBracketTokenOpening = new SymToken( "<", SymToken.TClass.EClassSymbol, SymToken.TType.ETypeUnidentified );
       
    56 			SymToken templateBracketTokenClosing = new SymToken( ">", SymToken.TClass.EClassSymbol, SymToken.TType.ETypeUnidentified );
       
    57 
       
    58 			// We want to track levels for square brackets and template arguments in order to ensure we balance correctly.
       
    59 			// We don't want to remove any redundancy here as these may have special meaning.
       
    60 			RegisterOpeningToken( squareBracketTokenOpening, squareBracketTokenClosing, true, true, TLevelExpectations.ELevelExpectationsAboveLevelNumber, 0, TAssociatedBehaviour.EBehaviourNone );
       
    61 			RegisterClosingToken( squareBracketTokenClosing, squareBracketTokenOpening, true, true, TLevelExpectations.ELevelExpectationsAboveLevelNumber, 0, TAssociatedBehaviour.EBehaviourNone );
       
    62 			RegisterOpeningToken( templateBracketTokenOpening, templateBracketTokenClosing, true, true, TLevelExpectations.ELevelExpectationsAboveLevelNumber, 0, TAssociatedBehaviour.EBehaviourNone );
       
    63 			RegisterClosingToken( templateBracketTokenClosing, templateBracketTokenOpening, true, true, TLevelExpectations.ELevelExpectationsAboveLevelNumber, 0, TAssociatedBehaviour.EBehaviourNone );
       
    64 
       
    65 			// Define our argument separation token(s).
       
    66 			TAssociatedBehaviour flags = TAssociatedBehaviour.EBehaviourNone;
       
    67 			flags |= TAssociatedBehaviour.EBehaviourCreateSubTree;
       
    68 			flags |= TAssociatedBehaviour.EBehaviourRemoveReduntantBracketing;
       
    69 			//
       
    70 			SymToken commaDelimiterToken = new SymToken( ",", SymToken.TClass.EClassSymbol, SymToken.TType.ETypeUnidentified );
       
    71 			RegisterArgumentSeparatorToken( commaDelimiterToken, new SymTokenBalancerMatchCriteria( SymToken.NullToken(), false, true, TLevelExpectations.ELevelExpectationsAtLevel, 1, flags ) );
       
    72 			RegisterArgumentSeparatorToken( commaDelimiterToken, new SymTokenBalancerMatchCriteria( SymToken.NullToken(), true, true, TLevelExpectations.ELevelExpectationsAboveLevelNumber, 1, TAssociatedBehaviour.EBehaviourNone ) );
       
    73 		}
       
    74 
       
    75 		public virtual SymArgument MakeArgument()
       
    76 		{
       
    77 			SymNode levelNodeToObtainArgumentsFrom = CurrentNode;
       
    78 
       
    79 			// Try to work out whether we have a leve node at the current
       
    80 			// scope which should be preferred t
       
    81 			object levelNodeObject = levelNodeToObtainArgumentsFrom.ChildByType( typeof(SymTokenBalancerMarkerLevelNode) );
       
    82 			if	( levelNodeObject != null )
       
    83 			{
       
    84 				levelNodeToObtainArgumentsFrom = (SymNode) levelNodeObject;
       
    85 			}
       
    86 
       
    87 			return MakeArgument( levelNodeToObtainArgumentsFrom );
       
    88 		}
       
    89 
       
    90 		public virtual SymArgument MakeArgument( SymNode aLevelToMakeArgumentsFrom )
       
    91 		{
       
    92 			SymArgument argument = new SymArgument();
       
    93 
       
    94 			// Convert (recursively) any emitted elements to real tokens
       
    95 			if	( aLevelToMakeArgumentsFrom is SymTokenBalancerMarkerLevelNode )
       
    96 			{
       
    97 				SymTokenBalancerMarkerLevelNode levelNode = (SymTokenBalancerMarkerLevelNode) aLevelToMakeArgumentsFrom;
       
    98 				levelNode.ConvertEmittedElementsToRealTokenNodes( true /*recurse*/ );
       
    99 			}
       
   100 
       
   101 			// Now actually obtain the argument tokens
       
   102 			int count = aLevelToMakeArgumentsFrom.ChildCount;
       
   103 			int i = 0;
       
   104 			//
       
   105 			while( i < count )
       
   106 			{
       
   107 				SymNode n = aLevelToMakeArgumentsFrom[ 0 ];
       
   108 
       
   109 				// We always remove any other nodes, irrespective of their type.
       
   110 				// This is to ensure that the document tree does not get cluttered
       
   111 				// with redundant argument token info.
       
   112 				n.Remove();
       
   113 
       
   114 				// Now we decide what to do...
       
   115 				if	( n is SymTokenBalancerMarkerArgumentNode )
       
   116 				{
       
   117 					// We've reached the argument itself. This is the
       
   118 					// signal to stop processing. We remove the argument node
       
   119 					// since its not relevant to the production of the tree.
       
   120 					break;
       
   121 				}
       
   122 				else if ( n is SymTokenBalancerMarkerLevelNode )
       
   123 				{
       
   124 					// Create a new sub-argument node and copy over the 
       
   125 					// children.
       
   126 					SymTokenBalancerMarkerLevelNode levelNode = (SymTokenBalancerMarkerLevelNode) n;
       
   127 					SymArgumentSubLevel subLevel = levelNode.AsArgumentSubLevel( true );
       
   128 					argument.CurrentNode.Add( subLevel );
       
   129 				}
       
   130 				else if ( n is SymTokenBalancerNodeEmittedElement )
       
   131 				{
       
   132 					System.Diagnostics.Debug.Assert( false ); // shouldn't get here anymore!
       
   133 				}
       
   134 				else if	( n is SymNodeToken )
       
   135 				{
       
   136 					// Node is implicitly removed since it transfers
       
   137 					// from one tree to another.
       
   138 					argument.CurrentNode.Add( n );
       
   139 				}
       
   140 
       
   141 				count = aLevelToMakeArgumentsFrom.ChildCount;
       
   142 			}
       
   143 			//
       
   144 			SymTokenUtils.RemoveWhiteSpace( argument, true );
       
   145 			return argument;
       
   146 		}
       
   147 
       
   148 		public string MakeFunctionName()
       
   149 		{
       
   150 			StringBuilder name = new StringBuilder();
       
   151 
       
   152 			// Pull out all the level one tokens from the balancer. These form
       
   153 			// the function/define name.
       
   154 			foreach( SymNode n in DocumentTree )
       
   155 			{
       
   156 				if	( n is SymTokenBalancerMarkerNode )
       
   157 				{
       
   158 					// Not part of the name
       
   159 					break;
       
   160 				}
       
   161 				else if ( n is SymTokenBalancerNodeEmittedElement )
       
   162 				{
       
   163 					// Not part of the name - also, this is indicator to stop iterating!
       
   164 					break;
       
   165 				}
       
   166 				else if ( n is SymNodeToken )
       
   167 				{
       
   168 					SymNodeToken tokenNode = (SymNodeToken) n;
       
   169 					name.Append( tokenNode.Token.Value );
       
   170 				}
       
   171 			}
       
   172 
       
   173 			return name.ToString();
       
   174 		}
       
   175 		#endregion
       
   176 
       
   177 		#region API - registration
       
   178 		public void RegisterArgumentSeparatorToken( SymToken aToken, SymTokenBalancerMatchCriteria aCriteria )
       
   179 		{
       
   180 			SymToken copy = new SymToken( aToken );
       
   181 			copy.Tag = aCriteria;
       
   182 			//
       
   183 			if	( IsTokenExactMatch( copy, iArgumentSeparators ) == false )
       
   184 			{
       
   185 				iArgumentSeparators.Append( copy );
       
   186 			}
       
   187 		}
       
   188 		#endregion
       
   189 
       
   190 		#region From SymTokenBalancer
       
   191 		public override bool OfferToken( SymToken aToken )
       
   192 		{
       
   193 			bool consumed = false;
       
   194 			int currentLevelNumber = CurrentLevelNumber;
       
   195 
       
   196 			// Check for bracket matches
       
   197 			SymTokenBalancerMatchCriteria tokenExtendedInfo;
       
   198 			if	( IsArgumentSeparatorMatch( aToken, out tokenExtendedInfo, currentLevelNumber ) )
       
   199 			{
       
   200 				ArgumentStarted( aToken, tokenExtendedInfo );
       
   201 				consumed = true;
       
   202 			}
       
   203 			
       
   204 			// If it wasn't an arg token or it was, but it wasn't at the 
       
   205 			// correct (expected) level, then give it to the base class
       
   206 			// to handle.
       
   207 			if	( consumed == false )
       
   208 			{
       
   209 				consumed = base.OfferToken( aToken );
       
   210 			}
       
   211 			//
       
   212 			return consumed;
       
   213 		}
       
   214 		#endregion
       
   215 
       
   216 		#region Notification framework
       
   217 		protected virtual void NotifyArgumentAvailable( SymArgument aArgument, SymToken aDelimitingToken )
       
   218 		{
       
   219 			if	( EventArgumentAvailableHandler != null )
       
   220 			{
       
   221 				EventArgumentAvailableHandler( aArgument, aDelimitingToken );
       
   222 			}
       
   223 		}
       
   224 		#endregion
       
   225 
       
   226 		#region New internal framework
       
   227 		protected virtual void ArgumentStarted( SymToken aToken, SymTokenBalancerMatchCriteria aCriteria )
       
   228 		{
       
   229 			System.Diagnostics.Debug.Write( aToken.Value );
       
   230 
       
   231 			int currentLevelNumber = CurrentLevelNumber;
       
   232 
       
   233 			// Perform any base class end level behaviour
       
   234 			PerformEndLevelBehaviour( CurrentNode, aCriteria );
       
   235 
       
   236 			// Add the emit node (the rest of the code will work out whether it needs to quote it when the final
       
   237 			// tree is formed).
       
   238 			SymTokenBalancerNodeEmittedElement argEmitElement = new SymTokenBalancerNodeEmittedElement( aToken, aCriteria );
       
   239 			DocumentTree.CurrentNode.Add( argEmitElement );
       
   240 
       
   241 			// Always add the argument node
       
   242 			SymTokenBalancerMarkerArgumentNode argNode = new SymTokenBalancerMarkerArgumentNode( aCriteria );
       
   243 			DocumentTree.CurrentNode.Add( argNode );
       
   244 
       
   245 			if	( aCriteria.IsAssociatedBehaviourCreateSubTree )
       
   246 			{
       
   247 				// Make a new argument definition based upon the tokens we have in
       
   248 				// the main document tree.
       
   249 				SymArgument argument = MakeArgument( DocumentTree.CurrentNode );
       
   250 
       
   251 				// Then notify the observer
       
   252 				NotifyArgumentAvailable( argument, aToken );
       
   253 			}
       
   254 		}
       
   255 		#endregion
       
   256 
       
   257 		#region Internal utility methods
       
   258 		protected bool IsArgumentSeparatorMatch( SymToken aToken, out SymTokenBalancerMatchCriteria aCriteria, int aLevelNumber )
       
   259 		{
       
   260 			aCriteria = null;
       
   261 			bool matchFound = false;
       
   262 			//
       
   263 			int index = iArgumentSeparators.IndexOf( aToken );
       
   264 			while( index >= 0 && matchFound == false )
       
   265 			{
       
   266 				SymToken token = iArgumentSeparators[ index ];
       
   267 				System.Diagnostics.Debug.Assert ( token.Tag != null && token.Tag is SymTokenBalancerMatchCriteria );
       
   268 				SymTokenBalancerMatchCriteria criteria = (SymTokenBalancerMatchCriteria) token.Tag;
       
   269 
       
   270 				if	( criteria.Matches( aLevelNumber ) )
       
   271 				{
       
   272 					aCriteria = criteria;
       
   273 					matchFound = true;
       
   274 				}
       
   275 				else
       
   276 				{
       
   277 					index = iArgumentSeparators.IndexOf( aToken, index+1 );
       
   278 				}
       
   279 			}
       
   280 
       
   281 			return matchFound;
       
   282 		}
       
   283 
       
   284 		#endregion
       
   285 
       
   286 		#region Data members
       
   287 		private SymTokenContainer iArgumentSeparators = new SymTokenContainer();
       
   288 		#endregion
       
   289 	}
       
   290 }