Skip to content

Commit

Permalink
Use ParserConfig more in parser.cc (#883)
Browse files Browse the repository at this point in the history
* Fix a lack of ParserConfig propagation.

Copied from patch in RobotLocomotion/drake#16791
by jwnimmer-tri.

* Pass ParserConfig to parser helper functions

Require ParserConfig argument in parser.cc's private
helper functions: initDoc, initXml, and _initFile.

Signed-off-by: Steve Peters <[email protected]>
  • Loading branch information
scpeters authored Mar 16, 2022
1 parent f453a5f commit 8733d08
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
53 changes: 33 additions & 20 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ static bool isSdfFile(const std::string &_fileName)

//////////////////////////////////////////////////
template <typename TPtr>
static inline bool _initFile(const std::string &_filename, TPtr _sdf)
static inline bool _initFile(const std::string &_filename,
const ParserConfig &_config,
TPtr _sdf)
{
auto xmlDoc = makeSdfDoc();
if (tinyxml2::XML_SUCCESS != xmlDoc.LoadFile(_filename.c_str()))
Expand All @@ -165,7 +167,7 @@ static inline bool _initFile(const std::string &_filename, TPtr _sdf)
return false;
}

return initDoc(&xmlDoc, _sdf);
return initDoc(&xmlDoc, _config, _sdf);
}

//////////////////////////////////////////////////
Expand All @@ -181,7 +183,9 @@ static inline bool _initFile(const std::string &_filename, TPtr _sdf)
/// \param[out] _errors Captures errors encountered during parsing.
static void insertIncludedElement(sdf::SDFPtr _includeSDF,
const SourceLocation &_sourceLoc, bool _merge,
sdf::ElementPtr _parent, sdf::Errors &_errors)
sdf::ElementPtr _parent,
const ParserConfig &_config,
sdf::Errors &_errors)
{
Error invalidFileError(ErrorCode::FILE_READ,
"Included model is invalid. Skipping model.");
Expand Down Expand Up @@ -230,7 +234,7 @@ static void insertIncludedElement(sdf::SDFPtr _includeSDF,
// We create a throwaway sdf::Root object in order to validate the
// included entity.
sdf::Root includedRoot;
sdf::Errors includeDOMerrors = includedRoot.Load(_includeSDF);
sdf::Errors includeDOMerrors = includedRoot.Load(_includeSDF, _config);
_errors.insert(_errors.end(), includeDOMerrors.begin(),
includeDOMerrors.end());

Expand Down Expand Up @@ -369,7 +373,7 @@ bool init(SDFPtr _sdf)
std::string xmldata = SDF::EmbeddedSpec("root.sdf", false);
auto xmlDoc = makeSdfDoc();
xmlDoc.Parse(xmldata.c_str());
return initDoc(&xmlDoc, _sdf);
return initDoc(&xmlDoc, ParserConfig::GlobalConfig(), _sdf);
}

//////////////////////////////////////////////////
Expand All @@ -387,9 +391,10 @@ bool initFile(
{
auto xmlDoc = makeSdfDoc();
xmlDoc.Parse(xmldata.c_str());
return initDoc(&xmlDoc, _sdf);
return initDoc(&xmlDoc, _config, _sdf);
}
return _initFile(sdf::findFile(_filename, true, false, _config), _sdf);
return _initFile(sdf::findFile(_filename, true, false, _config), _config,
_sdf);
}

//////////////////////////////////////////////////
Expand All @@ -407,14 +412,15 @@ bool initFile(
{
auto xmlDoc = makeSdfDoc();
xmlDoc.Parse(xmldata.c_str());
return initDoc(&xmlDoc, _sdf);
return initDoc(&xmlDoc, _config, _sdf);
}
return _initFile(sdf::findFile(_filename, true, false, _config), _sdf);
return _initFile(sdf::findFile(_filename, true, false, _config), _config,
_sdf);
}

//////////////////////////////////////////////////
bool initString(
const std::string &_xmlString, const ParserConfig &, SDFPtr _sdf)
const std::string &_xmlString, const ParserConfig &_config, SDFPtr _sdf)
{
auto xmlDoc = makeSdfDoc();
if (xmlDoc.Parse(_xmlString.c_str()))
Expand All @@ -423,7 +429,7 @@ bool initString(
return false;
}

return initDoc(&xmlDoc, _sdf);
return initDoc(&xmlDoc, _config, _sdf);
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -452,31 +458,37 @@ inline tinyxml2::XMLElement *_initDocGetElement(tinyxml2::XMLDocument *_xmlDoc)
}

