about summary refs log blame commit diff stats
path: root/html/014literal_string.cc.html
blob: 1a98bd3991afcb48f3a5bca876b7cbe99e69e930 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                                                          
                                        

                                                
                                  
                                        

                       
                                                                          
                                                                                            
                             
                               

                                                      
                            
                             
                              
                            

        

       
     








                                                                                                                     
          

                                                                                                                                                                           
                                                                                                        

                                                                     
          

                                                                                                               
                                                                                                            

                                                                                
                                                                                                                                                                                                                                         
 
                                                                                
                                                                                                                                                                                                                                            
                                                                                                 
                                                                                                                 

                                                                               


                                                                                                                                   
                                                   

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                     
                                  





                                                                                                                                         



                                                                                                                                                                                                                
                                                                                                                 


                                                                                                                
                                                                                                                                                                           







                                                                                                               





                                                                                                                                                                                                                                

                                                                              
                                                  


                                                                                                                                                                                                                       
                                  

                                                                                                                                                                                                                                                                         
                                                                              
                                  


                                                                                                            




                                                                                                                                                                                                                            

                                                                              
                                                                                                                                                                          
                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

                                                                              
                                                                                                                                                                          





                                                                                                                 
                                                                                                                                                                                                                
                                  
                                                                                                                                                                                      
                                                                            

                                
                                                                             
                                                                                                                                                                                                                                                                                                       


                                                                                                                                                                                                        
                                                                              
                                          
                                                                                                                                                                                                                                           

                                                                        
 

                                                                                                       
 
                                                                                          
                                                                                                                                                    
                                                                                                                                                                    

                                      
                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                   

                                
                                                                                                                                      

                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                        

                                


                                                                                                                                                                                                                                                                                                                                                                                                   



                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                                                                                                                       

                                
                                                                
          

                                                                                                             
                                                                                                          
 
                                                                 
          

                                                                                                             
                                                                                                         

                                                                               
          


                                                                                                   
                                                                                                            
 
                                                                     
          

                                                                                                                                               
                                                            
                                                            
                                                                     

                                                                                                                      

                                                                                   
          


           
                                                                                                         

                                                                                
          




                                                     
                                                                                                                                        

                                                               
          

         
                                                                                                 


       
<!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.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
.cSpecial { color: #008000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; }
.traceContains { color: #008000; }
.Special { color: #c00000; }
.Constant { color: #00a0a0; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment">//: For convenience, some instructions will take literal arrays of characters (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>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>copy [abc def]  <span class="Comment"># copy can't really take a string</span>
]
<span class="traceContains">+parse:   ingredient: &quot;abc def&quot;: &quot;literal-string&quot;</span>

<span class="Delimiter">:(scenario string_literal_with_colons)</span>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>copy [abc:def/ghi]
]
<span class="traceContains">+parse:   ingredient: &quot;abc:def/ghi&quot;: &quot;literal-string&quot;</span>

<span class="Delimiter">:(before &quot;End Mu Types Initialization&quot;)</span>
put<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;literal-string&quot;</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>

<span class="Delimiter">:(before &quot;End next_word Special-cases&quot;)</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&amp; 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 &lt;&lt; <span class="Normal">static_cast</span>&lt;<span class="Normal">char</span>&gt;<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 if it contains a newline before any non-whitespace</span>
<span class="Comment">// todo: support comments before the newline. But that gets messy.</span>
<span class="Normal">bool</span> is_code_string<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">,</span> ostream&amp; 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 &lt;&lt; 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&amp; in<span class="Delimiter">,</span> ostream&amp; 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>
      out &lt;&lt; <span class="Normal">static_cast</span>&lt;<span class="Normal">char</span>&gt;<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>
    out &lt;&lt; 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> &amp;&amp; brace_depth &gt; <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise &lt;&lt; <span class="Constant">&quot;unbalanced '['</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; 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&amp; in<span class="Delimiter">,</span> ostream&amp; 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 &gt;&gt; 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>
      out &lt;&lt; <span class="Normal">static_cast</span>&lt;<span class="Normal">char</span>&gt;<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>
      out &lt;&lt; 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> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> out &lt;&lt; <span class="Normal">static_cast</span>&lt;<span class="Normal">char</span>&gt;<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 &lt;&lt; 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 &lt;&lt; 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 &lt;&lt; <span class="Constant">&quot;unbalanced '['</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  out<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(after &quot;Parsing reagent(string s)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">'['</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>*s<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> == <span class="Constant">']'</span><span class="Delimiter">);</span>
  <span class="Comment">// delete [] delimiters</span>
  s<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>s<span class="Delimiter">);</span>
  name = s<span class="Delimiter">;</span>
  type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">&quot;literal-string&quot;</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 &quot;string to_string(const reagent&amp; r)&quot;)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal_string<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_string<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> x<span class="Delimiter">.</span>type &amp;&amp; x<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;literal-string&quot;</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">&quot;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">,</span> <span class="Constant">&quot;</span><span class="cSpecial">\\</span><span class="Constant">n&quot;</span><span class="Delimiter">,</span> pos<span class="Delimiter">);</span>
  <span class="Identifier">return</span> <span class="Constant">'&quot;'</span>+name+<span class="Constant">&quot;</span><span class="cSpecial">\&quot;</span><span class="Constant">: </span><span class="cSpecial">\&quot;</span><span class="Constant">literal-string</span><span class="cSpecial">\&quot;</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">size_t</span> replace<span class="Delimiter">(</span>string&amp; str<span class="Delimiter">,</span> <span class="Normal">const</span> string&amp; from<span class="Delimiter">,</span> <span class="Normal">const</span> string&amp; 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&amp; 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="Delimiter">:(scenario string_literal_nested)</span>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>copy [abc [def]]
]
<span class="traceContains">+parse:   ingredient: &quot;abc [def]&quot;: &quot;literal-string&quot;</span>

<span class="Delimiter">:(scenario string_literal_escaped)</span>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>copy [abc \[def]
]
<span class="traceContains">+parse:   ingredient: &quot;abc [def&quot;: &quot;literal-string&quot;</span>

<span class="Delimiter">:(scenario string_literal_escaped_comment_aware)</span>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>copy [
abc \\\[def]
]
<span class="traceContains">+parse:   ingredient: &quot;\nabc \[def&quot;: &quot;literal-string&quot;</span>

<span class="Delimiter">:(scenario string_literal_and_comment)</span>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </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: &quot;abc&quot;: &quot;literal-string&quot;</span>
<span class="traceContains">+parse:   product: 1: (&quot;address&quot; &quot;array&quot; &quot;character&quot;)</span>

<span class="Delimiter">:(scenario string_literal_escapes_newlines_in_trace)</span>
def main [
  copy [abc
def]
]
<span class="traceContains">+parse:   ingredient: &quot;abc\ndef&quot;: &quot;literal-string&quot;</span>

<span class="Delimiter">:(scenario string_literal_can_skip_past_comments)</span>
def main [
  copy [
    <span class="Comment"># ']' inside comment</span>
    bar
  ]
]
<span class="traceContains">+parse:   ingredient: &quot;\n    # ']' inside comment\n    bar\n  &quot;: &quot;literal-string&quot;</span>

<span class="Delimiter">:(scenario string_literal_empty)</span>
def main [
  copy []
]
<span class="traceContains">+parse:   ingredient: &quot;&quot;: &quot;literal-string&quot;</span>
</pre>
</body>
</html>