crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianParserLib/RegExTranslators/Info/RegExTranslatorExtractionInfo.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 System.Text.RegularExpressions;
       
    21 using System.Reflection;
       
    22 using SymbianParserLib.Enums;
       
    23 
       
    24 namespace SymbianParserLib.RegExTranslators
       
    25 {
       
    26     internal class RegExTranslatorExtractionInfo
       
    27     {
       
    28         #region Constructors
       
    29         public RegExTranslatorExtractionInfo( string aLine, int aStartPos )
       
    30         {
       
    31             iMatch = KRegEx.Match( aLine, aStartPos );
       
    32         }
       
    33         #endregion
       
    34 
       
    35         #region API
       
    36         #endregion
       
    37 
       
    38         #region Constants
       
    39         public const int KNoWidthSpecified = -1;
       
    40         #endregion
       
    41 
       
    42         #region Properties
       
    43         public bool Success
       
    44         {
       
    45             get { return Match.Success; }
       
    46         }
       
    47 
       
    48         public Match Match
       
    49         {
       
    50             get { return iMatch; }
       
    51         }
       
    52 
       
    53         public char ValueTypeChar
       
    54         {
       
    55             get
       
    56             {
       
    57                 char ret = '\0';
       
    58                 //
       
    59                 if ( iMatch.Success )
       
    60                 {
       
    61                     Group gpType = iMatch.Groups[ KGroupTypeChar ];
       
    62                     if ( gpType.Success )
       
    63                     {
       
    64                         string type = gpType.Value.ToUpper();
       
    65                         ret = type[ 0 ];
       
    66                     }
       
    67                 }
       
    68                 //
       
    69                 return ret;
       
    70             }
       
    71         }
       
    72 
       
    73         public TParserValueType ValueType
       
    74         {
       
    75             get
       
    76             {
       
    77                 TParserValueType ret = TParserValueType.EValueTypeUnknown;
       
    78                 //
       
    79                 if ( iMatch.Success )
       
    80                 {
       
    81                     char typeChar = ValueTypeChar;
       
    82                     switch ( typeChar )
       
    83                     {
       
    84                     case 'U':
       
    85                         ret = TParserValueType.EValueTypeUint32;
       
    86                         break;
       
    87                     case 'S':
       
    88                         ret = TParserValueType.EValueTypeString;
       
    89                         break;
       
    90                     case 'D':
       
    91                         ret = TParserValueType.EValueTypeInt32;
       
    92                         break;
       
    93                     case 'X':
       
    94                         if ( IsLong )
       
    95                         {
       
    96                             ret = TParserValueType.EValueTypeUint64;
       
    97                         }
       
    98                         else
       
    99                         {
       
   100                             ret = TParserValueType.EValueTypeUint32;
       
   101                         }
       
   102                         break;
       
   103                     default:
       
   104                         break;
       
   105                     }
       
   106                 }
       
   107                 //
       
   108                 return ret;
       
   109             }
       
   110         }
       
   111 
       
   112         public string ValuePrefix
       
   113         {
       
   114             get
       
   115             {
       
   116                 string ret = string.Empty;
       
   117                 //
       
   118                 if ( iMatch.Success )
       
   119                 {
       
   120                     Group gpValuePrefix = iMatch.Groups[ KGroupValuePrefix ];
       
   121                     if ( gpValuePrefix.Success )
       
   122                     {
       
   123                         ret = gpValuePrefix.Value;
       
   124                     }
       
   125                 }
       
   126                 //
       
   127                 return ret;
       
   128             }
       
   129         }
       
   130 
       
   131         public bool IsValuePrefixHex
       
   132         {
       
   133             get
       
   134             {
       
   135                 string prefix = ValuePrefix;
       
   136                 bool ret = ( prefix == KValuePrefixHex1 || prefix == KValuePrefixHex2 );
       
   137                 return ret;
       
   138             }
       
   139         }
       
   140 
       
   141         public int NumberBase
       
   142         {
       
   143             get
       
   144             {
       
   145                 int ret = 0;
       
   146                 //
       
   147                 if ( iMatch.Success )
       
   148                 {
       
   149                     if ( ValueType != TParserValueType.EValueTypeString )
       
   150                     {
       
   151                         char typeChar = ValueTypeChar;
       
   152                         switch ( typeChar )
       
   153                         {
       
   154                             case 'U':
       
   155                             case 'D':
       
   156                                 ret = KNumberBaseDecimal;
       
   157                                 break;
       
   158                             case 'X':
       
   159                                 ret = KNumberBaseHexadecimal;
       
   160                                 break;
       
   161                             default:
       
   162                                 break;
       
   163                         }
       
   164                     }
       
   165                 }
       
   166                 //
       
   167                 return ret;
       
   168             }
       
   169         }
       
   170 
       
   171         public bool IsLong
       
   172         {
       
   173             get
       
   174             {
       
   175                 // The 'long' (L) specifier gets shoehorned into the width group
       
   176                 bool ret = false;
       
   177                 //
       
   178                 if ( iMatch.Success )
       
   179                 {
       
   180                     Group gpWidth = iMatch.Groups[ KGroupWidth ];
       
   181                     if ( gpWidth.Success )
       
   182                     {
       
   183                         string val = gpWidth.Value.Trim().ToUpper();
       
   184                         ret = ( val == "L" );
       
   185                     }
       
   186                 }
       
   187                 //
       
   188                 return ret;
       
   189             }
       
   190         }
       
   191 
       
   192         public int Width
       
   193         {
       
   194             get
       
   195             {
       
   196                 int ret = KNoWidthSpecified;
       
   197                 //
       
   198                 if ( iMatch.Success )
       
   199                 {
       
   200                     Group gpWidth = iMatch.Groups[ KGroupWidth ];
       
   201                     if ( gpWidth.Success )
       
   202                     {
       
   203                         string val = gpWidth.Value.Trim();
       
   204                         if ( val.Length != 0 )
       
   205                         {
       
   206                             Match m = KNumericNumberRegex.Match( val );
       
   207                             if ( m.Success )
       
   208                             {
       
   209                                 ret = System.Convert.ToInt32( m.Value );
       
   210                             }
       
   211                         }
       
   212                     }
       
   213                 }
       
   214                 //
       
   215                 return ret;
       
   216             }
       
   217         }
       
   218 
       
   219         public int CapturePos
       
   220         {
       
   221             get
       
   222             {
       
   223                 Group gpPercent = Match.Groups[ KGroupPercent ];
       
   224                 int pos = gpPercent.Index;
       
   225                 return pos;
       
   226             }
       
   227         }
       
   228 
       
   229         public int CaptureLength
       
   230         {
       
   231             get
       
   232             {
       
   233                 // [StartPos] ..... [% Pos] .... [EndPos]
       
   234                 //                  <------------------->       = what we return
       
   235 
       
   236                 int startPos = Match.Index;
       
   237                 int length = Match.Length;
       
   238                 int endPos = startPos + length;
       
   239                 int percentPos = CapturePos;
       
   240                 int ret = ( endPos - percentPos );
       
   241                 return ret;
       
   242             }
       
   243         }
       
   244 
       
   245         public string Name
       
   246         {
       
   247             get
       
   248             {
       
   249                 string ret = string.Empty;
       
   250                 //
       
   251                 if ( Success )
       
   252                 {
       
   253                     Group propertyName = Match.Groups[ KGroupPropertyName ];
       
   254                     ret = propertyName.Value.Trim();
       
   255                 }
       
   256                 //
       
   257                 return ret;
       
   258             }
       
   259         }
       
   260 
       
   261         public string PadChar
       
   262         {
       
   263             get
       
   264             {
       
   265                 string ret = string.Empty;
       
   266                 //
       
   267                 if ( Success )
       
   268                 {
       
   269                     Group pad = Match.Groups[ KGroupPadChar ];
       
   270                     ret = pad.Value.Trim();
       
   271                 }
       
   272                 //
       
   273                 return ret;
       
   274             }
       
   275         }
       
   276         #endregion
       
   277 
       
   278         #region Internal methods
       
   279         #endregion
       
   280 
       
   281         #region Internal constants
       
   282         private const string KGroupValuePrefix = "ValuePrefix";
       
   283         private const string KGroupPropertyName = "PropertyName";
       
   284         private const string KGroupPadChar = "PadChar";
       
   285         private const string KGroupWidth = "Width";
       
   286         private const string KGroupTypeChar = "TypeChar";
       
   287         private const string KGroupPercent = "Percent";
       
   288         private const string KValuePrefixHex1 = "0x";
       
   289         private const string KValuePrefixHex2 = "x";
       
   290         private const int KNumberBaseDecimal = 10;
       
   291         private const int KNumberBaseHexadecimal = 16;
       
   292         #endregion
       
   293 
       
   294         #region From System.Object
       
   295         public override string ToString()
       
   296         {
       
   297             return base.ToString();
       
   298         }
       
   299         #endregion
       
   300 
       
   301         #region Data members
       
   302         /// <summary>
       
   303         ///  Regular expression built for C# on: Tue, May 20, 2008, 12:54:34 PM
       
   304         ///  Using Expresso Version: 3.0.2766, http://www.ultrapico.com
       
   305         ///  
       
   306         ///  A description of the regular expression:
       
   307         ///  
       
   308         ///  [PropertyName]: A named capture group. [(?:\w+\s*)+?], zero or one repetitions
       
   309         ///      Match expression but don't capture it. [\w+\s*], one or more repetitions, as few as possible
       
   310         ///          \w+\s*
       
   311         ///              Alphanumeric, one or more repetitions
       
   312         ///              Whitespace, any number of repetitions
       
   313         ///  Comment: Property name, essentially captures words separated by whitespace
       
   314         ///  Match expression but don't capture it. [\s*]
       
   315         ///      Whitespace, any number of repetitions
       
   316         ///  Comment: More whitespace
       
   317         ///  Match expression but don't capture it. [-], zero or one repetitions
       
   318         ///      -
       
   319         ///  Comment: Ignore any leading minus char
       
   320         ///  Match expression but don't capture it. [\s*]
       
   321         ///      Whitespace, any number of repetitions
       
   322         ///  Comment: More whitespace
       
   323         ///  Comment: Next parse and discard any optional assignment prefix - such a ";" or "="
       
   324         ///  Match expression but don't capture it. [\s*]
       
   325         ///      Whitespace, any number of repetitions
       
   326         ///  Match expression but don't capture it. [=|\x3A], zero or one repetitions
       
   327         ///      Select from 2 alternatives
       
   328         ///          =
       
   329         ///          Hex 3A
       
   330         ///  Match expression but don't capture it. [\s*]
       
   331         ///      Whitespace, any number of repetitions
       
   332         ///  [ValuePrefix]: A named capture group. [0x|x], zero or one repetitions
       
   333         ///      Select from 2 alternatives
       
   334         ///          0x
       
   335         ///              0x
       
   336         ///          x
       
   337         ///  Match expression but don't capture it. [%{1}]
       
   338         ///      %, exactly 1 repetitions
       
   339         ///  [PadChar]: A named capture group. [(?:0| )?]
       
   340         ///      Match expression but don't capture it. [0| ], zero or one repetitions
       
   341         ///          Select from 2 alternatives
       
   342         ///              0
       
   343         ///              NULL
       
   344         ///  [Width]: A named capture group. [(?:[0-9]?|l)]
       
   345         ///      Match expression but don't capture it. [[0-9]?|l]
       
   346         ///          Select from 2 alternatives
       
   347         ///              Any character in this class: [0-9], zero or one repetitions
       
   348         ///              l
       
   349         ///  [TypeChar]: A named capture group. [d|D|x|X|u|U|s|S]
       
   350         ///      Select from 8 alternatives
       
   351         ///          dDxXuUsS
       
   352         ///  
       
   353         ///
       
   354         /// </summary>
       
   355         private static readonly Regex KRegEx = new Regex(
       
   356               "(?<PropertyName>(?:\\w+\\s*)+?)? # Property name, essentiall" +
       
   357               "y captures words separated by whitespace\r\n(?:\\s*) # More wh" +
       
   358               "itespace\r\n(?:-)? # Ignore any leading minus char\r\n(?:\\s*) #" +
       
   359               " More whitespace\r\n\r\n# Next parse and discard any optional as" +
       
   360               "signment prefix - such a \";\" or \"=\"\r\n(?:\\s*)(?:=|\\x3A)" +
       
   361               "?(?:\\s*) \r\n\r\n(?<ValuePrefix>0x|x)?(?<Percent>%{1})\r\n\r\n(?<PadChar>(?" +
       
   362               ":0| )?)(?<Width>(?:[0-9]?|l))(?<TypeChar>d|D|x|X|u|U|s|S|i|I)",
       
   363             RegexOptions.CultureInvariant
       
   364             | RegexOptions.IgnorePatternWhitespace
       
   365             | RegexOptions.Compiled
       
   366             );
       
   367 
       
   368         private static readonly Regex KNumericNumberRegex = new Regex( @"[0-9]", RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled );
       
   369         private readonly Match iMatch;
       
   370         #endregion
       
   371     }
       
   372 }