diff options
author | Adrian Thurston <thurston@complang.org> | 2011-07-07 06:58:08 +0000 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2011-07-07 06:58:08 +0000 |
commit | 0b1b24f4d81aa85ed8a8025f8f8b4c1da1d21cf4 (patch) | |
tree | f0460633e1f0fbf115cd497736abdd632817ec38 | |
parent | 2f46e69d77da459a2857b857b1fbe65b5bd03228 (diff) | |
download | colm-0b1b24f4d81aa85ed8a8025f8f8b4c1da1d21cf4.tar.gz |
The getLangEl function split into declare and find.
-rw-r--r-- | colm/declare.cc | 33 | ||||
-rw-r--r-- | colm/parsedata.cc | 12 | ||||
-rw-r--r-- | colm/parsedata.h | 3 | ||||
-rw-r--r-- | colm/pdabuild.cc | 33 | ||||
-rw-r--r-- | colm/resolve.cc | 4 |
5 files changed, 37 insertions, 48 deletions
diff --git a/colm/declare.cc b/colm/declare.cc index 31d796ea..cab4fc37 100644 --- a/colm/declare.cc +++ b/colm/declare.cc @@ -198,13 +198,7 @@ void Namespace::declare( ParseData *pd ) for ( GenericList::Iter g = genericList; g.lte(); g++ ) { //std::cout << "generic " << g->name << std::endl; - LangEl *langEl = getKlangEl( pd, this, g->name ); - - /* Check that the element wasn't previously defined as something else. */ - if ( langEl->type != LangEl::Unknown ) { - error() << "'" << g->name << - "' already defined as something else" << endp; - } + LangEl *langEl = declareLangEl( pd, this, g->name ); langEl->type = LangEl::NonTerm; /* Add one empty production. */ @@ -230,8 +224,7 @@ void Namespace::declare( ParseData *pd ) for ( LiteralDict::Iter l = literalDict; l.lte(); l++ ) { /* Create a token for the literal. */ - LangEl *newLangEl = getKlangEl( pd, this, l->value->name ); - assert( newLangEl->type == LangEl::Unknown ); + LangEl *newLangEl = declareLangEl( pd, this, l->value->name ); newLangEl->type = LangEl::Term; newLangEl->lit = l->value->literal; newLangEl->isLiteral = true; @@ -241,13 +234,7 @@ void Namespace::declare( ParseData *pd ) } for ( ContextDefList::Iter c = contextDefList; c.lte(); c++ ) { - LangEl *lel = getKlangEl( pd, this, c->name ); - - /* Check that the element wasn't previously defined as something else. */ - if ( lel->type != LangEl::Unknown ) { - error(c->context->loc) << "'" << c->name << - "' has already been defined, maybe you want to use redef?" << endp; - } + LangEl *lel = declareLangEl( pd, this, c->name ); lel->type = LangEl::NonTerm; ProdElList *emptyList = new ProdElList; //addProduction( c->context->loc, c->name, emptyList, false, 0, 0 ); @@ -282,10 +269,7 @@ void Namespace::declare( ParseData *pd ) /* Literals already taken care of. */ if ( ! t->isLiteral ) { /* Create the token. */ - LangEl *tokEl = getKlangEl( pd, this, t->name ); - if ( tokEl->type != LangEl::Unknown ) - error(InputLoc()) << "'" << t->name << "' already defined" << endp; - + LangEl *tokEl = declareLangEl( pd, this, t->name ); tokEl->type = LangEl::Term; tokEl->ignore = t->ignore; tokEl->transBlock = t->codeBlock; @@ -299,14 +283,7 @@ void Namespace::declare( ParseData *pd ) for ( NtDefList::Iter n = ntDefList; n.lte(); n++ ) { /* Get the language element. */ - LangEl *langEl = getKlangEl( pd, this, n->name ); - - /* Check that the element wasn't previously defined as something else. */ - if ( langEl->type != LangEl::Unknown ) { - error(InputLoc()) << "'" << n->name << - "' has already been defined, maybe you want to use redef?" << endp; - } - + LangEl *langEl = declareLangEl( pd, this, n->name ); langEl->type = LangEl::NonTerm; //$$->langEl = langEl; diff --git a/colm/parsedata.cc b/colm/parsedata.cc index 03a97a9f..cbb94588 100644 --- a/colm/parsedata.cc +++ b/colm/parsedata.cc @@ -1110,8 +1110,7 @@ void ParseData::createDefaultScanner() /* Now create the one and only token -> "<chr>" / any / */ name = "___DEFAULT_SCANNER_CHR"; - defaultCharKlangEl = getKlangEl( this, defaultNamespace, name ); - assert( defaultCharKlangEl->type == LangEl::Unknown ); + defaultCharKlangEl = declareLangEl( this, defaultNamespace, name ); defaultCharKlangEl->type = LangEl::Term; tokenDef->token = defaultCharKlangEl; @@ -1120,7 +1119,7 @@ void ParseData::createDefaultScanner() LangEl *ParseData::makeRepeatProd( Namespace *nspace, const String &repeatName, NamespaceQual *nspaceQual, const String &name ) { - LangEl *prodName = getKlangEl( this, nspace, repeatName ); + LangEl *prodName = declareLangEl( this, nspace, repeatName ); prodName->type = LangEl::NonTerm; prodName->isRepeat = true; @@ -1157,7 +1156,7 @@ LangEl *ParseData::makeRepeatProd( Namespace *nspace, const String &repeatName, LangEl *ParseData::makeListProd( Namespace *nspace, const String &listName, NamespaceQual *nspaceQual, const String &name ) { - LangEl *prodName = getKlangEl( this, nspace, listName ); + LangEl *prodName = declareLangEl( this, nspace, listName ); prodName->type = LangEl::NonTerm; prodName->isList = true; @@ -1197,7 +1196,7 @@ LangEl *ParseData::makeListProd( Namespace *nspace, const String &listName, Name LangEl *ParseData::makeOptProd( Namespace *nspace, const String &optName, NamespaceQual *nspaceQual, const String &name ) { - LangEl *prodName = getKlangEl( this, nspace, optName ); + LangEl *prodName = declareLangEl( this, nspace, optName ); prodName->type = LangEl::NonTerm; prodName->isOpt = true; @@ -1313,8 +1312,7 @@ void ParseData::initEmptyScanners() /* These do not go in the namespace so so they cannot get declared * in the declare pass. */ - LangEl *lel = getKlangEl( this, rootNamespace, name ); - assert( lel->type == LangEl::Unknown ); + LangEl *lel = declareLangEl( this, rootNamespace, name ); lel->type = LangEl::Term; tokenDef->token = lel; diff --git a/colm/parsedata.h b/colm/parsedata.h index db69f19d..14892510 100644 --- a/colm/parsedata.h +++ b/colm/parsedata.h @@ -995,6 +995,7 @@ struct Parser; typedef AvlMap<String, Parser *, CmpStr> ParserDict; typedef AvlMapEl<String, Parser *> ParserDictEl; -LangEl *getKlangEl( ParseData *pd, Namespace *nspace, const String &data ); +LangEl *declareLangEl( ParseData *pd, Namespace *nspace, const String &data ); +LangEl *findLangEl( ParseData *pd, Namespace *nspace, const String &data ); #endif /* _PARSEDATA_H */ diff --git a/colm/pdabuild.cc b/colm/pdabuild.cc index a9709f36..a95c1274 100644 --- a/colm/pdabuild.cc +++ b/colm/pdabuild.cc @@ -130,18 +130,32 @@ PdaGraph *ProdElList::walk( ParseData *pd ) } -LangEl *getKlangEl( ParseData *pd, Namespace *nspace, const String &data ) +LangEl *declareLangEl( ParseData *pd, Namespace *nspace, const String &data ) { /* If the id is already in the dict, it will be placed in last found. If * it is not there then it will be inserted and last found will be set to it. */ SymbolMapEl *inDict = nspace->symbolMap.find( data ); - if ( inDict == 0 ) { - /* Language element not there. Make the new lang el and insert.. */ - LangEl *langEl = new LangEl( nspace, data, LangEl::Unknown ); - inDict = nspace->symbolMap.insert( langEl->name, langEl ); - pd->langEls.append( langEl ); - } - return inDict->value; + if ( inDict != 0 ) + error() << "'" << data << "' already defined as something else" << endp; + + /* Language element not there. Make the new lang el and insert.. */ + LangEl *langEl = new LangEl( nspace, data, LangEl::Unknown ); + inDict = nspace->symbolMap.insert( langEl->name, langEl ); + pd->langEls.append( langEl ); + + return inDict->value; +} + +LangEl *findLangEl( ParseData *pd, Namespace *nspace, const String &data ) +{ + /* If the id is already in the dict, it will be placed in last found. If + * it is not there then it will be inserted and last found will be set to it. */ + SymbolMapEl *inDict = nspace->symbolMap.find( data ); + + if ( inDict == 0 ) + error() << "'" << data << "' not declared as anything" << endp; + + return inDict->value; } ProdElList *makeProdElList( LangEl *langEl ) @@ -1180,8 +1194,7 @@ void ParseData::insertUniqueEmptyProductions() /* Get a language element. */ char name[20]; sprintf(name, "U%li", prodList.length()); - LangEl *prodName = getKlangEl( this, rootNamespace, name ); - assert( prodName->type == LangEl::Unknown ); + LangEl *prodName = declareLangEl( this, rootNamespace, name ); prodName->type = LangEl::NonTerm; Definition *newDef = new Definition( InputLoc(), prodName, 0 /* FIXME new VarDef( name, 0 )*/, diff --git a/colm/resolve.cc b/colm/resolve.cc index 097d15ae..d18e0765 100644 --- a/colm/resolve.cc +++ b/colm/resolve.cc @@ -422,7 +422,7 @@ void ParseData::resolveUses() error() << "do not have namespace for resolving reference" << endp; /* Look up the language element in the region. */ - LangEl *langEl = getKlangEl( this, nspace, lel->objectDefUses ); + LangEl *langEl = findLangEl( this, nspace, lel->objectDefUses ); lel->objectDef = langEl->objectDef; } } @@ -463,7 +463,7 @@ void ParseData::resolveReferenceFactor( ProdEl *fact ) fact->nspace = nspace; /* Look up the language element in the region. */ - LangEl *langEl = getKlangEl( this, nspace, fact->refName ); + LangEl *langEl = findLangEl( this, nspace, fact->refName ); if ( fact->repeatType == RepeatRepeat ) { /* If the factor is a repeat, create the repeat element and link the |