Defined in header <cparse/Parser.hh>

protected:
Cursor&       getCursor();                    // (1)
Cursor const& getCursor() const;              // (2)

Returns a reference to the active Cursor. Overload 1 for non-const access (call cursor methods that advance the position), overload 2 for const access (read line/column without touching the position).

Grammars use overload 1 pervasively — every character-consumption loop and every save/restore for backtracking goes through the active cursor.

Parameters

None.

Return value

A reference to the active cursor. The reference is stable for the duration of the current parse() call and any nested pushCursor scope, but a nested pushCursor() or a return from a nested parse changes which cursor is active.

Exceptions

Not marked noexcept in the declaration. The base implementation dereferences a shared_ptr<Cursor> and does not throw when the cursor is set. Calling getCursor() before any parse has run (when the internal cursor pointer is null) dereferences a null shared_ptr — undefined behaviour. Grammars only see getCursor() from inside start() or helpers it calls, where the cursor is always set by parse() Phase 3.

Notes

Never store the returned reference beyond the current call scope — a nested pushCursor() swaps the cursor and invalidates any stored reference. Always call getCursor() fresh each time.

For save/restore, use storeCursor() and restoreCursor(), or copy the cursor by value:

Cursor snapshot = getCursor();     // copy — see Cursor::Cursor(Cursor const&)
if (!tryAlternative())
    restoreCursor(snapshot);

Example

bool parseIdentifier(std::string& out)
{
    Cursor& c = getCursor();

    if (!std::isalpha(static_cast<unsigned char>(c.safePeek())))
        return false;

    while (std::isalnum(static_cast<unsigned char>(c.safePeek())) ||
           c.safePeek() == '_')
    {
        out += c.safeGet();
    }
    return !out.empty();
}

See also