7#ifndef PRISM_PARSER_HPP
8#define PRISM_PARSER_HPP
10#include "prism/compiler/cplusplus.hpp"
11#include "prism/compiler/lifetimebound.hpp"
63[[nodiscard]]
inline std::string_view
65 assert(constant_id != 0);
77[[nodiscard]]
inline std::string_view
79 return std::string_view(
reinterpret_cast<const char *
>(
pm_parser_start(parser) + location.start), location.length);
105template <
typename T, const T *(*Next)(const T *)>
class next_range {
133 explicit
iterator(const T *current) noexcept : current_(current) {}
149 assert(current_ !=
nullptr);
150 current_ = Next(current_);
173 const T *current_ =
nullptr;
203 std::size_t
size() const noexcept {
return size_; }
210 [[nodiscard]]
bool empty() const noexcept {
return size_ == 0; }
245[[nodiscard]]
inline diagnostic_range
256[[nodiscard]]
inline comment_range
267[[nodiscard]]
inline magic_comment_range
A bump allocator for the prism parser.
struct pm_arena_t pm_arena_t
An opaque pointer to an arena that is used for allocations.
Definition arena.h:22
Steps through the items one after another.
Definition parser.hpp:108
std::forward_iterator_tag iterator_category
The iterator category, for the standard algorithms.
Definition parser.hpp:111
iterator & operator++() noexcept
Advance to the next item.
Definition parser.hpp:148
reference operator*() const noexcept
The item at this position, which must not be the end.
Definition parser.hpp:140
iterator operator++(int) noexcept
Advance to the next item, returning the iterator from before.
Definition parser.hpp:160
const T * value_type
The type of each element.
Definition parser.hpp:114
const value_type & reference
The type of a reference to an element.
Definition parser.hpp:123
friend bool operator==(iterator left, iterator right) noexcept
Whether the two iterators are at the same item.
Definition parser.hpp:167
const value_type * pointer
The type of a pointer to an element.
Definition parser.hpp:120
std::ptrdiff_t difference_type
The type of the distance between two iterators.
Definition parser.hpp:117
friend bool operator!=(iterator left, iterator right) noexcept
Whether the two iterators are at different items.
Definition parser.hpp:170
iterator() noexcept=default
Create the iterator that marks the end of the items.
The diagnostics, comments, or magic comments found while parsing, usable in a range-based for loop an...
Definition parser.hpp:105
bool empty() const noexcept
Whether there are no items.
Definition parser.hpp:210
next_range(const T *first, std::size_t size) noexcept
Create a range of the given number of items starting at first.
Definition parser.hpp:182
std::size_t size() const noexcept
The number of items.
Definition parser.hpp:203
iterator end() const noexcept
An iterator past the last item.
Definition parser.hpp:196
iterator begin() const noexcept
An iterator at the first item.
Definition parser.hpp:189
A data structure that stores a set of strings.
PRISM_EXPORTED_FUNCTION const uint8_t * pm_constant_start(const pm_constant_t *constant) PRISM_NONNULL(1)
Return a raw pointer to the start of a constant.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
Definition constant_pool.h:30
struct pm_constant_t pm_constant_t
A constant in the pool which effectively stores a string.
Definition constant_pool.h:63
PRISM_EXPORTED_FUNCTION size_t pm_constant_length(const pm_constant_t *constant) PRISM_NONNULL(1)
Return the length of a constant.
A list of byte offsets of newlines in a string.
PRISM_EXPORTED_FUNCTION pm_line_column_t pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) PRISM_NONNULL(1)
Returns the line and column of the given offset.
struct pm_options_t pm_options_t
The options that can be passed to the parser.
Definition options.h:30
The parser used to parse Ruby source.
PRISM_EXPORTED_FUNCTION const uint8_t * pm_parser_start(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the raw pointer to the start of the source that is being parsed.
PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_parser_errors_first(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the first error associated with the given parser.
PRISM_EXPORTED_FUNCTION int32_t pm_parser_start_line(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the line that the parser was considered to have started on.
struct pm_parser_t pm_parser_t
The parser used to parse Ruby source.
Definition parser.h:26
PRISM_EXPORTED_FUNCTION size_t pm_parser_magic_comments_size(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the number of magic comments associated with the given parser.
PRISM_EXPORTED_FUNCTION const pm_constant_t * pm_parser_constant(const pm_parser_t *parser, pm_constant_id_t constant_id) PRISM_NONNULL(1)
Returns a pointer to the constant at the given id in the constant pool associated with the given pars...
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_parser_t * pm_parser_new(pm_arena_t *arena, const uint8_t *source, size_t size, const pm_options_t *options) PRISM_NONNULL(1)
Allocate and initialize a parser with the given start and end pointers.
PRISM_EXPORTED_FUNCTION size_t pm_parser_warnings_size(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the number of warnings associated with the given parser.
PRISM_EXPORTED_FUNCTION const pm_line_offset_list_t * pm_parser_line_offsets(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the line offsets that are associated with the given parser.
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser) PRISM_NONNULL(1)
Free both the memory held by the given parser and the parser itself.
PRISM_EXPORTED_FUNCTION size_t pm_parser_comments_size(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the number of comments associated with the given parser.
PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_parser_warnings_first(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the first warning associated with the given parser.
PRISM_EXPORTED_FUNCTION const pm_comment_t * pm_parser_comments_first(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the first comment associated with the given parser.
PRISM_EXPORTED_FUNCTION const pm_magic_comment_t * pm_parser_magic_comments_first(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the first magic comment associated with the given parser.
PRISM_EXPORTED_FUNCTION size_t pm_parser_errors_size(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the number of errors associated with the given parser.
std::string_view constant(const pm_parser_t *parser, pm_constant_id_t constant_id) noexcept
The name of the constant with the given id, which must not be zero.
Definition parser.hpp:64
unique_parser make_parser(pm_arena_t *arena, std::string_view source, const pm_options_t *options=nullptr) noexcept
Allocate a new parser over the given source.
Definition parser.hpp:51
std::string_view slice(const pm_parser_t *parser, pm_location_t location) noexcept
The bytes of source that the given location covers.
Definition parser.hpp:78
std::unique_ptr< pm_parser_t, parser_deleter > unique_parser
An owned parser.
Definition parser.hpp:38
comment_range comments(const pm_parser_t *parser) noexcept
The comments found while parsing, in source order.
Definition parser.hpp:257
diagnostic_range errors(const pm_parser_t *parser) noexcept
The errors found while parsing.
Definition parser.hpp:235
magic_comment_range magic_comments(const pm_parser_t *parser) noexcept
The magic comments found while parsing.
Definition parser.hpp:268
diagnostic_range warnings(const pm_parser_t *parser) noexcept
The warnings found while parsing.
Definition parser.hpp:246
pm_line_column_t line_column(const pm_parser_t *parser, std::uint32_t offset) noexcept
The line and column of the given byte offset into the source.
Definition parser.hpp:91
A line and column in a string.
Definition line_offset_list.h:45
This struct represents a slice in the source code, defined by an offset and a length.
Definition ast.h:584
Frees a parser with pm_parser_free.
Definition parser.hpp:28
void operator()(pm_parser_t *parser) const noexcept
Free the given parser.
Definition parser.hpp:34