title: Parsing mixed values and key-value pairs with Boost.Spirit
body: <p>I have a simple grammar consisting of mixed variables (<code>$(name)</code>) and variable-value pairs (<code>$(name:value)</code>). I have a hand-coded recursive parser, but am interested in using it as an exercise to learn Spirit, which I'll need for more complex grammars eventually(/soon).</p>
  
  <p>Anyway, the set of possible forms I'm working with (simplified from the full grammar) is:</p>
  
  <pre><code>$(variable)     // Uses simple look-up, recursion and inline replace
  $(name:value)   // Inserts a new variable into the local lookup table
  </code></pre>
  
  <p>My current rules look something like:</p>
  
  <pre><code>typedef std::map&lt;std::string, std::string&gt; dictionary;
  
  template &lt;typename Iterator&gt;
  bool parse_vars(Iterator first, Iterator last, dictionary &amp; vars, std::string &amp; output)
  {
      using qi::phrase_parse;
      using qi::_1;
      using ascii::char_;
      using ascii::string;
      using ascii::space;
      using phoenix::insert;
  
      dictionary statevars;
  
      typedef qi::rule&lt;Iterator, std::string()&gt; string_rule;
      typedef qi::rule&lt;Iterator, std::pair&lt;std::string, std::string&gt;()&gt; pair_rule;
  
      string_rule state = string &gt;&gt; ':' &gt;&gt; string; // Error 3
      pair_rule variable = 
      (
          char_('$') &gt;&gt; '(' &gt;&gt; 
          (
              state[insert(phoenix::ref(statevars), _1)] |
              string[output += vars[_1]] // Error 1, will eventually need to recurse
          ) &gt;&gt; ')'
      ); // Error 2
  
      bool result = phrase_parse
      (
          first, last, 
          (
              variable % ','
          ), 
          space
      );
  
      return r;
  }
  </code></pre>
  
  <p>If it wasn't obvious, I have no idea how Spirit works and the docs have everything but actual explanations, so this is about an hour of throwing examples together.</p>
  
  <p>The parts I particularly question are the leading <code>char_('$')</code> in the <em>variable</em> rule, but removing this causes a shift operator error (the compiler interprets <code>'$' &gt;&gt; '('</code> as a right-shift).</p>
  
  <p>When compiling, I get errors related to the <em>state</em> rule, particularly creating the pair, and the lookup:</p>
  
  <ol>
  <li>error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const boost::spirit::_1_type' (or there is no acceptable conversion)</li>
  <li>error C2512: 'boost::spirit::qi::rule::rule' : no appropriate default constructor available</li>
  </ol>
  
  <p>Changing the lookup (<code>vars[_1]</code>) to a simple <code>+=</code> gives:</p>
  
  <p><em>3</em>. error C2665: 'boost::spirit::char_class::classify::is' : none of the 15 overloads could convert all the argument types</p>
  
  <p>Error 1 seems to relate to the type (attribute?) of the <code>_1</code> placeholder, but that should be a string, and <em>is</em> when used for printing or concatenation to the output string. 2 appears to be noise caused by 1.</p>
  
  <p>Error 3, digging down the stack of template errors, seems to relate to not being able to turn the <em>state</em> rule into a pair, which seems odd as it almost exactly matches one of the rules from <a href="http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/" rel="nofollow">this example</a>.</p>
  
  <p>How can I modify the <em>variable</em> rule to properly handle both input forms?</p>