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); }
26 const pm_line_column_t line_column = pm_line_offset_list_line_column(pm_parser_line_offsets(parser), loc.
start, pm_parser_start_line(parser));
27 fprintf(stderr,
"%" PRIi32
":%" PRIu32
":%s\n", line_column.
line, line_column.
column, pm_diagnostic_message(diagnostic));
31usage(
const char *prog)
33 fprintf(stderr,
"Usage: %s [options]... <filename>\n"
35 " -v, --version: show Prism version\n"
36 " -h, --help: show this message\n"
41main(
int argc,
const char *argv[])
43 const char *filepath = 0;
45 for (
int i = 1; i < argc; ++i) {
46 const char *arg = argv[i];
49 fprintf(stderr,
"too many filename\n");
54 else if (arg[1] ==
'-') {
56 if (strcmp(arg + 2,
"version") == 0) {
61 if (strcmp(arg + 2,
"help") == 0) {
66 fprintf(stderr,
"unknown option %s\n", arg);
77 fprintf(stderr,
"unknown option -%c\n", *arg);
91 pm_source_t *source = pm_source_mapped_new(filepath, 0, &init_result);
94 fprintf(stderr,
"unable to map file: %s\n", filepath);
99 pm_options_line_set(options, 1);
100 pm_options_filepath_set(options, filepath);
108 if (pm_parser_errors_size(parser) > 0)
110 fprintf(stderr,
"error parsing %s\n", filepath);
111 pm_parser_errors_each(parser, print_error, parser);
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_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.
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.
#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.