9 #include <boost/format.hpp> 26 static bool IncludeNode( gp::Node* node,
const std::string& search_path, gp::IncludedFiles* included,
27 gp::Messages* messages )
29 gp::Node include_root;
30 bool status =
gp::Parse( node->position, node->value, search_path, included, messages, &include_root );
31 if ( !status )
return false;
32 node->value = include_root.value;
33 node->children.reserve( node->children.size() + include_root.children.size() );
39 static bool UnitsNode( gp::Node* node,
const std::string& search_path, gp::IncludedFiles* included,
40 gp::Messages* messages )
43 bool status =
gp::ParseUnits( node->position, node->value, search_path, included, messages, &units_root );
44 if ( !status )
return false;
45 node->value = units_root.value;
46 node->children.reserve( node->children.size() + units_root.children.size() );
53 if ( node->children.size() == 1 ) {
54 return std::make_unique<gp::PropertyName>( node->children[0].value, node->position );
58 for (
unsigned int i = 0; i < ( node->children.size() - 1 ); ++i ) {
59 client += delim + node->children[i].value;
62 return std::make_unique<gp::PropertyName>( client, node->children[node->children.size() - 1].value, node->position );
69 switch ( node->type ) {
71 case gp::Node::kReal: {
77 if ( node->children.size() == 1 ) {
80 double unit_value = 0;
81 if ( units->Find( unit_name, unit_value ) ) {
84 value = std::make_unique<gp::PropertyValue>(
std::to_string( val * unit_value ) );
87 throw gp::PositionalPropertyValueException::CouldNotFindUnit( node->children[0].position, unit_name );
90 value = std::make_unique<gp::PropertyValue>( node->value );
95 case gp::Node::kString: {
97 value = std::make_unique<gp::PropertyValue>(
'"' + node->value +
'"' );
101 case gp::Node::kBool: {
102 value = std::make_unique<gp::PropertyValue>( node->value );
106 case gp::Node::kVector: {
108 result.
reserve( node->children.size() );
110 [&](
const gp::Node& child ) { return GetPropertyValue( &child, catalog, units )->ToString(); } );
111 value = std::make_unique<gp::PropertyValue>(
std::move( result ) );
115 case gp::Node::kMap: {
117 for (
const auto& child : node->children ) {
118 auto kvalue = GetPropertyValue( &child.children[0], catalog, units );
119 auto vvalue = GetPropertyValue( &child.children[1], catalog, units );
120 result.
emplace( kvalue->ToString(), vvalue->ToString() );
122 value = std::make_unique<gp::PropertyValue>(
std::move( result ) );
126 case gp::Node::kProperty: {
127 auto property = GetPropertyName( node );
130 value = std::make_unique<gp::PropertyValue>( exists->property_value() );
132 throw gp::PositionalPropertyValueException::CouldNotFindProperty( node->position,
property->ToString() );
136 case gp::Node::kPropertyRef: {
137 auto property = GetPropertyName( node );
143 value = std::make_unique<gp::PropertyValue>(
std::move( reference ),
property->position(), true );
159 case gp::Node::kEqual: {
163 case gp::Node::kPlusEqual: {
167 case gp::Node::kMinusEqual: {
178 static bool AssignNode(
const gp::Node* node, gp::Messages* messages, gp::Catalog* catalog, gp::Units* units,
184 auto property = GetPropertyName( &node->children[0] );
186 value = GetPropertyValue( &node->children[2], catalog, units );
187 }
catch (
const gp::PositionalPropertyValueException& ex ) {
188 messages->AddError( ex.position(), ex.what() );
192 bool reassign =
false;
200 if ( node->children[1].type == gp::Node::kEqual ) {
202 if ( exists->HasDefinedPosition() ) {
203 message +=
" Previously defined at " + exists->DefinedPosition().ToString() +
".";
208 }
else if ( node->children[1].type == gp::Node::kPlusEqual ) {
210 }
else if ( node->children[1].type == gp::Node::kMinusEqual ) {
213 }
catch (
const gp::PropertyValueException& ex ) {
215 if ( exists->HasDefinedPosition() ) {
216 message +=
" Previously defined at " + exists->DefinedPosition().ToString() +
".";
218 messages->AddError( node->position, message );
224 if ( !exists || reassign ) {
228 if ( result && is_print ) {
230 SignString( node->children[1].type ) % value->ToString() );
231 messages->AddInfo( node->position, message );
236 static bool UnitNode(
const gp::Node* node, gp::Messages* messages, gp::Units* units,
bool is_print )
243 gp::Units::Container::mapped_type exists;
244 if ( units->Find( name, exists ) ) {
246 if ( exists.second.Exists() ) {
247 message +=
" at " + exists.second.ToString();
249 messages->AddError( node->children[1].position, message );
253 bool result = units->Add( name, right / left, node->children[1].position );
254 if ( result && is_print ) {
256 messages->AddInfo( node->position, message );
261 static bool ConditionNode( gp::Node* node, gp::Catalog* catalog, gp::Node** next )
264 auto property_name = GetPropertyName( &node->children[0] );
266 bool is_defined = (
nullptr != catalog->Find( property_name->client(), property_name->property() ) );
268 if ( ( is_defined && ( node->children[1].type == gp::Node::kIfdef ) ) ||
269 ( !is_defined && ( node->children[1].type == gp::Node::kIfndef ) ) ) {
270 *next = &node->children[1];
271 }
else if ( node->children.size() > 2 ) {
272 *next = &node->children[2];
279 static bool Analyze( gp::Node* node,
const std::string& search_path, gp::IncludedFiles* included,
280 gp::Messages* messages, gp::Catalog* catalog, gp::Units* units, gp::PragmaOptions* pragma )
284 bool local_result =
true;
285 bool skip_childs =
true;
286 gp::Node* next_root = node;
288 switch ( node->type ) {
290 case gp::Node::kRoot: {
295 case gp::Node::kInclude: {
296 local_result = IncludeNode( node, search_path, included, messages );
301 case gp::Node::kUnits: {
302 local_result = UnitsNode( node, search_path, included, messages );
307 case gp::Node::kAssign: {
308 local_result = AssignNode( node, messages, catalog, units, pragma->is_print() );
312 case gp::Node::kUnit: {
313 local_result = UnitNode( node, messages, units, pragma->is_print() );
317 case gp::Node::kCondition: {
318 local_result = ConditionNode( node, catalog, &next_root );
323 case gp::Node::kPrintOptions: {
324 pragma->setIsPrintOptions(
true );
328 case gp::Node::kPrintOn: {
329 pragma->setIsPrint(
true );
333 case gp::Node::kPrintOff: {
334 pragma->setIsPrint(
false );
338 case gp::Node::kPrintTree: {
339 pragma->setIsPrintTree(
true );
343 case gp::Node::kDumpFile: {
346 pragma->setDumpFile( file );
348 pragma->setDumpFile( node->value );
357 if ( result ) result = local_result;
359 if ( !skip_childs && next_root ) {
360 for (
auto& child : next_root->children ) {
361 local_result = Analyze( &child, search_path, included, messages, catalog, units, pragma );
362 if ( result ) result = local_result;
370 bool unreference_result =
true;
371 for (
auto& client : catalog ) {
372 for (
auto& current : client.second ) {
373 if ( current.IsReference() ) {
374 gp::PropertyValue& value = current.property_value();
376 gp::Property*
property = catalog.Find( names[0], names[1] );
378 messages->AddError( value.position(),
"Could not unreference " + current.ValueAsString() );
379 unreference_result =
false;
381 value =
property->property_value();
386 return unreference_result;
395 bool result =
Parse( filename, search_path, &included, messages, root );
396 if ( !result )
return false;
398 bool result1 = Analyze( root, search_path, &included, messages, catalog, units, pragma );
400 return result1 && result2;
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
GAUDI_API StatusCode resolveEnv(const std::string &var, std::string &res, int recusions=124)
bool ParseUnits(const Position &from, const std::string &filename, const std::string &search_path, IncludedFiles *included, Messages *messages, Node *root)
Gaudi::Details::PropertyBase * property(const std::string &name) const
bool ReadOptions(const std::string &filename, const std::string &search_path, Messages *messages, Catalog *catalog, Units *units, PragmaOptions *pragma, Node *root)
Parse and analyze filename, save all messages and properties.
Gaudi::Details::PropertyBase Property
backward compatibility hack for old Property base class
bool Parse(const std::string &filename, const std::string &search_path, IncludedFiles *included, Messages *messages, Node *root)
T back_inserter(T...args)
bool Unreference(gp::Catalog &catalog, gp::Messages *messages)