13void *ruby_xmalloc(
size_t size) {
return malloc(size); }
14void *ruby_xcalloc(
size_t nelems,
size_t elemsiz) {
return calloc(nelems, elemsiz); }
15void *ruby_xrealloc(
void *ptr,
size_t newsiz) {
return realloc(ptr, newsiz); }
16void ruby_xfree(
void *ptr) { free(ptr); }
17void ruby_xfree_sized(
void *ptr,
size_t _oldsize) { free(ptr); }
18void *ruby_xrealloc_sized(
void *ptr,
size_t newsiz,
size_t _oldsiz) {
return realloc(ptr, newsiz); }
27 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));
28 fprintf(stderr,
"%" PRIi32
":%" PRIu32
":%s\n", line_column.
line, line_column.
column, pm_diagnostic_message(diagnostic));
31#if defined(RUBY_RELEASE_DATETIME) && defined(RUBY_RELEASE_DATETIME)
32# define SHOW_PROGRAM_VERSION 2
33#elif defined(RUBY_RELEASE_DATETIME) || defined(RUBY_RELEASE_DATETIME)
34# define SHOW_PROGRAM_VERSION 1
36# define SHOW_PROGRAM_VERSION 0
38#if SHOW_PROGRAM_VERSION
39# define usage_versions "and program versions"
41# define usage_versions "version"
45usage(
const char *prog)
47 fprintf(stderr,
"Usage: %s [options]... <filename>\n"
49 " -v, --version: show Prism " usage_versions
"\n"
50 " -h, --help: show this message\n"
55main(
int argc,
const char *argv[])
57 const char *filepath = 0;
59 for (
int i = 1; i < argc; ++i) {
60 const char *arg = argv[i];
63 fprintf(stderr,
"too many filename\n");
68 else if (arg[1] ==
'-') {
70 if (strcmp(arg + 2,
"version") == 0) {
73#
if SHOW_PROGRAM_VERSION
75# ifdef RUBY_RELEASE_DATETIME
78#
if SHOW_PROGRAM_VERSION > 1
89 if (strcmp(arg + 2,
"help") == 0) {
94 fprintf(stderr,
"unknown option %s\n", arg);
105 fprintf(stderr,
"unknown option -%c\n", *arg);
119 pm_source_t *source = pm_source_mapped_new(filepath, 0, &init_result);
122 fprintf(stderr,
"unable to map file: %s\n", filepath);
127 pm_options_line_set(options, 1);
128 pm_options_filepath_set(options, filepath);
136 if (pm_parser_errors_size(parser) > 0)
138 fprintf(stderr,
"error parsing %s\n", filepath);
139 pm_parser_errors_each(parser, print_error, parser);
140 exit_status = EXIT_FAILURE;
144 pm_dump_json(json, parser, node);
145 printf(
"%.*s\n", (
int) pm_buffer_length(json), pm_buffer_value(json));
146 pm_buffer_free(json);
147 exit_status = EXIT_SUCCESS;
151 pm_arena_free(arena);
152 pm_source_free(source);
153 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.