9 #include <boost/format.hpp> 26 static bool IncludeNode( gp::Node* node,
const std::string& search_path, gp::IncludedFiles* included,
27 gp::Messages* messages ) {
28 gp::Node include_root;
29 bool status =
gp::Parse( node->position, node->value, search_path, included, messages, &include_root );
30 if ( !status )
return false;
31 node->value = include_root.value;
32 node->children.reserve( node->children.size() + include_root.children.size() );
38 static bool UnitsNode( gp::Node* node,
const std::string& search_path, gp::IncludedFiles* included,
39 gp::Messages* messages ) {
41 bool status =
gp::ParseUnits( node->position, node->value, search_path, included, messages, &units_root );
42 if ( !status )
return false;
43 node->value = units_root.value;
44 node->children.reserve( node->children.size() + units_root.children.size() );
50 if ( node->children.size() == 1 ) {
51 return std::make_unique<gp::PropertyName>( node->children[0].value, node->position );
55 for (
unsigned int i = 0; i < ( node->children.size() - 1 ); ++i ) {
56 client += delim + node->children[i].value;
59 return std::make_unique<gp::PropertyName>( client, node->children[node->children.size() - 1].value, node->position );
65 switch ( node->type ) {
67 case gp::Node::kReal: {
73 if ( node->children.size() == 1 ) {
76 double unit_value = 0;
77 if ( units->Find( unit_name, unit_value ) ) {
80 value = std::make_unique<gp::PropertyValue>(
std::to_string( val * unit_value ) );
83 throw gp::PositionalPropertyValueException::CouldNotFindUnit( node->children[0].position, unit_name );
86 value = std::make_unique<gp::PropertyValue>( node->value );
91 case gp::Node::kString: {
93 ss << std::quoted( node->value );
94 value = std::make_unique<gp::PropertyValue>( ss.
str() );
98 case gp::Node::kBool: {
99 value = std::make_unique<gp::PropertyValue>( node->value );
103 case gp::Node::kVector: {
105 result.
reserve( node->children.size() );
107 [&](
const gp::Node& child ) { return GetPropertyValue( &child, catalog, units )->ToString(); } );
108 value = std::make_unique<gp::PropertyValue>(
std::move( result ) );
112 case gp::Node::kMap: {
114 for (
const auto& child : node->children ) {
115 auto kvalue = GetPropertyValue( &child.children[0], catalog, units );
116 auto vvalue = GetPropertyValue( &child.children[1], catalog, units );
117 result.
emplace( kvalue->ToString(), vvalue->ToString() );
119 value = std::make_unique<gp::PropertyValue>(
std::move( result ) );
123 case gp::Node::kProperty: {
124 auto property = GetPropertyName( node );
125 gp::Property* exists = catalog->Find( property->client(),
property->property() );
127 value = std::make_unique<gp::PropertyValue>( exists->property_value() );
129 throw gp::PositionalPropertyValueException::CouldNotFindProperty( node->position, property->ToString() );
133 case gp::Node::kPropertyRef: {
134 auto property = GetPropertyName( node );
137 reference.
push_back( property->client() );
138 reference.
push_back( property->property() );
140 value = std::make_unique<gp::PropertyValue>(
std::move( reference ), property->position(), true );
155 case gp::Node::kEqual: {
159 case gp::Node::kPlusEqual: {
163 case gp::Node::kMinusEqual: {
174 static bool AssignNode(
const gp::Node* node, gp::Messages* messages, gp::Catalog* catalog, gp::Units* units,
179 auto property = GetPropertyName( &node->children[0] );
181 value = GetPropertyValue( &node->children[2], catalog, units );
182 }
catch (
const gp::PositionalPropertyValueException& ex ) {
183 messages->AddError( ex.position(), ex.what() );
187 bool reassign =
false;
188 gp::Property* exists = catalog->Find( property->client(),
property->property() );
195 if ( node->children[1].type == gp::Node::kEqual ) {
197 if ( exists->HasDefinedPosition() ) {
198 message +=
" Previously defined at " + exists->DefinedPosition().ToString() +
".";
203 }
else if ( node->children[1].type == gp::Node::kPlusEqual ) {
205 }
else if ( node->children[1].type == gp::Node::kMinusEqual ) {
208 }
catch (
const gp::PropertyValueException& ex ) {
210 if ( exists->HasDefinedPosition() ) {
211 message +=
" Previously defined at " + exists->DefinedPosition().ToString() +
".";
213 messages->AddError( node->position, message );
219 if ( !exists || reassign ) { result = catalog->Add(
new gp::Property( *property, *value ) ); }
221 if ( result && is_print ) {
223 SignString( node->children[1].type ) % value->ToString() );
224 messages->AddInfo( node->position, message );
229 static bool UnitNode(
const gp::Node* node, gp::Messages* messages, gp::Units* units,
bool is_print ) {
235 gp::Units::Container::mapped_type exists;
236 if ( units->Find(
name, exists ) ) {
238 if ( exists.second.Exists() ) { message +=
" at " + exists.second.ToString(); }
239 messages->AddError( node->children[1].position, message );
243 bool result = units->Add(
name, right / left, node->children[1].position );
244 if ( result && is_print ) {
246 messages->AddInfo( node->position, message );
251 static bool ConditionNode( gp::Node* node, gp::Catalog* catalog, gp::Node** next ) {
253 auto property_name = GetPropertyName( &node->children[0] );
255 bool is_defined = (
nullptr != catalog->Find( property_name->client(), property_name->property() ) );
257 if ( ( is_defined && ( node->children[1].type == gp::Node::kIfdef ) ) ||
258 ( !is_defined && ( node->children[1].type == gp::Node::kIfndef ) ) ) {
259 *
next = &node->children[1];
260 }
else if ( node->children.size() > 2 ) {
261 *
next = &node->children[2];
268 static bool Analyze( gp::Node* node,
const std::string& search_path, gp::IncludedFiles* included,
269 gp::Messages* messages, gp::Catalog* catalog, gp::Units* units, gp::PragmaOptions* pragma ) {
272 bool local_result =
true;
273 bool skip_childs =
true;
274 gp::Node* next_root = node;
276 switch ( node->type ) {
278 case gp::Node::kRoot: {
283 case gp::Node::kInclude: {
284 local_result = IncludeNode( node, search_path, included, messages );
289 case gp::Node::kUnits: {
290 local_result = UnitsNode( node, search_path, included, messages );
295 case gp::Node::kAssign: {
296 local_result = AssignNode( node, messages, catalog, units, pragma->is_print() );
300 case gp::Node::kUnit: {
301 local_result = UnitNode( node, messages, units, pragma->is_print() );
305 case gp::Node::kCondition: {
306 local_result = ConditionNode( node, catalog, &next_root );
311 case gp::Node::kPrintOptions: {
312 pragma->setIsPrintOptions(
true );
316 case gp::Node::kPrintOn: {
317 pragma->setIsPrint(
true );
321 case gp::Node::kPrintOff: {
322 pragma->setIsPrint(
false );
326 case gp::Node::kPrintTree: {
327 pragma->setIsPrintTree(
true );
331 case gp::Node::kDumpFile: {
334 pragma->setDumpFile( file );
336 pragma->setDumpFile( node->value );
345 if ( result ) result = local_result;
347 if ( !skip_childs && next_root ) {
348 for (
auto& child : next_root->children ) {
349 local_result = Analyze( &child, search_path, included, messages, catalog, units, pragma );
350 if ( result ) result = local_result;
356 bool Unreference( gp::Catalog& catalog, gp::Messages* messages ) {
357 bool unreference_result =
true;
358 for (
auto& client : catalog ) {
359 for (
auto& current : client.second ) {
360 if ( current.IsReference() ) {
361 gp::PropertyValue& value = current.property_value();
363 gp::Property*
property = catalog.Find( names[0], names[1] );
365 messages->AddError( value.position(),
"Could not unreference " + current.ValueAsString() );
366 unreference_result =
false;
368 value =
property->property_value();
373 return unreference_result;
382 if ( !result )
return false;
384 bool result1 = Analyze(
root, search_path, &included, messages, catalog, units, pragma );
386 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)
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
\fixme 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)