:(scenarios load)
:(scenario string_literal)
recipe main [
1:address:array:character <- copy [abc def]
]
+parse: ingredient: {name: "abc def", properties: [_: "literal-string"]}
:(scenario string_literal_with_colons)
recipe main [
1:address:array:character <- copy [abc:def/ghi]
]
+parse: ingredient: {name: "abc:def/ghi", properties: [_: "literal-string"]}
:(before "End Mu Types Initialization")
Type_ordinal["literal-string"] = 0;
:(after "string next_word(istream& in)")
if (in.peek() == '[') {
string result = slurp_quoted(in);
skip_whitespace(in);
skip_comment(in);
return result;
}
:(code)
string slurp_quoted(istream& in) {
ostringstream out;
assert(!in.eof()); assert(in.peek() == '['); out << static_cast<char>(in.get());
if (code_string(in, out))
slurp_quoted_comment_aware(in, out);
else
slurp_quoted_comment_oblivious(in, out);
return out.str();
}
bool code_string(istream& in, ostringstream& out) {
while (!in.eof()) {
char c = in.get();
if (!isspace(c)) {
in.putback(c);
return false;
}
out << c;
if (c == '\n') {
return true;
}
}
return false;
}
void slurp_quoted_comment_oblivious(istream& in, ostringstream& out) {
int brace_depth = 1;
while (!in.eof()) {
char c = in.get();
if (c == '\\') {
out << static_cast<char>(in.get());
continue;
}
out << c;
if (c == '[') ++brace_depth;
if (c == ']') --brace_depth;
if (brace_depth == 0) break;
}
if (in.eof() && brace_depth > 0) {
raise << "unbalanced '['\n";
out.clear();
}
}
void slurp_quoted_comment_aware(istream& in, ostringstream& out) {
char c;
while (in >> c) {
if (c == '\\') {
out << static_cast<char>(in.get());
continue;
}
if (c == '#') {
out << c;
while (!in.eof() && in.peek() != '\n') out << static_cast<char>(in.get());
continue;
}
if (c == '[') {
in.putback(c);
out << slurp_quoted(in);
continue;
}
out << c;
if (c == ']') return;
}
raise << "unbalanced '['\n";
out.clear();
}
:(after "reagent::reagent(string s)")
if (s.at(0) == '[') {
assert(*s.rbegin() == ']');
s.erase(0, 1);
s.erase(SIZE(s)-1);
name = s;
types.push_back(0);
properties.push_back(pair<string, vector<string> >(name, vector<string>()));
properties.back().second.push_back("literal-string");
return;
}
:(after "string reagent::to_string()")
if (!properties.at(0).second.empty() && properties.at(0).second.at(0) == "literal-string") {
return emit_literal_string(name);
}
:(code)
string emit_literal_string(string name) {
size_t pos = 0;
while (pos != string::npos)
pos = replace(name, "\n", "\\n", pos);
return "{name: \""+name+"\", properties: [_: \"literal-string\"]}";
}
size_t replace(string& str, const string& from, const string& to, size_t n) {
size_t result = str.find(from, n);
if (result != string::npos)
str.replace(result, from.length(), to);
return result;
}
:(scenario string_literal_nested)
recipe main [
1:address:array:character <- copy [abc [def]]
]
+parse: ingredient: {name: "abc [def]", properties: [_: "literal-string"]}
:(scenario string_literal_escaped)
recipe main [
1:address:array:character <- copy [abc \[def]
]
+parse: ingredient: {name: "abc [def", properties: [_: "literal-string"]}
:(scenario string_literal_escaped_comment_aware)
recipe main [
1:address:array:character <- copy [
abc \\\[def]
]
+parse: ingredient: {name: "\nabc \[def", properties: [_: "literal-string"]}
:(scenario string_literal_and_comment)
recipe main [
1:address:array:character <- copy [abc]
]
+parse: instruction: copy
+parse: ingredient: {name: "abc", properties: [_: "literal-string"]}
+parse: product: {name: "1", properties: ["1": "address":"array":"character"]}
$parse: 3
:(scenario string_literal_escapes_newlines_in_trace)
recipe main [
copy [abc
def]
]
+parse: ingredient: {name: "abc\ndef", properties: [_: "literal-string"]}
:(scenario string_literal_can_skip_past_comments)
recipe main [
copy [
bar
]
]
+parse: ingredient: {name: "\n # ']' inside comment\n bar\n ", properties: [_: "literal-string"]}
:(scenario string_literal_empty)
recipe main [
copy []
]
+parse: ingredient: {name: "", properties: [_: "literal-string"]}