Ruby: prism/prism.h Source File

Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)

prism.h
Go to the documentation of this file.
1
6#ifndef PRISM_H
7#define PRISM_H
8
9#include "prism/defines.h"
10#include "prism/util/pm_buffer.h"
11#include "prism/util/pm_char.h"
12#include "prism/util/pm_integer.h"
13#include "prism/util/pm_memchr.h"
14#include "prism/util/pm_strncasecmp.h"
15#include "prism/util/pm_strpbrk.h"
16#include "prism/ast.h"
17#include "prism/diagnostic.h"
18#include "prism/node.h"
19#include "prism/options.h"
20#include "prism/pack.h"
21#include "prism/parser.h"
22#include "prism/prettyprint.h"
23#include "prism/regexp.h"
24#include "prism/static_literals.h"
25#include "prism/version.h"
26
27#include <assert.h>
28#include <errno.h>
29#include <locale.h>
30#include <math.h>
31#include <stdarg.h>
32#include <stdbool.h>
33#include <stdint.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38#ifndef _WIN32
39#include <strings.h>
40#endif
41
47 PRISM_EXPORTED_FUNCTION const char * pm_version(void);
48
57 PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options);
58
66 PRISM_EXPORTED_FUNCTION void pm_parser_register_encoding_changed_callback(pm_parser_t *parser, pm_encoding_changed_callback_t callback);
67
73 PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser);
74
81 PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser);
82
88 typedef char * (pm_parse_stream_fgets_t)(char *string, int size, void *stream);
89
100 PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *fgets, const pm_options_t *options);
101
102// We optionally support serializing to a binary string. For systems that don't
103// want or need this functionality, it can be turned off with the
104// PRISM_EXCLUDE_SERIALIZATION define.
105#ifndef PRISM_EXCLUDE_SERIALIZATION
106
116 PRISM_EXPORTED_FUNCTION void pm_serialize_parse_stream(pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *fgets, const char *data);
117
125void pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer);
126
133void pm_serialize_encoding(const pm_encoding_t *encoding, pm_buffer_t *buffer);
134
142void pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer);
143
151 PRISM_EXPORTED_FUNCTION void pm_serialize(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer);
152
161 PRISM_EXPORTED_FUNCTION void pm_serialize_parse(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
162
171 PRISM_EXPORTED_FUNCTION void pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
172
181 PRISM_EXPORTED_FUNCTION void pm_serialize_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
182
192 PRISM_EXPORTED_FUNCTION void pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
193
194#endif
195
204 PRISM_EXPORTED_FUNCTION bool pm_parse_success_p(const uint8_t *source, size_t size, const char *data);
205
212 PRISM_EXPORTED_FUNCTION const char * pm_token_type_name(pm_token_type_t token_type);
213
220const char * pm_token_type_human(pm_token_type_t token_type);
221
222// We optionally support dumping to JSON. For systems that don't want or need
223// this functionality, it can be turned off with the PRISM_EXCLUDE_JSON define.
224#ifndef PRISM_EXCLUDE_JSON
225
233 PRISM_EXPORTED_FUNCTION void pm_dump_json(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node);
234
235#endif
236
240 typedef enum {
242 PM_STRING_QUERY_ERROR = -1,
243
245 PM_STRING_QUERY_FALSE,
246
248 PM_STRING_QUERY_TRUE
249} pm_string_query_t;
250
260 PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name);
261
271 PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name);
272
282 PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name);
283
382
383#endif
assert.h
ast.h
The abstract syntax tree.
pm_token_type_t
enum pm_token_type pm_token_type_t
This enum represents every type of token in the Ruby source.
pm_node_t
struct pm_node pm_node_t
This is the base structure that represents a node in the syntax tree.
diagnostic.h
A list of diagnostics generated during parsing.
options.h
The options that can be passed to parsing.
pm_options_t
struct pm_options pm_options_t
The options that can be passed to the parser.
pack.h
A pack template string parser.
parser.h
The parser used to parse Ruby source.
pm_parser_t
struct pm_parser pm_parser_t
The parser used to parse Ruby source.
Definition parser.h:267
pm_encoding_changed_callback_t
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:496
pm_buffer.h
A wrapper around a contiguous block of allocated memory.
pm_char.h
Functions for working with characters and strings.
pm_integer.h
This module provides functions for working with arbitrary-sized integers.
pm_memchr.h
A custom memchr implementation.
pm_strncasecmp.h
A custom strncasecmp implementation.
pm_strpbrk.h
A custom strpbrk implementation.
prettyprint.h
An AST node pretty-printer.
defines.h
Macro definitions used throughout the prism library.
PRISM_EXPORTED_FUNCTION
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:50
node.h
Functions related to nodes in the AST.
version.h
The version of the Prism library.
pm_parse
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser)
Initiate the parser with the given parser.
Definition prism.c:22543
pm_string_query_method_name
PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name)
Check that the slice is a valid method name.
Definition prism.c:22891
pm_parser_register_encoding_changed_callback
PRISM_EXPORTED_FUNCTION void pm_parser_register_encoding_changed_callback(pm_parser_t *parser, pm_encoding_changed_callback_t callback)
Register a callback that will be called whenever prism changes the encoding it is using to parse base...
Definition prism.c:22479
pm_token_type_name
PRISM_EXPORTED_FUNCTION const char * pm_token_type_name(pm_token_type_t token_type)
Returns a string representation of the given token type.
Definition token_type.c:17
pm_parse_stream
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *fgets, const pm_options_t *options)
Parse a stream of Ruby source and return the tree.
Definition prism.c:22624
pm_serialize_lex
PRISM_EXPORTED_FUNCTION void pm_serialize_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data)
Lex the given source and serialize to the given buffer.
Definition serialize.c:2207
pm_parser_free
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser)
Free any memory associated with the given parser.
Definition prism.c:22517
pm_string_query_t
pm_string_query_t
Represents the results of a slice query.
Definition prism.h:240
PM_STRING_QUERY_TRUE
@ PM_STRING_QUERY_TRUE
Returned if the result of the slice query is true.
Definition prism.h:248
PM_STRING_QUERY_ERROR
@ PM_STRING_QUERY_ERROR
Returned if the encoding given to a slice query was invalid.
Definition prism.h:242
PM_STRING_QUERY_FALSE
@ PM_STRING_QUERY_FALSE
Returned if the result of the slice query is false.
Definition prism.h:245
pm_serialize_content
void pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
Serialize the encoding, metadata, nodes, and constant pool.
Definition serialize.c:2133
pm_serialize_parse_lex
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data)
Parse and serialize both the AST and the tokens represented by the given source to the given buffer.
Definition serialize.c:2237
pm_serialize
PRISM_EXPORTED_FUNCTION void pm_serialize(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
Serialize the AST represented by the given node to the given buffer.
Definition prism.c:22691
pm_serialize_parse_comments
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data)
Parse and serialize the comments in the given source to the given buffer.
Definition prism.c:22746
pm_serialize_parse_stream
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_stream(pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *fgets, const char *data)
Parse and serialize the AST represented by the source that is read out of the given stream into to th...
Definition prism.c:22725
pm_serialize_parse
PRISM_EXPORTED_FUNCTION void pm_serialize_parse(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data)
Parse the given source to the AST and dump the AST to the given buffer.
Definition prism.c:22702
pm_serialize_encoding
void pm_serialize_encoding(const pm_encoding_t *encoding, pm_buffer_t *buffer)
Serialize the name of the encoding to the buffer.
Definition serialize.c:2110
pm_dump_json
PRISM_EXPORTED_FUNCTION void pm_dump_json(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node)
Dump JSON to the given buffer.
Definition node.c:3019
pm_parser_init
PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options)
Initialize a parser with the given start and end pointers.
Definition prism.c:22224
pm_parse_stream_fgets_t
char * pm_parse_stream_fgets_t(char *string, int size, void *stream)
This function is used in pm_parse_stream to retrieve a line of input from a stream.
Definition prism.h:88
pm_parse_success_p
PRISM_EXPORTED_FUNCTION bool pm_parse_success_p(const uint8_t *source, size_t size, const char *data)
Parse the source and return true if it parses without errors or warnings.
Definition prism.c:22647
pm_string_query_constant
PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name)
Check that the slice is a valid constant name.
Definition prism.c:22871
pm_string_query_local
PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name)
Check that the slice is a valid local variable name.
Definition prism.c:22851
pm_serialize_comment_list
void pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer)
Serialize the given list of comments to the given buffer.
Definition serialize.c:2040
pm_version
PRISM_EXPORTED_FUNCTION const char * pm_version(void)
The prism version and the serialization format.
Definition prism.c:7
pm_token_type_human
const char * pm_token_type_human(pm_token_type_t token_type)
Returns the human name of the given token type.
Definition token_type.c:362
regexp.h
A regular expression parser.
static_literals.h
A set of static literal nodes that can be checked for duplicates.
stdarg.h
Defines old _.
stdbool.h
C99 shim for <stdbool.h>
pm_buffer_t
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
Definition pm_buffer.h:22
pm_encoding_t
This struct defines the functions necessary to implement the encoding interface so we can determine h...
Definition encoding.h:23
pm_list_t
This represents the overall linked list.
Definition pm_list.h:55