22 #include "Teuchos_Reader.hpp"
24 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
25 #include "yaml-cpp/yaml.h"
26 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
27 #include "Teuchos_YAML.hpp"
32 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
47 static T eval(::YAML::Node
const& node) {
49 if (node.Tag() ==
"!") {
50 throw std::runtime_error(
"quoted_as from quoted string to number");
57 struct QuotedAs<std::string> {
59 static std::string eval(::YAML::Node
const& node) {
return node.as<std::string>(); }
63 static T quoted_as(::YAML::Node
const& node) {
return QuotedAs<T>::eval(node); }
69 for(::YAML::const_iterator it = node.begin(); it != node.end(); it++)
76 bool checkYamlTwoDArrayIsRagged(const ::YAML::Node& node)
79 for (::YAML::const_iterator it = node.begin(); it != node.end(); ++it)
81 if (it->size() != node.begin()->size())
96 for (::YAML::const_iterator rit = node.begin(); rit != node.end(); ++rit)
99 for (::YAML::const_iterator cit = rit->begin(); cit != rit->end(); ++cit)
101 arr(i, j) = quoted_as<T>(*cit);
109 int getYamlArrayDim(const ::YAML::Node& node)
112 if (node.Type() == ::YAML::NodeType::Sequence)
115 if (node.begin()->Type() == ::YAML::NodeType::Sequence)
118 if (node.begin()->begin()->Type() == ::YAML::NodeType::Sequence)
127 template <
typename tarray_t,
typename T>
128 tarray_t getYaml2DRaggedArray(::YAML::Node node,
int ndim, std::string key)
133 for (::YAML::const_iterator it1 = node.begin(); it1 != node.end(); ++it1) {
134 for (::YAML::const_iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
136 } base_arr.push_back(sub_arr);
142 throw YamlSequenceError(std::string(
"MDArray \"" + key +
"\" must have dim 2."));
150 template <
typename tarray_t,
typename T>
151 tarray_t getYaml3DArray(::YAML::Node node,
int ndim, std::string key)
157 for (::YAML::const_iterator it1 = node.begin(); it1 != node.end(); ++it1) {
158 for (::YAML::const_iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
159 for (::YAML::const_iterator it3 = it2->begin(); it3 != it2->end(); ++it3) {
160 sub_sub_arr.
push_back(quoted_as<T>(*it3));
163 } base_arr.push_back(sub_arr);
169 throw YamlSequenceError(std::string(
"MDArray \"" + key +
"\" must have dim 3."));
174 template <
typename T>
175 void safe_set_entry(ParameterList& list, std::string
const& name_in, T
const& entry_in) {
177 "Parameter \"" << name_in <<
"\" already exists in list \"" << list.name() <<
"\"\n");
178 list.set(name_in, entry_in);
180 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
183 std::size_t new_end = 0;
184 for (std::size_t ri = 0; ri < in.size(); ++ri) {
185 std::size_t i = in.size() - 1 - ri;
186 if (in[i] !=
' ' && in[i] !=
'\t') {
191 return in.substr(0, new_end);
195 std::size_t new_end = 0;
196 for (std::size_t ri = 0; ri < in.size(); ++ri) {
197 std::size_t i = in.size() - 1 - ri;
198 if (in[i] !=
' ' && in[i] !=
'\t' && in[i] !=
'\n' && in[i] !=
'\r') {
203 return in.substr(0, new_end);
206 template <
typename T>
208 std::istringstream ss(text);
210 ss >> std::noskipws >> val;
211 return ss.eof() && !ss.fail();
216 std::istringstream ss(text);
217 using LL =
long long;
219 ss >> std::noskipws >> val;
220 return ss.eof() && !ss.fail() &&
221 (val >= LL(std::numeric_limits<int>::min())) &&
222 (val <= LL(std::numeric_limits<int>::max()));
225 template <
typename T>
227 std::istringstream ss(text);
236 return std::tolower(static_cast<unsigned char>(ch));
242 return std::isdigit(static_cast<unsigned char>(ch));
248 for (std::size_t i = 0; i < text.size(); ++i) {
251 return lower ==
"true" || lower ==
"yes" ||
252 lower ==
"false" || lower ==
"no";
258 for (std::size_t i = 0; i < text.size(); ++i) {
261 return !(lower ==
"false" || lower ==
"no");
300 if (is_parseable_as<long long>(
text)) {
303 if (is_parseable_as<double>(
text)) {
315 "Parameter \"" << name_in <<
"\" already exists in list \"" << list.
name() <<
"\"\n");
319 namespace YAMLParameterList {
330 virtual void at_shift(
any& result_any,
int token, std::string& text) {
333 case Teuchos::YAML::TOK_NEWLINE: {
334 std::string& result = make_any_ref<std::string>(result_any);
338 case Teuchos::YAML::TOK_SPACE:
339 case Teuchos::YAML::TOK_OTHER: {
340 result_any = text.at(0);
345 virtual void at_reduce(
any& result_any,
int prod, std::vector<any>& rhs) {
348 case Teuchos::YAML::PROD_DOC:
349 case Teuchos::YAML::PROD_DOC2: {
350 std::size_t offset = prod == Teuchos::YAML::PROD_DOC2 ? 1 : 0;
352 swap(result_any, rhs.at(offset));
356 case Teuchos::YAML::PROD_TOP_BMAP: {
361 result_any = pair_rhs_any;
364 case Teuchos::YAML::PROD_TOP_FIRST: {
366 swap(result_any, rhs.at(0));
370 case Teuchos::YAML::PROD_TOP_NEXT: {
373 "Can't specify multiple top-level ParameterLists in one YAML file!\n");
374 swap(result_any, rhs.at(1));
376 swap(result_any, rhs.at(0));
380 case Teuchos::YAML::PROD_BMAP_FIRST:
381 case Teuchos::YAML::PROD_FMAP_FIRST: {
387 case Teuchos::YAML::PROD_BMAP_NEXT: {
391 case Teuchos::YAML::PROD_FMAP_NEXT: {
395 case Teuchos::YAML::PROD_BMAP_SCALAR:
396 case Teuchos::YAML::PROD_FMAP_SCALAR:
397 case Teuchos::YAML::PROD_FMAP_FMAP:
398 case Teuchos::YAML::PROD_FMAP_FSEQ: {
400 map_item(result_any, rhs.at(0), rhs.at(4), scalar_type);
403 case Teuchos::YAML::PROD_BMAP_BSCALAR: {
407 case Teuchos::YAML::PROD_BMAP_BVALUE: {
408 map_item(result_any, rhs.at(0), rhs.at(4));
411 case Teuchos::YAML::PROD_BVALUE_EMPTY: {
415 case Teuchos::YAML::PROD_BVALUE_BMAP:
416 case Teuchos::YAML::PROD_BVALUE_BSEQ: {
417 swap(result_any, rhs.at(1));
420 case Teuchos::YAML::PROD_BMAP_FMAP: {
421 map_item(result_any, rhs.at(0), rhs.at(4));
424 case Teuchos::YAML::PROD_BMAP_FSEQ: {
428 map_item(result_any, rhs.at(0), rhs.at(4), scalar_type);
431 case Teuchos::YAML::PROD_BSEQ_FIRST: {
435 case Teuchos::YAML::PROD_BSEQ_NEXT: {
439 case Teuchos::YAML::PROD_BSEQ_SCALAR: {
440 swap(result_any, rhs.at(3));
445 case Teuchos::YAML::PROD_BSEQ_BSCALAR: {
446 swap(result_any, rhs.at(2));
449 case Teuchos::YAML::PROD_BSEQ_BMAP:
450 case Teuchos::YAML::PROD_BSEQ_BMAP_TRAIL:
451 case Teuchos::YAML::PROD_BSEQ_FMAP: {
452 throw ParserFail(
"Can't interpret a map inside a sequence as a Teuchos Parameter");
454 case Teuchos::YAML::PROD_BSEQ_BSEQ: {
455 swap(result_any, rhs.at(3));
458 case Teuchos::YAML::PROD_BSEQ_BSEQ_TRAIL: {
459 swap(result_any, rhs.at(4));
462 case Teuchos::YAML::PROD_BSEQ_FSEQ: {
463 swap(result_any, rhs.at(3));
466 case Teuchos::YAML::PROD_FMAP: {
467 swap(result_any, rhs.at(2));
470 case Teuchos::YAML::PROD_FMAP_EMPTY: {
474 case Teuchos::YAML::PROD_FSEQ: {
475 swap(result_any, rhs.at(2));
480 case Teuchos::YAML::PROD_FSEQ_EMPTY: {
484 case Teuchos::YAML::PROD_FSEQ_FIRST: {
488 case Teuchos::YAML::PROD_FSEQ_NEXT: {
492 case Teuchos::YAML::PROD_FSEQ_SCALAR: {
493 swap(result_any, rhs.at(1));
498 case Teuchos::YAML::PROD_FSEQ_FSEQ:
499 case Teuchos::YAML::PROD_FSEQ_FMAP: {
500 swap(result_any, rhs.at(1));
503 case Teuchos::YAML::PROD_SCALAR_QUOTED:
504 case Teuchos::YAML::PROD_MAP_SCALAR_QUOTED: {
505 swap(result_any, rhs.at(0));
508 case Teuchos::YAML::PROD_SCALAR_RAW:
509 case Teuchos::YAML::PROD_MAP_SCALAR_RAW: {
510 Scalar& scalar = make_any_ref<Scalar>(result_any);
512 scalar.
text = any_ref_cast<std::string>(rhs.at(0));
513 scalar.
text += any_ref_cast<std::string>(rhs.at(1));
514 if (prod == Teuchos::YAML::PROD_MAP_SCALAR_RAW) {
515 scalar.
text += any_ref_cast<std::string>(rhs.at(2));
522 case Teuchos::YAML::PROD_SCALAR_HEAD_OTHER:
523 case Teuchos::YAML::PROD_SCALAR_HEAD_DOT:
524 case Teuchos::YAML::PROD_SCALAR_HEAD_DASH:
525 case Teuchos::YAML::PROD_SCALAR_HEAD_DOT_DOT: {
527 if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_OTHER) offset = 0;
528 else if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DOT_DOT) offset = 2;
530 char second = any_cast<
char>(rhs.at(offset));
531 std::string& result = make_any_ref<std::string>(result_any);
532 if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DOT) result +=
'.';
533 else if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DASH) result +=
'-';
534 else if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DOT_DOT) result +=
"..";
538 case Teuchos::YAML::PROD_SCALAR_DQUOTED:
539 case Teuchos::YAML::PROD_SCALAR_SQUOTED: {
540 std::string& first = any_ref_cast<std::string>(rhs.at(1));
541 std::string& rest = any_ref_cast<std::string>(rhs.at(2));
542 Scalar& scalar = make_any_ref<Scalar>(result_any);
543 scalar.
text += first;
545 if (prod == Teuchos::YAML::PROD_SCALAR_DQUOTED) {
547 }
else if (prod == Teuchos::YAML::PROD_SCALAR_SQUOTED) {
550 scalar.tag_type = -1;
553 case Teuchos::YAML::PROD_MAP_SCALAR_ESCAPED_EMPTY: {
554 result_any = std::string();
557 case Teuchos::YAML::PROD_MAP_SCALAR_ESCAPED_NEXT: {
558 swap(result_any, rhs.at(0));
559 std::string& str = any_ref_cast<std::string>(result_any);
561 str += any_ref_cast<std::string>(rhs.at(2));
564 case Teuchos::YAML::PROD_TAG: {
565 swap(result_any, rhs.at(2));
568 case Teuchos::YAML::PROD_BSCALAR: {
569 std::size_t parent_indent_level =
570 this->symbol_indentation_stack.at(
571 this->symbol_indentation_stack.size() - 5);
572 std::string& header = any_ref_cast<std::string>(rhs.at(0));
573 std::string& leading_empties_or_comments =
574 any_ref_cast<std::string>(rhs.at(2));
575 std::string& rest = any_ref_cast<std::string>(rhs.at(4));
576 std::string& content = make_any_ref<std::string>(result_any);
580 header, leading_empties_or_comments, rest,
584 case Teuchos::YAML::PROD_BSCALAR_FIRST: {
585 swap(result_any, rhs.at(0));
589 case Teuchos::YAML::PROD_BSCALAR_NEXT:
590 case Teuchos::YAML::PROD_BSCALAR_LINE:
591 case Teuchos::YAML::PROD_DESCAPE_NEXT:
592 case Teuchos::YAML::PROD_SESCAPE_NEXT: {
593 swap(result_any, rhs.at(0));
594 std::string& str = any_ref_cast<std::string>(result_any);
595 str += any_ref_cast<std::string>(rhs.at(1));
598 case Teuchos::YAML::PROD_BSCALAR_INDENT: {
599 swap(result_any, rhs.at(1));
602 case Teuchos::YAML::PROD_BSCALAR_HEADER_LITERAL:
603 case Teuchos::YAML::PROD_BSCALAR_HEADER_FOLDED: {
604 std::string& result = make_any_ref<std::string>(result_any);
605 if (prod == Teuchos::YAML::PROD_BSCALAR_HEADER_LITERAL) {
610 std::string& rest = any_ref_cast<std::string>(rhs.at(1));
614 case Teuchos::YAML::PROD_DESCAPE: {
615 std::string& str = make_any_ref<std::string>(result_any);
616 std::string& rest = any_ref_cast<std::string>(rhs.at(2));
617 str += any_cast<
char>(rhs.at(1));
621 case Teuchos::YAML::PROD_SESCAPE: {
622 std::string& str = make_any_ref<std::string>(result_any);
623 std::string& rest = any_ref_cast<std::string>(rhs.at(2));
628 case Teuchos::YAML::PROD_OTHER_FIRST:
629 case Teuchos::YAML::PROD_SPACE_PLUS_FIRST: {
630 std::string& str = make_any_ref<std::string>(result_any);
631 str.push_back(any_cast<char>(rhs.at(0)));
634 case Teuchos::YAML::PROD_SCALAR_TAIL_SPACE:
635 case Teuchos::YAML::PROD_SCALAR_TAIL_OTHER:
636 case Teuchos::YAML::PROD_DESCAPED_DQUOTED:
637 case Teuchos::YAML::PROD_DQUOTED_COMMON:
638 case Teuchos::YAML::PROD_SQUOTED_COMMON:
639 case Teuchos::YAML::PROD_ANY_COMMON:
640 case Teuchos::YAML::PROD_COMMON_SPACE:
641 case Teuchos::YAML::PROD_COMMON_OTHER:
642 case Teuchos::YAML::PROD_BSCALAR_HEAD_OTHER: {
643 swap(result_any, rhs.at(0));
647 case Teuchos::YAML::PROD_DQUOTED_NEXT:
648 case Teuchos::YAML::PROD_SQUOTED_NEXT:
649 case Teuchos::YAML::PROD_ANY_NEXT:
650 case Teuchos::YAML::PROD_SCALAR_TAIL_NEXT:
651 case Teuchos::YAML::PROD_SPACE_STAR_NEXT:
652 case Teuchos::YAML::PROD_SPACE_PLUS_NEXT:
653 case Teuchos::YAML::PROD_BSCALAR_HEAD_NEXT: {
655 "leading characters in " << prod <<
": any was empty\n");
656 swap(result_any, rhs.at(0));
657 std::string& str = any_ref_cast<std::string>(result_any);
658 str += any_cast<
char>(rhs.at(1));
661 case Teuchos::YAML::PROD_DQUOTED_EMPTY:
662 case Teuchos::YAML::PROD_SQUOTED_EMPTY:
663 case Teuchos::YAML::PROD_ANY_EMPTY:
664 case Teuchos::YAML::PROD_DESCAPE_EMPTY:
665 case Teuchos::YAML::PROD_SESCAPE_EMPTY:
666 case Teuchos::YAML::PROD_SCALAR_TAIL_EMPTY:
667 case Teuchos::YAML::PROD_SPACE_STAR_EMPTY:
668 case Teuchos::YAML::PROD_BSCALAR_HEAD_EMPTY: {
669 result_any = std::string();
672 case Teuchos::YAML::PROD_DESCAPED_DQUOT:
673 case Teuchos::YAML::PROD_SQUOTED_DQUOT:
674 case Teuchos::YAML::PROD_ANY_DQUOT: {
678 case Teuchos::YAML::PROD_DESCAPED_SLASH:
679 case Teuchos::YAML::PROD_SQUOTED_SLASH:
680 case Teuchos::YAML::PROD_ANY_SLASH: {
684 case Teuchos::YAML::PROD_SCALAR_TAIL_SQUOT:
685 case Teuchos::YAML::PROD_DQUOTED_SQUOT:
686 case Teuchos::YAML::PROD_ANY_SQUOT: {
690 case Teuchos::YAML::PROD_COMMON_COLON: {
694 case Teuchos::YAML::PROD_SCALAR_TAIL_DOT:
695 case Teuchos::YAML::PROD_COMMON_DOT: {
699 case Teuchos::YAML::PROD_SCALAR_TAIL_DASH:
700 case Teuchos::YAML::PROD_COMMON_DASH:
701 case Teuchos::YAML::PROD_BSCALAR_HEAD_DASH: {
705 case Teuchos::YAML::PROD_COMMON_PIPE: {
709 case Teuchos::YAML::PROD_COMMON_LSQUARE: {
713 case Teuchos::YAML::PROD_COMMON_RSQUARE: {
717 case Teuchos::YAML::PROD_COMMON_LCURLY: {
721 case Teuchos::YAML::PROD_COMMON_RCURLY: {
725 case Teuchos::YAML::PROD_COMMON_RANGLE: {
729 case Teuchos::YAML::PROD_COMMON_COMMA: {
733 case Teuchos::YAML::PROD_COMMON_PERCENT: {
737 case Teuchos::YAML::PROD_COMMON_EXCL: {
744 ParameterList& list = make_any_ref<ParameterList>(result_any);
751 swap(result_any, items);
758 PLPair& result = make_any_ref<PLPair>(result_any);
760 std::string& key = any_ref_cast<
Scalar>(key_any).text;
761 swap(result.
key, key);
764 if (value_any.
type() ==
typeid(bool)) {
765 bool value = any_cast<
bool>(value_any);
767 }
else if (value_any.
type() ==
typeid(int)) {
768 int value = any_cast<
int>(value_any);
770 }
else if (value_any.
type() ==
typeid(
long long)) {
771 long long value = any_cast<
long long>(value_any);
773 }
else if (value_any.
type() ==
typeid(double)) {
774 double value = any_cast<
double>(value_any);
776 }
else if (value_any.
type() ==
typeid(std::string)) {
777 std::string& value = any_ref_cast<std::string >(value_any);
806 swap(result_pl, value);
807 result_pl.setName(result.
key);
809 std::string msg =
"unexpected YAML map value type ";
810 msg += value_any.
type().name();
811 msg +=
" for key \"";
814 throw ParserFail(msg);
820 if (scalar_type == -1) {
826 value_any = parse_as<int>(scalar_value.
text);
828 value_any = parse_as<long long>(scalar_value.
text);
830 value_any = parse_as<double>(scalar_value.
text);
832 value_any = scalar_value.
text;
836 if (scalar_type == -1) {
837 if (scalars.
size() == 0) {
838 throw ParserFail(
"implicitly typed arrays can't be empty\n"
839 "(need to determine their element type)\n");
845 scalar_type = std::min(scalar_type, scalars[i].infer_type());
851 result[i] = parse_as<int>(scalars[i].text);
857 result[i] = parse_as<long long>(scalars[i].text);
863 result[i] = parse_as<double>(scalars[i].text);
869 result[i] = scalars[i].text;
875 if (scalar_type == -1) {
876 if (scalars.
size() == 0) {
877 throw ParserFail(
"implicitly typed 2D arrays can't be empty\n"
878 "(need to determine their element type)\n");
884 if (scalars[0].size() == 0) {
885 throw ParserFail(
"implicitly typed 2D arrays can't have empty rows\n"
886 "(need to determine their element type)\n");
888 if (scalars[i].size() != scalars[0].size()) {
889 throw ParserFail(
"2D array: sub-arrays are different sizes");
892 int item_type = scalars[i][j].infer_type();
893 scalar_type = std::min(scalar_type, item_type);
901 result(i, j) = parse_as<int>(scalars[i][j].text);
909 result(i, j) = parse_as<long long>(scalars[i][j].text);
917 result(i, j) = parse_as<double>(scalars[i][j].text);
925 result(i, j) = scalars[i][j].text;
933 if (tag_any.
type() !=
typeid(std::string))
return -1;
934 std::string& text = any_ref_cast<std::string>(tag_any);
935 if (text.find(
"bool") != std::string::npos)
return Scalar::BOOL;
936 else if (text.find(
"int") != std::string::npos)
return Scalar::INT;
937 else if (text.find(
"double") != std::string::npos)
return Scalar::DOUBLE;
938 else if (text.find(
"string") != std::string::npos)
return Scalar::STRING;
940 std::string msg =
"Unable to parse type tag \"";
943 throw ParserFail(msg);
959 throw Teuchos::ParserFail(
960 "bug in YAMLParameterList::Reader: unexpected type for first sequence item");
965 swap(result_any, items);
977 throw Teuchos::ParserFail(
978 "bug in YAMLParameterList::Reader: unexpected type for next sequence item");
983 std::size_t parent_indent_level,
984 std::string
const& header,
985 std::string
const& leading_empties_or_comments,
986 std::string
const& rest,
987 std::string& content,
988 std::string& comment) {
991 char chomping_indicator;
992 std::size_t indentation_indicator = 0;
994 std::stringstream ss(header.substr(1,std::string::npos));
995 if (header.size() > 1 &&
my_isdigit(header[1])) {
996 ss >> indentation_indicator;
998 indentation_indicator += parent_indent_level;
1000 if (!(ss >> chomping_indicator)) chomping_indicator =
'\0';
1003 std::size_t first_newline = leading_empties_or_comments.find_first_of(
"\r\n");
1004 std::string newline;
1005 if (first_newline > 0 && leading_empties_or_comments[first_newline - 1] ==
'\r') {
1010 std::size_t keep_beg = first_newline + 1 - newline.size();
1011 if (leading_empties_or_comments[0] ==
'#') {
1012 comment = leading_empties_or_comments.substr(1, keep_beg);
1015 std::size_t content_beg = leading_empties_or_comments.find_first_not_of(
"\r\n ");
1016 if (content_beg == std::string::npos) content_beg = leading_empties_or_comments.size();
1017 std::size_t newline_before_content = leading_empties_or_comments.rfind(
"\n", content_beg);
1018 std::size_t num_indent_spaces = (content_beg - newline_before_content) - 1;
1021 if (indentation_indicator > 0) {
1023 Teuchos::ParserFail,
1024 "Indentation indicator " << indentation_indicator <<
" > leading spaces " << num_indent_spaces);
1025 num_indent_spaces = indentation_indicator;
1028 content = leading_empties_or_comments.substr(keep_beg, std::string::npos);
1037 auto last_newline = content.find_last_of(
"\n", content.size() - 2);
1038 if (last_newline == std::string::npos)
break;
1039 std::size_t num_spaces = 0;
1040 for (
auto ispace = last_newline + 1;
1041 ispace < content.size() && content[ispace] ==
' ';
1045 if (num_spaces >= num_indent_spaces)
break;
1046 content.erase(content.begin() + last_newline + 1, content.end());
1049 std::size_t unindent_pos = 0;
1051 std::size_t next_newline = content.find_first_of(
"\n", unindent_pos);
1052 if (next_newline == std::string::npos)
break;
1053 std::size_t start_cut = next_newline + 1;
1055 if (style ==
'>') start_cut -= newline.size();
1056 std::size_t end_cut = next_newline + 1;
1059 while (end_cut < content.size() && content[end_cut] ==
' ') {
1063 end_cut = std::min(next_newline + 1 + num_indent_spaces, end_cut);
1065 content = content.substr(0, start_cut) +
1066 content.substr(end_cut, std::string::npos);
1067 unindent_pos = start_cut;
1069 if (chomping_indicator !=
'+') {
1071 if (chomping_indicator !=
'-') content += newline;
1075 content = content.substr(newline.size(), std::string::npos);
1089 if (paramList->
name() ==
"ANONYMOUS") {
1103 if (paramList->
name() ==
"ANONYMOUS") {
1117 const std::string& name)
1122 if (paramList->
name() ==
"ANONYMOUS") {
1140 std::stringstream ss(yamlStr);
1146 std::ostream &yamlOut
1154 const std::string &yamlFileName
1165 std::string yamlFileName;
1166 if(xmlFileName.find(
".xml") == std::string::npos)
1168 yamlFileName = xmlFileName +
".yaml";
1172 yamlFileName = xmlFileName.substr(0, xmlFileName.length() - 4) +
".yaml";
1175 return yamlFileName;
1187 std::istreambuf_iterator<char> begin(xmlStream);
1188 std::istreambuf_iterator<char> end;
1189 std::string xmlString(begin, end);
1194 namespace YAMLParameterList
1199 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1200 auto yaml_input = ::YAML::LoadAll(text);
1201 return readParams(yaml_input);
1205 reader.read_string(result, text, name);
1208 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1213 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1214 auto yaml_input = ::YAML::LoadAllFromFile(yamlFile);
1215 return readParams(yaml_input);
1219 reader.read_file(result, yamlFile);
1222 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1227 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1228 auto yaml_input = ::YAML::LoadAll(yaml);
1229 return readParams(yaml_input);
1233 reader.read_stream(result, yaml,
"parseYamlStream");
1236 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1242 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1249 for(
size_t i = 0; i < lists.size(); i++)
1251 processMapNode(lists[i], *pl,
true);
1258 if (node.Type() != ::YAML::NodeType::Map)
1260 throw YamlStructureError(
"All top-level elements of the YAML file must be maps.");
1264 parent.
setName(node.begin()->first.as<std::string>());
1265 processMapNode(node.begin()->second, parent);
1269 for (::YAML::const_iterator i = node.begin(); i != node.end(); i++)
1272 if(i->first.Type() != ::YAML::NodeType::Scalar)
1274 throw YamlKeyError(
"Keys must be YAML scalars (int, double, or string)");
1277 const std::string key = quoted_as<std::string>(i->first);
1278 processKeyValueNode(key, i->second, parent, topLevel);
1283 void processKeyValueNode(
const std::string& key, const ::YAML::Node& node,
Teuchos::ParameterList& parent,
bool topLevel)
1287 if(node.Type() == ::YAML::NodeType::Scalar)
1291 safe_set_entry<int>(parent, key, quoted_as<int>(node));
1297 safe_set_entry<long long>(parent, key, quoted_as<long long>(node));
1303 safe_set_entry<double>(parent, key, quoted_as<double>(node));
1309 bool raw_bool = quoted_as<bool>(node);
1317 std::string raw_string = quoted_as<std::string>(node);
1320 safe_set_entry<bool>(parent, key, raw_bool);
1324 safe_set_entry<std::string>(parent, key, raw_string);
1329 safe_set_entry<std::string>(parent, key, quoted_as<std::string>(node));
1335 else if(node.Type() == ::YAML::NodeType::Map)
1339 processMapNode(node, parent);
1344 processMapNode(node, sublist);
1347 else if(node.Type() == ::YAML::NodeType::Sequence)
1349 int ndim = getYamlArrayDim(node);
1352 ::YAML::Node
const& first_value = *(node.begin());
1355 quoted_as<int>(first_value);
1356 safe_set_entry<Teuchos::Array<int>>(parent, key, getYamlArray<int>(node));
1362 quoted_as<double>(first_value);
1363 safe_set_entry<Teuchos::Array<double>>(parent, key, getYamlArray<double>(node));
1369 quoted_as<std::string>(first_value);
1370 safe_set_entry<Teuchos::Array<std::string>>(parent, key, getYamlArray<std::string>(node));
1374 throw YamlSequenceError(std::string(
"Array \"") + key +
"\" must contain int, double, bool or string");
1381 bool is_ragged = checkYamlTwoDArrayIsRagged(node);
1382 ::YAML::Node
const& first_value = *(node.begin()->begin());
1385 quoted_as<int>(first_value);
1388 safe_set_entry<arr_t>(parent, key, getYaml2DRaggedArray<arr_t, int>(node, ndim, key));
1390 safe_set_entry<Teuchos::TwoDArray<int>>(parent, key, getYamlTwoDArray<int>(node));
1397 quoted_as<double>(first_value);
1400 safe_set_entry<arr_t>(parent, key, getYaml2DRaggedArray<arr_t, double>(node, ndim, key));
1402 safe_set_entry<Teuchos::TwoDArray<double>>(parent, key, getYamlTwoDArray<double>(node));
1409 quoted_as<std::string>(first_value);
1412 safe_set_entry<arr_t>(parent, key, getYaml2DRaggedArray<arr_t, std::string>(node, ndim, key));
1414 safe_set_entry<Teuchos::TwoDArray<std::string>>(parent, key, getYamlTwoDArray<std::string>(node));
1419 throw YamlSequenceError(std::string(
"TwoDArray \"") + key +
"\" must contain int, double, bool or string");
1426 ::YAML::Node
const& first_value = *(node.begin()->begin()->begin());
1429 quoted_as<int>(first_value);
1431 safe_set_entry<arr_t>(parent, key, getYaml3DArray<arr_t, int>(node, ndim, key));
1437 quoted_as<double>(first_value);
1439 safe_set_entry<arr_t>(parent, key, getYaml3DArray<arr_t, double>(node, ndim, key));
1446 quoted_as<std::string>(first_value);
1448 safe_set_entry<arr_t>(parent, key, getYaml3DArray<arr_t, std::string>(node, ndim, key));
1453 throw YamlSequenceError(std::string(
"3DArray \"") + key +
"\" must contain int, double, bool or string");
1459 else if(node.Type() == ::YAML::NodeType::Null)
1467 throw YamlUndefinedNodeError(
"Value type in a key-value pair must be one of: int, double, string, array, sublist.");
1471 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1476 std::ios_base::fmtflags flags = yaml.flags();
1478 std::ostringstream testStream;
1479 testStream.flags(flags);
1481 testStream << testVal;
1482 bool popFlags =
false;
1483 if(testStream.str() ==
"1")
1489 std::cout <<
"Warning: yaml stream format flags would confuse double with integer value with int.\n";
1490 std::cout <<
"Setting std::ios::showpoint on the stream to fix this (will restore flags when done)\n";
1491 std::ios_base::fmtflags flagsCopy = flags;
1492 flagsCopy |= std::ios::showpoint;
1495 yaml <<
"%YAML 1.1\n---\n";
1496 yaml << pl.
name() <<
':';
1515 std::ofstream yaml(yamlFile.c_str());
1522 yaml << std::scientific << std::setprecision(17);
1542 template <
typename T>
1544 static void write(T
const& x, std::ostream& stream) {
1551 static void write(
double const& x, std::ostream& stream) {
1558 static void write(std::string
const& x, std::ostream& stream) {
1563 template <
typename T>
1570 if (i) stream <<
", ";
1574 if (j) stream <<
", ";
1584 for(
int i = 0; i < indentLevel; i++)
1592 writeParameterList(Teuchos::getValue<Teuchos::ParameterList>(entry), yaml, indentLevel + 2);
1601 for(
int i = 0; i < arr.
size(); i++)
1604 if(i != arr.size() - 1)
1611 for(
int i = 0; i < arr.
size(); i++)
1614 if(i != arr.size() - 1)
1621 for(
int i = 0; i < arr.
size(); i++)
1624 if(i != arr.
size() - 1)
1631 for(
int i = 0; i < arr.
size(); i++)
1634 if(i != arr.
size() - 1)
1644 writeYamlTwoDArray<int>(
1645 Teuchos::getValue<Teuchos::TwoDArray<int> >(entry), yaml);
1649 writeYamlTwoDArray<long long>(
1650 Teuchos::getValue<Teuchos::TwoDArray<long long> >(entry), yaml);
1654 writeYamlTwoDArray<double>(
1655 Teuchos::getValue<Teuchos::TwoDArray<double> >(entry), yaml);
1659 writeYamlTwoDArray<std::string>(
1660 Teuchos::getValue<Teuchos::TwoDArray<std::string> >(entry), yaml);
1663 else if(entry.
isType<
int>())
1665 yaml << Teuchos::getValue<int>(entry);
1667 else if(entry.
isType<
long long>())
1669 yaml << Teuchos::getValue<long long>(entry);
1671 else if(entry.
isType<
double>())
1675 else if(entry.
isType<std::string>())
1677 std::string& str = Teuchos::getValue<std::string>(entry);
1678 if(strchr(str.c_str(),
'\n'))
1684 std::size_t first_non_newline_pos = str.find_first_not_of(
"\r\n");
1685 if (first_non_newline_pos != std::string::npos &&
1686 str[first_non_newline_pos] ==
' ') {
1689 if (str[str.size() - 1] !=
'\n') yaml <<
"-";
1695 size_t next = str.find(
'\n', index);
1696 for(
int i = 0; i < indentLevel + 2; i++)
1700 if(next == std::string::npos)
1702 yaml << str.substr(index, std::string::npos);
1707 yaml << str.substr(index, next - index) <<
'\n';
1717 else if(entry.
isType<
bool>())
1719 yaml << (Teuchos::getValue<bool>(entry) ?
"true" :
"false");
1730 for (std::size_t i = 0; i < str.size(); ++i) {
1731 if (str[i] ==
'\'') yaml <<
"''";
1732 else yaml << str[i];
1748 char const*
const control_chars =
":'{}[],&*#?|<>=!%@\\";
1749 return s.find_first_of(control_chars) != std::string::npos;
1758 is_parseable_as<long long>(s) ||
1759 is_parseable_as<double>(s);
A thin wrapper around the Teuchos Array class that allows for 2 dimensional arrays.
Teuchos::RCP< Teuchos::ParameterList > parseYamlStream(std::istream &yaml)
const std::string & name() const
The name of this ParameterList.
C++ Standard Library compatable filtered iterator.
void writeYamlTwoDArray(Teuchos::TwoDArray< T > const &arr, std::ostream &stream)
ParameterList & setEntry(const std::string &name, U &&entry)
Set a parameter directly as a ParameterEntry.
ConstIterator end() const
An iterator pointing beyond the last entry.
void updateParametersFromYamlFile(const std::string &yamlFileName, const Teuchos::Ptr< Teuchos::ParameterList > ¶mList)
static bool my_isdigit(char ch)
Functions to convert between ParameterList and YAML.
static void write(T const &x, std::ostream &stream)
static bool containsSpecialCharacters(std::string const &s)
std::string remove_trailing_whitespace_and_newlines(std::string const &in)
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromYamlCString(const char *const data, const Teuchos::Ptr< Teuchos::ParameterList > ¶mList, bool overwrite)
void writeParameterListToYamlOStream(const ParameterList ¶mList, std::ostream &yamlOut)
This object is held as the "value" in the Teuchos::ParameterList std::map.
void safe_set_entry(ParameterList &list, std::string const &name_in, ParameterEntry const &entry_in)
void generalWriteString(const std::string &str, std::ostream &yaml)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool isArray() const
Test if the type of data being contained is a Teuchos::Array.
bool has_value() const
Checks whether the object contains a value.
int interpret_tag(any &tag_any)
void generalWriteDouble(double d, std::ostream &yaml)
bool isType() const
Test the type of the data being contained.
Teuchos::RCP< Teuchos::ParameterList > getParametersFromYamlString(const std::string &yamlStr)
Ordinal numParams() const
Get the number of stored parameters.
virtual void at_shift(any &result_any, int token, std::string &text)
void writeYamlFile(const std::string &yamlFile, const Teuchos::ParameterList &pl)
static void write(std::string const &x, std::ostream &stream)
Modified boost::any class, which is a container for a templated value.
void seq_next_item(any &result_any, any &items, any &next_item)
void resolve_map_value(any &value_any, int scalar_type=-1) const
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
void seq_first_item(any &result_any, any &first_any)
std::string remove_trailing_whitespace(std::string const &in)
ParameterList & setList(bool isDefault=false, const std::string &docString="")
Create a parameter entry that is an empty list.
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
T parse_as(std::string const &text)
A thin wrapper around the Array class which causes it to be interpreted as a 2D Array.
std::ostream & operator<<(std::ostream &os, BigUInt< n > a)
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
void map_first_item(any &result_any, any &first_item)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Teuchos::RCP< Teuchos::ParameterList > parseYamlText(const std::string &text, const std::string &name)
void writeParameter(const std::string ¶mName, const Teuchos::ParameterEntry &entry, std::ostream &yaml, int indentLevel)
void writeParameterList(const Teuchos::ParameterList &pl, std::ostream &yaml, int indentLevel)
void handle_block_scalar(std::size_t parent_indent_level, std::string const &header, std::string const &leading_empties_or_comments, std::string const &rest, std::string &content, std::string &comment)
size_type getNumCols() const
returns the number of columns in the TwoDArray.
void resizeCols(size_type numberOfCols)
Changes the number of rows in the matrix.
void resizeRows(size_type numberOfRows)
Changes the number of rows in the matrix.
void map_next_item(any &result_any, any &items, any &next_item)
virtual void at_reduce(any &result_any, int prod, std::vector< any > &rhs)
ConstIterator begin() const
An iterator pointing to the first entry.
bool isList() const
Return whether or not the value itself is a list.
void writeParameterListToYamlFile(const ParameterList ¶mList, const std::string &yamlFileName)
bool is_parseable_as< int >(std::string const &text)
A list of parameters of arbitrary type.
ParameterList & setParameters(const ParameterList &source)
static char my_tolower(char ch)
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i->second)
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
Teuchos::RCP< Teuchos::ParameterList > parseYamlFile(const std::string &yamlFile)
bool is_parseable_as(std::string const &text)
size_type getNumRows() const
returns the number of rows in the TwoDArray.
void push_back(const value_type &x)
void updateParametersFromYamlString(const std::string &yamlData, const Teuchos::Ptr< Teuchos::ParameterList > ¶mList, bool overwrite, const std::string &name)
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
bool parse_as< bool >(std::string const &text)
bool isTwoDArray() const
Test if the type of data being contained is a Teuchos::TwoDArray.
void map_item(any &result_any, any &key_any, any &value_any, int scalar_type=-1)
bool is_parseable_as< bool >(std::string const &text)
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...
ParameterList & setName(const std::string &name)
Set the name of *this list.
bool stringNeedsQuotes(const std::string &s)
static void write(double const &x, std::ostream &stream)
const std::type_info & type() const
Return the type of value being stored.
std::string convertXmlToYaml(const std::string &xmlFileName)
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
Teuchos::RCP< Teuchos::ParameterList > getParametersFromYamlFile(const std::string &yamlFileName)
bool operator==(BigUInt< n > const &a, BigUInt< n > const &b)
void writeYamlStream(std::ostream &yaml, const Teuchos::ParameterList &pl)
Simple helper functions that make it easy to read and write Yaml to and from a parameterlist.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...