Skip to content

Commit

Permalink
#5977: Reduce DeclarationFileParser responsibility to cut the incomin…
Browse files Browse the repository at this point in the history
…g streams into blocks and determine their type.

The DeclarationManager will receive all identified blocks and will process them further.
  • Loading branch information
codereader committed Jun 25, 2022
1 parent 81fd9fe commit 612a530
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 68 deletions.
3 changes: 2 additions & 1 deletion include/idecltypes.h
Expand Up @@ -10,20 +10,21 @@ namespace decl
// Enumeration of declaration types supported by DarkRadiant
enum class Type
{
Undetermined = -2,
None = -1,
Material = 0,
EntityDef,
SoundShader,
Model,
Particle,
Skin,
NumDeclarationTypes,
};

inline std::string getTypeName(Type type)
{
switch (type)
{
case Type::Undetermined: return _("Undetermined");
case Type::None: return _("None");
case Type::Material: return _("Material");
case Type::EntityDef: return _("EntityDef");
Expand Down
52 changes: 40 additions & 12 deletions radiantcore/decl/DeclarationFileParser.cpp
Expand Up @@ -2,20 +2,31 @@

#include "DeclarationManager.h"
#include "parser/DefBlockTokeniser.h"
#include "string/trim.h"

namespace decl
{

namespace
{
inline std::string getBlockTypeName(const std::string& declaration)
{
auto spacePos = declaration.find(' ');

if (spacePos == std::string::npos) return {};

return string::trim_copy(declaration.substr(0, spacePos), " \t"); // remove all tabs and spaces
}

inline DeclarationBlockSyntax createBlock(const parser::BlockTokeniser::Block& block,
const vfs::FileInfo& fileInfo, const std::string& modName)
{
auto spacePos = block.name.find(' ');

DeclarationBlockSyntax syntax;

syntax.typeName = spacePos != std::string::npos ? block.name.substr(0, spacePos) : std::string();
syntax.typeName = getBlockTypeName(block.name);

syntax.name = spacePos != std::string::npos ? block.name.substr(spacePos + 1) : block.name;
syntax.contents = block.contents;
syntax.modName = modName;
Expand All @@ -25,34 +36,30 @@ namespace
}
}

DeclarationFileParser::DeclarationFileParser(Type declType,
const std::map<std::string, IDeclarationCreator::Ptr>& creatorsByTypename) :
_defaultDeclType(declType),
_creatorsByTypename(creatorsByTypename)
DeclarationFileParser::DeclarationFileParser(Type defaultDeclType, const std::map<std::string, Type>& typeMapping) :
_defaultDeclType(defaultDeclType),
_typeMapping(typeMapping)
{
#if 0
_defaultTypeCreator = getCreatorByType(declType);

if (!_defaultTypeCreator)
{
throw std::invalid_argument("No creator has been associated to the default type " + getTypeName(declType));
}
#endif
}

std::map<Type, NamedDeclarations>& DeclarationFileParser::getParsedDecls()
std::map<Type, std::vector<DeclarationBlockSyntax>>& DeclarationFileParser::getParsedBlocks()
{
return _parsedDecls;
return _parsedBlocks;
}

const std::set<DeclarationFile>& DeclarationFileParser::getParsedFiles() const
{
return _parsedFiles;
}

const std::vector<DeclarationBlockSyntax>& DeclarationFileParser::getUnrecognisedBlocks() const
{
return _unrecognisedBlocks;
}

void DeclarationFileParser::parse(std::istream& stream, const vfs::FileInfo& fileInfo, const std::string& modDir)
{
_parsedFiles.emplace(DeclarationFile{ fileInfo.fullPath(), _defaultDeclType });
Expand All @@ -67,6 +74,11 @@ void DeclarationFileParser::parse(std::istream& stream, const vfs::FileInfo& fil
// Convert the incoming block to a DeclarationBlockSyntax
auto blockSyntax = createBlock(block, fileInfo, modDir);

// Move the block in the correct bucket
auto declType = determineBlockType(blockSyntax);
auto& blockList = _parsedBlocks.try_emplace(declType).first->second;
blockList.emplace_back(std::move(blockSyntax));
#if 0
auto spacePos = block.name.find(' ');

if (spacePos == std::string::npos)
Expand All @@ -87,9 +99,24 @@ void DeclarationFileParser::parse(std::istream& stream, const vfs::FileInfo& fil

// Unknown block type, move to buffer
_unrecognisedBlocks.emplace_back(std::move(blockSyntax));
#endif
}
}

Type DeclarationFileParser::determineBlockType(const DeclarationBlockSyntax& block)
{
if (block.typeName.empty())
{
return _defaultDeclType;
}

auto foundType = _typeMapping.find(block.typeName);

return foundType != _typeMapping.end() ? foundType->second : Type::Undetermined;
}


#if 0
void DeclarationFileParser::processBlock(IDeclarationCreator& creator, const DeclarationBlockSyntax& block)
{
auto declaration = creator.createDeclaration(block.name);
Expand All @@ -115,5 +142,6 @@ IDeclarationCreator::Ptr DeclarationFileParser::getCreatorByType(Type declType)

return {};
}
#endif

}
13 changes: 10 additions & 3 deletions radiantcore/decl/DeclarationFileParser.h
Expand Up @@ -13,24 +13,31 @@ class DeclarationFileParser
private:
Type _defaultDeclType;

#if 0
std::map<std::string, IDeclarationCreator::Ptr> _creatorsByTypename;
IDeclarationCreator::Ptr _defaultTypeCreator;
#endif
std::map<std::string, Type> _typeMapping;

std::set<DeclarationFile> _parsedFiles;
std::map<Type, std::vector<DeclarationBlockSyntax>> _parsedBlocks;

#if 0
std::map<Type, NamedDeclarations> _parsedDecls;

std::vector<DeclarationBlockSyntax> _unrecognisedBlocks;
#endif

public:
DeclarationFileParser(Type declType, const std::map<std::string, IDeclarationCreator::Ptr>& creatorsByTypename);
DeclarationFileParser(Type defaultDeclType, const std::map<std::string, Type>& typeMapping);

void parse(std::istream& stream, const vfs::FileInfo& fileInfo, const std::string& modDir);

std::map<Type, NamedDeclarations>& getParsedDecls();
std::map<Type, std::vector<DeclarationBlockSyntax>>& getParsedBlocks();
const std::set<DeclarationFile>& getParsedFiles() const;
const std::vector<DeclarationBlockSyntax>& getUnrecognisedBlocks() const;

private:
Type determineBlockType(const DeclarationBlockSyntax& block);
void processBlock(IDeclarationCreator& creator, const DeclarationBlockSyntax& block);
IDeclarationCreator::Ptr getCreatorByType(Type declType) const;
};
Expand Down
6 changes: 3 additions & 3 deletions radiantcore/decl/DeclarationFolderParser.cpp
Expand Up @@ -7,10 +7,10 @@ namespace decl

DeclarationFolderParser::DeclarationFolderParser(DeclarationManager& owner, Type declType,
const std::string& baseDir, const std::string& extension,
const std::map<std::string, IDeclarationCreator::Ptr>& creatorsByTypename) :
const std::map<std::string, Type>& typeMapping) :
ThreadedDeclParser<void>(declType, baseDir, extension, 1),
_owner(owner),
_fileParser(declType, creatorsByTypename),
_fileParser(declType, typeMapping),
_defaultDeclType(declType)
{}

Expand All @@ -22,7 +22,7 @@ void DeclarationFolderParser::parse(std::istream& stream, const vfs::FileInfo& f
void DeclarationFolderParser::onFinishParsing()
{
// Submit all parsed declarations to the decl manager
_owner.onParserFinished(_defaultDeclType, _fileParser.getParsedDecls(), _fileParser.getUnrecognisedBlocks(), _fileParser.getParsedFiles());
_owner.onParserFinished(_defaultDeclType, _fileParser.getParsedBlocks(), _fileParser.getParsedFiles());
}

}
2 changes: 1 addition & 1 deletion radiantcore/decl/DeclarationFolderParser.h
Expand Up @@ -25,7 +25,7 @@ class DeclarationFolderParser :
public:
DeclarationFolderParser(DeclarationManager& owner, Type declType,
const std::string& baseDir, const std::string& extension,
const std::map<std::string, IDeclarationCreator::Ptr>& creatorsByTypename);
const std::map<std::string, Type>& typeMapping);

protected:
void parse(std::istream& stream, const vfs::FileInfo& fileInfo, const std::string& modDir) override;
Expand Down

0 comments on commit 612a530

Please sign in to comment.