title: parse nested conditional statements
body: <p>I need to parse a file that contains conditional statements, sometimes nested inside one another.</p>
  
  <p>I have a file that stores configuration data but the configuration data is slightly different depending on user defined options. I can deal with the conditional statements, they're all just booleans with no operations but I don't know how to recursively evaluate the nested conditionals. For instance, a piece of the file might look like:</p>
  
  <pre><code>...
  #if CELSIUS
      #if FROM_KELVIN ; this is a comment about converting kelvin to celsius.
          temp_conversion = 1, 273
      #else
          temp_conversion = 0.556, -32
      #endif
  #else
      #if FROM_KELVIN
          temp_conversion = 1.8, -255.3
      #else
          temp_conversion = 1.8, 17.778
      #endif
  #endif
  ...
  </code></pre>
  
  <p>... Also, some conditionals don't have an <code>#else</code> statement, just <code>#if CONDITION statement(s) #endif</code>.</p>
  
  <p>I realize that this could be easy if the file were just written in XML or something else with a nice parser to begin with, but this is what I have to work with so I'm wondering if there's any relatively simple way to parse this file. It's similar to parenthesis matching so I imagine there would be some module for it but I haven't found anything.</p>
  
  <p>I'm working in python but I can switch for this function if it's easier to solve this in another language.</p>