Header lexy/lexeme.hpp

Class lexy::lexeme

lexy/lexeme.hpp
namespace lexy
{
    template <reader Reader>
    class lexeme
    {
    public:
        using encoding  = typename Reader::encoding;
        using char_type = typename encoding::char_type;
        using iterator  = typename Reader::iterator;

        //=== constructors ===//
        constexpr lexeme() noexcept;
        constexpr lexeme(iterator begin, iterator end) noexcept;
        constexpr explicit lexeme(const Reader& reader, iterator begin) noexcept;

        //=== access ===//
        constexpr bool empty() const noexcept;

        constexpr iterator begin() const noexcept;
        constexpr iterator end() const noexcept;

        //=== iterator-specific access ===//
        constexpr const char_type* data() const noexcept;
        constexpr std::size_t size() const noexcept;
        constexpr char_type operator[](std::size_t idx) const noexcept;
    };

    template <input Input>
    using lexeme_for = lexeme<input_reader<Input>>;
}

The class lexeme represents a sub-range of the input.

It is a lightweight, non-owning type. It is templated on the reader of an input, which minimizes code bloat. The alias lexeme_for instantiates the lexeme with the reader type of an input.

Constructors

lexy/lexeme.hpp
constexpr lexeme() noexcept;                                              (1)
constexpr lexeme(iterator begin, iterator end) noexcept;                  (2)
constexpr explicit lexeme(const Reader& reader, iterator begin) noexcept; (3)
  1. Creates a lexeme for the empty range.

  2. Creates a lexeme for the range [begin, end).

  3. Creates a lexeme for the range [begin, reader.cur()), i.e. from begin until the current position of the reader.

Iterator-specific access

lexy/lexeme.hpp
constexpr const char_type* data() const noexcept;               (1)
constexpr std::size_t size() const noexcept;                    (2)
constexpr char_type operator[](std::size_t idx) const noexcept; (3)
  1. Returns a pointer to the first code unit of the range. Requires that the iterator type is a pointer.

  2. Returns the size of the range. Requires that the iterator type has an operator-.

  3. Returns the code unit at the specific index. Requires that the iterator is a random access iterator.