57 #include "Teuchos_Reader.hpp"
59 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
60 #include "yaml-cpp/yaml.h"
61 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
62 #include "Teuchos_YAML.hpp"
67 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
82 static T eval(::YAML::Node
const& node) {
84 if (node.Tag() ==
"!") {
85 throw std::runtime_error(
"quoted_as from quoted string to number");
92 struct QuotedAs<std::string> {
94 static std::string eval(::YAML::Node
const& node) {
return node.as<std::string>(); }
98 static T quoted_as(::YAML::Node
const& node) {
return QuotedAs<T>::eval(node); }
104 for(::YAML::const_iterator it = node.begin(); it != node.end(); it++)
111 bool checkYamlTwoDArrayIsRagged(const ::YAML::Node& node)
114 for (::YAML::const_iterator it = node.begin(); it != node.end(); ++it)
116 if (it->size() != node.begin()->size())
131 for (::YAML::const_iterator rit = node.begin(); rit != node.end(); ++rit)
134 for (::YAML::const_iterator cit = rit->begin(); cit != rit->end(); ++cit)
136 arr(i, j) = quoted_as<T>(*cit);
144 int getYamlArrayDim(const ::YAML::Node& node)
147 if (node.Type() == ::YAML::NodeType::Sequence)
150 if (node.begin()->Type() == ::YAML::NodeType::Sequence)
153 if (node.begin()->begin()->Type() == ::YAML::NodeType::Sequence)
162 template <
typename tarray_t,
typename T>
163 tarray_t getYaml2DRaggedArray(::YAML::Node node,
int ndim, std::string key)
168 for (::YAML::const_iterator it1 = node.begin(); it1 != node.end(); ++it1) {
169 for (::YAML::const_iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
171 } base_arr.push_back(sub_arr);
177 throw YamlSequenceError(std::string(
"MDArray \"" + key +
"\" must have dim 2."));
185 template <
typename tarray_t,
typename T>
186 tarray_t getYaml3DArray(::YAML::Node node,
int ndim, std::string key)
192 for (::YAML::const_iterator it1 = node.begin(); it1 != node.end(); ++it1) {
193 for (::YAML::const_iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
194 for (::YAML::const_iterator it3 = it2->begin(); it3 != it2->end(); ++it3) {
195 sub_sub_arr.
push_back(quoted_as<T>(*it3));
198 } base_arr.push_back(sub_arr);
204 throw YamlSequenceError(std::string(
"MDArray \"" + key +
"\" must have dim 3."));
209 template <
typename T>
210 void safe_set_entry(ParameterList& list, std::string
const& name_in, T
const& entry_in) {
212 "Parameter \"" << name_in <<
"\" already exists in list \"" << list.name() <<
"\"\n");
213 list.set(name_in, entry_in);
215 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
218 std::size_t new_end = 0;
219 for (std::size_t ri = 0; ri < in.size(); ++ri) {
220 std::size_t i = in.size() - 1 - ri;
221 if (in[i] !=
' ' && in[i] !=
'\t') {
226 return in.substr(0, new_end);
230 std::size_t new_end = 0;
231 for (std::size_t ri = 0; ri < in.size(); ++ri) {
232 std::size_t i = in.size() - 1 - ri;
233 if (in[i] !=
' ' && in[i] !=
'\t' && in[i] !=
'\n' && in[i] !=
'\r') {
238 return in.substr(0, new_end);
241 template <
typename T>
243 std::istringstream ss(text);
245 ss >> std::noskipws >> val;
246 return ss.eof() && !ss.fail();
251 std::istringstream ss(text);
252 using LL =
long long;
254 ss >> std::noskipws >> val;
255 return ss.eof() && !ss.fail() &&
256 (val >= LL(std::numeric_limits<int>::min())) &&
257 (val <= LL(std::numeric_limits<int>::max()));
260 template <
typename T>
262 std::istringstream ss(text);
271 return std::tolower(static_cast<unsigned char>(ch));
277 return std::isdigit(static_cast<unsigned char>(ch));
283 for (std::size_t i = 0; i < text.size(); ++i) {
286 return lower ==
"true" || lower ==
"yes" ||
287 lower ==
"false" || lower ==
"no";
293 for (std::size_t i = 0; i < text.size(); ++i) {
296 return !(lower ==
"false" || lower ==
"no");
335 if (is_parseable_as<long long>(
text)) {
338 if (is_parseable_as<double>(
text)) {
350 "Parameter \"" << name_in <<
"\" already exists in list \"" << list.
name() <<
"\"\n");
354 namespace YAMLParameterList {
365 virtual void at_shift(
any& result_any,
int token, std::string& text) {
368 case Teuchos::YAML::TOK_NEWLINE: {
369 std::string& result = make_any_ref<std::string>(result_any);
373 case Teuchos::YAML::TOK_SPACE:
374 case Teuchos::YAML::TOK_OTHER: {
375 result_any = text.at(0);
380 virtual void at_reduce(
any& result_any,
int prod, std::vector<any>& rhs) {
383 case Teuchos::YAML::PROD_DOC:
384 case Teuchos::YAML::PROD_DOC2: {
385 std::size_t offset = prod == Teuchos::YAML::PROD_DOC2 ? 1 : 0;
387 swap(result_any, rhs.at(offset));
391 case Teuchos::YAML::PROD_TOP_BMAP: {
396 result_any = pair_rhs_any;
399 case Teuchos::YAML::PROD_TOP_FIRST: {
401 swap(result_any, rhs.at(0));
405 case Teuchos::YAML::PROD_TOP_NEXT: {
408 "Can't specify multiple top-level ParameterLists in one YAML file!\n");
409 swap(result_any, rhs.at(1));
411 swap(result_any, rhs.at(0));
415 case Teuchos::YAML::PROD_BMAP_FIRST:
416 case Teuchos::YAML::PROD_FMAP_FIRST: {
422 case Teuchos::YAML::PROD_BMAP_NEXT: {
426 case Teuchos::YAML::PROD_FMAP_NEXT: {
430 case Teuchos::YAML::PROD_BMAP_SCALAR:
431 case Teuchos::YAML::PROD_FMAP_SCALAR:
432 case Teuchos::YAML::PROD_FMAP_FMAP:
433 case Teuchos::YAML::PROD_FMAP_FSEQ: {
435 map_item(result_any, rhs.at(0), rhs.at(4), scalar_type);
438 case Teuchos::YAML::PROD_BMAP_BSCALAR: {
442 case Teuchos::YAML::PROD_BMAP_BVALUE: {
443 map_item(result_any, rhs.at(0), rhs.at(4));
446 case Teuchos::YAML::PROD_BVALUE_EMPTY: {
450 case Teuchos::YAML::PROD_BVALUE_BMAP:
451 case Teuchos::YAML::PROD_BVALUE_BSEQ: {
452 swap(result_any, rhs.at(1));
455 case Teuchos::YAML::PROD_BMAP_FMAP: {
456 map_item(result_any, rhs.at(0), rhs.at(4));
459 case Teuchos::YAML::PROD_BMAP_FSEQ: {
463 map_item(result_any, rhs.at(0), rhs.at(4), scalar_type);
466 case Teuchos::YAML::PROD_BSEQ_FIRST: {
470 case Teuchos::YAML::PROD_BSEQ_NEXT: {
474 case Teuchos::YAML::PROD_BSEQ_SCALAR: {
475 swap(result_any, rhs.at(3));
480 case Teuchos::YAML::PROD_BSEQ_BSCALAR: {
481 swap(result_any, rhs.at(2));
484 case Teuchos::YAML::PROD_BSEQ_BMAP:
485 case Teuchos::YAML::PROD_BSEQ_BMAP_TRAIL:
486 case Teuchos::YAML::PROD_BSEQ_FMAP: {
487 throw ParserFail(
"Can't interpret a map inside a sequence as a Teuchos Parameter");
489 case Teuchos::YAML::PROD_BSEQ_BSEQ: {
490 swap(result_any, rhs.at(3));
493 case Teuchos::YAML::PROD_BSEQ_BSEQ_TRAIL: {
494 swap(result_any, rhs.at(4));
497 case Teuchos::YAML::PROD_BSEQ_FSEQ: {
498 swap(result_any, rhs.at(3));
501 case Teuchos::YAML::PROD_FMAP: {
502 swap(result_any, rhs.at(2));
505 case Teuchos::YAML::PROD_FMAP_EMPTY: {
509 case Teuchos::YAML::PROD_FSEQ: {
510 swap(result_any, rhs.at(2));
515 case Teuchos::YAML::PROD_FSEQ_EMPTY: {
519 case Teuchos::YAML::PROD_FSEQ_FIRST: {
523 case Teuchos::YAML::PROD_FSEQ_NEXT: {
527 case Teuchos::YAML::PROD_FSEQ_SCALAR: {
528 swap(result_any, rhs.at(1));
533 case Teuchos::YAML::PROD_FSEQ_FSEQ:
534 case Teuchos::YAML::PROD_FSEQ_FMAP: {
535 swap(result_any, rhs.at(1));
538 case Teuchos::YAML::PROD_SCALAR_QUOTED:
539 case Teuchos::YAML::PROD_MAP_SCALAR_QUOTED: {
540 swap(result_any, rhs.at(0));
543 case Teuchos::YAML::PROD_SCALAR_RAW:
544 case Teuchos::YAML::PROD_MAP_SCALAR_RAW: {
545 Scalar& scalar = make_any_ref<Scalar>(result_any);
547 scalar.
text = any_ref_cast<std::string>(rhs.at(0));
548 scalar.
text += any_ref_cast<std::string>(rhs.at(1));
549 if (prod == Teuchos::YAML::PROD_MAP_SCALAR_RAW) {
550 scalar.
text += any_ref_cast<std::string>(rhs.at(2));
557 case Teuchos::YAML::PROD_SCALAR_HEAD_OTHER:
558 case Teuchos::YAML::PROD_SCALAR_HEAD_DOT:
559 case Teuchos::YAML::PROD_SCALAR_HEAD_DASH:
560 case Teuchos::YAML::PROD_SCALAR_HEAD_DOT_DOT: {
562 if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_OTHER) offset = 0;
563 else if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DOT_DOT) offset = 2;
565 char second = any_cast<
char>(rhs.at(offset));
566 std::string& result = make_any_ref<std::string>(result_any);
567 if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DOT) result +=
'.';
568 else if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DASH) result +=
'-';
569 else if (prod == Teuchos::YAML::PROD_SCALAR_HEAD_DOT_DOT) result +=
"..";
573 case Teuchos::YAML::PROD_SCALAR_DQUOTED:
574 case Teuchos::YAML::PROD_SCALAR_SQUOTED: {
575 std::string& first = any_ref_cast<std::string>(rhs.at(1));
576 std::string& rest = any_ref_cast<std::string>(rhs.at(2));
577 Scalar& scalar = make_any_ref<Scalar>(result_any);
578 scalar.
text += first;
580 if (prod == Teuchos::YAML::PROD_SCALAR_DQUOTED) {
582 }
else if (prod == Teuchos::YAML::PROD_SCALAR_SQUOTED) {
585 scalar.tag_type = -1;
588 case Teuchos::YAML::PROD_MAP_SCALAR_ESCAPED_EMPTY: {
589 result_any = std::string();
592 case Teuchos::YAML::PROD_MAP_SCALAR_ESCAPED_NEXT: {
593 swap(result_any, rhs.at(0));
594 std::string& str = any_ref_cast<std::string>(result_any);
596 str += any_ref_cast<std::string>(rhs.at(2));
599 case Teuchos::YAML::PROD_TAG: {
600 swap(result_any, rhs.at(2));
603 case Teuchos::YAML::PROD_BSCALAR: {
604 std::size_t parent_indent_level =
605 this->symbol_indentation_stack.at(
606 this->symbol_indentation_stack.size() - 5);
607 std::string& header = any_ref_cast<std::string>(rhs.at(0));
608 std::string& leading_empties_or_comments =
609 any_ref_cast<std::string>(rhs.at(2));
610 std::string& rest = any_ref_cast<std::string>(rhs.at(4));
611 std::string& content = make_any_ref<std::string>(result_any);
615 header, leading_empties_or_comments, rest,
619 case Teuchos::YAML::PROD_BSCALAR_FIRST: {
620 swap(result_any, rhs.at(0));
624 case Teuchos::YAML::PROD_BSCALAR_NEXT:
625 case Teuchos::YAML::PROD_BSCALAR_LINE:
626 case Teuchos::YAML::PROD_DESCAPE_NEXT:
627 case Teuchos::YAML::PROD_SESCAPE_NEXT: {
628 swap(result_any, rhs.at(0));
629 std::string& str = any_ref_cast<std::string>(result_any);
630 str += any_ref_cast<std::string>(rhs.at(1));
633 case Teuchos::YAML::PROD_BSCALAR_INDENT: {
634 swap(result_any, rhs.at(1));
637 case Teuchos::YAML::PROD_BSCALAR_HEADER_LITERAL:
638 case Teuchos::YAML::PROD_BSCALAR_HEADER_FOLDED: {
639 std::string& result = make_any_ref<std::string>(result_any);
640 if (prod == Teuchos::YAML::PROD_BSCALAR_HEADER_LITERAL) {
645 std::string& rest = any_ref_cast<std::string>(rhs.at(1));
649 case Teuchos::YAML::PROD_DESCAPE: {
650 std::string& str = make_any_ref<std::string>(result_any);
651 std::string& rest = any_ref_cast<std::string>(rhs.at(2));
652 str += any_cast<
char>(rhs.at(1));
656 case Teuchos::YAML::PROD_SESCAPE: {
657 std::string& str = make_any_ref<std::string>(result_any);
658 std::string& rest = any_ref_cast<std::string>(rhs.at(2));
663 case Teuchos::YAML::PROD_OTHER_FIRST:
664 case Teuchos::YAML::PROD_SPACE_PLUS_FIRST: {
665 std::string& str = make_any_ref<std::string>(result_any);
666 str.push_back(any_cast<char>(rhs.at(0)));
669 case Teuchos::YAML::PROD_SCALAR_TAIL_SPACE:
670 case Teuchos::YAML::PROD_SCALAR_TAIL_OTHER:
671 case Teuchos::YAML::PROD_DESCAPED_DQUOTED:
672 case Teuchos::YAML::PROD_DQUOTED_COMMON:
673 case Teuchos::YAML::PROD_SQUOTED_COMMON:
674 case Teuchos::YAML::PROD_ANY_COMMON:
675 case Teuchos::YAML::PROD_COMMON_SPACE:
676 case Teuchos::YAML::PROD_COMMON_OTHER:
677 case Teuchos::YAML::PROD_BSCALAR_HEAD_OTHER: {
678 swap(result_any, rhs.at(0));
682 case Teuchos::YAML::PROD_DQUOTED_NEXT:
683 case Teuchos::YAML::PROD_SQUOTED_NEXT:
684 case Teuchos::YAML::PROD_ANY_NEXT:
685 case Teuchos::YAML::PROD_SCALAR_TAIL_NEXT:
686 case Teuchos::YAML::PROD_SPACE_STAR_NEXT:
687 case Teuchos::YAML::PROD_SPACE_PLUS_NEXT:
688 case Teuchos::YAML::PROD_BSCALAR_HEAD_NEXT: {
690 "leading characters in " << prod <<
": any was empty\n");
691 swap(result_any, rhs.at(0));
692 std::string& str = any_ref_cast<std::string>(result_any);
693 str += any_cast<
char>(rhs.at(1));
696 case Teuchos::YAML::PROD_DQUOTED_EMPTY:
697 case Teuchos::YAML::PROD_SQUOTED_EMPTY:
698 case Teuchos::YAML::PROD_ANY_EMPTY:
699 case Teuchos::YAML::PROD_DESCAPE_EMPTY:
700 case Teuchos::YAML::PROD_SESCAPE_EMPTY:
701 case Teuchos::YAML::PROD_SCALAR_TAIL_EMPTY:
702 case Teuchos::YAML::PROD_SPACE_STAR_EMPTY:
703 case Teuchos::YAML::PROD_BSCALAR_HEAD_EMPTY: {
704 result_any = std::string();
707 case Teuchos::YAML::PROD_DESCAPED_DQUOT:
708 case Teuchos::YAML::PROD_SQUOTED_DQUOT:
709 case Teuchos::YAML::PROD_ANY_DQUOT: {
713 case Teuchos::YAML::PROD_DESCAPED_SLASH:
714 case Teuchos::YAML::PROD_SQUOTED_SLASH:
715 case Teuchos::YAML::PROD_ANY_SLASH: {
719 case Teuchos::YAML::PROD_SCALAR_TAIL_SQUOT:
720 case Teuchos::YAML::PROD_DQUOTED_SQUOT:
721 case Teuchos::YAML::PROD_ANY_SQUOT: {
725 case Teuchos::YAML::PROD_COMMON_COLON: {
729 case Teuchos::YAML::PROD_SCALAR_TAIL_DOT:
730 case Teuchos::YAML::PROD_COMMON_DOT: {
734 case Teuchos::YAML::PROD_SCALAR_TAIL_DASH:
735 case Teuchos::YAML::PROD_COMMON_DASH:
736 case Teuchos::YAML::PROD_BSCALAR_HEAD_DASH: {
740 case Teuchos::YAML::PROD_COMMON_PIPE: {
744 case Teuchos::YAML::PROD_COMMON_LSQUARE: {
748 case Teuchos::YAML::PROD_COMMON_RSQUARE: {
752 case Teuchos::YAML::PROD_COMMON_LCURLY: {
756 case Teuchos::YAML::PROD_COMMON_RCURLY: {
760 case Teuchos::YAML::PROD_COMMON_RANGLE: {
764 case Teuchos::YAML::PROD_COMMON_COMMA: {
768 case Teuchos::YAML::PROD_COMMON_PERCENT: {
772 case Teuchos::YAML::PROD_COMMON_EXCL: {
779 ParameterList& list = make_any_ref<ParameterList>(result_any);
786 swap(result_any, items);
793 PLPair& result = make_any_ref<PLPair>(result_any);
795 std::string& key = any_ref_cast<
Scalar>(key_any).text;
796 swap(result.
key, key);
799 if (value_any.
type() ==
typeid(bool)) {
800 bool value = any_cast<
bool>(value_any);
802 }
else if (value_any.
type() ==
typeid(int)) {
803 int value = any_cast<
int>(value_any);
805 }
else if (value_any.
type() ==
typeid(
long long)) {
806 long long value = any_cast<
long long>(value_any);
808 }
else if (value_any.
type() ==
typeid(double)) {
809 double value = any_cast<
double>(value_any);
811 }
else if (value_any.
type() ==
typeid(std::string)) {
812 std::string& value = any_ref_cast<std::string >(value_any);
841 swap(result_pl, value);
842 result_pl.setName(result.
key);
844 std::string msg =
"unexpected YAML map value type ";
845 msg += value_any.
type().name();
846 msg +=
" for key \"";
849 throw ParserFail(msg);
855 if (scalar_type == -1) {
861 value_any = parse_as<int>(scalar_value.
text);
863 value_any = parse_as<long long>(scalar_value.
text);
865 value_any = parse_as<double>(scalar_value.
text);
867 value_any = scalar_value.
text;
871 if (scalar_type == -1) {
872 if (scalars.
size() == 0) {
873 throw ParserFail(
"implicitly typed arrays can't be empty\n"
874 "(need to determine their element type)\n");
880 scalar_type = std::min(scalar_type, scalars[i].infer_type());
886 result[i] = parse_as<int>(scalars[i].text);
892 result[i] = parse_as<long long>(scalars[i].text);
898 result[i] = parse_as<double>(scalars[i].text);
904 result[i] = scalars[i].text;
910 if (scalar_type == -1) {
911 if (scalars.
size() == 0) {
912 throw ParserFail(
"implicitly typed 2D arrays can't be empty\n"
913 "(need to determine their element type)\n");
919 if (scalars[0].size() == 0) {
920 throw ParserFail(
"implicitly typed 2D arrays can't have empty rows\n"
921 "(need to determine their element type)\n");
923 if (scalars[i].size() != scalars[0].size()) {
924 throw ParserFail(
"2D array: sub-arrays are different sizes");
927 int item_type = scalars[i][j].infer_type();
928 scalar_type = std::min(scalar_type, item_type);
936 result(i, j) = parse_as<int>(scalars[i][j].text);
944 result(i, j) = parse_as<long long>(scalars[i][j].text);
952 result(i, j) = parse_as<double>(scalars[i][j].text);
960 result(i, j) = scalars[i][j].text;
968 if (tag_any.
type() !=
typeid(std::string))
return -1;
969 std::string& text = any_ref_cast<std::string>(tag_any);
970 if (text.find(
"bool") != std::string::npos)
return Scalar::BOOL;
971 else if (text.find(
"int") != std::string::npos)
return Scalar::INT;
972 else if (text.find(
"double") != std::string::npos)
return Scalar::DOUBLE;
973 else if (text.find(
"string") != std::string::npos)
return Scalar::STRING;
975 std::string msg =
"Unable to parse type tag \"";
978 throw ParserFail(msg);
994 throw Teuchos::ParserFail(
995 "bug in YAMLParameterList::Reader: unexpected type for first sequence item");
1000 swap(result_any, items);
1005 swap(a.
back(), val);
1012 throw Teuchos::ParserFail(
1013 "bug in YAMLParameterList::Reader: unexpected type for next sequence item");
1018 std::size_t parent_indent_level,
1019 std::string
const& header,
1020 std::string
const& leading_empties_or_comments,
1021 std::string
const& rest,
1022 std::string& content,
1023 std::string& comment) {
1026 char chomping_indicator;
1027 std::size_t indentation_indicator = 0;
1029 std::stringstream ss(header.substr(1,std::string::npos));
1030 if (header.size() > 1 &&
my_isdigit(header[1])) {
1031 ss >> indentation_indicator;
1033 indentation_indicator += parent_indent_level;
1035 if (!(ss >> chomping_indicator)) chomping_indicator =
'\0';
1038 std::size_t first_newline = leading_empties_or_comments.find_first_of(
"\r\n");
1039 std::string newline;
1040 if (first_newline > 0 && leading_empties_or_comments[first_newline - 1] ==
'\r') {
1045 std::size_t keep_beg = first_newline + 1 - newline.size();
1046 if (leading_empties_or_comments[0] ==
'#') {
1047 comment = leading_empties_or_comments.substr(1, keep_beg);
1050 std::size_t content_beg = leading_empties_or_comments.find_first_not_of(
"\r\n ");
1051 if (content_beg == std::string::npos) content_beg = leading_empties_or_comments.size();
1052 std::size_t newline_before_content = leading_empties_or_comments.rfind(
"\n", content_beg);
1053 std::size_t num_indent_spaces = (content_beg - newline_before_content) - 1;
1056 if (indentation_indicator > 0) {
1058 Teuchos::ParserFail,
1059 "Indentation indicator " << indentation_indicator <<
" > leading spaces " << num_indent_spaces);
1060 num_indent_spaces = indentation_indicator;
1063 content = leading_empties_or_comments.substr(keep_beg, std::string::npos);
1072 auto last_newline = content.find_last_of(
"\n", content.size() - 2);
1073 if (last_newline == std::string::npos)
break;
1074 std::size_t num_spaces = 0;
1075 for (
auto ispace = last_newline + 1;
1076 ispace < content.size() && content[ispace] ==
' ';
1080 if (num_spaces >= num_indent_spaces)
break;
1081 content.erase(content.begin() + last_newline + 1, content.end());
1084 std::size_t unindent_pos = 0;
1086 std::size_t next_newline = content.find_first_of(
"\n", unindent_pos);
1087 if (next_newline == std::string::npos)
break;
1088 std::size_t start_cut = next_newline + 1;
1090 if (style ==
'>') start_cut -= newline.size();
1091 std::size_t end_cut = next_newline + 1;
1094 while (end_cut < content.size() && content[end_cut] ==
' ') {
1098 end_cut = std::min(next_newline + 1 + num_indent_spaces, end_cut);
1100 content = content.substr(0, start_cut) +
1101 content.substr(end_cut, std::string::npos);
1102 unindent_pos = start_cut;
1104 if (chomping_indicator !=
'+') {
1106 if (chomping_indicator !=
'-') content += newline;
1110 content = content.substr(newline.size(), std::string::npos);
1124 if (paramList->
name() ==
"ANONYMOUS") {
1138 if (paramList->
name() ==
"ANONYMOUS") {
1152 const std::string& name)
1157 if (paramList->
name() ==
"ANONYMOUS") {
1175 std::stringstream ss(yamlStr);
1181 std::ostream &yamlOut
1189 const std::string &yamlFileName
1200 std::string yamlFileName;
1201 if(xmlFileName.find(
".xml") == std::string::npos)
1203 yamlFileName = xmlFileName +
".yaml";
1207 yamlFileName = xmlFileName.substr(0, xmlFileName.length() - 4) +
".yaml";
1210 return yamlFileName;
1222 std::istreambuf_iterator<char> begin(xmlStream);
1223 std::istreambuf_iterator<char> end;
1224 std::string xmlString(begin, end);
1229 namespace YAMLParameterList
1234 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1235 auto yaml_input = ::YAML::LoadAll(text);
1236 return readParams(yaml_input);
1240 reader.read_string(result, text, name);
1243 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1248 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1249 auto yaml_input = ::YAML::LoadAllFromFile(yamlFile);
1250 return readParams(yaml_input);
1254 reader.read_file(result, yamlFile);
1257 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1262 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1263 auto yaml_input = ::YAML::LoadAll(yaml);
1264 return readParams(yaml_input);
1268 reader.read_stream(result, yaml,
"parseYamlStream");
1271 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1277 #ifdef HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1284 for(
size_t i = 0; i < lists.size(); i++)
1286 processMapNode(lists[i], *pl,
true);
1293 if (node.Type() != ::YAML::NodeType::Map)
1295 throw YamlStructureError(
"All top-level elements of the YAML file must be maps.");
1299 parent.
setName(node.begin()->first.as<std::string>());
1300 processMapNode(node.begin()->second, parent);
1304 for (::YAML::const_iterator i = node.begin(); i != node.end(); i++)
1307 if(i->first.Type() != ::YAML::NodeType::Scalar)
1309 throw YamlKeyError(
"Keys must be YAML scalars (int, double, or string)");
1312 const std::string key = quoted_as<std::string>(i->first);
1313 processKeyValueNode(key, i->second, parent, topLevel);
1318 void processKeyValueNode(
const std::string& key, const ::YAML::Node& node,
Teuchos::ParameterList& parent,
bool topLevel)
1322 if(node.Type() == ::YAML::NodeType::Scalar)
1326 safe_set_entry<int>(parent, key, quoted_as<int>(node));
1332 safe_set_entry<long long>(parent, key, quoted_as<long long>(node));
1338 safe_set_entry<double>(parent, key, quoted_as<double>(node));
1344 bool raw_bool = quoted_as<bool>(node);
1352 std::string raw_string = quoted_as<std::string>(node);
1355 safe_set_entry<bool>(parent, key, raw_bool);
1359 safe_set_entry<std::string>(parent, key, raw_string);
1364 safe_set_entry<std::string>(parent, key, quoted_as<std::string>(node));
1370 else if(node.Type() == ::YAML::NodeType::Map)
1374 processMapNode(node, parent);
1379 processMapNode(node, sublist);
1382 else if(node.Type() == ::YAML::NodeType::Sequence)
1384 int ndim = getYamlArrayDim(node);
1387 ::YAML::Node
const& first_value = *(node.begin());
1390 quoted_as<int>(first_value);
1391 safe_set_entry<Teuchos::Array<int>>(parent, key, getYamlArray<int>(node));
1397 quoted_as<double>(first_value);
1398 safe_set_entry<Teuchos::Array<double>>(parent, key, getYamlArray<double>(node));
1404 quoted_as<std::string>(first_value);
1405 safe_set_entry<Teuchos::Array<std::string>>(parent, key, getYamlArray<std::string>(node));
1409 throw YamlSequenceError(std::string(
"Array \"") + key +
"\" must contain int, double, bool or string");
1416 bool is_ragged = checkYamlTwoDArrayIsRagged(node);
1417 ::YAML::Node
const& first_value = *(node.begin()->begin());
1420 quoted_as<int>(first_value);
1423 safe_set_entry<arr_t>(parent, key, getYaml2DRaggedArray<arr_t, int>(node, ndim, key));
1425 safe_set_entry<Teuchos::TwoDArray<int>>(parent, key, getYamlTwoDArray<int>(node));
1432 quoted_as<double>(first_value);
1435 safe_set_entry<arr_t>(parent, key, getYaml2DRaggedArray<arr_t, double>(node, ndim, key));
1437 safe_set_entry<Teuchos::TwoDArray<double>>(parent, key, getYamlTwoDArray<double>(node));
1444 quoted_as<std::string>(first_value);
1447 safe_set_entry<arr_t>(parent, key, getYaml2DRaggedArray<arr_t, std::string>(node, ndim, key));
1449 safe_set_entry<Teuchos::TwoDArray<std::string>>(parent, key, getYamlTwoDArray<std::string>(node));
1454 throw YamlSequenceError(std::string(
"TwoDArray \"") + key +
"\" must contain int, double, bool or string");
1461 ::YAML::Node
const& first_value = *(node.begin()->begin()->begin());
1464 quoted_as<int>(first_value);
1466 safe_set_entry<arr_t>(parent, key, getYaml3DArray<arr_t, int>(node, ndim, key));
1472 quoted_as<double>(first_value);
1474 safe_set_entry<arr_t>(parent, key, getYaml3DArray<arr_t, double>(node, ndim, key));
1481 quoted_as<std::string>(first_value);
1483 safe_set_entry<arr_t>(parent, key, getYaml3DArray<arr_t, std::string>(node, ndim, key));
1488 throw YamlSequenceError(std::string(
"3DArray \"") + key +
"\" must contain int, double, bool or string");
1494 else if(node.Type() == ::YAML::NodeType::Null)
1502 throw YamlUndefinedNodeError(
"Value type in a key-value pair must be one of: int, double, string, array, sublist.");
1506 #endif // HAVE_TEUCHOSPARAMETERLIST_YAMLCPP
1511 std::ios_base::fmtflags flags = yaml.flags();
1513 std::ostringstream testStream;
1514 testStream.flags(flags);
1516 testStream << testVal;
1517 bool popFlags =
false;
1518 if(testStream.str() ==
"1")
1524 std::cout <<
"Warning: yaml stream format flags would confuse double with integer value with int.\n";
1525 std::cout <<
"Setting std::ios::showpoint on the stream to fix this (will restore flags when done)\n";
1526 std::ios_base::fmtflags flagsCopy = flags;
1527 flagsCopy |= std::ios::showpoint;
1530 yaml <<
"%YAML 1.1\n---\n";
1531 yaml << pl.
name() <<
':';
1550 std::ofstream yaml(yamlFile.c_str());
1557 yaml << std::scientific << std::setprecision(17);
1577 template <
typename T>
1579 static void write(T
const& x, std::ostream& stream) {
1586 static void write(
double const& x, std::ostream& stream) {
1593 static void write(std::string
const& x, std::ostream& stream) {
1598 template <
typename T>
1605 if (i) stream <<
", ";
1609 if (j) stream <<
", ";
1619 for(
int i = 0; i < indentLevel; i++)
1627 writeParameterList(Teuchos::getValue<Teuchos::ParameterList>(entry), yaml, indentLevel + 2);
1636 for(
int i = 0; i < arr.
size(); i++)
1639 if(i != arr.size() - 1)
1646 for(
int i = 0; i < arr.
size(); i++)
1649 if(i != arr.size() - 1)
1656 for(
int i = 0; i < arr.
size(); i++)
1659 if(i != arr.
size() - 1)
1666 for(
int i = 0; i < arr.
size(); i++)
1669 if(i != arr.
size() - 1)
1679 writeYamlTwoDArray<int>(
1680 Teuchos::getValue<Teuchos::TwoDArray<int> >(entry), yaml);
1684 writeYamlTwoDArray<long long>(
1685 Teuchos::getValue<Teuchos::TwoDArray<long long> >(entry), yaml);
1689 writeYamlTwoDArray<double>(
1690 Teuchos::getValue<Teuchos::TwoDArray<double> >(entry), yaml);
1694 writeYamlTwoDArray<std::string>(
1695 Teuchos::getValue<Teuchos::TwoDArray<std::string> >(entry), yaml);
1698 else if(entry.
isType<
int>())
1700 yaml << Teuchos::getValue<int>(entry);
1702 else if(entry.
isType<
long long>())
1704 yaml << Teuchos::getValue<long long>(entry);
1706 else if(entry.
isType<
double>())
1710 else if(entry.
isType<std::string>())
1712 std::string& str = Teuchos::getValue<std::string>(entry);
1713 if(strchr(str.c_str(),
'\n'))
1719 std::size_t first_non_newline_pos = str.find_first_not_of(
"\r\n");
1720 if (first_non_newline_pos != std::string::npos &&
1721 str[first_non_newline_pos] ==
' ') {
1724 if (str[str.size() - 1] !=
'\n') yaml <<
"-";
1730 size_t next = str.find(
'\n', index);
1731 for(
int i = 0; i < indentLevel + 2; i++)
1735 if(next == std::string::npos)
1737 yaml << str.substr(index, std::string::npos);
1742 yaml << str.substr(index, next - index) <<
'\n';
1752 else if(entry.
isType<
bool>())
1754 yaml << (Teuchos::getValue<bool>(entry) ?
"true" :
"false");
1765 for (std::size_t i = 0; i < str.size(); ++i) {
1766 if (str[i] ==
'\'') yaml <<
"''";
1767 else yaml << str[i];
1783 char const*
const control_chars =
":'{}[],&*#?|<>=!%@\\";
1784 return s.find_first_of(control_chars) != std::string::npos;
1793 is_parseable_as<long long>(s) ||
1794 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...