Prism Ruby parser
Loading...
Searching...
No Matches
parser.hpp
Go to the documentation of this file.
1
7#ifndef PRISM_PARSER_HPP
8#define PRISM_PARSER_HPP
9
10#include "prism/compiler/cplusplus.hpp"
11#include "prism/compiler/lifetimebound.hpp"
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 {
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 {
237}
238
245[[nodiscard]] inline diagnostic_range
246warnings(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
248}
249
256[[nodiscard]] inline comment_range
257comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
259}
260
267[[nodiscard]] inline magic_comment_range
268magic_comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
270}
271
272}
273
274#endif
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