Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_YAML.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_YAML.hpp"
11 
12 #include <iostream>
13 
14 namespace Teuchos {
15 namespace YAML {
16 
17 namespace {
18 
19 Language::Productions make_productions() {
20  Language::Productions prods;
21  prods.resize(NPRODS);
22  prods[PROD_DOC]("doc") >> "top_items";
23  prods[PROD_DOC2]("doc") >> "NEWLINE", "top_items";
24  prods[PROD_TOP_FIRST]("top_items") >> "top_item";
25  prods[PROD_TOP_NEXT]("top_items") >> "top_items", "top_item";
26  prods[PROD_TOP_DIRECT]("top_item") >> "%", "any*", "NEWLINE";
27  prods[PROD_TOP_BEGIN]("top_item") >> "-", "-", "-", "NEWLINE";
28  prods[PROD_TOP_END]("top_item") >> ".", ".", ".", "NEWLINE";
29  prods[PROD_TOP_BMAP]("top_item") >> "bmap_item";
30  prods[PROD_BMAP_FIRST]("bmap_items") >> "bmap_item";
31  prods[PROD_BMAP_NEXT]("bmap_items") >> "bmap_items", "bmap_item";
32  prods[PROD_BMAP_SCALAR]("bmap_item") >> "scalar", ":", "WS*", "tag?", "map_scalar", "NEWLINE";
33  prods[PROD_BMAP_BSCALAR]("bmap_item") >> "scalar", ":", "WS*", "bscalar";
34  prods[PROD_BMAP_BVALUE]("bmap_item") >> "scalar", ":", "WS*", "NEWLINE", "bvalue";
35  prods[PROD_BVALUE_EMPTY]("bvalue");
36  prods[PROD_BVALUE_BMAP]("bvalue") >> "INDENT", "bmap_items", "DEDENT";
37  /* TODO: allow a tag in this */
38  prods[PROD_BVALUE_BSEQ]("bvalue") >> "INDENT", "bseq_items", "DEDENT";
39  prods[PROD_BMAP_FMAP]("bmap_item") >> "scalar", ":", "WS*", "tag?", "fmap", "NEWLINE";
40  prods[PROD_BMAP_FSEQ]("bmap_item") >> "scalar", ":", "WS*", "tag?", "fseq", "NEWLINE";
41  prods[PROD_BSEQ_FIRST]("bseq_items") >> "bseq_item";
42  prods[PROD_BSEQ_NEXT]("bseq_items") >> "bseq_items", "bseq_item";
43  prods[PROD_BSEQ_SCALAR]("bseq_item") >> "-", "WS+", "tag?", "scalar", "NEWLINE";
44  prods[PROD_BSEQ_BSCALAR]("bseq_item") >> "-", "WS+", "bscalar";
45  prods[PROD_BSEQ_BMAP]("bseq_item") >> "-", "NEWLINE", "INDENT", "bmap_items", "DEDENT";
46  prods[PROD_BSEQ_BMAP_TRAIL]("bseq_item") >> "-", "WS+", "NEWLINE", "INDENT", "bmap_items", "DEDENT";
47  prods[PROD_BSEQ_BSEQ]("bseq_item") >> "-", "NEWLINE", "INDENT", "bseq_items", "DEDENT";
48  prods[PROD_BSEQ_BSEQ_TRAIL]("bseq_item") >> "-", "WS+", "NEWLINE", "INDENT", "bseq_items", "DEDENT";
49  prods[PROD_BSEQ_FMAP]("bseq_item") >> "-", "WS+", "tag?", "fmap", "NEWLINE";
50  prods[PROD_BSEQ_FSEQ]("bseq_item") >> "-", "WS+", "tag?", "fseq", "NEWLINE";
51  prods[PROD_FMAP]("fmap") >> "{", "WS*", "fmap_items", "}", "WS*";
52  prods[PROD_FMAP_EMPTY]("fmap") >> "{", "WS*", "}", "WS*";
53  prods[PROD_FMAP_FIRST]("fmap_items") >> "fmap_item";
54  prods[PROD_FMAP_NEXT]("fmap_items") >> "fmap_items", ",", "WS*", "fmap_item";
55  prods[PROD_FMAP_SCALAR]("fmap_item") >> "scalar", ":", "WS*", "tag?", "scalar";
56  prods[PROD_FMAP_FMAP]("fmap_item") >> "scalar", ":", "WS*", "tag?", "fmap";
57  prods[PROD_FMAP_FSEQ]("fmap_item") >> "scalar", ":", "WS*", "tag?", "fseq";
58  prods[PROD_FSEQ]("fseq") >> "[", "WS*", "fseq_items", "]", "WS*";
59  prods[PROD_FSEQ_EMPTY]("fseq") >> "[", "WS*", "]", "WS*";
60  prods[PROD_FSEQ_FIRST]("fseq_items") >> "fseq_item";
61  prods[PROD_FSEQ_NEXT]("fseq_items") >> "fseq_items", ",", "WS*", "fseq_item";
62  prods[PROD_FSEQ_SCALAR]("fseq_item") >> "tag?", "scalar";
63  prods[PROD_FSEQ_FMAP]("fseq_item") >> "tag?", "fmap";
64  prods[PROD_FSEQ_FSEQ]("fseq_item") >> "tag?", "fseq";
65  prods[PROD_SCALAR_RAW]("scalar") >> "scalar_head", "scalar_tail*";
66  prods[PROD_SCALAR_QUOTED]("scalar") >> "scalar_quoted";
67  prods[PROD_MAP_SCALAR_RAW]("map_scalar") >> "scalar_head", "scalar_tail*", "map_scalar_escaped*";
68  prods[PROD_MAP_SCALAR_QUOTED]("map_scalar") >> "scalar_quoted";
69  prods[PROD_SCALAR_DQUOTED]("scalar_quoted") >> "\"", "dquoted*", "descape*", "\"", "WS*";
70  prods[PROD_SCALAR_SQUOTED]("scalar_quoted") >> "'", "squoted*", "sescape*", "'", "WS*";
71  prods[PROD_SCALAR_HEAD_OTHER]("scalar_head") >> "OTHERCHAR";
72  prods[PROD_SCALAR_HEAD_DOT]("scalar_head") >> ".", "OTHERCHAR";
73  prods[PROD_SCALAR_HEAD_DASH]("scalar_head") >> "-", "OTHERCHAR";
74  prods[PROD_SCALAR_HEAD_DOT_DOT]("scalar_head") >> ".", ".", "OTHERCHAR";
75  prods[PROD_MAP_SCALAR_ESCAPED_EMPTY]("map_scalar_escaped*");
76  prods[PROD_MAP_SCALAR_ESCAPED_NEXT]("map_scalar_escaped*") >> "map_scalar_escaped*", ",", "scalar_tail*";
77  prods[PROD_TAG_EMPTY]("tag?");
78  prods[PROD_TAG]("tag?") >> "!", "!", "OTHERCHAR+", "WS+";
79  prods[PROD_BSCALAR]("bscalar") >> "bscalar_header", "WS*", "NEWLINE", "INDENT", "bscalar_items", "DEDENT";
80  prods[PROD_BSCALAR_FIRST]("bscalar_items") >> "bscalar_item";
81  prods[PROD_BSCALAR_NEXT]("bscalar_items") >> "bscalar_items", "bscalar_item";
82  prods[PROD_BSCALAR_LINE]("bscalar_item") >> "any*", "NEWLINE";
83  prods[PROD_BSCALAR_INDENT]("bscalar_item") >> "INDENT", "bscalar_items", "DEDENT";
84  prods[PROD_BSCALAR_HEADER_LITERAL]("bscalar_header") >> "|", "bscalar_head_c*";
85  prods[PROD_BSCALAR_HEADER_FOLDED]("bscalar_header") >> ">", "bscalar_head_c*";
86  prods[PROD_BSCALAR_HEAD_EMPTY]("bscalar_head_c*");
87  prods[PROD_BSCALAR_HEAD_NEXT]("bscalar_head_c*") >> "bscalar_head_c*", "bscalar_head_c";
88  prods[PROD_BSCALAR_HEAD_OTHER]("bscalar_head_c") >> "OTHERCHAR";
89  prods[PROD_BSCALAR_HEAD_DASH]("bscalar_head_c") >> "-";
90  prods[PROD_DQUOTED_EMPTY]("dquoted*");
91  prods[PROD_DQUOTED_NEXT]("dquoted*") >> "dquoted*", "dquoted";
92  prods[PROD_SQUOTED_EMPTY]("squoted*");
93  prods[PROD_SQUOTED_NEXT]("squoted*") >> "squoted*", "squoted";
94  prods[PROD_ANY_EMPTY]("any*");
95  prods[PROD_ANY_NEXT]("any*") >> "any*", "any";
96  prods[PROD_DESCAPE_EMPTY]("descape*");
97  prods[PROD_DESCAPE_NEXT]("descape*") >> "descape*", "descape";
98  prods[PROD_DESCAPE]("descape") >> "\\", "descaped", "dquoted*";
99  prods[PROD_SESCAPE_EMPTY]("sescape*");
100  prods[PROD_SESCAPE_NEXT]("sescape*") >> "sescape*", "sescape";
101  prods[PROD_SESCAPE]("sescape") >> "'", "'", "squoted*";
102  prods[PROD_SCALAR_TAIL_EMPTY]("scalar_tail*");
103  prods[PROD_SCALAR_TAIL_NEXT]("scalar_tail*") >> "scalar_tail*", "scalar_tail";
104  prods[PROD_OTHER_FIRST]("OTHERCHAR+") >> "OTHERCHAR";
105  prods[PROD_OTHER_NEXT]("OTHERCHAR+") >> "OTHERCHAR+", "OTHERCHAR";
106  prods[PROD_SCALAR_TAIL_SPACE]("scalar_tail") >> "WS";
107  prods[PROD_SCALAR_TAIL_DOT]("scalar_tail") >> ".";
108  prods[PROD_SCALAR_TAIL_DASH]("scalar_tail") >> "-";
109  prods[PROD_SCALAR_TAIL_SQUOT]("scalar_tail") >> "'";
110  prods[PROD_SCALAR_TAIL_OTHER]("scalar_tail") >> "OTHERCHAR";
111  prods[PROD_DESCAPED_DQUOT]("descaped") >> "\"";
112  prods[PROD_DESCAPED_SLASH]("descaped") >> "\\";
113  prods[PROD_DESCAPED_DQUOTED]("descaped") >> "dquoted";
114  prods[PROD_DQUOTED_COMMON]("dquoted") >> "common";
115  prods[PROD_DQUOTED_SQUOT]("dquoted") >> "'";
116  prods[PROD_SQUOTED_COMMON]("squoted") >> "common";
117  prods[PROD_SQUOTED_DQUOT]("squoted") >> "\"";
118  prods[PROD_SQUOTED_SLASH]("squoted") >> "\\";
119  prods[PROD_ANY_COMMON]("any") >> "common";
120  prods[PROD_ANY_DQUOT]("any") >> "\"";
121  prods[PROD_ANY_SQUOT]("any") >> "'";
122  prods[PROD_ANY_SLASH]("any") >> "\\";
123  prods[PROD_COMMON_SPACE]("common") >> "WS";
124  prods[PROD_COMMON_COLON]("common") >> ":";
125  prods[PROD_COMMON_DOT]("common") >> ".";
126  prods[PROD_COMMON_DASH]("common") >> "-";
127  prods[PROD_COMMON_PIPE]("common") >> "|";
128  prods[PROD_COMMON_LSQUARE]("common") >> "[";
129  prods[PROD_COMMON_RSQUARE]("common") >> "]";
130  prods[PROD_COMMON_LCURLY]("common") >> "{";
131  prods[PROD_COMMON_RCURLY]("common") >> "}";
132  prods[PROD_COMMON_RANGLE]("common") >> ">";
133  prods[PROD_COMMON_COMMA]("common") >> ",";
134  prods[PROD_COMMON_PERCENT]("common") >> "%";
135  prods[PROD_COMMON_EXCL]("common") >> "!";
136  prods[PROD_COMMON_OTHER]("common") >> "OTHERCHAR";
137  prods[PROD_SPACE_STAR_EMPTY]("WS*");
138  prods[PROD_SPACE_STAR_NEXT]("WS*") >> "WS*", "WS";
139  prods[PROD_SPACE_PLUS_FIRST]("WS+") >> "WS";
140  prods[PROD_SPACE_PLUS_NEXT]("WS+") >> "WS+", "WS";
141  return prods;
142 }
143 
144 } // end anonymous namespace
145 
146 Language make_language() {
147  Language out;
148  Language::Productions& prods = out.productions;
149  prods = make_productions();
150  Language::Tokens& toks = out.tokens;
151  toks.resize(NTOKS);
152  toks[TOK_NEWLINE]("NEWLINE", "((#[^\r\n]*)?\r?\n[ \t]*)+");
153  toks[TOK_INDENT]("INDENT", "((#[^\r\n]*)?\r?\n[ \t]*)+");
154  toks[TOK_DEDENT]("DEDENT", "((#[^\r\n]*)?\r?\n[ \t]*)+");
155  toks[TOK_SPACE]("WS", "[ \t]");
156  toks[TOK_COLON](":", ":");
157  toks[TOK_DOT](".", "\\.");
158  toks[TOK_DASH]("-", "\\-");
159  toks[TOK_DQUOT]("\"", "\"");
160  toks[TOK_SQUOT]("'", "'");
161  toks[TOK_SLASH]("\\", "\\\\");
162  toks[TOK_PIPE]("|", "\\|");
163  toks[TOK_LSQUARE]("[", "\\[");
164  toks[TOK_RSQUARE]("]", "\\]");
165  toks[TOK_LCURLY]("{", "{");
166  toks[TOK_RCURLY]("}", "}");
167  toks[TOK_RANGLE](">", ">");
168  toks[TOK_COMMA](",", ",");
169  toks[TOK_PERCENT]("%", "%");
170  toks[TOK_EXCL]("!", "!");
171  toks[TOK_OTHER]("OTHERCHAR", "[^ \t:\\.\\-\"'\\\\\\|\\[\\]{}>,%#!\n\r]");
172  return out;
173 }
174 
175 LanguagePtr ask_language() {
176  static LanguagePtr ptr;
177  if (ptr.strong_count() == 0) {
178  ptr.reset(new Language(make_language()));
179  }
180  return ptr;
181 }
182 
183 ReaderTablesPtr ask_reader_tables() {
184  static ReaderTablesPtr ptr;
185  if (ptr.strong_count() == 0) {
186  ptr = make_reader_tables(*(YAML::ask_language()));
187  }
188  return ptr;
189 }
190 
191 } // end namespace YAML
192 } // end namespace Teuchos
A TeuchosParser Language for a subset of YAML.
Productions productions
vector of productions
RCP< const ReaderTables > ReaderTablesPtr
an RCP to a const ReaderTables
ReaderTablesPtr make_reader_tables(Language const &language)
constructs ReaderTables for the given Language.
RCP< const Language > LanguagePtr
an RCP to a const Language