Skip to content

Instantly share code, notes, and snippets.

/**
* Parses a message and replaces all [a,b,c] structures with a random
* selection.
*
* Supports nested structures and escaping those special characters.
*
* The general approach is to make a single pass through the whole message
* character by character, keeping a stack of lists representing all the choices
* at the current index. The outer stack tracks the current nesting level while
* the inner list tracks the choices at the given nesting level.