commsfwtools/commstools/utracedecoder/src/messagedefparser/contextprocessor.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "messagedefparser\contextprocessor.h"
       
    17 #include "messagedefparser\initialstate.h"
       
    18 #include "messagedefparser\definitiontokenizer.h"
       
    19 #include "messagedefparser\integeridentifier.h"
       
    20 #include "util.h"
       
    21 
       
    22 namespace Parser
       
    23 {
       
    24 
       
    25 void CContextProcessor::ProcessState()
       
    26     {
       
    27     Tokens::TTokenType tokenType = CurrentTokenType();
       
    28     iResult = Parser::EUnexpectedToken;
       
    29     const CIdentifierBase* ident = NULL;
       
    30 
       
    31     switch (iInternalState)
       
    32         {
       
    33         case EStateExpectContextIdentifier:
       
    34             if (tokenType == Tokens::EIdentifier)
       
    35                 {
       
    36                 CreateTempContext();
       
    37                 }
       
    38             break;
       
    39 
       
    40         case EStateExpectBaseOrFirstMember:
       
    41             iResult = Parser::ENoError; // default to ok - most cases here will succeed
       
    42             switch (tokenType)
       
    43                 {
       
    44                 case Tokens::EColon:
       
    45                     iInternalState = EStateExpectBaseIdentifier;
       
    46                     break;
       
    47 
       
    48                 case Tokens::EIdentifier:
       
    49                 case Tokens::EIntType:
       
    50                 case Tokens::EPadType:
       
    51                 case Tokens::EMessageIdType:
       
    52                     CreateTempMember();
       
    53                     break;
       
    54 
       
    55                 default:
       
    56                     iResult = Parser::EUnexpectedToken;
       
    57                 }
       
    58             break;
       
    59 
       
    60         case EStateExpectBaseIdentifier:
       
    61             iInternalState = EStateExpectMoreMembersOrEnd;
       
    62             if (tokenType == Tokens::EIdentifier)
       
    63                 {
       
    64                 ident = ParserSM().FindIdentifier(CurrentToken());
       
    65                 if (!ident)
       
    66                     {
       
    67                     iResult = Parser::EUnknownIdentifier;
       
    68                     }
       
    69                 else if (ident->Type() == Parser::EContextIdentifier)
       
    70                     {
       
    71                     iIdentifier->SetBaseContext(static_cast<const CContextIdentifier*>(ident));
       
    72                     iResult = Parser::ENoError;
       
    73                     }
       
    74                 else
       
    75                     {
       
    76                     iResult = Parser::EInvalidType;
       
    77                     }
       
    78                 }
       
    79             break;
       
    80 
       
    81         case EStateExpectMemberNameOrFormat:
       
    82             switch (tokenType)
       
    83                 {
       
    84                 case Tokens::EIdentifier:
       
    85                     if (iIdentifier->FindMember(CurrentToken()))
       
    86                         {
       
    87                         iResult = Parser::EDuplicateIdentifier;
       
    88                         delete iTempMember;
       
    89                         }
       
    90                     else
       
    91                         {
       
    92                         iTempMember->iMemberName = _strdup(CurrentToken());
       
    93                         iIdentifier->AddMember(iTempMember);
       
    94                         iResult = Parser::ENoError;
       
    95                         }
       
    96                     iInternalState = EStateExpectMoreMembersOrEnd;
       
    97                     iTempMember = NULL;
       
    98                     break;
       
    99 
       
   100                 case Tokens::EDisplayDec:
       
   101                 case Tokens::EDisplayHex:
       
   102                     iTempMember->iIdentifierOptions = new TIntegerIdentifierOptions(tokenType == Tokens::EDisplayHex);
       
   103                     iInternalState = EStateExpectMemberName;
       
   104                     iResult = Parser::ENoError;
       
   105                     break;
       
   106                 }
       
   107             break;
       
   108 
       
   109         case EStateExpectMemberName:
       
   110             if (tokenType == Tokens::EIdentifier)
       
   111                 {
       
   112                 if (iIdentifier->FindMember(CurrentToken()))
       
   113                     {
       
   114                     iResult = Parser::EDuplicateIdentifier;
       
   115                     delete iTempMember;
       
   116                     }
       
   117                 else
       
   118                     {
       
   119                     iTempMember->iMemberName = _strdup(CurrentToken());
       
   120                     iIdentifier->AddMember(iTempMember);
       
   121                     iResult = Parser::ENoError;
       
   122                     }
       
   123                 iInternalState = EStateExpectMoreMembersOrEnd;
       
   124                 iTempMember = NULL;
       
   125                 }
       
   126             break;
       
   127 
       
   128         case EStateExpectMoreMembersOrEnd:
       
   129             iResult = Parser::ENoError;
       
   130             switch (tokenType)
       
   131                 {
       
   132                 case Tokens::EEnd:
       
   133                     if (iIdentifier->iMembers.size() > 0)
       
   134                         {
       
   135                         iInternalState = EStateExpectEndContext;
       
   136                         }
       
   137                     else
       
   138                         {
       
   139                         iResult = Parser::EUnexpectedToken;
       
   140                         }
       
   141                     break;
       
   142 
       
   143                 case Tokens::EIdentifier:
       
   144                 case Tokens::EIntType:
       
   145                 case Tokens::EPadType:
       
   146                 case Tokens::EMessageIdType:
       
   147                     CreateTempMember();
       
   148                     break;
       
   149 
       
   150                 default:
       
   151                     iResult = Parser::EUnexpectedToken;
       
   152                 }
       
   153             break;
       
   154 
       
   155         case EStateExpectEndContext:
       
   156             if (tokenType == Tokens::EContext)
       
   157                 {
       
   158                 if (ParserSM().FindContextBySize(iIdentifier->TotalSize()))
       
   159                     {
       
   160                     iResult = Parser::EDuplicateIdentifier;
       
   161                     }
       
   162                 else
       
   163                     {
       
   164                     iResult = Parser::ENoError;
       
   165                     ParserSM().AddIdentifier(iIdentifier);
       
   166                     iIdentifier = NULL;
       
   167                     }
       
   168                 }
       
   169             ParserSM().SetState(new CInitialState(ParserSM()));
       
   170             break;
       
   171 
       
   172         case EStateExpectPadSize:
       
   173             {
       
   174             iResult = Parser::ENoError;
       
   175             iInternalState = EStateExpectMoreMembersOrEnd;
       
   176             unsigned int v = 0;
       
   177             if (tokenType == Tokens::ENumberHex)
       
   178                 {
       
   179                 v = HexToVal(CurrentToken());
       
   180                 }
       
   181             else if (tokenType == Tokens::ENumberDec)
       
   182                 {
       
   183                 v = atol(CurrentToken());
       
   184                 }
       
   185             else
       
   186                 {
       
   187                 iResult = Parser::EUnexpectedToken;
       
   188                 delete iTempMember;
       
   189                 }
       
   190             if (iResult == Parser::ENoError)
       
   191                 {
       
   192                 if (v >= 1)
       
   193                     {
       
   194                     iTempMember->iArraySize = v;
       
   195                     iIdentifier->AddMember(iTempMember);
       
   196                     }
       
   197                 else
       
   198                     {
       
   199                     iResult = Parser::EValueOutOfRange;
       
   200                     delete iTempMember;
       
   201                     }
       
   202                 }
       
   203             iTempMember = NULL;
       
   204             }
       
   205             break;
       
   206         }
       
   207 
       
   208     if (iResult != Parser::ENoError)
       
   209         {
       
   210         ParserSM().SetError(iResult);
       
   211         }
       
   212     }
       
   213 
       
   214 void CContextProcessor::CreateTempContext()
       
   215     {
       
   216     if (!ParserSM().FindIdentifier(CurrentToken()))
       
   217         {
       
   218         iIdentifier = new CContextIdentifier(CurrentToken());
       
   219         iInternalState = EStateExpectBaseOrFirstMember;
       
   220         iResult = Parser::ENoError;
       
   221         }
       
   222     else
       
   223         {
       
   224         iResult = Parser::EDuplicateIdentifier;
       
   225         }
       
   226     }
       
   227 
       
   228 void CContextProcessor::CreateTempMember()
       
   229     {
       
   230     // Expect type of the member - can be a builtin (PAD, UINTx, INTx, TMESSAGEID)
       
   231     // or an identifier of a predefined struct/enum
       
   232     const CIdentifierBase* ident = ParserSM().FindIdentifier(CurrentToken());
       
   233     if (!ident) 
       
   234         {
       
   235         iResult = EUnknownIdentifier;
       
   236         }
       
   237     else if (ident->Type() == EEnumTypeIdentifier || ident->Type() == EStructIdentifier
       
   238         || ident->Type() == EIntegerTypeIdentifier || ident->Type() == Parser::EMessageIdTypeIdentifier)
       
   239         {
       
   240         iTempMember = new TMember();
       
   241         iTempMember->iMemberType = ident;
       
   242 
       
   243         if (ident->Type() == EIntegerTypeIdentifier)
       
   244             {
       
   245             iInternalState = EStateExpectMemberNameOrFormat;
       
   246             }
       
   247         else
       
   248             {
       
   249             iInternalState = EStateExpectMemberName;
       
   250             }
       
   251         }
       
   252     else if (ident->Type() == EPadTypeIdentifier)
       
   253         {
       
   254         iTempMember = new TMember();
       
   255         iTempMember->iMemberType = ident;
       
   256         iInternalState = EStateExpectPadSize;
       
   257         }
       
   258     else
       
   259         {
       
   260         iResult = Parser::EInvalidType;
       
   261         }
       
   262     }
       
   263 
       
   264 } // namespace Parser
       
   265