//////////////////////////////////////////////////
bool initDoc(tinyxml2::XMLDocument *_xmlDoc, SDFPtr _sdf)
bool initDoc(tinyxml2::XMLDocument *_xmlDoc,
const ParserConfig &_config,
SDFPtr _sdf)
{
auto element = _initDocGetElement(_xmlDoc);
if (!element)
{
return false;
}

return initXml(element, _sdf->Root());
return initXml(element, _config, _sdf->Root());
}

//////////////////////////////////////////////////
bool initDoc(tinyxml2::XMLDocument *_xmlDoc, ElementPtr _sdf)
bool initDoc(tinyxml2::XMLDocument *_xmlDoc,
const ParserConfig &_config,
ElementPtr _sdf)
{
auto element = _initDocGetElement(_xmlDoc);
if (!element)
{
return false;
}

return initXml(element, _sdf);
return initXml(element, _config, _sdf);
}

//////////////////////////////////////////////////
bool initXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf)
bool initXml(tinyxml2::XMLElement *_xml,
const ParserConfig &_config,
ElementPtr _sdf)
{
const char *refString = _xml->Attribute("ref");
if (refString)
Expand Down Expand Up @@ -594,7 +606,7 @@ bool initXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf)
else
{
ElementPtr element(new Element);
initXml(child, element);
initXml(child, _config, element);
_sdf->AddElementDescription(element);
}
}
Expand All @@ -607,7 +619,7 @@ bool initXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf)

ElementPtr element(new Element);

initFile(filename, element);
initFile(filename, _config, element);

// override description for include elements
tinyxml2::XMLElement *description = child->FirstChildElement("description");
Expand Down Expand Up @@ -1510,7 +1522,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
ElementPtr refSDF;
refSDF.reset(new Element);
std::string refFilename = refSDFStr + ".sdf";
initFile(refFilename, refSDF);
initFile(refFilename, _config, refSDF);
_sdf->RemoveFromParent();
_sdf->Copy(refSDF);

Expand Down Expand Up @@ -1803,7 +1815,8 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
SourceLocation sourceLoc{includeXmlPath, _source,
elemXml->GetLineNum()};

insertIncludedElement(includeSDF, sourceLoc, toMerge, _sdf, _errors);
insertIncludedElement(includeSDF, sourceLoc, toMerge, _sdf, _config,
_errors);
continue;
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/parser_private.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,37 @@ namespace sdf
/// This actually forwards to initXml after converting the inputs
/// \param[in] _xmlDoc TinyXML2 document containing the SDFormat description
/// file that corresponds with the input SDFPtr
/// \param[in] _config Custom parser configuration
/// \param[out] _sdf SDF interface to be initialized
bool initDoc(tinyxml2::XMLDocument *_xmlDoc, SDFPtr _sdf);
/// \return True on success, false on error.
bool initDoc(tinyxml2::XMLDocument *_xmlDoc,
const ParserConfig &_config,
SDFPtr _sdf);

/// \brief Initialize the SDF Element using a TinyXML2 document
///
/// This actually forwards to initXml after converting the inputs
/// \param[in] _xmlDoc TinyXML2 document containing the SDFormat description
/// file that corresponds with the input ElementPtr
/// \param[in] _config Custom parser configuration
/// \param[out] _sdf SDF Element to be initialized
bool initDoc(tinyxml2::XMLDocument *_xmlDoc, ElementPtr _sdf);
/// \return True on success, false on error.
bool initDoc(tinyxml2::XMLDocument *_xmlDoc,
const ParserConfig &_config,
ElementPtr _sdf);

/// \brief Initialize the SDF Element by parsing the SDFormat description in
/// the input TinyXML2 element. This is where SDFormat spec/description files
/// are parsed
/// \remark For internal use only. Do not use this function.
/// \param[in] _xml TinyXML2 element containing the SDFormat description
/// file that corresponds with the input ElementPtr
/// \param[in] _config Custom parser configuration
/// \param[out] _sdf SDF ElementPtr to be initialized
bool initXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf);
/// \return True on success, false on error.
bool initXml(tinyxml2::XMLElement *_xml,
const ParserConfig &_config,
ElementPtr _sdf);

/// \brief Populate the SDF values from a TinyXML document
bool readDoc(tinyxml2::XMLDocument *_xmlDoc, SDFPtr _sdf,
Expand All @@ -83,6 +95,7 @@ namespace sdf
/// \param[in] _xmlRoot Pointer to the root level TinyXML element.
/// \param[in] _source Source of the XML document.
/// \param[in] _errors Captures errors found during the checks.
/// \return True on success, false on error.
bool checkXmlFromRoot(tinyxml2::XMLElement *_xmlRoot,
const std::string &_source, Errors &_errors);

Expand Down

0 comments on commit 8733d08

Please sign in to comment.