crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/SourceParser/Parsers/ParserSrcMethod.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 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 using System;
       
    18 using System.Collections.Generic;
       
    19 using System.Text;
       
    20 using SymbianUtils.SourceParser.Objects;
       
    21 using SymbianUtils.SourceParser.Parsers;
       
    22 
       
    23 namespace SymbianUtils.SourceParser.Parsers
       
    24 {
       
    25     public class ParserSrcMethod
       
    26     {
       
    27         #region Constructors
       
    28         public ParserSrcMethod()
       
    29         {
       
    30         }
       
    31         #endregion
       
    32 
       
    33         #region API
       
    34         public SrcMethod Parse( ref string aText )
       
    35         {
       
    36             SrcMethod ret = null;
       
    37             //
       
    38             if ( aText.Length > 0 )
       
    39             {
       
    40                 string parameters = string.Empty;
       
    41                 //
       
    42                 if ( ContainsParameters( aText ) )
       
    43                 {
       
    44                     // This leaves modifiers intact
       
    45                     parameters = ExtractParameters( ref aText );
       
    46                 }
       
    47 
       
    48                 // Look for the class separator. If we find that, then everything after
       
    49                 // it is the method name.
       
    50                 //
       
    51                 // If no class separator exists, then we treat the whole thing as the method
       
    52                 // name.
       
    53                 int pos = aText.IndexOf( SrcClass.KClassSeparator );
       
    54 
       
    55                 // By default, treat the whole text as the class name
       
    56                 string methodText = aText;
       
    57                 if ( pos >= 0 )
       
    58                 {
       
    59                     methodText = aText.Substring( pos + SrcClass.KClassSeparator.Length );
       
    60                     aText = aText.Substring( 0, pos + SrcClass.KClassSeparator.Length );
       
    61                 }
       
    62                 else
       
    63                 {
       
    64                     // Everything was consumed...
       
    65                     aText = string.Empty;
       
    66                 }
       
    67 
       
    68                 // Make a new method. Work out if the method text
       
    69                 // actually has any parameters
       
    70                 ret = new SrcMethod();
       
    71 
       
    72                 // Try to parse the modifiers. We extract that first
       
    73                 // to leave us with just the method name and the parameters.
       
    74                 bool hasModifier = ContainsModifier( methodText );
       
    75                 if ( hasModifier )
       
    76                 {
       
    77                     ParserSrcMethodModifier parser = new ParserSrcMethodModifier();
       
    78                     SrcMethodModifier modifier = parser.Parse( ref methodText );
       
    79                     if ( modifier != null )
       
    80                     {
       
    81                         ret.Modifier = modifier;
       
    82                     }
       
    83                 }
       
    84 
       
    85                 // Try to parse the parameters. We can also use this
       
    86                 // to calculate the exact method name.
       
    87                 if ( parameters.Length > 0 )
       
    88                 {
       
    89                     ParserSrcMethodParameter parser = new ParserSrcMethodParameter();
       
    90                     parser.Parse( ref parameters, ret );
       
    91                 }
       
    92 
       
    93                 // What's left should be the method name followed by "()" if the
       
    94                 // 'method' wasn't a label.
       
    95                 if ( ContainsParameters( methodText ) )
       
    96                 {
       
    97                     // Discard "()";
       
    98                     pos = methodText.LastIndexOf( "(" );
       
    99                     methodText = methodText.Substring( 0, pos );
       
   100                 }
       
   101 
       
   102                 ret.Name = methodText;
       
   103             }
       
   104             //
       
   105             return ret;
       
   106         }
       
   107         #endregion
       
   108 
       
   109         #region Properties
       
   110         #endregion
       
   111 
       
   112         #region Internal methods
       
   113         private static bool ContainsParameters( string aText )
       
   114         {
       
   115             // Search initiall for '(' - if that is found, then
       
   116             // we should also find a closing bracket.
       
   117             bool parameters = false;
       
   118             int openingBracketPos = aText.IndexOf( "(" );
       
   119             //
       
   120             if ( openingBracketPos > 0 )
       
   121             {
       
   122                 // Should also be a closing bracket and it should
       
   123                 // appear after the opening bracket
       
   124                 int closingBracketPos = aText.LastIndexOf( ")" );
       
   125                 parameters = ( closingBracketPos > openingBracketPos );
       
   126             }
       
   127             //
       
   128             return parameters;
       
   129         }
       
   130 
       
   131         private static bool ContainsModifier( string aText )
       
   132         {
       
   133             bool modifiers = false;
       
   134 
       
   135             int openingBracketPos = aText.IndexOf( "(" );
       
   136             if ( openingBracketPos >= 0 )
       
   137             {
       
   138                 int closingBracketPos = openingBracketPos;
       
   139                 SymbianUtils.Strings.StringParsingUtils.SkipToEndOfSection( ref aText, ref closingBracketPos, '(', ')' );
       
   140 
       
   141                 if ( closingBracketPos > openingBracketPos )
       
   142                 {
       
   143                     // everything here on is the modifier text;
       
   144                     string modifierText = aText.Substring( closingBracketPos + 1 );
       
   145                     modifiers = ( modifierText.Trim().Length > 0 );
       
   146                 }
       
   147             }
       
   148             //
       
   149             return modifiers;
       
   150         }
       
   151 
       
   152         private static string ExtractParameters( ref string aText )
       
   153         {
       
   154             const string KOperatorChevronText = "operator <<";
       
   155 
       
   156             // DoAppendFormatList<TDes16, (int)2>(T1&, const T3&, std::__va_list, T2*)
       
   157             // DoAppendFormatList<TDes16, (int)2>(T1&, TBuf<(int)256>, std::__va_list, T2*)
       
   158             // Method<TDes16>::Wibble( something )
       
   159             // Method::Wibble( RPointerArray<HBufC> )
       
   160             // RTest::operator ()(int, int, const unsigned short*)
       
   161             // TDesC16::Left(int) const
       
   162             // CObjectCon::AtL(int) const
       
   163             // User::Panic(const TDesC16&, int)
       
   164             // operator <<(RWriteStream&, const unsigned char&)
       
   165 
       
   166             // Handle special case of "operator <<" confusing matters
       
   167             string workingText = aText;
       
   168             int operatorOpeningChevronPos = aText.IndexOf( KOperatorChevronText );
       
   169             if ( operatorOpeningChevronPos >= 0 )
       
   170             {
       
   171                 aText = aText.Substring( 0, operatorOpeningChevronPos + KOperatorChevronText.Length );
       
   172                 workingText = workingText.Substring( operatorOpeningChevronPos + KOperatorChevronText.Length );
       
   173             }
       
   174             else
       
   175             {
       
   176                 aText = string.Empty;
       
   177             }
       
   178 
       
   179             string ret = string.Empty;
       
   180             //
       
   181             int closingPos = 0;
       
   182             int openingPos = 0;
       
   183             int templatePos = 0;
       
   184             //
       
   185             while ( openingPos >= 0 )
       
   186             {
       
   187                 if ( templatePos >= 0 )
       
   188                     templatePos = workingText.IndexOf( "<", templatePos );
       
   189                 openingPos = workingText.IndexOf( "(", openingPos );
       
   190 
       
   191                 if ( templatePos >= 0 && templatePos < openingPos )
       
   192                 {
       
   193                     // Template region appears before the next bracket. Skip
       
   194                     // over all characters until we hit the end of the template
       
   195                     // section
       
   196                     int endingPos = templatePos;
       
   197                     SymbianUtils.Strings.StringParsingUtils.SkipToEndOfSection( ref workingText, ref endingPos, '<', '>' );
       
   198 
       
   199                     if ( endingPos < 0 )
       
   200                     {
       
   201                         // Matching closing brace was never found - dealing with operator << ?
       
   202                         templatePos = -1;
       
   203                     }
       
   204                     else
       
   205                     {
       
   206                         // Something like DoAppendFormatList<TDes16, (int)2>(T1&, const T3&, std::__va_list, T2*) ???
       
   207                         templatePos = endingPos;
       
   208                         openingPos = endingPos;
       
   209                     }
       
   210                 }
       
   211                 else if ( openingPos >= 0 )
       
   212                 {
       
   213                     // Skipped over any template nonsense. Work backward from the end 
       
   214                     // in order to locate start of parameters.
       
   215                     closingPos = workingText.LastIndexOf( ')' );
       
   216                     openingPos = closingPos;
       
   217                     SymbianUtils.Strings.StringParsingUtils.SkipToBeginningOfSection( ref workingText, ref openingPos, '(', ')' );
       
   218 
       
   219                     string parameters = workingText.Substring( openingPos + 1, ( closingPos - openingPos ) - 1 ).Trim();
       
   220                     ret = parameters;
       
   221                     workingText = workingText.Substring( 0, openingPos + 1 ) + workingText.Substring( closingPos );
       
   222                     aText = aText + workingText;
       
   223                     break;
       
   224                 }
       
   225             }
       
   226             //
       
   227             return ret;
       
   228         }
       
   229         #endregion
       
   230 
       
   231         #region Data members
       
   232         #endregion
       
   233     }
       
   234 }