Note that there are some explanatory texts on larger screens.

plurals
  1. POMost efficient algorithm for parsing nested blocks with escapes or delimiters
    text
    copied!<p>I define a nested block as something with separate opening and closing characters. I.e. <code>{</code> and <code>}</code>, <code>[</code> and <code>]</code>, etc. An algorithm must ignore opening and closing characters if they are enclosed in a delimiter (such as <code>'{'</code> or <code>"{"</code>), or explicitly escaped such as in a comment block. </p> <p>This is not homework (I'm not a student) or idle speculation. My immediate goal is to alphabetically reorder function declarations in ActionScript code files to assist in debugging / comparing different versions. But the real question and more useful for other readers is the general algorithm as described above. In my case the plug-in parameters are just opening = <code>{</code>, closing = <code>}</code>, delimiter = <code>"</code>, escape = <code>//..[end of line]</code>.</p> <p>Please see the following for existing questions that explain why <strong>regular expressions are not an option</strong> for parsing arbitrarily deep nested expressions:</p> <ul> <li><a href="https://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns">Can regular expressions be used to match nested patterns?</a></li> <li><a href="https://stackoverflow.com/questions/7151468/how-can-i-parse-nested-blocks-using-regex">How can I parse nested blocks using Regex? [closed]</a></li> </ul> <p>The obvious blunt solution is to chug through character-by-character and build a context stack and state variables ("inQuote", "inComment", etc). I've done this before. I'm just wondering if there is a more formal or efficient solution; or if this is irreducable. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload