title: How to annotate the AST with position information?
body: <p>I'm using Boost Spirit to parse source files in a little compiler project. </p>
  
  <p>If an error arise during the parsing itself, I can print the position of the error, but how do I do in later phase, typically when performing semantic checks ? </p>
  
  <p>My source file is parsed into an Abstract Syntax Tree using auto rules. I want to add line and col informations into the AST nodes. Is there a easy way to achieve that during parsing ? </p>
  
  <p>I'm using boost::spirit::classic::position_iterator2 in my Lexer and then use this lexer in my grammar. </p>
  
  <p>Thank you</p>
  
  <p><strong>EDIT for sehe:</strong> </p>
  
  <p>The lexer is defined like that:</p>
  
  <pre><code>typedef std::string::iterator base_iterator_type;
  typedef boost::spirit::classic::position_iterator2&lt;base_iterator_type&gt; pos_iterator_type;
  typedef boost::spirit::lex::lexertl::token&lt;pos_iterator_type&gt; Tok;
  typedef boost::spirit::lex::lexertl::actor_lexer&lt;Tok&gt; lexer_type;
  
  template&lt;typename L&gt;
  class SpiritLexer : public lex::lexer&lt;L&gt; {
     //Token definitions
  }
  
  typedef lexer_type::iterator_type Iterator;
  typedef SpiritLexer&lt;lexer_type&gt; Lexer;
  </code></pre>
  
  <p>The grammars are defined like that:</p>
  
  <pre><code>struct EddiGrammar : qi::grammar&lt;lexer::Iterator, ast::SourceFile()&gt; {
      EddiGrammar(const lexer::Lexer&amp; lexer);
  
      //Token definitions
  };
  </code></pre>
  
  <p>And finally, here is how I parse the source: </p>
  
  <pre><code>ast::SourceFile program
  
  std::ifstream in(file.c_str());
  in.unsetf(std::ios::skipws);
  
  in.seekg(0, std::istream::end);
  std::size_t size(static_cast&lt;size_t&gt;(in.tellg()));
  
  in.seekg(0, std::istream::beg);
  
  std::string contents(size, 0);
  in.read(&amp;contents[0], size);
  
  pos_iterator_type position_begin(contents.begin(), contents.end(), file);
  pos_iterator_type position_end;
  
  Lexer lexer;
  EddiGrammar grammar(lexer);
  
  bool r = spirit::lex::tokenize_and_parse(position_begin, position_end, lexer, grammar, program);
  </code></pre>
  
  <p>In my grammar, I use the lexer by referring to some lexer tokens. For example:</p>
  
  <pre><code>else_ %=
          lexer.else_
      &gt;&gt;  lexer.left_brace
      &gt;&gt;  *(instruction)
      &gt;&gt;  lexer.right_brace;
  </code></pre>
  
  <p>All my AST nodes are constructed using auto rules. </p>