title: Twig token parser: How to parse tags with prefixes?
body: <p>My templates contain Twig “tags” that describe the pages. These are simple name/value pairs like</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% role "editor" %}
  {% title "Foobar" %}
  </code></pre>
  
  <p>I can process them very nicely with a Twig extension and a custom token parser, like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>class RoleTokenParser extends \Twig_TokenParser
  {
      public function parse(\Twig_Token $Token)
      {
          $TokenStream = $this-&gt;parser-&gt;getStream();
          $role = $TokenStream-&gt;expect(\Twig_Token::STRING_TYPE)-&gt;getValue();
          $TokenStream-&gt;expect(\Twig_Token::BLOCK_END_TYPE);
          return new RoleNode(['role' =&gt; $role], $Token-&gt;getLine(), $this-&gt;getTag());
      }
  
      public function getTag()
      {
          return 'role';
      }
  }
  </code></pre>
  
  <p>But now I want to “prefix” the name parts like this:</p>
  
  <pre><code>{% myapp.role "editor" %}
  </code></pre>
  
  <p>I understand that the dot between <code>myapp</code> and <code>role</code> is a seperate token. What I don't get to work is to tell the <code>TokenParser</code> what to expect. I tried the following:</p>
  
  <pre class="lang-php prettyprint-override"><code>// ...
  $prefix = $TokenStream-&gt;expect(\Twig_Token::STRING_TYPE)-&gt;getValue();
  $dot = $TokenStream-&gt;expect(\Twig_Token::PUNCTUATION_TYPE)-&gt;getValue();
  $role = $TokenStream-&gt;expect(\Twig_Token::STRING_TYPE)-&gt;getValue();
  // ...
  </code></pre>
  
  <p>… but that results in exceptions:</p>
  
  <p>When my <code>Twig_TokenParser::getTag()</code> returns <code>myapp.role</code>, I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>[Twig_Error_Syntax]
  Unknown tag name "myapp". Did you mean "myapp.role" at line 3
  </code></pre>
  
  <p>When my <code>Twig_TokenParser::getTag()</code> returns only <code>myapp</code>, I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>[Twig_Error_Syntax]                                                        
  Unexpected token "punctuation" of value "." ("string" expected) at line 3  
  </code></pre>
  
  <p>So, the question is: How do I tell the <code>Token_Parser</code> what tokens to expect, when the pattern is <code>{% myapp.role "editor" %}</code>?</p>
  
  <p>(Btw, a solution that works on a similar syntax (like <code>{% myapp role "editor" %}</code>) would also be acceptable.)</p>