TokenTree withState(TreeType state, Runnable errorRecovery, Runnable parser) {
        TokenTree tree = TokenTree.of(state);
        trees.getFirst().appendChild(tree);
        // Temporarily make this tree the current tree to capture tokens.
        trees.addFirst(tree);
        try {
            parser.run();
        } catch (ModelSyntaxException e) {
            TokenTree errorTree = TokenTree.fromError(e.getMessageWithoutLocation());
            tree.appendChild(errorTree);
            if (getCurrentToken() != IdlToken.EOF) {
                // Temporarily make the error tree the current tree to capture error recovery tokens.
                trees.addFirst(errorTree);
                next();
                errorRecovery.run();
                trees.removeFirst();
            }
        } finally {
            trees.removeFirst();
        }
        return tree;
    }
Captures tokens to a stack of token trees.
smithy-syntax/src/main/java/software/amazon/smithy/syntax/CapturingTokenizer.java