|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 014literal_string.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.cSpecial { color: #008000; }
.muRecipe { color: #ff8700; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.traceContains { color: #008000; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Identifier { color: #c0a020; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: For convenience, some instructions will take literal arrays of characters</span>
<span class="Comment">//: (text or strings).</span>
<span class="Comment">//:</span>
<span class="Comment">//: Instead of quotes, we'll use [] to delimit strings. That'll reduce the</span>
<span class="Comment">//: need for escaping since we can support nested brackets. And we can also</span>
<span class="Comment">//: imagine that 'recipe' might one day itself be defined in Mu, doing its own</span>
<span class="Comment">//: parsing.</span>
<span class="Delimiter">:(scenarios load)</span>
<span class="Delimiter">:(scenario string_literal)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span>copy [abc def]
]
<span class="traceContains">+parse: ingredient: {"abc def": "literal-string"}</span>
<span class="Delimiter">:(scenario string_literal_with_colons)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span>copy [abc:def/ghi]
]
<span class="traceContains">+parse: ingredient: {"abc:def/ghi": "literal-string"}</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
put<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"literal-string"</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">:(before "End next_word Special-cases")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'['</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
string result = slurp_quoted<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
skip_whitespace_and_comments_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
string slurp_quoted<span class="Delimiter">(</span>istream& in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
ostringstream out<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">));</span> assert<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'['</span><span class="Delimiter">);</span> out << <span class="Normal">static_cast</span><<span class="Normal">char</span>><span class="Delimiter">(</span>in<span class="Delimiter">.</span>get<span class="Delimiter">());</span> <span class="Comment">// slurp the '['</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_code_string<span class="Delimiter">(</span>in<span class="Delimiter">,</span> out<span class="Delimiter">))</span>
slurp_quoted_comment_aware<span class="Delimiter">(</span>in<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
<span class="Normal">else</span>
slurp_quoted_comment_oblivious<span class="Delimiter">(</span>in<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
<span class="Identifier">return</span> out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Comment">// A string is a code string (ignores comments when scanning for matching</span>
<span class="Comment">// brackets) if it contains a newline at the start before any non-whitespace.</span>
<span class="Normal">bool</span> is_code_string<span class="Delimiter">(</span>istream& in<span class="Delimiter">,</span> ostream& out<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Normal">char</span> c = in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!isspace<span class="Delimiter">(</span>c<span class="Delimiter">))</span> <span class="Delimiter">{</span>
in<span class="Delimiter">.</span>putback<span class="Delimiter">(</span>c<span class="Delimiter">);</span>
<span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
out << c<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// Read a regular string. Regular strings can only contain other regular</span>
<span class="Comment">// strings.</span>
<span class="Normal">void</span> slurp_quoted_comment_oblivious<span class="Delimiter">(</span>istream& in<span class="Delimiter">,</span> ostream& out<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">int</span> brace_depth = <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Normal">char</span> c = in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="cSpecial">'\\'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
slurp_one_past_backslashes<span class="Delimiter">(</span>in<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
out << c<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="Constant">'['</span><span class="Delimiter">)</span> ++brace_depth<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="Constant">']'</span><span class="Delimiter">)</span> --brace_depth<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>brace_depth == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> && brace_depth > <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"unbalanced '['</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
out<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// Read a code string. Code strings can contain either code or regular strings.</span>
<span class="Normal">void</span> slurp_quoted_comment_aware<span class="Delimiter">(</span>istream& in<span class="Delimiter">,</span> ostream& out<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">char</span> c<span class="Delimiter">;</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>in >> c<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="cSpecial">'\\'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
slurp_one_past_backslashes<span class="Delimiter">(</span>in<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="Constant">'#'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
out << c<span class="Delimiter">;</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> && in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> out << <span class="Normal">static_cast</span><<span class="Normal">char</span>><span class="Delimiter">(</span>in<span class="Delimiter">.</span>get<span class="Delimiter">());</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="Constant">'['</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
in<span class="Delimiter">.</span>putback<span class="Delimiter">(</span>c<span class="Delimiter">);</span>
<span class="Comment">// recurse</span>
out << slurp_quoted<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
out << c<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="Constant">']'</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
raise << <span class="Constant">"unbalanced '['</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
out<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after "Parsing reagent(string s)")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>starts_with<span class="Delimiter">(</span>s<span class="Delimiter">,</span> <span class="Constant">"["</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>*s<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> != <span class="Constant">']'</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Comment">// unbalanced bracket; handled elsewhere</span>
name = s<span class="Delimiter">;</span>
<span class="Comment">// delete [] delimiters</span>
name<span class="Delimiter">.</span>erase<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">);</span>
strip_last<span class="Delimiter">(</span>name<span class="Delimiter">);</span>
type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">"literal-string"</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Unlike other reagents, escape newlines in literal strings to make them</span>
<span class="Comment">//: more friendly to trace().</span>
<span class="Delimiter">:(after "string to_string(const reagent& r)")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_literal_text<span class="Delimiter">(</span>r<span class="Delimiter">))</span>
<span class="Identifier">return</span> emit_literal_string<span class="Delimiter">(</span>r<span class="Delimiter">.</span>name<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> is_literal_text<span class="Delimiter">(</span><span class="Normal">const</span> reagent& x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> x<span class="Delimiter">.</span>type && x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name == <span class="Constant">"literal-string"</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
string emit_literal_string<span class="Delimiter">(</span>string name<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">size_t</span> pos = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>pos != string::npos<span class="Delimiter">)</span>
pos = replace<span class="Delimiter">(</span>name<span class="Delimiter">,</span> <span class="Constant">"</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">,</span> <span class="Constant">"</span><span class="cSpecial">\\</span><span class="Constant">n"</span><span class="Delimiter">,</span> pos<span class="Delimiter">);</span>
<span class="Identifier">return</span> <span class="Constant">"{</span><span class="cSpecial">\"</span><span class="Constant">"</span>+name+<span class="Constant">"</span><span class="cSpecial">\"</span><span class="Constant">: </span><span class="cSpecial">\"</span><span class="Constant">literal-string</span><span class="cSpecial">\"</span><span class="Constant">}"</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">size_t</span> replace<span class="Delimiter">(</span>string& str<span class="Delimiter">,</span> <span class="Normal">const</span> string& from<span class="Delimiter">,</span> <span class="Normal">const</span> string& to<span class="Delimiter">,</span> <span class="Normal">size_t</span> n<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">size_t</span> result = str<span class="Delimiter">.</span>find<span class="Delimiter">(</span>from<span class="Delimiter">,</span> n<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>result != string::npos<span class="Delimiter">)</span>
str<span class="Delimiter">.</span>replace<span class="Delimiter">(</span>result<span class="Delimiter">,</span> from<span class="Delimiter">.</span>length<span class="Delimiter">(),</span> to<span class="Delimiter">);</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> strip_last<span class="Delimiter">(</span>string& s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!s<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> s<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>s<span class="Delimiter">)</span>-<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> slurp_one_past_backslashes<span class="Delimiter">(</span>istream& in<span class="Delimiter">,</span> ostream& out<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// When you encounter a backslash, strip it out and pass through any</span>
<span class="Comment">// following run of backslashes. If we 'escaped' a single following</span>
<span class="Comment">// character, then the character '\' would be:</span>
<span class="Comment">// '\\' escaped once</span>
<span class="Comment">// '\\\\' escaped twice</span>
<span class="Comment">// '\\\\\\\\' escaped thrice (8 backslashes)</span>
<span class="Comment">// ..and so on. With our approach it'll be:</span>
<span class="Comment">// '\\' escaped once</span>
<span class="Comment">// '\\\' escaped twice</span>
<span class="Comment">// '\\\\' escaped thrice</span>
<span class="Comment">// This only works as long as backslashes aren't also overloaded to create</span>
<span class="Comment">// special characters. So Mu doesn't follow C's approach of overloading</span>
<span class="Comment">// backslashes both to escape quote characters and also as a notation for</span>
<span class="Comment">// unprintable characters like '\n'.</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Normal">char</span> c = in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
out << c<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>c != <span class="cSpecial">'\\'</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario string_literal_nested)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span>copy [abc [def]]
]
<span class="traceContains">+parse: ingredient: {"abc [def]": "literal-string"}</span>
<span class="Delimiter">:(scenario string_literal_escaped)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span>copy [abc \[def]
]
<span class="traceContains">+parse: ingredient: {"abc [def": "literal-string"}</span>
<span class="Delimiter">:(scenario string_literal_escaped_twice)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span>copy [
abc \\[def]
]
<span class="traceContains">+parse: ingredient: {"\nabc \[def": "literal-string"}</span>
<span class="Delimiter">:(scenario string_literal_and_comment)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span>copy [abc] <span class="Comment"># comment</span>
]
<span class="traceContains">+parse: --- defining main</span>
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse: number of ingredients: 1</span>
<span class="traceContains">+parse: ingredient: {"abc": "literal-string"}</span>
<span class="traceContains">+parse: product: {1: ("address" "array" "character")}</span>
<span class="Delimiter">:(scenario string_literal_escapes_newlines_in_trace)</span>
<span class="muRecipe">def</span> main [
copy [abc
<span class="muRecipe">def</span>]
]
<span class="traceContains">+parse: ingredient: {"abc\ndef": "literal-string"}</span>
<span class="Delimiter">:(scenario string_literal_can_skip_past_comments)</span>
<span class="muRecipe">def</span> main [
copy [
<span class="Comment"># ']' inside comment</span>
bar
]
]
<span class="traceContains">+parse: ingredient: {"\n # ']' inside comment\n bar\n ": "literal-string"}</span>
<span class="Delimiter">:(scenario string_literal_empty)</span>
<span class="muRecipe">def</span> main [
copy []
]
<span class="traceContains">+parse: ingredient: {"": "literal-string"}</span>
<span class="Delimiter">:(scenario multiple_unfinished_recipes)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> f1 [
<span class="muRecipe">def</span> f2 [
<span class="traceContains">+error: unbalanced '['</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|