:(scenarios load)
:(scenario dilated_reagent)
def main [
{1: number, foo: bar} <- copy 34
]
+parse: product: {1: "number", "foo": "bar"}
:(scenario load_trailing_space_after_curly_bracket)
def main [
{
]
:(scenario dilated_reagent_with_comment)
def main [
{1: number, foo: bar} <- copy 34
]
+parse: product: {1: "number", "foo": "bar"}
$error: 0
:(scenario dilated_reagent_with_comment_immediately_following)
def main [
1:number <- copy {34: literal}
]
$error: 0
:(before "End next_word Special-cases")
if (in.peek() == '(')
return slurp_balanced_bracket(in);
if (start_of_dilated_reagent(in))
return slurp_balanced_bracket(in);
:(code)
bool start_of_dilated_reagent(istream& in) {
if (in.peek() != '{') return false;
int pos = in.tellg();
in.get();
skip_whitespace_but_not_newline(in);
char next = in.peek();
in.seekg(pos);
return next != '\n';
}
string slurp_balanced_bracket(istream& in) {
ostringstream result;
char c;
list<char> open_brackets;
while (in >> c) {
if (c == '(') open_brackets.push_back(c);
if (c == ')') {
if (open_brackets.empty() || open_brackets.back() != '(') {
raise << "unbalanced ')'\n" << end();
continue;
}
assert(open_brackets.back() == '(');
open_brackets.pop_back();
}
if (c == '[') open_brackets.push_back(c);
if (c == ']') {
if (open_brackets.empty() || open_brackets.back() != '[') {
raise << "unbalanced ']'\n" << end();
continue;
}
open_brackets.pop_back();
}
if (c == '{') open_brackets.push_back(c);
if (c == '}') {
if (open_brackets.empty() || open_brackets.back() != '{') {
raise << "unbalanced '}'\n" << end();
continue;
}
open_brackets.pop_back();
}
result << c;
if (open_brackets.empty()) break;
}
skip_whitespace_and_comments_but_not_newline(in);
return result.str();
}
:(after "Parsing reagent(string s)")
if (s.at(0) == '{') {
assert(properties.empty());
istringstream in(s);
in >> std::noskipws;
in.get();
name = slurp_key(in);
if (name.empty()) {
raise << "invalid reagent '" << s << "' without a name\n" << end();
return;
}
if (name == "}") {
raise << "invalid empty reagent '" << s << "'\n" << end();
return;
}
{
string_tree* type_names = new string_tree(next_word(in));
type = new_type_tree(type_names);
delete type_names;
}
while (has_data(in)) {
string key = slurp_key(in);
if (key.empty()) continue;
if (key == "}") continue;
string_tree* value = new string_tree(next_word(in));
properties.push_back(pair<string, string_tree*>(key, value));
}
return;
}
:(code)
string slurp_key(istream& in) {
string result = next_word(in);
while (!result.empty() && *result.rbegin() == ':')
strip_last(result);
while (isspace(in.peek()) || in.peek() == ':')
in.get();
return result;
}