I can see one bug just glancing at the code - feeding a stray '}' at the top level can result in depth becoming negative