1#include "prism/internal/parser.h"
3#include "prism/internal/allocator.h"
4#include "prism/internal/comments.h"
5#include "prism/internal/diagnostic.h"
6#include "prism/internal/encoding.h"
7#include "prism/internal/magic_comments.h"
8#include "prism/internal/xxh3.h"
20 parser->encoding_changed_callback = callback;
28 parser->lex_callback.callback = callback;
29 parser->lex_callback.data = data;
37 return parser->lex_callback.data;
64 return parser->start_line;
72 return parser->encoding->name;
80pm_parser_encoding_char_width(
const pm_parser_t *parser,
const uint8_t *start, ptrdiff_t remaining) {
81 return parser->encoding->char_width(start, remaining);
89 return parser->encoding == PM_ENCODING_US_ASCII_ENTRY;
97 return &parser->filepath;
105pm_parser_constant_find(
const pm_parser_t *parser,
const uint8_t *start,
size_t length) {
106 return pm_constant_pool_find(&parser->constant_pool, start, length);
114pm_parser_constant_insert(
pm_parser_t *parser,
const uint8_t *start,
size_t length) {
115 pm_constant_id_t id = pm_constant_pool_find(&parser->constant_pool, start, length);
116 if (
id != PM_CONSTANT_ID_UNSET)
return id;
118 const uint8_t *copy = (
const uint8_t *) pm_arena_memdup(&parser->metadata_arena, start, length,
PRISM_ALIGNOF(uint8_t));
119 return pm_constant_pool_insert_constant(&parser->metadata_arena, &parser->constant_pool, copy, length);
127 return parser->frozen_string_literal;
138 return &parser->line_offsets;
147 return &parser->data_loc;
155 const uint8_t *end = parser->end;
157 if (parser->data_loc.
length != 0) {
158 const uint8_t *cursor = parser->start + parser->data_loc.
start;
159 const uint8_t *newline = memchr(cursor,
'\n', (
size_t) (end - cursor));
160 if (newline != NULL) end = newline + 1;
163 return pm_xxh3(parser->start, (
size_t) (end - parser->start));
172 return parser->continuable;
183 return (
int) parser->lex_state;
191 return comment->location;
199 return comment->type;
215 return parser->comment_list.size;
223 return (
const pm_comment_t *) parser->comment_list.head;
231 return pm_parser_comment_range(parser, node->
node_id).
length;
241 if (index >= range.length)
return NULL;
243 return parser->comment_attachments.comments[parser->comment_attachments.attachments[range.index + index]];
251pm_parser_comment_attachment(
const pm_parser_t *parser, uint32_t index) {
252 if (parser->comment_attachments.attachments == NULL || index >= parser->comment_list.size)
return UINT32_MAX;
253 return parser->comment_attachments.attachments[index];
261 return parser->comment_attachments.ranges_size;
268pm_parser_comment_range(
const pm_parser_t *parser, uint32_t node_id) {
269 if (node_id >= parser->comment_attachments.ranges_size)
return (
pm_comment_range_t) { .
index = 0, .length = 0 };
270 return parser->comment_attachments.ranges[node_id];
302 return parser->magic_comment_list.size;
318 return parser->error_list.size;
326 return parser->warning_list.size;
351 return parser->constant_pool.size;
360 assert(index < parser->constant_pool.size);
361 return &parser->constant_pool.constants[index];
370 return pm_constant_pool_id_to_constant(&parser->constant_pool, constant_id);
#define PRISM_ALIGNOF
Get the alignment requirement of a type.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
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.
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 ...
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.
uint32_t start
The offset of the location from the start of the source.
uint32_t length
The length of the location.
This is the base structure that represents a node in the syntax tree.
uint32_t node_id
The unique identifier for this node, which is deterministic based on the source.
A view of a sequence of bytes.