Changeset 814 in dev


Ignore:
Timestamp:
Mar 19, 2011, 1:29:12 AM (13 years ago)
Author:
イグトランス (egtra)
Message:

LexicalAnalyzer周りの修正

Location:
branches/egtra/ab5.0/abdev/BasicCompiler_Common
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/include/LexicalAnalyzer.h

    r812 r814  
    2929
    3030    // TypeDefを収集する
    31     static void AddTypeDef( TypeDefCollection &typeDefs, const NamespaceScopes &namespaceScopes, const std::string &expression, int nowLine );
     31    static void AddTypeDef( TypeDefCollection &typeDefs, const NamespaceScopes &namespaceScopes, const boost::iterator_range<char const*> &expression, int nowLine );
    3232    static void CollectTypeDefs( const char *source, TypeDefCollection &typeDefs );
    3333
     
    3535    static void AddConstEnum( Consts &consts, const NamespaceScopes &namespaceScopes, const char *buffer );
    3636    static void CollectConsts( const char *source, Consts &consts, ConstMacros &constMacros );
    37     static bool ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest );
     37    static bool ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, std::string& dest );
     38    static bool ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest, std::size_t destSize );
     39    template<std::size_t N>
     40    static bool ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char (&dest)[N] )
     41    {
     42        return ConstMacroToExpression(constMacro, parameterStr, dest, N);
     43    }
    3844
    3945    // クラスを収集する
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer.cpp

    r625 r814  
    33using namespace ActiveBasic::Compiler;
    44
    5 bool LexicalAnalyzer::CollectNamespaces( const char *source, NamespaceScopesCollection &namespaceScopesCollection )
     5using boost::numeric_cast;
     6
     7bool LexicalAnalyzer::CollectNamespaces(char const* source, NamespaceScopesCollection &namespaceScopesCollection)
    68{
    7     int i, i2;
    8     char temporary[1024];
    9 
    109    bool isSuccessful = true;
    1110
     
    1312    NamespaceScopes namespaceScopes;
    1413
    15     for(i=0;;i++){
     14    for (int i = 0; ; i++)
     15    {
    1616        if(source[i]=='\0') break;
    1717
    1818        if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
    19             for(i+=2,i2=0;;i2++,i++){
    20                 if( IsCommandDelimitation( source[i] ) ){
    21                     temporary[i2]=0;
    22                     break;
    23                 }
    24                 temporary[i2]=source[i];
     19            i+=2;
     20            char const* p = &source[i];
     21            while (!IsCommandDelimitation(source[i]))
     22            {
     23                ++i;
    2524            }
    26             namespaceScopes.push_back( temporary );
     25            namespaceScopes.push_back(std::string(p, &source[i]));
    2726
    2827            if( !namespaceScopesCollection.IsExist( namespaceScopes ) ){
     
    4645    }
    4746
    48     if( namespaceScopes.size() > 0 ){
     47    if( !namespaceScopes.empty() ){
    4948        compiler.errorMessenger.Output( 63, NULL, cp );
    5049        isSuccessful = false;
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_Class.cpp

    r803 r814  
    1515void LexicalAnalyzer::CollectClassesForNameOnly( const char *source, Classes &classes )
    1616{
    17     int i, i2;
     17    int i2;
    1818    char temporary[VN_SIZE];
    1919
     
    2525    compiler.GetNamespaceSupporter().ClearImportedNamespaces();
    2626
    27     for(i=0;;i++){
     27    for(int i=0;;i++){
    2828        if(source[i]=='\0') break;
    2929
    3030        if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
    31             for(i+=2,i2=0;;i2++,i++){
    32                 if( IsCommandDelimitation( source[i] ) ){
    33                     temporary[i2]=0;
    34                     break;
    35                 }
    36                 temporary[i2]=source[i];
    37             }
    38             namespaceScopes.push_back( temporary );
     31            i+=2;
     32            char const* p = &source[i];
     33            while (!IsCommandDelimitation(source[i]))
     34            {
     35                ++i;
     36            }
     37            namespaceScopes.push_back(std::string(p, &source[i]));
    3938
    4039            continue;
     
    5251        }
    5352        else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
    54             for(i+=2,i2=0;;i2++,i++){
    55                 if( IsCommandDelimitation( source[i] ) ){
    56                     temporary[i2]=0;
    57                     break;
    58                 }
    59                 temporary[i2]=source[i];
    60             }
    61             if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
    62             {
    63                 compiler.errorMessenger.Output(64,temporary,i );
     53            i+=2;
     54            char const* p = &source[i];
     55            while (!IsCommandDelimitation(source[i]))
     56            {
     57                ++i;
     58            }
     59            std::string s(p, &source[i]);
     60            if (!compiler.GetNamespaceSupporter().ImportsNamespace(s))
     61            {
     62                compiler.errorMessenger.Output(64, s.c_str(), i);
    6463            }
    6564
     
    15181517        else
    15191518        {
    1520             for( int i=0; i<pInterface->GetDynamicMethods().size(); i++ )
     1519            for( std::size_t i=0; i<pInterface->GetDynamicMethods().size(); i++ )
    15211520            {
    15221521                if( pInterface->GetDynamicMethods()[i]->IsAbstract() != pExpandedInterface->GetDynamicMethods()[i]->IsAbstract() )
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_Const.cpp

    r803 r814  
    8585
    8686    int i2;
    87     char temporary[1024];
    8887
    8988    // 名前空間管理
     
    9190    namespaceScopes.clear();
    9291
    93     for(int i=0;;i++){
     92    for (int i = 0; ; ++i)
     93    {
    9494        if( source[i] == '\0' ) break;
    9595
    9696        if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
    97             for(i+=2,i2=0;;i2++,i++){
    98                 if( IsCommandDelimitation( source[i] ) ){
    99                     temporary[i2]=0;
    100                     break;
    101                 }
    102                 temporary[i2]=source[i];
    103             }
    104             namespaceScopes.push_back( temporary );
     97            i+=2;
     98            char const* p = &source[i];
     99            while (!IsCommandDelimitation(source[i]))
     100            {
     101                ++i;
     102            }
     103            namespaceScopes.push_back(std::string(p, &source[i]));
    105104
    106105            continue;
     
    131130                }
    132131
     132                char const* beginTemp = &source[i];
     133                char const* endTemp = &source[i];
     134
    133135                for(i2=0;;i++,i2++){
    134136                    if(source[i]=='\"'){
    135                         temporary[i2]=source[i];
    136137                        for(i++,i2++;;i++,i2++){
    137                             temporary[i2]=source[i];
    138138                            if(source[i]=='\"') break;
    139139                        }
     
    141141                    }
    142142                    if(IsCommandDelimitation(source[i])){
    143                         temporary[i2]=0;
     143                        endTemp = beginTemp + i2;
    144144                        break;
    145145                    }
    146                     temporary[i2]=source[i];
    147146                }
    148147
    149148                //名前を取得
    150                 char name[VN_SIZE];
     149                char const* nameEnd;
    151150                for(i2=0;;i2++){
    152                     if(temporary[i2]=='\0'){
     151                    if(beginTemp[i2]=='\0'){
    153152                        compiler.errorMessenger.Output(10,"Const",cp);
    154153                        return;
    155154                    }
    156                     if(temporary[i2]=='='||temporary[i2]=='('){
    157                         name[i2]=0;
     155                    if(beginTemp[i2]=='='||beginTemp[i2]=='('){
     156                        nameEnd = beginTemp + i2;
    158157                        break;
    159158                    }
    160                     name[i2]=temporary[i2];
    161                 }
     159                }
     160                std::string name(beginTemp, nameEnd);
    162161
    163162                //重複チェック
     
    165164                    || compiler.GetObjectModule().meta.GetGlobalConsts().IsExistDuplicationKeyName( name ) )
    166165                {
    167                     compiler.errorMessenger.Output(15,name,cp);
     166                    compiler.errorMessenger.Output(15, name, cp);
    168167                    return;
    169168                }
    170169
    171                 if( temporary[i2] == '=' )
     170                if( beginTemp[i2] == '=' )
    172171                {
    173172                    // 定数
    174                     const char *expression = temporary + i2 + 1;
     173                    std::string expression(beginTemp + i2 + 1, endTemp);
    175174
    176175                    _int64 i64data;
    177176                    Type resultType;
    178                     if( StaticCalculation(false, expression, 0, &i64data, resultType) )
     177                    if( StaticCalculation(false, expression.c_str(), 0, &i64data, resultType) )
    179178                    {
    180179                        consts.Add( Symbol( namespaceScopes, name ), i64data, resultType );
     
    184183                {
    185184                    // 定数マクロ
    186                     const char *params = temporary + i2;
    187                     if( !constMacros.Add( Symbol( namespaceScopes, name ), params ) )
     185                    std::string params(beginTemp + i2, endTemp);
     186                    if( !constMacros.Add( Symbol( namespaceScopes, name ), params.c_str() ) )
    188187                    {
    189188                        compiler.errorMessenger.Output( 1, NULL, i );
     
    208207}
    209208
    210 bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest )
    211 {
    212     extern HANDLE hHeap;
    213     int i2,i3,i4,num;
     209bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest, std::size_t destSize )
     210{
     211    std::string s;
     212    auto ret = ConstMacroToExpression(constMacro, parameterStr, s);
     213    strcpy_s(dest, destSize, s.c_str());
     214    return ret;
     215}
     216
     217bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, std::string& dest )
     218{
     219    int i2,i3;
    214220    char temporary[VN_SIZE];
    215     char *pParms[MAX_PARMS];
    216     num=0;
     221    std::vector<std::string> Parms;
     222    dest.reserve(8192);
    217223    i2=0;
    218224    while(1){
    219225        i2=GetOneParameter(parameterStr,i2,temporary);
    220226
    221         pParms[num]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
    222         lstrcpy(pParms[num],temporary);
    223 
    224         num++;
     227        Parms.push_back(temporary);
     228
    225229        if(parameterStr[i2]=='\0') break;
    226230    }
    227     if( num != constMacro.GetParameters().size() ){
     231    if( Parms.size() != constMacro.GetParameters().size() ){
    228232        extern int cp;
    229         for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
    230233        compiler.errorMessenger.Output(10,constMacro.GetName().c_str(),cp);
    231         lstrcpy(dest,"0");
     234        dest = '0';
    232235        return true;
    233236    }
    234237
    235238    i2=0;
    236     i4=0;
    237239    while(1){
    238240
     
    253255        if( i3 == (int)constMacro.GetParameters().size() ){
    254256            //パラメータでないとき
    255             lstrcpy(dest+i4,temporary);
    256             i4+=lstrlen(temporary);
     257            dest += temporary;
    257258        }
    258259        else{
    259260            //パラメータのとき
    260             lstrcpy(dest+i4,pParms[i3]);
    261             i4+=lstrlen(pParms[i3]);
     261            dest += Parms[i3];
    262262        }
    263263
    264264        //演算子をコピー
    265         for(;;i2++,i4++){
     265        for(;;i2++){
    266266            if( constMacro.GetExpression()[i2] == 1 ){
    267                 dest[i4++] = constMacro.GetExpression()[i2++];
    268                 dest[i4] = constMacro.GetExpression()[i2];
     267                dest += constMacro.GetExpression()[i2++];
     268                dest += constMacro.GetExpression()[i2];
    269269                continue;
    270270            }
    271271            if(IsVariableTopChar( constMacro.GetExpression()[i2] )) break;
    272             dest[i4] = constMacro.GetExpression()[i2];
     272            dest += constMacro.GetExpression()[i2];
    273273            if( constMacro.GetExpression()[i2] == '\0' ) break;
    274274        }
     
    277277    }
    278278
    279     for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
    280 
    281279    return true;
    282280}
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_Delegate.cpp

    r803 r814  
    55void LexicalAnalyzer::CollectDelegates( const char *source, Delegates &delegates )
    66{
    7     int i2;
    8     char temporary[VN_SIZE];
    9 
    107    // 名前空間管理
    118    NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
     
    1512    compiler.GetNamespaceSupporter().ClearImportedNamespaces();
    1613
    17     int length = lstrlen( source );
    18     for( int i=0; i<length; i++ )
     14    for (int i=0; source[i] != '\0'; i++)
    1915    {
    2016        if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
    21             for(i+=2,i2=0;;i2++,i++){
    22                 if( IsCommandDelimitation( source[i] ) ){
    23                     temporary[i2]=0;
    24                     break;
    25                 }
    26                 temporary[i2]=source[i];
    27             }
    28             namespaceScopes.push_back( temporary );
     17            i+=2;
     18            char const* p = &source[i];
     19            while (!IsCommandDelimitation(source[i]))
     20            {
     21                ++i;
     22            }
     23            namespaceScopes.push_back(std::string(p, &source[i]));
    2924
    3025            continue;
     
    4237        }
    4338        else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
    44             for(i+=2,i2=0;;i2++,i++){
    45                 if( IsCommandDelimitation( source[i] ) ){
    46                     temporary[i2]=0;
    47                     break;
    48                 }
    49                 temporary[i2]=source[i];
    50             }
    51             if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
    52             {
    53                 compiler.errorMessenger.Output(64,temporary,i );
     39            i+=2;
     40            char const* p = &source[i];
     41            while (!IsCommandDelimitation(source[i]))
     42            {
     43                ++i;
     44            }
     45            std::string s(p, &source[i]);
     46            if (!compiler.GetNamespaceSupporter().ImportsNamespace(s))
     47            {
     48                compiler.errorMessenger.Output(64, s.c_str(), i);
    5449            }
    5550
     
    112107                {
    113108                    compiler.errorMessenger.Output(-104,name,nowLine);
    114                     lstrcpy( returnTypeName, "Double" );
     109                    strcpy( returnTypeName, "Double" );
    115110                }
    116111            }
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_Procedure.cpp

    r750 r814  
    913913
    914914        if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
    915             for(i+=2,i2=0;;i2++,i++){
    916                 if( IsCommandDelimitation( source[i] ) ){
    917                     temporary[i2]=0;
    918                     break;
    919                 }
    920                 temporary[i2]=source[i];
    921             }
    922             namespaceScopes.push_back( temporary );
     915            i+=2;
     916            char const* p = &source[i];
     917            while (!IsCommandDelimitation(source[i]))
     918            {
     919                ++i;
     920            }
     921            namespaceScopes.push_back(std::string(p, &source[i]));
    923922
    924923            continue;
     
    936935        }
    937936        else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
    938             for(i+=2,i2=0;;i2++,i++){
    939                 if( IsCommandDelimitation( source[i] ) ){
    940                     temporary[i2]=0;
    941                     break;
    942                 }
    943                 temporary[i2]=source[i];
    944             }
    945             if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
    946             {
    947                 compiler.errorMessenger.Output(64,temporary,cp );
     937            i+=2;
     938            char const* p = &source[i];
     939            while (!IsCommandDelimitation(source[i]))
     940            {
     941                ++i;
     942            }
     943            std::string s(p, &source[i]);
     944            if (!compiler.GetNamespaceSupporter().ImportsNamespace(s))
     945            {
     946                compiler.errorMessenger.Output(64, s.c_str(), i);
    948947            }
    949948
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_TypeDef.cpp

    r812 r814  
    44#include "Compiler.h"
    55#include "StrOperation.h"
     6#include <boost/algorithm/string/predicate.hpp>
    67
    78using namespace ActiveBasic::Compiler;
    89
    9 void LexicalAnalyzer::AddTypeDef( TypeDefCollection &typeDefs, const NamespaceScopes &namespaceScopes, const std::string &expression, int nowLine )
     10void LexicalAnalyzer::AddTypeDef( TypeDefCollection &typeDefs, const NamespaceScopes &namespaceScopes, const boost::iterator_range<char const*> &expression, int nowLine )
    1011{
    11     int i;
    12     char temporary[VN_SIZE];
     12    auto name = boost::find<boost::return_begin_found>(expression, '=');
    1313
    14     for(i=0;;i++){
    15         if(expression[i]=='='||expression[i]=='\0'){
    16             temporary[i]=0;
    17             break;
    18         }
    19         temporary[i]=expression[i];
    20     }
    21 
    22     if(expression[i]!='='){
    23         compiler.errorMessenger.Output(10,"TypeDef",nowLine);
     14    if (boost::end(name) == boost::end(expression))
     15    {
     16        compiler.errorMessenger.Output(10, "TypeDef", nowLine);
    2417        return;
    2518    }
    2619
    27     const char *pTemp=expression.c_str()+i+1;
     20    const char *pTemp = expression.begin() + boost::size(name) + 1;
    2821
    2922    //識別文字のエラーチェック(新しい型)
    30     i=0;
    31     for(;;i++){
    32         if(temporary[i]=='\0') break;
    33         if( !( IsVariableChar( temporary[i], true) ) ){
    34             compiler.errorMessenger.Output(10,"TypeDef",nowLine);
    35             return;
    36         }
     23    if (!boost::algorithm::all(name, [](char c) {return IsVariableChar(c, true);}))
     24    {
     25        compiler.errorMessenger.Output(10, "TypeDef", nowLine);
     26        return;
    3727    }
    3828
     
    4535        }
    4636    }
    47     else{
    48         i=0;
     37    else
     38    {
     39        int i=0;
    4940        while(pTemp[i]=='*') i++;
    50         for(;;i++){
    51             if(pTemp[i]=='\0') break;
    52             if( !( IsVariableChar( pTemp[i], true) ) )
    53             {
    54                 compiler.errorMessenger.Output(10,"TypeDef",nowLine);
    55                 return;
    56             }
     41        if (!boost::algorithm::all(name, [](char c) {return IsVariableChar(c, true);}))
     42        {
     43            compiler.errorMessenger.Output(10,"TypeDef",nowLine);
     44            return;
    5745        }
    5846    }
    5947
     48    std::string baseName(pTemp, expression.end());
     49
    6050    //識別子が重複している場合はエラーにする
    61     if(lstrcmp(temporary,pTemp)==0){
     51    if (boost::algorithm::equals(name, baseName))
     52    {
    6253        compiler.errorMessenger.Output(1,NULL,nowLine);
    6354        return;
     
    7162
    7263    Type baseType;
    73     if( !compiler.StringToType( pTemp, baseType ) )
     64    if( !compiler.StringToType( baseName, baseType ) )
    7465    {
    75         compiler.errorMessenger.Output(3, pTemp, nowLine );
     66        compiler.errorMessenger.Output(3, baseName, nowLine );
    7667        return;
    7768    }
     
    7970    typeDefs.push_back(
    8071        TypeDef(
    81             Symbol( namespaceScopes, temporary ),
    82             pTemp,
     72            Symbol(namespaceScopes, boost::copy_range<std::string>(name)),
     73            baseName,
    8374            baseType
    8475        )
     
    9485    compiler.GetNamespaceSupporter().ClearImportedNamespaces();
    9586
    96     int i=-1, i2;
    97     char temporary[VN_SIZE];
    98     while(1){
    99 
    100         i++;
    101 
     87    for (int i = 0; ; ++i)
     88    {
    10289        if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
    103             for(i+=2,i2=0;;i2++,i++){
    104                 if( IsCommandDelimitation( source[i] ) ){
    105                     temporary[i2]=0;
    106                     break;
    107                 }
    108                 temporary[i2]=source[i];
     90            i+=2;
     91            char const* p = &source[i];
     92            while (!IsCommandDelimitation(source[i]))
     93            {
     94                ++i;
    10995            }
    110             namespaceScopes.push_back( temporary );
     96            namespaceScopes.push_back(std::string(p, &source[i]));
    11197
    11298            continue;
     
    124110        }
    125111        else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
    126             for(i+=2,i2=0;;i2++,i++){
    127                 if( IsCommandDelimitation( source[i] ) ){
    128                     temporary[i2]=0;
    129                     break;
    130                 }
    131                 temporary[i2]=source[i];
     112            i+=2;
     113            char const* p = &source[i];
     114            while (!IsCommandDelimitation(source[i]))
     115            {
     116                ++i;
    132117            }
    133             if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
     118            std::string s(p, &source[i]);
     119            if (!compiler.GetNamespaceSupporter().ImportsNamespace(s))
    134120            {
    135                 compiler.errorMessenger.Output(64,temporary,i );
     121                compiler.errorMessenger.Output(64, s.c_str(), i);
    136122            }
    137123
     
    148134            char temporary[VN_SIZE];
    149135            if(source[i+1]==ESC_TYPEDEF){
    150                 int i2 = 0;
    151                 for(i+=2;;i2++,i++){
    152                     if(source[i]=='\n'){
    153                         temporary[i2]=0;
    154                         break;
    155                     }
    156                     temporary[i2]=source[i];
    157                     if(source[i]=='\0') break;
     136                i+=2;
     137                char const* p = &source[i];
     138                while (!IsCommandDelimitation(source[i]))
     139                {
     140                    ++i;
    158141                }
    159                 AddTypeDef( typeDefs, namespaceScopes, temporary, i );
    160 
     142                AddTypeDef(typeDefs, namespaceScopes, boost::make_iterator_range(p, &source[i]), i);
    161143                continue;
    162144            }
     
    172154                }
    173155
    174                 Type baseType;
    175                 if( !compiler.StringToType( "Long", baseType ) )
    176                 {
    177                     throw;
    178                 }
    179 
    180156                typeDefs.push_back(
    181157                    TypeDef(
    182158                        Symbol( namespaceScopes, temporary ),
    183159                        "Long",
    184                         baseType
     160                        Type(DEF_LONG)
    185161                    )
    186162                );
Note: See TracChangeset for help on using the changeset viewer.