Skip to content

Commit

Permalink
[ruby/prism] Provide options for reducing size
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 20, 2024
1 parent 0e8b6c6 commit af7bf9e
Show file tree
Hide file tree
Showing 17 changed files with 393 additions and 254 deletions.
318 changes: 178 additions & 140 deletions prism/encoding.c

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions prism/encoding.h
Expand Up @@ -135,7 +135,14 @@ extern const uint8_t pm_encoding_unicode_table[256];
*/
typedef enum {
PM_ENCODING_UTF_8 = 0,
PM_ENCODING_US_ASCII,
PM_ENCODING_ASCII_8BIT,
PM_ENCODING_EUC_JP,
PM_ENCODING_WINDOWS_31J,

// We optionally support excluding the full set of encodings to only support the
// minimum necessary to process Ruby code without encoding comments.
#ifndef PRISM_ENCODING_EXCLUDE_FULL
PM_ENCODING_BIG5,
PM_ENCODING_BIG5_HKSCS,
PM_ENCODING_BIG5_UAO,
Expand All @@ -148,7 +155,6 @@ typedef enum {
PM_ENCODING_CP950,
PM_ENCODING_CP951,
PM_ENCODING_EMACS_MULE,
PM_ENCODING_EUC_JP,
PM_ENCODING_EUC_JP_MS,
PM_ENCODING_EUC_JIS_2004,
PM_ENCODING_EUC_KR,
Expand Down Expand Up @@ -208,7 +214,6 @@ typedef enum {
PM_ENCODING_STATELESS_ISO_2022_JP,
PM_ENCODING_STATELESS_ISO_2022_JP_KDDI,
PM_ENCODING_TIS_620,
PM_ENCODING_US_ASCII,
PM_ENCODING_UTF8_MAC,
PM_ENCODING_UTF8_DOCOMO,
PM_ENCODING_UTF8_KDDI,
Expand All @@ -222,8 +227,9 @@ typedef enum {
PM_ENCODING_WINDOWS_1256,
PM_ENCODING_WINDOWS_1257,
PM_ENCODING_WINDOWS_1258,
PM_ENCODING_WINDOWS_31J,
PM_ENCODING_WINDOWS_874,
#endif

PM_ENCODING_MAXIMUM
} pm_encoding_type_t;

Expand Down
8 changes: 4 additions & 4 deletions prism/extension.c
Expand Up @@ -311,7 +311,7 @@ dump(int argc, VALUE *argv, VALUE self) {
pm_options_t options = { 0 };
string_options(argc, argv, &input, &options);

#ifdef PRISM_DEBUG_MODE_BUILD
#ifdef PRISM_BUILD_DEBUG
size_t length = pm_string_length(&input);
char* dup = xmalloc(length);
memcpy(dup, pm_string_source(&input), length);
Expand All @@ -320,7 +320,7 @@ dump(int argc, VALUE *argv, VALUE self) {

VALUE value = dump_input(&input, &options);

#ifdef PRISM_DEBUG_MODE_BUILD
#ifdef PRISM_BUILD_DEBUG
xfree(dup);
#endif

Expand Down Expand Up @@ -737,7 +737,7 @@ parse(int argc, VALUE *argv, VALUE self) {
pm_options_t options = { 0 };
string_options(argc, argv, &input, &options);

#ifdef PRISM_DEBUG_MODE_BUILD
#ifdef PRISM_BUILD_DEBUG
size_t length = pm_string_length(&input);
char* dup = xmalloc(length);
memcpy(dup, pm_string_source(&input), length);
Expand All @@ -746,7 +746,7 @@ parse(int argc, VALUE *argv, VALUE self) {

VALUE value = parse_input(&input, &options);

#ifdef PRISM_DEBUG_MODE_BUILD
#ifdef PRISM_BUILD_DEBUG
xfree(dup);
#endif

Expand Down
50 changes: 33 additions & 17 deletions prism/pack.c
@@ -1,16 +1,43 @@
#include "prism/pack.h"

// We optionally support parsing String#pack templates. For systems that don't
// want or need this functionality, it can be turned off with the
// PRISM_EXCLUDE_PACK define.
#ifdef PRISM_EXCLUDE_PACK

void pm_pack_parse(void) {}

#else

#include <stdbool.h>
#include <errno.h>

static uintmax_t
strtoumaxc(const char **format);
strtoumaxc(const char **format) {
uintmax_t value = 0;
while (**format >= '0' && **format <= '9') {
if (value > UINTMAX_MAX / 10) {
errno = ERANGE;
}
value = value * 10 + ((uintmax_t) (**format - '0'));
(*format)++;
}
return value;
}

PRISM_EXPORTED_FUNCTION pm_pack_result
pm_pack_parse(pm_pack_variant variant, const char **format, const char *format_end,
pm_pack_type *type, pm_pack_signed *signed_type, pm_pack_endian *endian, pm_pack_size *size,
pm_pack_length_type *length_type, uint64_t *length, pm_pack_encoding *encoding) {

pm_pack_parse(
pm_pack_variant variant,
const char **format,
const char *format_end,
pm_pack_type *type,
pm_pack_signed *signed_type,
pm_pack_endian *endian,
pm_pack_size *size,
pm_pack_length_type *length_type,
uint64_t *length,
pm_pack_encoding *encoding
) {
if (*encoding == PM_PACK_ENCODING_START) {
*encoding = PM_PACK_ENCODING_US_ASCII;
}
Expand Down Expand Up @@ -479,15 +506,4 @@ pm_size_to_native(pm_pack_size size) {
}
}

static uintmax_t
strtoumaxc(const char **format) {
uintmax_t value = 0;
while (**format >= '0' && **format <= '9') {
if (value > UINTMAX_MAX / 10) {
errno = ERANGE;
}
value = value * 10 + ((uintmax_t) (**format - '0'));
(*format)++;
}
return value;
}
#endif
11 changes: 11 additions & 0 deletions prism/pack.h
Expand Up @@ -6,6 +6,15 @@
#ifndef PRISM_PACK_H
#define PRISM_PACK_H

// We optionally support parsing String#pack templates. For systems that don't
// want or need this functionality, it can be turned off with the
// PRISM_EXCLUDE_PACK define.
#ifdef PRISM_EXCLUDE_PACK

void pm_pack_parse(void);

#else

#include "prism/defines.h"

#include <stdint.h>
Expand Down Expand Up @@ -150,3 +159,5 @@ pm_pack_parse(
PRISM_EXPORTED_FUNCTION size_t pm_size_to_native(pm_pack_size size);

#endif

#endif
8 changes: 8 additions & 0 deletions prism/prettyprint.h
Expand Up @@ -6,6 +6,12 @@
#ifndef PRISM_PRETTYPRINT_H
#define PRISM_PRETTYPRINT_H

#ifdef PRISM_EXCLUDE_PRETTYPRINT

void pm_prettyprint(void);

#else

#include "prism/defines.h"

#include <stdio.h>
Expand All @@ -24,3 +30,5 @@
PRISM_EXPORTED_FUNCTION void pm_prettyprint(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm_node_t *node);

#endif

#endif
44 changes: 36 additions & 8 deletions prism/prism.c
Expand Up @@ -19316,6 +19316,41 @@ pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse
return node;
}

/**
* Parse the source and return true if it parses without errors or warnings.
*/
PRISM_EXPORTED_FUNCTION bool
pm_parse_success_p(const uint8_t *source, size_t size, const char *data) {
pm_options_t options = { 0 };
pm_options_read(&options, data);

pm_parser_t parser;
pm_parser_init(&parser, source, size, &options);

pm_node_t *node = pm_parse(&parser);
pm_node_destroy(&parser, node);

bool result = parser.error_list.size == 0 && parser.warning_list.size == 0;
pm_parser_free(&parser);
pm_options_free(&options);

return result;
}

#undef PM_CASE_KEYWORD
#undef PM_CASE_OPERATOR
#undef PM_CASE_WRITABLE
#undef PM_STRING_EMPTY
#undef PM_LOCATION_NODE_BASE_VALUE
#undef PM_LOCATION_NODE_VALUE
#undef PM_LOCATION_NULL_VALUE
#undef PM_LOCATION_TOKEN_VALUE

// We optionally support serializing to a binary string. For systems that don't
// want or need this functionality, it can be turned off with the
// PRISM_EXCLUDE_SERIALIZATION define.
#ifndef PRISM_EXCLUDE_SERIALIZATION

static inline void
pm_serialize_header(pm_buffer_t *buffer) {
pm_buffer_append_string(buffer, "PRISM", 5);
Expand Down Expand Up @@ -19402,14 +19437,7 @@ pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t s
pm_options_free(&options);
}

#undef PM_CASE_KEYWORD
#undef PM_CASE_OPERATOR
#undef PM_CASE_WRITABLE
#undef PM_STRING_EMPTY
#undef PM_LOCATION_NODE_BASE_VALUE
#undef PM_LOCATION_NODE_VALUE
#undef PM_LOCATION_NULL_VALUE
#undef PM_LOCATION_TOKEN_VALUE
#endif

/** An error that is going to be formatted into the output. */
typedef struct {
Expand Down
13 changes: 13 additions & 0 deletions prism/prism.h
Expand Up @@ -98,6 +98,11 @@ typedef char * (pm_parse_stream_fgets_t)(char *string, int size, void *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);

// We optionally support serializing to a binary string. For systems that don't
// want or need this functionality, it can be turned off with the
// PRISM_EXCLUDE_SERIALIZATION define.
#ifndef PRISM_EXCLUDE_SERIALIZATION

/**
* Parse and serialize the AST represented by the source that is read out of the
* given stream into to the given buffer.
Expand Down Expand Up @@ -185,6 +190,8 @@ PRISM_EXPORTED_FUNCTION void pm_serialize_lex(pm_buffer_t *buffer, const uint8_t
*/
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);

#endif

/**
* Parse the source and return true if it parses without errors or warnings.
*
Expand Down Expand Up @@ -220,6 +227,10 @@ const char * pm_token_type_human(pm_token_type_t token_type);
*/
PRISM_EXPORTED_FUNCTION void pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool colorize);

// We optionally support dumping to JSON. For systems that don't want or need
// this functionality, it can be turned off with the PRISM_EXCLUDE_JSON define.
#ifndef PRISM_EXCLUDE_JSON

/**
* Dump JSON to the given buffer.
*
Expand All @@ -229,6 +240,8 @@ PRISM_EXPORTED_FUNCTION void pm_parser_errors_format(const pm_parser_t *parser,
*/
PRISM_EXPORTED_FUNCTION void pm_dump_json(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node);

#endif

/**
* @mainpage
*
Expand Down
6 changes: 6 additions & 0 deletions prism/templates/src/node.c.erb
Expand Up @@ -247,6 +247,10 @@ pm_visit_child_nodes(const pm_node_t *node, bool (*visitor)(const pm_node_t *nod
}
}

// We optionally support dumping to JSON. For systems that don't want or need
// this functionality, it can be turned off with the PRISM_EXCLUDE_JSON define.
#ifndef PRISM_EXCLUDE_JSON

static void
pm_dump_json_constant(pm_buffer_t *buffer, const pm_parser_t *parser, pm_constant_id_t constant_id) {
const pm_constant_t *constant = pm_constant_pool_id_to_constant(&parser->constant_pool, constant_id);
Expand Down Expand Up @@ -360,3 +364,5 @@ pm_dump_json(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *no
break;
}
}

#endif
11 changes: 11 additions & 0 deletions prism/templates/src/prettyprint.c.erb
@@ -1,6 +1,15 @@
<%# encoding: ASCII -%>
#include "prism/prettyprint.h"

// We optionally support pretty printing nodes. For systems that don't want or
// need this functionality, it can be turned off with the
// PRISM_EXCLUDE_PRETTYPRINT define.
#ifdef PRISM_EXCLUDE_PRETTYPRINT

void pm_prettyprint(void) {}

#else

static inline void
prettyprint_location(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm_location_t *location) {
pm_line_column_t start = pm_newline_list_line_column(&parser->newline_list, location->start, parser->start_line);
Expand Down Expand Up @@ -154,3 +163,5 @@ pm_prettyprint(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm_n
prettyprint_node(output_buffer, parser, node, &prefix_buffer);
pm_buffer_free(&prefix_buffer);
}

#endif
26 changes: 6 additions & 20 deletions prism/templates/src/serialize.c.erb
@@ -1,5 +1,10 @@
#include "prism.h"

// We optionally support serializing to a binary string. For systems that don't
// want or need this functionality, it can be turned off with the
// PRISM_EXCLUDE_SERIALIZATION define.
#ifndef PRISM_EXCLUDE_SERIALIZATION

#include <stdio.h>

static inline uint32_t
Expand Down Expand Up @@ -394,23 +399,4 @@ pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size,
pm_options_free(&options);
}

/**
* Parse the source and return true if it parses without errors or warnings.
*/
PRISM_EXPORTED_FUNCTION bool
pm_parse_success_p(const uint8_t *source, size_t size, const char *data) {
pm_options_t options = { 0 };
pm_options_read(&options, data);

pm_parser_t parser;
pm_parser_init(&parser, source, size, &options);

pm_node_t *node = pm_parse(&parser);
pm_node_destroy(&parser, node);

bool result = parser.error_list.size == 0 && parser.warning_list.size == 0;
pm_parser_free(&parser);
pm_options_free(&options);

return result;
}
#endif

0 comments on commit af7bf9e

Please sign in to comment.