diff --git a/src/shared/cplusplus/MemoryPool.cpp b/src/shared/cplusplus/MemoryPool.cpp
index 2002a6543504d58dc8828e149bc128e3e2b74774..1453c62cd694e079ab2d71275bb7928f7b7ee9b3 100644
--- a/src/shared/cplusplus/MemoryPool.cpp
+++ b/src/shared/cplusplus/MemoryPool.cpp
@@ -110,6 +110,19 @@ void *MemoryPool::allocate_helper(size_t size)
     return addr;
 }
 
+MemoryPool::State MemoryPool::state() const
+{ return State(ptr, _blockCount); }
+
+void MemoryPool::rewind(const State &state)
+{
+    if (_blockCount == state.blockCount && state.ptr < ptr) {
+        if (_initializeAllocatedMemory)
+            memset(state.ptr, '\0', ptr - state.ptr);
+
+        ptr = state.ptr;
+    }
+}
+
 Managed::Managed()
 { }
 
diff --git a/src/shared/cplusplus/MemoryPool.h b/src/shared/cplusplus/MemoryPool.h
index e0f1ff870170c179e8246679e55e2eac1c50c76c..5b6fae925ff42a1da1d68162f9906dbfd5bcf022 100644
--- a/src/shared/cplusplus/MemoryPool.h
+++ b/src/shared/cplusplus/MemoryPool.h
@@ -79,6 +79,22 @@ public:
         return allocate_helper(size);
     }
 
+    struct State
+    {
+        char *ptr;
+        char *end;
+        int blockCount;
+
+        inline bool isValid() const
+        { return ptr != 0; }
+
+        inline State(char *ptr = 0, int blockCount = 0)
+            : ptr(ptr), blockCount(blockCount) {}
+    };
+
+    State state() const;
+    void rewind(const State &state);
+
 private:
     void *allocate_helper(size_t size);
 
diff --git a/src/shared/cplusplus/Parser.cpp b/src/shared/cplusplus/Parser.cpp
index 8f3bd549ce490ed864df5074c5ff3c939bede71e..24851c991da3ac1d59fe1a52695d0f1faaced93e 100644
--- a/src/shared/cplusplus/Parser.cpp
+++ b/src/shared/cplusplus/Parser.cpp
@@ -88,6 +88,30 @@ int DebugRule::depth = 0;
 #  define DEBUG_THIS_RULE() do {} while (0)
 #endif
 
+class Parser::Rewind
+{
+    Parser *_parser;
+    MemoryPool::State _state;
+
+public:
+    inline Rewind(Parser *parser)
+        : _parser(parser) {}
+
+    inline void operator()(unsigned tokenIndex)
+    { rewind(tokenIndex); }
+
+    inline void mark()
+    { _state = _parser->_pool->state(); }
+
+    inline void rewind(unsigned tokenIndex)
+    {
+        _parser->rewind(tokenIndex);
+
+        if (_state.isValid())
+            _parser->_pool->rewind(_state);
+    }
+};
+
 Parser::Parser(TranslationUnit *unit)
     : _translationUnit(unit),
       _control(_translationUnit->control()),
@@ -302,6 +326,11 @@ bool Parser::parseClassOrNamespaceName(NameAST *&node)
 bool Parser::parseTemplateId(NameAST *&node)
 {
     DEBUG_THIS_RULE();
+
+    const unsigned start = cursor();
+    Rewind rewind(this);
+    rewind.mark();
+
     if (LA() == T_IDENTIFIER && LA(2) == T_LESS) {
         TemplateIdAST *ast = new (_pool) TemplateIdAST;
         ast->identifier_token = consumeToken();
@@ -315,6 +344,9 @@ bool Parser::parseTemplateId(NameAST *&node)
             }
         }
     }
+
+    rewind(start);
+
     return false;
 }
 
diff --git a/src/shared/cplusplus/Parser.h b/src/shared/cplusplus/Parser.h
index 3ca3371d815956cbbe47ced8f091a0010b89194e..2a7c15dfa3e98b5969840a53cde04fed039c23df 100644
--- a/src/shared/cplusplus/Parser.h
+++ b/src/shared/cplusplus/Parser.h
@@ -298,6 +298,9 @@ private:
     bool _inFunctionBody: 1;
     bool _inObjCImplementationContext: 1;
 
+    class Rewind;
+    friend class Rewind;
+
 private:
     Parser(const Parser& source);
     void operator =(const Parser& source);