:(scenarios load)
:(scenario dilated_reagent_with_nested_brackets)
def main [
{1: number, foo: (bar (baz quux))} <- copy 34
]
+parse: product: {1: "number", "foo": ("bar" ("baz" "quux"))}
:(before "End Parsing Dilated Reagent Property(value)")
value = parse_string_tree(value);
:(before "End Parsing Dilated Reagent Type Property(type_names)")
type_names = parse_string_tree(type_names);
:(code)
string_tree* parse_string_tree(string_tree* s) {
assert(s->atom);
if (!starts_with(s->value, "(")) return s;
string_tree* result = parse_string_tree(s->value);
delete s;
return result;
}
string_tree* parse_string_tree(const string& s) {
istringstream in(s);
in >> std::noskipws;
return parse_string_tree(in);
}
string_tree* parse_string_tree(istream& in) {
skip_whitespace_but_not_newline(in);
if (!has_data(in)) return NULL;
if (in.peek() == ')') {
in.get();
return NULL;
}
if (in.peek() != '(') {
string_tree* result = new string_tree(next_word(in));
return result;
}
in.get();
string_tree* result = NULL;
string_tree** curr = &result;
while (true) {
skip_whitespace_but_not_newline(in);
assert(has_data(in));
if (in.peek() == ')') break;
*curr = new string_tree(NULL, NULL);
if (in.peek() == '(')
(*curr)->left = parse_string_tree(in);
else
(*curr)->left = new string_tree(next_word(in));
curr = &(*curr)->right;
}
in.get();
assert(*curr == NULL);
string_tree* tmp = result;
while (tmp->right && tmp->right->right) tmp = tmp->right;
assert(!tmp->right->atom);
if (!tmp->right->left->atom) return result;
string_tree* tmp2 = tmp->right;
tmp->right = tmp2->left;
tmp2->left = NULL;
assert(tmp2->right == NULL);
delete tmp2;
return result;
}
:(scenario dilated_reagent_with_type_tree)
% Hide_errors = true; // 'map' isn't defined yet
def main [
{1: (foo (address array character) (bar number))} <- copy 34
]
container foo [
]
container bar [
]
+parse: product: {1: ("foo" ("address" "array" "character") ("bar" "number"))}