title: Parsing conditions in a custom DSL using a tokenizer
body: <p>I am trying to parse a "rules string" provided over an API in PHP. </p>
  
  <p>Given that I have an array of items such as</p>
  
  <pre><code>$items = [41, 42, 51, 54, 65, 12];
  </code></pre>
  
  <p>I wish to evaluate this expression</p>
  
  <pre><code>({41} AND {51}) OR ({31} AND {42})
  </code></pre>
  
  <p>In this instance it should return true, since the $items array has both the 41 and 51 items.</p>
  
  <p>I started with creating a tokenizer using the <a href="https://github.com/nette/tokenizer" rel="nofollow noreferrer">https://github.com/nette/tokenizer</a> library and currently have it parsing the string into an array of tokens.</p>
  
  <pre><code>array:25 [
    0 =&gt; array:3 [
      0 =&gt; "("
      1 =&gt; 0
      2 =&gt; 1
    ]
    1 =&gt; array:3 [
      0 =&gt; "{"
      1 =&gt; 1
      2 =&gt; 2
    ]
    2 =&gt; array:3 [
      0 =&gt; "41"
      1 =&gt; 2
      2 =&gt; 3
    ]
    3 =&gt; array:3 [
      0 =&gt; "}"
      1 =&gt; 4
      2 =&gt; 4
    ]
    4 =&gt; array:3 [
      0 =&gt; " "
      1 =&gt; 5
      2 =&gt; 8
    ]
    5 =&gt; array:3 [
      0 =&gt; "AND"
      1 =&gt; 6
      2 =&gt; 5
    ]
    6 =&gt; array:3 [
      0 =&gt; " "
      1 =&gt; 9
      2 =&gt; 8
    ]
    7 =&gt; array:3 [
      0 =&gt; "{"
      1 =&gt; 10
      2 =&gt; 2
    ]
    8 =&gt; array:3 [
      0 =&gt; "51"
      1 =&gt; 11
      2 =&gt; 3
    ]
    9 =&gt; array:3 [
      0 =&gt; "}"
      1 =&gt; 13
      2 =&gt; 4
    ]
    10 =&gt; array:3 [
      0 =&gt; ")"
      1 =&gt; 14
      2 =&gt; 7
    ]
    11 =&gt; array:3 [
      0 =&gt; " "
      1 =&gt; 15
      2 =&gt; 8
    ]
    12 =&gt; array:3 [
      0 =&gt; "OR"
      1 =&gt; 16
      2 =&gt; 6
    ]
    13 =&gt; array:3 [
      0 =&gt; " "
      1 =&gt; 18
      2 =&gt; 8
    ]
    14 =&gt; array:3 [
      0 =&gt; "("
      1 =&gt; 19
      2 =&gt; 1
    ]
    15 =&gt; array:3 [
      0 =&gt; "{"
      1 =&gt; 20
      2 =&gt; 2
    ]
    16 =&gt; array:3 [
      0 =&gt; "31"
      1 =&gt; 21
      2 =&gt; 3
    ]
    17 =&gt; array:3 [
      0 =&gt; "}"
      1 =&gt; 23
      2 =&gt; 4
    ]
    18 =&gt; array:3 [
      0 =&gt; " "
      1 =&gt; 24
      2 =&gt; 8
    ]
    19 =&gt; array:3 [
      0 =&gt; "AND"
      1 =&gt; 25
      2 =&gt; 5
    ]
    20 =&gt; array:3 [
      0 =&gt; " "
      1 =&gt; 28
      2 =&gt; 8
    ]
    21 =&gt; array:3 [
      0 =&gt; "{"
      1 =&gt; 29
      2 =&gt; 2
    ]
    22 =&gt; array:3 [
      0 =&gt; "42"
      1 =&gt; 30
      2 =&gt; 3
    ]
    23 =&gt; array:3 [
      0 =&gt; "}"
      1 =&gt; 32
      2 =&gt; 4
    ]
    24 =&gt; array:3 [
      0 =&gt; ")"
      1 =&gt; 33
      2 =&gt; 7
    ]
  ]
  </code></pre>
  
  <p>However this is where my talent runs out and I am not sure how to process the tokens so that I can run the condition against my $items array.</p>
  
  <p>It could be that I am approaching this completely wrong. </p>