prism 1.9.0 → 2.0.0.rc2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +88 -0
- data/CODE_OF_CONDUCT.md +1 -1
- data/LICENSE.md +1 -1
- data/Makefile +41 -12
- data/README.md +35 -97
- data/config.yml +174 -161
- data/docs/build_system.md +2 -2
- data/docs/configuration.md +5 -4
- data/docs/encoding.md +2 -2
- data/docs/fuzzing.md +4 -11
- data/docs/javascript.md +19 -4
- data/docs/mapping.md +1 -1
- data/docs/releasing.md +77 -5
- data/docs/relocation.md +2 -4
- data/docs/serialization.md +22 -24
- data/docs/testing.md +46 -29
- data/ext/prism/api_node.c +1461 -1401
- data/ext/prism/extconf.rb +3 -4
- data/ext/prism/extension.c +796 -422
- data/ext/prism/extension.h +3 -3
- data/include/prism/arena.h +37 -0
- data/include/prism/ast.h +2727 -610
- data/include/prism/buffer.h +52 -0
- data/include/prism/comments.h +55 -0
- data/include/prism/compiler/accel.h +26 -0
- data/include/prism/compiler/align.h +36 -0
- data/include/prism/compiler/assume.h +25 -0
- data/include/prism/compiler/bswap.h +46 -0
- data/include/prism/compiler/endianness.h +34 -0
- data/include/prism/compiler/exported.h +24 -0
- data/include/prism/compiler/fallthrough.h +22 -0
- data/include/prism/compiler/filesystem.h +32 -0
- data/include/prism/compiler/flex_array.h +19 -0
- data/include/prism/compiler/force_inline.h +21 -0
- data/include/prism/compiler/format.h +25 -0
- data/include/prism/compiler/inline.h +17 -0
- data/include/prism/compiler/nodiscard.h +22 -0
- data/include/prism/compiler/nonnull.h +18 -0
- data/include/prism/compiler/unused.h +18 -0
- data/include/prism/constant_pool.h +81 -0
- data/include/prism/diagnostic.h +38 -403
- data/include/prism/errors_format.h +53 -0
- data/include/prism/excludes.h +29 -0
- data/include/prism/integer.h +41 -0
- data/include/prism/internal/allocator.h +68 -0
- data/include/prism/internal/allocator_debug.h +88 -0
- data/include/prism/internal/arena.h +108 -0
- data/include/prism/internal/bit.h +42 -0
- data/include/prism/internal/buffer.h +94 -0
- data/include/prism/internal/char.h +145 -0
- data/include/prism/internal/comments.h +53 -0
- data/include/prism/internal/constant_pool.h +152 -0
- data/include/prism/internal/diagnostic.h +389 -0
- data/include/prism/{encoding.h → internal/encoding.h} +27 -68
- data/include/prism/internal/integer.h +68 -0
- data/include/prism/internal/isinf.h +16 -0
- data/include/prism/internal/line_offset_list.h +34 -0
- data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
- data/include/prism/internal/magic_comments.h +23 -0
- data/include/prism/internal/memchr.h +15 -0
- data/include/prism/internal/node.h +38 -0
- data/include/prism/internal/options.h +232 -0
- data/include/prism/internal/parser.h +1004 -0
- data/include/prism/internal/regexp.h +26 -0
- data/include/prism/internal/serialize.h +39 -0
- data/include/prism/internal/source.h +72 -0
- data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
- data/include/prism/internal/stringy.h +35 -0
- data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
- data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
- data/include/prism/internal/tokens.h +11 -0
- data/include/prism/internal/xxh3.h +16 -0
- data/include/prism/json.h +32 -0
- data/include/prism/line_offset_list.h +61 -0
- data/include/prism/magic_comments.h +35 -0
- data/include/prism/node.h +18 -53
- data/include/prism/options.h +146 -280
- data/include/prism/parser.h +356 -867
- data/include/prism/prettyprint.h +6 -9
- data/include/prism/serialize.h +115 -0
- data/include/prism/source.h +155 -0
- data/include/prism/stream.h +28 -0
- data/include/prism/string_query.h +74 -0
- data/include/prism/stringy.h +72 -0
- data/include/prism/version.h +12 -3
- data/include/prism.h +43 -320
- data/lib/prism/compiler.rb +319 -305
- data/lib/prism/desugar_compiler.rb +105 -34
- data/lib/prism/dispatcher.rb +646 -473
- data/lib/prism/dot_visitor.rb +592 -511
- data/lib/prism/dsl.rb +391 -25
- data/lib/prism/ffi.rb +258 -90
- data/lib/prism/inspect_visitor.rb +357 -325
- data/lib/prism/lex_compat.rb +254 -212
- data/lib/prism/mutation_compiler.rb +313 -305
- data/lib/prism/node.rb +20966 -6992
- data/lib/prism/node_ext.rb +180 -187
- data/lib/prism/node_find.rb +215 -0
- data/lib/prism/parse_result/errors.rb +7 -1
- data/lib/prism/parse_result/newlines.rb +276 -8
- data/lib/prism/parse_result.rb +468 -139
- data/lib/prism/pattern.rb +65 -20
- data/lib/prism/reflection.rb +23 -12
- data/lib/prism/relocation.rb +149 -87
- data/lib/prism/serialize.rb +3460 -539
- data/lib/prism/string_query.rb +24 -1
- data/lib/prism/translation/parser/builder.rb +10 -2
- data/lib/prism/translation/parser/compiler.rb +44 -52
- data/lib/prism/translation/parser/lexer.rb +41 -92
- data/lib/prism/translation/parser.rb +1 -1
- data/lib/prism/translation/parser_current.rb +1 -1
- data/lib/prism/translation/ripper/lexer.rb +8 -10
- data/lib/prism/translation/ripper/sexp.rb +2 -10
- data/lib/prism/translation/ripper/shim.rb +2 -0
- data/lib/prism/translation/ripper.rb +967 -209
- data/lib/prism/translation/ruby_parser.rb +73 -316
- data/lib/prism/translation.rb +2 -0
- data/lib/prism/visitor.rb +632 -5
- data/lib/prism.rb +79 -9
- data/prism.gemspec +128 -62
- data/rbi/generated/prism/compiler.rbi +489 -0
- data/rbi/generated/prism/desugar_compiler.rbi +306 -0
- data/rbi/generated/prism/dispatcher.rbi +1138 -0
- data/rbi/generated/prism/dot_visitor.rbi +618 -0
- data/rbi/generated/prism/dsl.rbi +751 -0
- data/rbi/generated/prism/inspect_visitor.rbi +503 -0
- data/rbi/generated/prism/lex_compat.rbi +162 -0
- data/rbi/generated/prism/mutation_compiler.rbi +464 -0
- data/rbi/generated/prism/node.rbi +14407 -0
- data/rbi/generated/prism/node_ext.rbi +259 -0
- data/rbi/generated/prism/node_find.rbi +79 -0
- data/rbi/generated/prism/parse_result/errors.rbi +21 -0
- data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
- data/rbi/generated/prism/parse_result.rbi +776 -0
- data/rbi/generated/prism/pattern.rbi +134 -0
- data/rbi/generated/prism/reflection.rbi +89 -0
- data/rbi/generated/prism/relocation.rbi +329 -0
- data/rbi/generated/prism/serialize.rbi +184 -0
- data/rbi/generated/prism/string_query.rbi +43 -0
- data/rbi/generated/prism/translation.rbi +7 -0
- data/rbi/generated/prism/visitor.rbi +650 -0
- data/rbi/generated/prism.rbi +86 -0
- data/rbi/prism/translation/parser_versions.rbi +5 -0
- data/rbi/rubyvm/node_find.rbi +14 -0
- data/sig/generated/prism/compiler.rbs +492 -0
- data/sig/generated/prism/desugar_compiler.rbs +305 -0
- data/sig/generated/prism/dispatcher.rbs +1292 -0
- data/sig/generated/prism/dot_visitor.rbs +635 -0
- data/sig/generated/prism/dsl.rbs +928 -0
- data/sig/generated/prism/inspect_visitor.rbs +509 -0
- data/sig/generated/prism/lex_compat.rbs +166 -0
- data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
- data/sig/generated/prism/node.rbs +20896 -0
- data/sig/generated/prism/node_ext.rbs +290 -0
- data/sig/generated/prism/node_find.rbs +90 -0
- data/sig/generated/prism/parse_result/errors.rbs +22 -0
- data/sig/generated/prism/parse_result/newlines.rbs +223 -0
- data/sig/generated/prism/parse_result.rbs +871 -0
- data/sig/generated/prism/pattern.rbs +156 -0
- data/sig/generated/prism/reflection.rbs +101 -0
- data/sig/generated/prism/relocation.rbs +428 -0
- data/sig/generated/prism/serialize.rbs +198 -0
- data/sig/generated/prism/string_query.rbs +43 -0
- data/sig/generated/prism/translation.rbs +8 -0
- data/sig/generated/prism/visitor.rbs +1111 -0
- data/sig/generated/prism.rbs +84 -0
- data/sig/manifest.yaml +2 -0
- data/src/arena.c +117 -0
- data/src/{util/pm_buffer.c → buffer.c} +53 -21
- data/src/{util/pm_char.c → char.c} +28 -57
- data/src/comments.c +454 -0
- data/src/constant_pool.c +406 -0
- data/src/diagnostic.c +97 -65
- data/src/encoding.c +8 -3
- data/src/errors_format.c +519 -0
- data/src/{util/pm_integer.c → integer.c} +33 -22
- data/src/json.c +5733 -0
- data/src/line_offset_list.c +100 -0
- data/src/list.c +24 -0
- data/src/{util/pm_memchr.c → memchr.c} +8 -6
- data/src/node.c +3504 -7674
- data/src/options.c +162 -49
- data/src/parser.c +367 -0
- data/src/prettyprint.c +421 -354
- data/src/prism.c +8402 -7161
- data/src/regexp.c +961 -45
- data/src/serialize.c +460 -424
- data/src/source.c +518 -0
- data/src/static_literals.c +111 -43
- data/src/string_query.c +180 -0
- data/src/stringy.c +91 -0
- data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
- data/src/strpbrk.c +447 -0
- data/src/{token_type.c → tokens.c} +39 -18
- data/src/xxh3.c +292 -0
- metadata +128 -63
- data/ext/prism/api_pack.c +0 -276
- data/include/prism/defines.h +0 -260
- data/include/prism/pack.h +0 -163
- data/include/prism/regexp.h +0 -43
- data/include/prism/util/pm_buffer.h +0 -236
- data/include/prism/util/pm_char.h +0 -204
- data/include/prism/util/pm_constant_pool.h +0 -218
- data/include/prism/util/pm_integer.h +0 -130
- data/include/prism/util/pm_memchr.h +0 -29
- data/include/prism/util/pm_newline_list.h +0 -113
- data/include/prism/util/pm_string.h +0 -200
- data/lib/prism/pack.rb +0 -230
- data/lib/prism/parse_result/comments.rb +0 -188
- data/rbi/prism/compiler.rbi +0 -12
- data/rbi/prism/dsl.rbi +0 -524
- data/rbi/prism/inspect_visitor.rbi +0 -12
- data/rbi/prism/node.rbi +0 -8750
- data/rbi/prism/node_ext.rbi +0 -107
- data/rbi/prism/parse_result.rbi +0 -404
- data/rbi/prism/reflection.rbi +0 -58
- data/rbi/prism/string_query.rbi +0 -12
- data/rbi/prism/visitor.rbi +0 -473
- data/rbi/prism.rbi +0 -63
- data/sig/prism/compiler.rbs +0 -9
- data/sig/prism/dispatcher.rbs +0 -19
- data/sig/prism/dot_visitor.rbs +0 -6
- data/sig/prism/dsl.rbs +0 -351
- data/sig/prism/inspect_visitor.rbs +0 -22
- data/sig/prism/lex_compat.rbs +0 -10
- data/sig/prism/node.rbs +0 -4042
- data/sig/prism/node_ext.rbs +0 -149
- data/sig/prism/pack.rbs +0 -43
- data/sig/prism/parse_result/comments.rbs +0 -38
- data/sig/prism/parse_result.rbs +0 -197
- data/sig/prism/pattern.rbs +0 -13
- data/sig/prism/reflection.rbs +0 -50
- data/sig/prism/relocation.rbs +0 -185
- data/sig/prism/serialize.rbs +0 -8
- data/sig/prism/string_query.rbs +0 -11
- data/sig/prism/visitor.rbs +0 -169
- data/sig/prism.rbs +0 -272
- data/src/pack.c +0 -509
- data/src/util/pm_constant_pool.c +0 -342
- data/src/util/pm_list.c +0 -49
- data/src/util/pm_newline_list.c +0 -125
- data/src/util/pm_string.c +0 -381
- data/src/util/pm_strpbrk.c +0 -206
data/include/prism.h
CHANGED
|
@@ -6,306 +6,26 @@
|
|
|
6
6
|
#ifndef PRISM_H
|
|
7
7
|
#define PRISM_H
|
|
8
8
|
|
|
9
|
-
#
|
|
10
|
-
|
|
11
|
-
#
|
|
12
|
-
|
|
13
|
-
#include "prism/
|
|
14
|
-
#include "prism/util/pm_strncasecmp.h"
|
|
15
|
-
#include "prism/util/pm_strpbrk.h"
|
|
9
|
+
#ifdef __cplusplus
|
|
10
|
+
extern "C" {
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
#include "prism/arena.h"
|
|
16
14
|
#include "prism/ast.h"
|
|
15
|
+
#include "prism/buffer.h"
|
|
17
16
|
#include "prism/diagnostic.h"
|
|
17
|
+
#include "prism/errors_format.h"
|
|
18
|
+
#include "prism/json.h"
|
|
18
19
|
#include "prism/node.h"
|
|
19
20
|
#include "prism/options.h"
|
|
20
|
-
#include "prism/pack.h"
|
|
21
21
|
#include "prism/parser.h"
|
|
22
22
|
#include "prism/prettyprint.h"
|
|
23
|
-
#include "prism/
|
|
24
|
-
#include "prism/
|
|
23
|
+
#include "prism/serialize.h"
|
|
24
|
+
#include "prism/source.h"
|
|
25
|
+
#include "prism/stream.h"
|
|
26
|
+
#include "prism/string_query.h"
|
|
25
27
|
#include "prism/version.h"
|
|
26
28
|
|
|
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
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The prism version and the serialization format.
|
|
44
|
-
*
|
|
45
|
-
* @returns The prism version as a constant string.
|
|
46
|
-
*/
|
|
47
|
-
PRISM_EXPORTED_FUNCTION const char * pm_version(void);
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Initialize a parser with the given start and end pointers.
|
|
51
|
-
*
|
|
52
|
-
* The resulting parser must eventually be freed with `pm_parser_free()`.
|
|
53
|
-
*
|
|
54
|
-
* @param parser The parser to initialize.
|
|
55
|
-
* @param source The source to parse.
|
|
56
|
-
* @param size The size of the source.
|
|
57
|
-
* @param options The optional options to use when parsing. These options must
|
|
58
|
-
* live for the whole lifetime of this parser.
|
|
59
|
-
*
|
|
60
|
-
* \public \memberof pm_parser
|
|
61
|
-
*/
|
|
62
|
-
PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options);
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Register a callback that will be called whenever prism changes the encoding
|
|
66
|
-
* it is using to parse based on the magic comment.
|
|
67
|
-
*
|
|
68
|
-
* @param parser The parser to register the callback with.
|
|
69
|
-
* @param callback The callback to register.
|
|
70
|
-
*
|
|
71
|
-
* \public \memberof pm_parser
|
|
72
|
-
*/
|
|
73
|
-
PRISM_EXPORTED_FUNCTION void pm_parser_register_encoding_changed_callback(pm_parser_t *parser, pm_encoding_changed_callback_t callback);
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Free any memory associated with the given parser.
|
|
77
|
-
*
|
|
78
|
-
* This does not free the `pm_options_t` object that was used to initialize the
|
|
79
|
-
* parser.
|
|
80
|
-
*
|
|
81
|
-
* @param parser The parser to free.
|
|
82
|
-
*
|
|
83
|
-
* \public \memberof pm_parser
|
|
84
|
-
*/
|
|
85
|
-
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser);
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Initiate the parser with the given parser.
|
|
89
|
-
*
|
|
90
|
-
* @param parser The parser to use.
|
|
91
|
-
* @return The AST representing the source.
|
|
92
|
-
*
|
|
93
|
-
* \public \memberof pm_parser
|
|
94
|
-
*/
|
|
95
|
-
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser);
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* This function is used in pm_parse_stream() to retrieve a line of input from a
|
|
99
|
-
* stream. It closely mirrors that of fgets so that fgets can be used as the
|
|
100
|
-
* default implementation.
|
|
101
|
-
*/
|
|
102
|
-
typedef char * (pm_parse_stream_fgets_t)(char *string, int size, void *stream);
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* This function is used in pm_parse_stream to check whether a stream is EOF.
|
|
106
|
-
* It closely mirrors that of feof so that feof can be used as the
|
|
107
|
-
* default implementation.
|
|
108
|
-
*/
|
|
109
|
-
typedef int (pm_parse_stream_feof_t)(void *stream);
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Parse a stream of Ruby source and return the tree.
|
|
113
|
-
*
|
|
114
|
-
* @param parser The parser to use.
|
|
115
|
-
* @param buffer The buffer to use.
|
|
116
|
-
* @param stream The stream to parse.
|
|
117
|
-
* @param stream_fgets The function to use to read from the stream.
|
|
118
|
-
* @param stream_feof The function to use to determine if the stream has hit eof.
|
|
119
|
-
* @param options The optional options to use when parsing.
|
|
120
|
-
* @return The AST representing the source.
|
|
121
|
-
*
|
|
122
|
-
* \public \memberof pm_parser
|
|
123
|
-
*/
|
|
124
|
-
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *stream_fgets, pm_parse_stream_feof_t *stream_feof, const pm_options_t *options);
|
|
125
|
-
|
|
126
|
-
// We optionally support serializing to a binary string. For systems that don't
|
|
127
|
-
// want or need this functionality, it can be turned off with the
|
|
128
|
-
// PRISM_EXCLUDE_SERIALIZATION define.
|
|
129
|
-
#ifndef PRISM_EXCLUDE_SERIALIZATION
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Parse and serialize the AST represented by the source that is read out of the
|
|
133
|
-
* given stream into to the given buffer.
|
|
134
|
-
*
|
|
135
|
-
* @param buffer The buffer to serialize to.
|
|
136
|
-
* @param stream The stream to parse.
|
|
137
|
-
* @param stream_fgets The function to use to read from the stream.
|
|
138
|
-
* @param stream_feof The function to use to tell if the stream has hit eof.
|
|
139
|
-
* @param data The optional data to pass to the parser.
|
|
140
|
-
*/
|
|
141
|
-
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_stream(pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *stream_fgets, pm_parse_stream_feof_t *stream_feof, const char *data);
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Serialize the given list of comments to the given buffer.
|
|
145
|
-
*
|
|
146
|
-
* @param parser The parser to serialize.
|
|
147
|
-
* @param list The list of comments to serialize.
|
|
148
|
-
* @param buffer The buffer to serialize to.
|
|
149
|
-
*/
|
|
150
|
-
void pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer);
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Serialize the name of the encoding to the buffer.
|
|
154
|
-
*
|
|
155
|
-
* @param encoding The encoding to serialize.
|
|
156
|
-
* @param buffer The buffer to serialize to.
|
|
157
|
-
*/
|
|
158
|
-
void pm_serialize_encoding(const pm_encoding_t *encoding, pm_buffer_t *buffer);
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Serialize the encoding, metadata, nodes, and constant pool.
|
|
162
|
-
*
|
|
163
|
-
* @param parser The parser to serialize.
|
|
164
|
-
* @param node The node to serialize.
|
|
165
|
-
* @param buffer The buffer to serialize to.
|
|
166
|
-
*/
|
|
167
|
-
void pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer);
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Serialize the AST represented by the given node to the given buffer.
|
|
171
|
-
*
|
|
172
|
-
* @param parser The parser to serialize.
|
|
173
|
-
* @param node The node to serialize.
|
|
174
|
-
* @param buffer The buffer to serialize to.
|
|
175
|
-
*/
|
|
176
|
-
PRISM_EXPORTED_FUNCTION void pm_serialize(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer);
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Parse the given source to the AST and dump the AST to the given buffer.
|
|
180
|
-
*
|
|
181
|
-
* @param buffer The buffer to serialize to.
|
|
182
|
-
* @param source The source to parse.
|
|
183
|
-
* @param size The size of the source.
|
|
184
|
-
* @param data The optional data to pass to the parser.
|
|
185
|
-
*/
|
|
186
|
-
PRISM_EXPORTED_FUNCTION void pm_serialize_parse(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Parse and serialize the comments in the given source to the given buffer.
|
|
190
|
-
*
|
|
191
|
-
* @param buffer The buffer to serialize to.
|
|
192
|
-
* @param source The source to parse.
|
|
193
|
-
* @param size The size of the source.
|
|
194
|
-
* @param data The optional data to pass to the parser.
|
|
195
|
-
*/
|
|
196
|
-
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Lex the given source and serialize to the given buffer.
|
|
200
|
-
*
|
|
201
|
-
* @param source The source to lex.
|
|
202
|
-
* @param size The size of the source.
|
|
203
|
-
* @param buffer The buffer to serialize to.
|
|
204
|
-
* @param data The optional data to pass to the lexer.
|
|
205
|
-
*/
|
|
206
|
-
PRISM_EXPORTED_FUNCTION void pm_serialize_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Parse and serialize both the AST and the tokens represented by the given
|
|
210
|
-
* source to the given buffer.
|
|
211
|
-
*
|
|
212
|
-
* @param buffer The buffer to serialize to.
|
|
213
|
-
* @param source The source to parse.
|
|
214
|
-
* @param size The size of the source.
|
|
215
|
-
* @param data The optional data to pass to the parser.
|
|
216
|
-
*/
|
|
217
|
-
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
|
218
|
-
|
|
219
|
-
#endif
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Parse the source and return true if it parses without errors or warnings.
|
|
223
|
-
*
|
|
224
|
-
* @param source The source to parse.
|
|
225
|
-
* @param size The size of the source.
|
|
226
|
-
* @param data The optional data to pass to the parser.
|
|
227
|
-
* @return True if the source parses without errors or warnings.
|
|
228
|
-
*/
|
|
229
|
-
PRISM_EXPORTED_FUNCTION bool pm_parse_success_p(const uint8_t *source, size_t size, const char *data);
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Returns a string representation of the given token type.
|
|
233
|
-
*
|
|
234
|
-
* @param token_type The token type to convert to a string.
|
|
235
|
-
* @return A string representation of the given token type.
|
|
236
|
-
*/
|
|
237
|
-
PRISM_EXPORTED_FUNCTION const char * pm_token_type_name(pm_token_type_t token_type);
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Returns the human name of the given token type.
|
|
241
|
-
*
|
|
242
|
-
* @param token_type The token type to convert to a human name.
|
|
243
|
-
* @return The human name of the given token type.
|
|
244
|
-
*/
|
|
245
|
-
const char * pm_token_type_human(pm_token_type_t token_type);
|
|
246
|
-
|
|
247
|
-
// We optionally support dumping to JSON. For systems that don't want or need
|
|
248
|
-
// this functionality, it can be turned off with the PRISM_EXCLUDE_JSON define.
|
|
249
|
-
#ifndef PRISM_EXCLUDE_JSON
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Dump JSON to the given buffer.
|
|
253
|
-
*
|
|
254
|
-
* @param buffer The buffer to serialize to.
|
|
255
|
-
* @param parser The parser that parsed the node.
|
|
256
|
-
* @param node The node to serialize.
|
|
257
|
-
*/
|
|
258
|
-
PRISM_EXPORTED_FUNCTION void pm_dump_json(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node);
|
|
259
|
-
|
|
260
|
-
#endif
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Represents the results of a slice query.
|
|
264
|
-
*/
|
|
265
|
-
typedef enum {
|
|
266
|
-
/** Returned if the encoding given to a slice query was invalid. */
|
|
267
|
-
PM_STRING_QUERY_ERROR = -1,
|
|
268
|
-
|
|
269
|
-
/** Returned if the result of the slice query is false. */
|
|
270
|
-
PM_STRING_QUERY_FALSE,
|
|
271
|
-
|
|
272
|
-
/** Returned if the result of the slice query is true. */
|
|
273
|
-
PM_STRING_QUERY_TRUE
|
|
274
|
-
} pm_string_query_t;
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Check that the slice is a valid local variable name.
|
|
278
|
-
*
|
|
279
|
-
* @param source The source to check.
|
|
280
|
-
* @param length The length of the source.
|
|
281
|
-
* @param encoding_name The name of the encoding of the source.
|
|
282
|
-
* @return PM_STRING_QUERY_TRUE if the query is true, PM_STRING_QUERY_FALSE if
|
|
283
|
-
* the query is false, and PM_STRING_QUERY_ERROR if the encoding was invalid.
|
|
284
|
-
*/
|
|
285
|
-
PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name);
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Check that the slice is a valid constant name.
|
|
289
|
-
*
|
|
290
|
-
* @param source The source to check.
|
|
291
|
-
* @param length The length of the source.
|
|
292
|
-
* @param encoding_name The name of the encoding of the source.
|
|
293
|
-
* @return PM_STRING_QUERY_TRUE if the query is true, PM_STRING_QUERY_FALSE if
|
|
294
|
-
* the query is false, and PM_STRING_QUERY_ERROR if the encoding was invalid.
|
|
295
|
-
*/
|
|
296
|
-
PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name);
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Check that the slice is a valid method name.
|
|
300
|
-
*
|
|
301
|
-
* @param source The source to check.
|
|
302
|
-
* @param length The length of the source.
|
|
303
|
-
* @param encoding_name The name of the encoding of the source.
|
|
304
|
-
* @return PM_STRING_QUERY_TRUE if the query is true, PM_STRING_QUERY_FALSE if
|
|
305
|
-
* the query is false, and PM_STRING_QUERY_ERROR if the encoding was invalid.
|
|
306
|
-
*/
|
|
307
|
-
PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name);
|
|
308
|
-
|
|
309
29
|
/**
|
|
310
30
|
* @mainpage
|
|
311
31
|
*
|
|
@@ -328,27 +48,27 @@ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint
|
|
|
328
48
|
*
|
|
329
49
|
* @section parsing Parsing
|
|
330
50
|
*
|
|
331
|
-
* In order to parse Ruby code, the
|
|
332
|
-
*
|
|
51
|
+
* In order to parse Ruby code, the functions that you are going to want to use
|
|
52
|
+
* and be aware of are:
|
|
333
53
|
*
|
|
334
|
-
* * `
|
|
335
|
-
* * `
|
|
54
|
+
* * `pm_arena_new()` - create a new arena to hold all AST-lifetime allocations
|
|
55
|
+
* * `pm_parser_new()` - allocate and initialize a new parser
|
|
336
56
|
* * `pm_parse()` - parse and return the root node
|
|
337
|
-
* * `
|
|
338
|
-
* * `
|
|
57
|
+
* * `pm_parser_free()` - free the parser and its internal memory
|
|
58
|
+
* * `pm_arena_free()` - free all AST-lifetime memory
|
|
339
59
|
*
|
|
340
60
|
* Putting all of this together would look something like:
|
|
341
61
|
*
|
|
342
62
|
* ```c
|
|
343
63
|
* void parse(const uint8_t *source, size_t length) {
|
|
344
|
-
*
|
|
345
|
-
*
|
|
64
|
+
* pm_arena_t *arena = pm_arena_new();
|
|
65
|
+
* pm_parser_t *parser = pm_parser_new(arena, source, length, NULL);
|
|
346
66
|
*
|
|
347
|
-
* pm_node_t *root = pm_parse(
|
|
67
|
+
* pm_node_t *root = pm_parse(parser);
|
|
348
68
|
* printf("PARSED!\n");
|
|
349
69
|
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
70
|
+
* pm_parser_free(parser);
|
|
71
|
+
* pm_arena_free(arena);
|
|
352
72
|
* }
|
|
353
73
|
* ```
|
|
354
74
|
*
|
|
@@ -361,24 +81,23 @@ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint
|
|
|
361
81
|
* Prism provides the ability to serialize the AST and its related metadata into
|
|
362
82
|
* a binary format. This format is designed to be portable to different
|
|
363
83
|
* languages and runtimes so that you only need to make one FFI call in order to
|
|
364
|
-
* parse Ruby code. The
|
|
365
|
-
*
|
|
84
|
+
* parse Ruby code. The functions that you are going to want to use and be
|
|
85
|
+
* aware of are:
|
|
366
86
|
*
|
|
367
|
-
* * `
|
|
368
|
-
* * `pm_buffer_free()` - free the
|
|
369
|
-
* * `pm_serialize()` - serialize the AST into a buffer
|
|
87
|
+
* * `pm_buffer_new()` - create a new buffer
|
|
88
|
+
* * `pm_buffer_free()` - free the buffer and its internal memory
|
|
370
89
|
* * `pm_serialize_parse()` - parse and serialize the AST into a buffer
|
|
371
90
|
*
|
|
372
91
|
* Putting all of this together would look something like:
|
|
373
92
|
*
|
|
374
93
|
* ```c
|
|
375
94
|
* void serialize(const uint8_t *source, size_t length) {
|
|
376
|
-
* pm_buffer_t buffer =
|
|
95
|
+
* pm_buffer_t *buffer = pm_buffer_new();
|
|
377
96
|
*
|
|
378
|
-
* pm_serialize_parse(
|
|
97
|
+
* pm_serialize_parse(buffer, source, length, NULL);
|
|
379
98
|
* printf("SERIALIZED!\n");
|
|
380
99
|
*
|
|
381
|
-
* pm_buffer_free(
|
|
100
|
+
* pm_buffer_free(buffer);
|
|
382
101
|
* }
|
|
383
102
|
* ```
|
|
384
103
|
*
|
|
@@ -389,20 +108,24 @@ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint
|
|
|
389
108
|
*
|
|
390
109
|
* ```c
|
|
391
110
|
* void prettyprint(const uint8_t *source, size_t length) {
|
|
392
|
-
*
|
|
393
|
-
*
|
|
111
|
+
* pm_arena_t *arena = pm_arena_new();
|
|
112
|
+
* pm_parser_t *parser = pm_parser_new(arena, source, length, NULL);
|
|
394
113
|
*
|
|
395
|
-
* pm_node_t *root = pm_parse(
|
|
396
|
-
* pm_buffer_t buffer =
|
|
114
|
+
* pm_node_t *root = pm_parse(parser);
|
|
115
|
+
* pm_buffer_t *buffer = pm_buffer_new();
|
|
397
116
|
*
|
|
398
|
-
* pm_prettyprint(
|
|
399
|
-
* printf("%*.s\n", (int) buffer
|
|
117
|
+
* pm_prettyprint(buffer, parser, root);
|
|
118
|
+
* printf("%*.s\n", (int) pm_buffer_length(buffer), pm_buffer_value(buffer));
|
|
400
119
|
*
|
|
401
|
-
* pm_buffer_free(
|
|
402
|
-
*
|
|
403
|
-
*
|
|
120
|
+
* pm_buffer_free(buffer);
|
|
121
|
+
* pm_parser_free(parser);
|
|
122
|
+
* pm_arena_free(arena);
|
|
404
123
|
* }
|
|
405
124
|
* ```
|
|
406
125
|
*/
|
|
407
126
|
|
|
127
|
+
#ifdef __cplusplus
|
|
128
|
+
}
|
|
129
|
+
#endif
|
|
130
|
+
|
|
408
131
|
#endif
|