diff options
author | Adrian Thurston <thurston@complang.org> | 2009-02-25 03:18:23 +0000 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2009-02-25 03:18:23 +0000 |
commit | 0ab005c28a931a20aec401cd6730bed72cc64c42 (patch) | |
tree | 02184a30ed1e5c7c7e93429e5fdcd684579e4398 | |
parent | 9edcc5097c9555169076aa8271c158dfd5bbaaca (diff) | |
download | colm-0ab005c28a931a20aec401cd6730bed72cc64c42.tar.gz |
cleaned up a large function
-rw-r--r-- | colm/parsedata.cpp | 235 | ||||
-rw-r--r-- | colm/parsedata.h | 3 |
2 files changed, 125 insertions, 113 deletions
diff --git a/colm/parsedata.cpp b/colm/parsedata.cpp index 4d91fbaa..b7b3e526 100644 --- a/colm/parsedata.cpp +++ b/colm/parsedata.cpp @@ -1127,152 +1127,161 @@ void ParseData::resolveLiteralFactor( PdaFactor *fact ) fact->langEl = tokenDef->token; } -void ParseData::resolveReferenceFactor( PdaFactor *fact ) +KlangEl *ParseData::makeRepeatProd( Namespace *nspace, const String &repeatName, PdaFactor *fact ) { - /* Look for the production's associated region. */ - Namespace *nspace = fact->nspaceQual->getQual( this ); + KlangEl *prodName = getKlangEl( this, nspace, repeatName ); + prodName->type = KlangEl::NonTerm; + prodName->isRepeat = true; - if ( nspace == 0 ) - error(fact->loc) << "do not have namespace for resolving reference" << endp; - - fact->nspace = nspace; + ProdElList *prodElList1 = new ProdElList; - /* Look up the language element in the region. */ - KlangEl *langEl = getKlangEl( this, nspace, fact->refName ); + /* Build the first production of the repeat. */ + PdaFactor *factor1 = new PdaFactor( InputLoc(), false, fact->nspaceQual, + fact->refName, 0, RepeatNone, false, false ); + PdaFactor *factor2 = new PdaFactor( InputLoc(), false, fact->nspaceQual, + repeatName, 0, RepeatNone, false, false ); - if ( fact->repeatType == RepeatOpt ) { - /* If the factor is an opt, create the opt element and link the factor - * to it. */ - String optName( 32, "_opt_%s", fact->refName.data ); + prodElList1->append( factor1 ); + prodElList1->append( factor2 ); - SymbolMapEl *inDict = nspace->symbolMap.find( optName ); - if ( inDict != 0 ) { - fact->langEl = inDict->value; - } - else { - KlangEl *prodName = getKlangEl( this, nspace, optName ); - prodName->type = KlangEl::NonTerm; - prodName->isOpt = true; + Definition *newDef1 = new Definition( InputLoc(), + prodName, prodElList1, false, 0, + prodList.length(), Definition::Production ); - ProdElList *prodElList1 = new ProdElList; + prodName->defList.append( newDef1 ); + prodList.append( newDef1 ); - /* Build the first production of the repeat. */ - PdaFactor *factor1 = new PdaFactor( InputLoc(), false, fact->nspaceQual, - fact->refName, 0, RepeatNone, false, false ); - prodElList1->append( factor1 ); + /* Build the second production of the repeat. */ + ProdElList *prodElList2 = new ProdElList; - Definition *newDef1 = new Definition( InputLoc(), - prodName, prodElList1, false, 0, - prodList.length(), Definition::Production ); + Definition *newDef2 = new Definition( InputLoc(), + prodName, prodElList2, false, 0, + prodList.length(), Definition::Production ); - prodName->defList.append( newDef1 ); - prodList.append( newDef1 ); + prodName->defList.append( newDef2 ); + prodList.append( newDef2 ); - /* Build the second production of the repeat. */ - ProdElList *prodElList2 = new ProdElList; + return prodName; +} - Definition *newDef2 = new Definition( InputLoc(), - prodName, prodElList2, false, 0, - prodList.length(), Definition::Production ); +KlangEl *ParseData::makeListProd( Namespace *nspace, const String &listName, PdaFactor *fact ) +{ + KlangEl *prodName = getKlangEl( this, nspace, listName ); + prodName->type = KlangEl::NonTerm; + prodName->isList = true; - prodName->defList.append( newDef2 ); - prodList.append( newDef2 ); + /* Build the first production of the list. */ + PdaFactor *factor1 = new PdaFactor( InputLoc(), false, fact->nspaceQual, + fact->refName, 0, RepeatNone, false, false ); + PdaFactor *factor2 = new PdaFactor( InputLoc(), false, fact->nspaceQual, + listName, 0, RepeatNone, false, false ); - fact->langEl = prodName; - } - } - else if ( fact->repeatType == RepeatRepeat ) { - /* If the factor is a repeat, create the repeat element and link the - * factor to it. */ - String repeatName( 32, "_repeat_%s", fact->refName.data ); + ProdElList *prodElList1 = new ProdElList; + prodElList1->append( factor1 ); + prodElList1->append( factor2 ); - SymbolMapEl *inDict = nspace->symbolMap.find( repeatName ); - if ( inDict != 0 ) { - fact->langEl = inDict->value; - } - else { - KlangEl *prodName = getKlangEl( this, nspace, repeatName ); - prodName->type = KlangEl::NonTerm; - prodName->isRepeat = true; + Definition *newDef1 = new Definition( InputLoc(), + prodName, prodElList1, false, 0, + prodList.length(), Definition::Production ); - ProdElList *prodElList1 = new ProdElList; + prodName->defList.append( newDef1 ); + prodList.append( newDef1 ); - /* Build the first production of the repeat. */ - PdaFactor *factor1 = new PdaFactor( InputLoc(), false, fact->nspaceQual, - fact->refName, 0, RepeatNone, false, false ); - PdaFactor *factor2 = new PdaFactor( InputLoc(), false, fact->nspaceQual, - repeatName, 0, RepeatNone, false, false ); + /* Build the second production of the list. */ + PdaFactor *factor3 = new PdaFactor( InputLoc(), false, fact->nspaceQual, + fact->refName, 0, RepeatNone, false, false ); - prodElList1->append( factor1 ); - prodElList1->append( factor2 ); + ProdElList *prodElList2 = new ProdElList; + prodElList2->append( factor3 ); - Definition *newDef1 = new Definition( InputLoc(), - prodName, prodElList1, false, 0, - prodList.length(), Definition::Production ); + Definition *newDef2 = new Definition( InputLoc(), + prodName, prodElList2, false, 0, + prodList.length(), Definition::Production ); - prodName->defList.append( newDef1 ); - prodList.append( newDef1 ); + prodName->defList.append( newDef2 ); + prodList.append( newDef2 ); - /* Build the second production of the repeat. */ - ProdElList *prodElList2 = new ProdElList; + return prodName; +} - Definition *newDef2 = new Definition( InputLoc(), - prodName, prodElList2, false, 0, - prodList.length(), Definition::Production ); +KlangEl *ParseData::makeOptProd( Namespace *nspace, const String &optName, PdaFactor *fact ) +{ + KlangEl *prodName = getKlangEl( this, nspace, optName ); + prodName->type = KlangEl::NonTerm; + prodName->isOpt = true; - prodName->defList.append( newDef2 ); - prodList.append( newDef2 ); + ProdElList *prodElList1 = new ProdElList; - fact->langEl = prodName; - } - } - else if ( fact->repeatType == RepeatList ) { - /* If the factor is a repeat, create the repeat element and link the - * factor to it. */ - String repeatName( 32, "_list_%s", fact->refName.data ); + /* Build the first production of the repeat. */ + PdaFactor *factor1 = new PdaFactor( InputLoc(), false, fact->nspaceQual, + fact->refName, 0, RepeatNone, false, false ); + prodElList1->append( factor1 ); - SymbolMapEl *inDict = nspace->symbolMap.find( repeatName ); - if ( inDict != 0 ) { - fact->langEl = inDict->value; - } - else { - KlangEl *prodName = getKlangEl( this, nspace, repeatName ); - prodName->type = KlangEl::NonTerm; - prodName->isList = true; + Definition *newDef1 = new Definition( InputLoc(), + prodName, prodElList1, false, 0, + prodList.length(), Definition::Production ); - /* Build the first production of the list. */ - PdaFactor *factor1 = new PdaFactor( InputLoc(), false, fact->nspaceQual, - fact->refName, 0, RepeatNone, false, false ); - PdaFactor *factor2 = new PdaFactor( InputLoc(), false, fact->nspaceQual, - repeatName, 0, RepeatNone, false, false ); + prodName->defList.append( newDef1 ); + prodList.append( newDef1 ); - ProdElList *prodElList1 = new ProdElList; - prodElList1->append( factor1 ); - prodElList1->append( factor2 ); + /* Build the second production of the repeat. */ + ProdElList *prodElList2 = new ProdElList; - Definition *newDef1 = new Definition( InputLoc(), - prodName, prodElList1, false, 0, - prodList.length(), Definition::Production ); + Definition *newDef2 = new Definition( InputLoc(), + prodName, prodElList2, false, 0, + prodList.length(), Definition::Production ); - prodName->defList.append( newDef1 ); - prodList.append( newDef1 ); + prodName->defList.append( newDef2 ); + prodList.append( newDef2 ); - /* Build the second production of the list. */ - PdaFactor *factor3 = new PdaFactor( InputLoc(), false, fact->nspaceQual, - fact->refName, 0, RepeatNone, false, false ); + return prodName; +} - ProdElList *prodElList2 = new ProdElList; - prodElList2->append( factor3 ); +void ParseData::resolveReferenceFactor( PdaFactor *fact ) +{ + /* Look for the production's associated region. */ + Namespace *nspace = fact->nspaceQual->getQual( this ); - Definition *newDef2 = new Definition( InputLoc(), - prodName, prodElList2, false, 0, - prodList.length(), Definition::Production ); + if ( nspace == 0 ) + error(fact->loc) << "do not have namespace for resolving reference" << endp; + + fact->nspace = nspace; - prodName->defList.append( newDef2 ); - prodList.append( newDef2 ); + /* Look up the language element in the region. */ + KlangEl *langEl = getKlangEl( this, nspace, fact->refName ); - fact->langEl = prodName; - } + if ( fact->repeatType == RepeatRepeat ) { + /* If the factor is a repeat, create the repeat element and link the + * factor to it. */ + String repeatName( 32, "_repeat_%s", fact->refName.data ); + + SymbolMapEl *inDict = nspace->symbolMap.find( repeatName ); + if ( inDict != 0 ) + fact->langEl = inDict->value; + else + fact->langEl = makeRepeatProd( nspace, repeatName, fact );; + } + else if ( fact->repeatType == RepeatList ) { + /* If the factor is a repeat, create the repeat element and link the + * factor to it. */ + String listName( 32, "_list_%s", fact->refName.data ); + + SymbolMapEl *inDict = nspace->symbolMap.find( listName ); + if ( inDict != 0 ) + fact->langEl = inDict->value; + else + fact->langEl = makeListProd( nspace, listName, fact ); + } + else if ( fact->repeatType == RepeatOpt ) { + /* If the factor is an opt, create the opt element and link the factor + * to it. */ + String optName( 32, "_opt_%s", fact->refName.data ); + + SymbolMapEl *inDict = nspace->symbolMap.find( optName ); + if ( inDict != 0 ) + fact->langEl = inDict->value; + else + fact->langEl = makeOptProd( nspace, optName, fact ); } else { /* The factor is not a repeat. Link to the language element. */ diff --git a/colm/parsedata.h b/colm/parsedata.h index 28db05ab..caf65e44 100644 --- a/colm/parsedata.h +++ b/colm/parsedata.h @@ -687,6 +687,9 @@ struct ParseData void printNonTermFirstSets(); void printFirstSets(); + KlangEl *makeRepeatProd( Namespace *nspace, const String &repeatName, PdaFactor *fact ); + KlangEl *makeListProd( Namespace *nspace, const String &listName, PdaFactor *fact ); + KlangEl *makeOptProd( Namespace *nspace, const String &optName, PdaFactor *fact ); void resolveLiteralFactor( PdaFactor *fact ); void resolveReferenceFactor( PdaFactor *fact ); void resolveFactor( PdaFactor *fact ); |