You can try to implement something around PushBackReader , which has an unread method that allows you to push unwanted characters into the pushback buffer.
char a, b; // reader holds "ab" a = reader.read(); // reader holds "b" b = reader.read(); // reader holds "" reader.unread(b); // reader holds "b" b = reader.read(); // reader holds "" reader.unread(b); // reader holds "b" reader.unread(a); // reader holds "ab" a = reader.read(); // reader holds "b" b = reader.read(); // reader holds ""
For parsing applications, this kind of tool is enough to do what you need.
Another option is for each nesting layer that needs to be labeled in order to build a new Reader on top of the one with which they were transferred, and mark this instead.
source share