Ruby 4.1.0dev (2026-04-04 revision 6ab9b22553ac802819aa1643a9ac9575e75d1286)
parser.h
Go to the documentation of this file.
1
6#ifndef PRISM_PARSER_H
7#define PRISM_PARSER_H
8
11
12#include "prism/ast.h"
13#include "prism/comments.h"
14#include "prism/diagnostic.h"
17#include "prism/options.h"
18
22typedef struct pm_parser_t pm_parser_t;
23
36PRISM_EXPORTED_FUNCTION PRISM_NODISCARD 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);
37
44
50
55typedef void (*pm_lex_callback_t)(pm_parser_t *parser, pm_token_t *token, void *data);
56
65
74
82
90
97PRISM_EXPORTED_FUNCTION const uint8_t * pm_parser_end(const pm_parser_t *parser) PRISM_NONNULL(1);
98
106
114
124PRISM_EXPORTED_FUNCTION size_t pm_parser_encoding_char_width(const pm_parser_t *parser, const uint8_t *start, ptrdiff_t remaining) PRISM_NONNULL(1, 2);
125
133
141
151PRISM_EXPORTED_FUNCTION pm_constant_id_t pm_parser_constant_find(const pm_parser_t *parser, const uint8_t *start, size_t length) PRISM_NONNULL(1, 2);
152
161
169
179
188
199
207
211typedef void (*pm_comment_callback_t)(const pm_comment_t *comment, void *data);
212
225
233
238
251
259
267
272typedef void (*pm_diagnostic_callback_t)(const pm_diagnostic_t *diagnostic, void *data);
273
286
299
310
315typedef void (*pm_constant_callback_t)(const pm_constant_t *constant, void *data);
316
329
339
347
348#endif
Types and functions related to comments found during parsing.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
A list of diagnostics generated during parsing.
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition exported.h:20
A list of byte offsets of newlines in a string.
Types and functions related to magic comments found during parsing.
The options that can be passed to parsing.
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.
Definition parser.c:41
PRISM_EXPORTED_FUNCTION size_t PRISM_EXPORTED_FUNCTION bool pm_parser_encoding_us_ascii(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns whether or not the parser is using the US-ASCII encoding.
Definition parser.c:85
PRISM_EXPORTED_FUNCTION void pm_parser_lex_callback_set(pm_parser_t *parser, pm_lex_callback_t callback, void *data) PRISM_NONNULL(1)
Register a callback that will be called whenever a token is lexed.
Definition parser.c:24
PRISM_EXPORTED_FUNCTION void pm_parser_comments_each(const pm_parser_t *parser, pm_comment_callback_t callback, void *data) PRISM_NONNULL(1)
Iterates over the comments associated with the given parser and calls the given callback for each com...
Definition parser.c:183
void(* pm_magic_comment_callback_t)(const pm_magic_comment_t *magic_comment, void *data)
A callback function that can be used to process magic comments found while parsing.
Definition parser.h:237
PRISM_EXPORTED_FUNCTION void pm_parser_encoding_changed_callback_set(pm_parser_t *parser, pm_encoding_changed_callback_t callback) PRISM_NONNULL(1)
Register a callback that will be called whenever prism changes the encoding it is using to parse base...
Definition parser.c:16
void(* pm_lex_callback_t)(pm_parser_t *parser, pm_token_t *token, void *data)
This is the callback that is called when a token is lexed.
Definition parser.h:55
PRISM_EXPORTED_FUNCTION void pm_parser_constants_each(const pm_parser_t *parser, pm_constant_callback_t callback, void *data) PRISM_NONNULL(1)
Iterates over the constants in the constant pool associated with the given parser and calls the given...
Definition parser.c:288
PRISM_EXPORTED_FUNCTION void * pm_parser_lex_callback_data(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the opaque data that is passed to the lex callback when it is called.
Definition parser.c:33
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.
Definition parser.c:60
PRISM_EXPORTED_FUNCTION const pm_string_t * pm_parser_filepath(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the filepath that is being used to parse the source.
Definition parser.c:93
PRISM_EXPORTED_FUNCTION void pm_parser_warnings_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) PRISM_NONNULL(1)
Iterates over the warnings associated with the given parser and calls the given callback for each war...
Definition parser.c:270
PRISM_EXPORTED_FUNCTION void pm_parser_errors_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) PRISM_NONNULL(1)
Iterates over the errors associated with the given parser and calls the given callback for each error...
Definition parser.c:261
PRISM_EXPORTED_FUNCTION pm_constant_id_t PRISM_EXPORTED_FUNCTION int8_t pm_parser_frozen_string_literal(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the frozen string literal value of the parser, as determined by the frozen_string_literal mag...
Definition parser.c:110
PRISM_EXPORTED_FUNCTION PRISM_NODISCARD 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:22593
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.
Definition parser.c:212
void(* pm_diagnostic_callback_t)(const pm_diagnostic_t *diagnostic, void *data)
A callback function that can be used to process diagnostics found while parsing.
Definition parser.h:272
PRISM_EXPORTED_FUNCTION void pm_parser_magic_comments_each(const pm_parser_t *parser, pm_magic_comment_callback_t callback, void *data) PRISM_NONNULL(1)
Iterates over the magic comments associated with the given parser and calls the given callback for ea...
Definition parser.c:221
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...
Definition parser.c:300
PRISM_EXPORTED_FUNCTION int pm_parser_lex_state(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the lex state of the parser.
Definition parser.c:150
void(* pm_comment_callback_t)(const pm_comment_t *comment, void *data)
A callback function that can be used to process comments found while parsing.
Definition parser.h:211
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.
Definition parser.c:242
PRISM_EXPORTED_FUNCTION const pm_location_t * pm_parser_data_loc(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the location of the DATA section that is associated with the given parser.
Definition parser.c:130
PRISM_EXPORTED_FUNCTION size_t pm_parser_constants_size(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the number of constants in the constant pool associated with the given parser.
Definition parser.c:279
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.
Definition parser.c:121
PRISM_EXPORTED_FUNCTION size_t pm_parser_encoding_char_width(const pm_parser_t *parser, const uint8_t *start, ptrdiff_t remaining) PRISM_NONNULL(1
Returns the width of the character at the given pointer in the encoding that is being used to parse t...
PRISM_EXPORTED_FUNCTION pm_constant_id_t pm_parser_constant_find(const pm_parser_t *parser, const uint8_t *start, size_t length) PRISM_NONNULL(1
Find a constant in the parser's constant pool.
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:22626
void(* pm_encoding_changed_callback_t)(pm_parser_t *parser)
When the encoding that is being used to parse the source is changed by prism, we provide the ability ...
Definition parser.h:49
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser) PRISM_NONNULL(1)
Initiate the parser with the given parser.
Definition prism.c:22797
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.
Definition parser.c:174
PRISM_EXPORTED_FUNCTION const char * pm_parser_encoding_name(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the name of the encoding that is being used to parse the source.
Definition parser.c:68
PRISM_EXPORTED_FUNCTION const uint8_t * pm_parser_end(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns the raw pointer to the end of the source that is being parsed.
Definition parser.c:49
PRISM_EXPORTED_FUNCTION bool pm_parser_continuable(const pm_parser_t *parser) PRISM_NONNULL(1)
Returns whether the given parser is continuable, meaning that it could become valid if more input wer...
Definition parser.c:139
void(* pm_constant_callback_t)(const pm_constant_t *constant, void *data)
A callback function that can be used to process constants found while parsing.
Definition parser.h:315
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.
Definition parser.c:234
#define PRISM_NODISCARD
Mark the return value of a function as important so that the compiler warns if a caller ignores it.
Definition nodiscard.h:15
#define PRISM_NONNULL(...)
Mark the parameters of a function as non-null.
Definition nonnull.h:13
A list of offsets of the start of lines in a string.
This struct represents a slice in the source code, defined by an offset and a length.
Definition ast.h:554
This is the base structure that represents a node in the syntax tree.
Definition ast.h:1065
A generic string type that can have various ownership semantics.
Definition stringy.h:18
This struct represents a token in the Ruby source.
Definition ast.h:526