Ruby 4.1.0dev (2026-10-21 revision 536dc6f98b72e6999a84bb6373562c43cb1ddf8a)
parser.hpp
Go to the documentation of this file.
1
7#ifndef PRISM_PARSER_HPP
8#define PRISM_PARSER_HPP
9
12
13#include "prism/arena.h"
14#include "prism/constant_pool.h"
16#include "prism/parser.h"
17
18#include <cassert>
19#include <cstddef>
20#include <cstdint>
21#include <iterator>
22#include <memory>
23#include <string_view>
24
25namespace prism {
26
34 void operator()(pm_parser_t *parser) const noexcept { pm_parser_free(parser); }
35};
36
38using unique_parser = std::unique_ptr<pm_parser_t, parser_deleter>;
39
50[[nodiscard]] inline unique_parser
51make_parser(pm_arena_t *arena PRISM_LIFETIMEBOUND, std::string_view source PRISM_LIFETIMEBOUND, const pm_options_t *options PRISM_LIFETIMEBOUND = nullptr) noexcept {
52 return unique_parser(pm_parser_new(arena, reinterpret_cast<const std::uint8_t *>(source.data()), source.size(), options));
53}
54
63[[nodiscard]] inline std::string_view
64constant(const pm_parser_t *parser PRISM_LIFETIMEBOUND, pm_constant_id_t constant_id) noexcept {
65 assert(constant_id != 0);
66 const pm_constant_t *entry = pm_parser_constant(parser, constant_id);
67 return std::string_view(reinterpret_cast<const char *>(pm_constant_start(entry)), pm_constant_length(entry));
68}
69
77[[nodiscard]] inline std::string_view
78slice(const pm_parser_t *parser PRISM_LIFETIMEBOUND, pm_location_t location) noexcept {
79 return std::string_view(reinterpret_cast<const char *>(pm_parser_start(parser) + location.start), location.length);
80}
81
90[[nodiscard]] inline pm_line_column_t
91line_column(const pm_parser_t *parser, std::uint32_t offset) noexcept {
92 return pm_line_offset_list_line_column(pm_parser_line_offsets(parser), offset, pm_parser_start_line(parser));
93}
94
95namespace detail {
96
105template <typename T, const T *(*Next)(const T *)> class next_range {
106public:
108 class iterator {
109 public:
111 using iterator_category = std::forward_iterator_tag;
112
114 using value_type = const T *;
115
117 using difference_type = std::ptrdiff_t;
118
120 using pointer = const value_type *;
121
123 using reference = const value_type &;
124
126 iterator() noexcept = default;
127
133 explicit iterator(const T *current) noexcept : current_(current) {}
134
140 reference operator*() const noexcept { return current_; }
141
147 iterator &
148 operator++() noexcept {
149 assert(current_ != nullptr);
150 current_ = Next(current_);
151 return *this;
152 }
153
160 operator++(int) noexcept {
161 iterator previous = *this;
162 ++*this;
163 return previous;
164 }
165
167 friend bool operator==(iterator left, iterator right) noexcept { return left.current_ == right.current_; }
168
170 friend bool operator!=(iterator left, iterator right) noexcept { return left.current_ != right.current_; }
171
172 private:
173 const T *current_ = nullptr;
174 };
175
182 next_range(const T *first, std::size_t size) noexcept : first_(first), size_(size) {}
183
189 iterator begin() const noexcept { return iterator(first_); }
190
196 iterator end() const noexcept { return iterator(); }
197
203 std::size_t size() const noexcept { return size_; }
204
210 [[nodiscard]] bool empty() const noexcept { return size_ == 0; }
211
212private:
213 const T *first_;
214 std::size_t size_;
215};
216
217}
218
221
224
227
234[[nodiscard]] inline diagnostic_range
235errors(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
236 return diagnostic_range(pm_parser_errors_first(parser), pm_parser_errors_size(parser));
237}
238
245[[nodiscard]] inline diagnostic_range
246warnings(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
247 return diagnostic_range(pm_parser_warnings_first(parser), pm_parser_warnings_size(parser));
248}
249
256[[nodiscard]] inline comment_range
257comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
258 return comment_range(pm_parser_comments_first(parser), pm_parser_comments_size(parser));
259}
260
267[[nodiscard]] inline magic_comment_range
269 return magic_comment_range(pm_parser_magic_comments_first(parser), pm_parser_magic_comments_size(parser));
270}
271
272}
273
274#endif
A bump allocator for the prism parser.
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.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
#define PRISM_LIFETIMEBOUND
Mark a parameter that the return value points into, so that the compiler warns if a temporary is pass...
A list of byte offsets of newlines in a string.
The parser used to parse Ruby source.
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.
Definition prism.c:23538
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.
Definition prism.c:23562
std::string_view slice(const pm_parser_t *parser PRISM_LIFETIMEBOUND, pm_location_t location) noexcept
The bytes of source that the given location covers.
Definition parser.hpp:78
comment_range comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The comments found while parsing, in source order.
Definition parser.hpp:257
std::unique_ptr< pm_parser_t, parser_deleter > unique_parser
An owned parser.
Definition parser.hpp:38
magic_comment_range magic_comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The magic comments found while parsing.
Definition parser.hpp:268
std::string_view constant(const pm_parser_t *parser PRISM_LIFETIMEBOUND, 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
diagnostic_range errors(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The errors found while parsing.
Definition parser.hpp:235
unique_parser make_parser(pm_arena_t *arena PRISM_LIFETIMEBOUND, std::string_view source PRISM_LIFETIMEBOUND, const pm_options_t *options PRISM_LIFETIMEBOUND=nullptr) noexcept
Allocate a new parser over the given source.
Definition parser.hpp:51
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
diagnostic_range warnings(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The warnings found while parsing.
Definition parser.hpp:246
A line and column in a string.
This struct represents a slice in the source code, defined by an offset and a length.
Definition ast.h:579
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