public function parse($pattern)
    {
        $lexer = $this->createLexer();

        try {
            $result = $lexer->lex($pattern);
        } catch (LexingException $e) {
            $message = sprintf('Lexing failed with error: %s', $e->getMessage());
            throw new TokenizeException($message, 0, $e);
        }

        // If the lexer still has state, then it reached the end of the pattern prematurely. An example of when this
        // can happen is unclosed character groupings (e.g. "foo[a-z") - lack of closing bracket will mean the lexer
        // does not pop out of the IN_GROUP state.
        if ($lexer->hasPushedStates()) {
            throw new TokenizeException('Premature end of pattern');
        }

        return $result;
    }