12void *ruby_xmalloc(
size_t size) {
return malloc(size); }
13void *ruby_xcalloc(
size_t nelems,
size_t elemsiz) {
return calloc(nelems, elemsiz); }
14void *ruby_xrealloc(
void *ptr,
size_t newsiz) {
return realloc(ptr, newsiz); }
15void ruby_xfree(
void *ptr) { free(ptr); }
16void ruby_xfree_sized(
void *ptr,
size_t _oldsize) { free(ptr); }
17void *ruby_xrealloc_sized(
void *ptr,
size_t newsiz,
size_t _oldsiz) {
return realloc(ptr, newsiz); }
22usage(
const char *prog)
24 fprintf(stderr,
"Usage: %s [options]... <filename>\n"
26 " -v, --version: show Prism version\n"
27 " -h, --help: show this message\n"
32main(
int argc,
const char *argv[])
34 const char *filepath = 0;
36 for (
int i = 1; i < argc; ++i) {
37 const char *arg = argv[i];
40 fprintf(stderr,
"too many filename\n");
45 else if (arg[1] ==
'-') {
47 if (strcmp(arg + 2,
"version") == 0) {
52 if (strcmp(arg + 2,
"help") == 0) {
57 fprintf(stderr,
"unknown option %s\n", arg);
68 fprintf(stderr,
"unknown option -%c\n", *arg);
82 pm_source_t *source = pm_source_mapped_new(filepath, 0, &init_result);
85 fprintf(stderr,
"unable to map file: %s\n", filepath);
90 pm_options_line_set(options, 1);
91 pm_options_filepath_set(options, filepath);
99 if (pm_parser_errors_size(parser) > 0) {
100 fprintf(stderr,
"error parsing %s\n", filepath);
105 diagnostic = pm_diagnostic_next(diagnostic)
112 exit_status = EXIT_FAILURE;
116 pm_dump_json(json, parser, node);
117 printf(
"%.*s\n", (
int) pm_buffer_length(json), pm_buffer_value(json));
118 pm_buffer_free(json);
119 exit_status = EXIT_SUCCESS;
123 pm_arena_free(arena);
124 pm_source_free(source);
125 pm_options_free(options);
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 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 pm_node_t * pm_parse(pm_parser_t *parser) PRISM_NONNULL(1)
Initiate the parser with the given parser.
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.
#define PRISM_VERSION
The version of the Prism library as a constant string.
The main header file for the prism parser.
pm_source_init_result_t
Represents the result of initializing a source from a file.
@ PM_SOURCE_INIT_SUCCESS
Indicates that the source was successfully initialized.
A line and column in a string.
uint32_t column
The column in bytes.
int32_t line
The line number.
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.
This is the base structure that represents a node in the syntax tree.