jruby-prism-parser 0.23.0.pre.SNAPSHOT-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +401 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +62 -0
- data/LICENSE.md +7 -0
- data/Makefile +101 -0
- data/README.md +98 -0
- data/config.yml +2902 -0
- data/docs/build_system.md +91 -0
- data/docs/configuration.md +64 -0
- data/docs/cruby_compilation.md +27 -0
- data/docs/design.md +53 -0
- data/docs/encoding.md +121 -0
- data/docs/fuzzing.md +88 -0
- data/docs/heredocs.md +36 -0
- data/docs/javascript.md +118 -0
- data/docs/local_variable_depth.md +229 -0
- data/docs/mapping.md +117 -0
- data/docs/parser_translation.md +34 -0
- data/docs/parsing_rules.md +19 -0
- data/docs/releasing.md +98 -0
- data/docs/ripper.md +36 -0
- data/docs/ruby_api.md +43 -0
- data/docs/ruby_parser_translation.md +19 -0
- data/docs/serialization.md +209 -0
- data/docs/testing.md +55 -0
- data/ext/prism/api_node.c +5098 -0
- data/ext/prism/api_pack.c +267 -0
- data/ext/prism/extconf.rb +110 -0
- data/ext/prism/extension.c +1155 -0
- data/ext/prism/extension.h +18 -0
- data/include/prism/ast.h +5807 -0
- data/include/prism/defines.h +102 -0
- data/include/prism/diagnostic.h +339 -0
- data/include/prism/encoding.h +265 -0
- data/include/prism/node.h +57 -0
- data/include/prism/options.h +230 -0
- data/include/prism/pack.h +152 -0
- data/include/prism/parser.h +732 -0
- data/include/prism/prettyprint.h +26 -0
- data/include/prism/regexp.h +33 -0
- data/include/prism/util/pm_buffer.h +155 -0
- data/include/prism/util/pm_char.h +205 -0
- data/include/prism/util/pm_constant_pool.h +209 -0
- data/include/prism/util/pm_list.h +97 -0
- data/include/prism/util/pm_memchr.h +29 -0
- data/include/prism/util/pm_newline_list.h +93 -0
- data/include/prism/util/pm_state_stack.h +42 -0
- data/include/prism/util/pm_string.h +150 -0
- data/include/prism/util/pm_string_list.h +44 -0
- data/include/prism/util/pm_strncasecmp.h +32 -0
- data/include/prism/util/pm_strpbrk.h +46 -0
- data/include/prism/version.h +29 -0
- data/include/prism.h +289 -0
- data/jruby-prism.jar +0 -0
- data/lib/prism/compiler.rb +486 -0
- data/lib/prism/debug.rb +206 -0
- data/lib/prism/desugar_compiler.rb +207 -0
- data/lib/prism/dispatcher.rb +2150 -0
- data/lib/prism/dot_visitor.rb +4634 -0
- data/lib/prism/dsl.rb +785 -0
- data/lib/prism/ffi.rb +346 -0
- data/lib/prism/lex_compat.rb +908 -0
- data/lib/prism/mutation_compiler.rb +753 -0
- data/lib/prism/node.rb +17864 -0
- data/lib/prism/node_ext.rb +212 -0
- data/lib/prism/node_inspector.rb +68 -0
- data/lib/prism/pack.rb +224 -0
- data/lib/prism/parse_result/comments.rb +177 -0
- data/lib/prism/parse_result/newlines.rb +64 -0
- data/lib/prism/parse_result.rb +498 -0
- data/lib/prism/pattern.rb +250 -0
- data/lib/prism/serialize.rb +1354 -0
- data/lib/prism/translation/parser/compiler.rb +1838 -0
- data/lib/prism/translation/parser/lexer.rb +335 -0
- data/lib/prism/translation/parser/rubocop.rb +37 -0
- data/lib/prism/translation/parser.rb +178 -0
- data/lib/prism/translation/ripper.rb +577 -0
- data/lib/prism/translation/ruby_parser.rb +1521 -0
- data/lib/prism/translation.rb +11 -0
- data/lib/prism/version.rb +3 -0
- data/lib/prism/visitor.rb +495 -0
- data/lib/prism.rb +99 -0
- data/prism.gemspec +135 -0
- data/rbi/prism.rbi +7767 -0
- data/rbi/prism_static.rbi +207 -0
- data/sig/prism.rbs +4773 -0
- data/sig/prism_static.rbs +201 -0
- data/src/diagnostic.c +400 -0
- data/src/encoding.c +5132 -0
- data/src/node.c +2786 -0
- data/src/options.c +213 -0
- data/src/pack.c +493 -0
- data/src/prettyprint.c +8881 -0
- data/src/prism.c +18406 -0
- data/src/regexp.c +638 -0
- data/src/serialize.c +1554 -0
- data/src/token_type.c +700 -0
- data/src/util/pm_buffer.c +190 -0
- data/src/util/pm_char.c +318 -0
- data/src/util/pm_constant_pool.c +322 -0
- data/src/util/pm_list.c +49 -0
- data/src/util/pm_memchr.c +35 -0
- data/src/util/pm_newline_list.c +84 -0
- data/src/util/pm_state_stack.c +25 -0
- data/src/util/pm_string.c +203 -0
- data/src/util/pm_string_list.c +28 -0
- data/src/util/pm_strncasecmp.c +24 -0
- data/src/util/pm_strpbrk.c +180 -0
- metadata +156 -0
data/include/prism.h
ADDED
@@ -0,0 +1,289 @@
|
|
1
|
+
/**
|
2
|
+
* @file prism.h
|
3
|
+
*
|
4
|
+
* The main header file for the prism parser.
|
5
|
+
*/
|
6
|
+
#ifndef PRISM_H
|
7
|
+
#define PRISM_H
|
8
|
+
|
9
|
+
#include "prism/defines.h"
|
10
|
+
#include "prism/util/pm_buffer.h"
|
11
|
+
#include "prism/util/pm_char.h"
|
12
|
+
#include "prism/util/pm_memchr.h"
|
13
|
+
#include "prism/util/pm_strncasecmp.h"
|
14
|
+
#include "prism/util/pm_strpbrk.h"
|
15
|
+
#include "prism/ast.h"
|
16
|
+
#include "prism/diagnostic.h"
|
17
|
+
#include "prism/node.h"
|
18
|
+
#include "prism/options.h"
|
19
|
+
#include "prism/pack.h"
|
20
|
+
#include "prism/parser.h"
|
21
|
+
#include "prism/prettyprint.h"
|
22
|
+
#include "prism/regexp.h"
|
23
|
+
#include "prism/version.h"
|
24
|
+
|
25
|
+
#include <assert.h>
|
26
|
+
#include <errno.h>
|
27
|
+
#include <stdarg.h>
|
28
|
+
#include <stdbool.h>
|
29
|
+
#include <stdint.h>
|
30
|
+
#include <stdio.h>
|
31
|
+
#include <stdlib.h>
|
32
|
+
#include <string.h>
|
33
|
+
|
34
|
+
#ifndef _WIN32
|
35
|
+
#include <strings.h>
|
36
|
+
#endif
|
37
|
+
|
38
|
+
/**
|
39
|
+
* The prism version and the serialization format.
|
40
|
+
*
|
41
|
+
* @returns The prism version as a constant string.
|
42
|
+
*/
|
43
|
+
PRISM_EXPORTED_FUNCTION const char * pm_version(void);
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Initialize a parser with the given start and end pointers.
|
47
|
+
*
|
48
|
+
* @param parser The parser to initialize.
|
49
|
+
* @param source The source to parse.
|
50
|
+
* @param size The size of the source.
|
51
|
+
* @param options The optional options to use when parsing.
|
52
|
+
*/
|
53
|
+
PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options);
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Register a callback that will be called whenever prism changes the encoding
|
57
|
+
* it is using to parse based on the magic comment.
|
58
|
+
*
|
59
|
+
* @param parser The parser to register the callback with.
|
60
|
+
* @param callback The callback to register.
|
61
|
+
*/
|
62
|
+
PRISM_EXPORTED_FUNCTION void pm_parser_register_encoding_changed_callback(pm_parser_t *parser, pm_encoding_changed_callback_t callback);
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Free any memory associated with the given parser.
|
66
|
+
*
|
67
|
+
* @param parser The parser to free.
|
68
|
+
*/
|
69
|
+
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser);
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Initiate the parser with the given parser.
|
73
|
+
*
|
74
|
+
* @param parser The parser to use.
|
75
|
+
* @return The AST representing the source.
|
76
|
+
*/
|
77
|
+
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser);
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Serialize the given list of comments to the given buffer.
|
81
|
+
*
|
82
|
+
* @param parser The parser to serialize.
|
83
|
+
* @param list The list of comments to serialize.
|
84
|
+
* @param buffer The buffer to serialize to.
|
85
|
+
*/
|
86
|
+
void pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer);
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Serialize the name of the encoding to the buffer.
|
90
|
+
*
|
91
|
+
* @param encoding The encoding to serialize.
|
92
|
+
* @param buffer The buffer to serialize to.
|
93
|
+
*/
|
94
|
+
void pm_serialize_encoding(const pm_encoding_t *encoding, pm_buffer_t *buffer);
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Serialize the encoding, metadata, nodes, and constant pool.
|
98
|
+
*
|
99
|
+
* @param parser The parser to serialize.
|
100
|
+
* @param node The node to serialize.
|
101
|
+
* @param buffer The buffer to serialize to.
|
102
|
+
*/
|
103
|
+
void pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer);
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Serialize the AST represented by the given node to the given buffer.
|
107
|
+
*
|
108
|
+
* @param parser The parser to serialize.
|
109
|
+
* @param node The node to serialize.
|
110
|
+
* @param buffer The buffer to serialize to.
|
111
|
+
*/
|
112
|
+
PRISM_EXPORTED_FUNCTION void pm_serialize(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer);
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Parse the given source to the AST and dump the AST to the given buffer.
|
116
|
+
*
|
117
|
+
* @param buffer The buffer to serialize to.
|
118
|
+
* @param source The source to parse.
|
119
|
+
* @param size The size of the source.
|
120
|
+
* @param data The optional data to pass to the parser.
|
121
|
+
*/
|
122
|
+
PRISM_EXPORTED_FUNCTION void pm_serialize_parse(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Parse and serialize the comments in the given source to the given buffer.
|
126
|
+
*
|
127
|
+
* @param buffer The buffer to serialize to.
|
128
|
+
* @param source The source to parse.
|
129
|
+
* @param size The size of the source.
|
130
|
+
* @param data The optional data to pass to the parser.
|
131
|
+
*/
|
132
|
+
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
133
|
+
|
134
|
+
/**
|
135
|
+
* Lex the given source and serialize to the given buffer.
|
136
|
+
*
|
137
|
+
* @param source The source to lex.
|
138
|
+
* @param size The size of the source.
|
139
|
+
* @param buffer The buffer to serialize to.
|
140
|
+
* @param data The optional data to pass to the lexer.
|
141
|
+
*/
|
142
|
+
PRISM_EXPORTED_FUNCTION void pm_serialize_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
143
|
+
|
144
|
+
/**
|
145
|
+
* Parse and serialize both the AST and the tokens represented by the given
|
146
|
+
* source to the given buffer.
|
147
|
+
*
|
148
|
+
* @param buffer The buffer to serialize to.
|
149
|
+
* @param source The source to parse.
|
150
|
+
* @param size The size of the source.
|
151
|
+
* @param data The optional data to pass to the parser.
|
152
|
+
*/
|
153
|
+
PRISM_EXPORTED_FUNCTION void pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
|
154
|
+
|
155
|
+
/**
|
156
|
+
* Parse the source and return true if it parses without errors or warnings.
|
157
|
+
*
|
158
|
+
* @param source The source to parse.
|
159
|
+
* @param size The size of the source.
|
160
|
+
* @param data The optional data to pass to the parser.
|
161
|
+
* @return True if the source parses without errors or warnings.
|
162
|
+
*/
|
163
|
+
PRISM_EXPORTED_FUNCTION bool pm_parse_success_p(const uint8_t *source, size_t size, const char *data);
|
164
|
+
|
165
|
+
/**
|
166
|
+
* Returns a string representation of the given token type.
|
167
|
+
*
|
168
|
+
* @param token_type The token type to convert to a string.
|
169
|
+
* @return A string representation of the given token type.
|
170
|
+
*/
|
171
|
+
PRISM_EXPORTED_FUNCTION const char * pm_token_type_name(pm_token_type_t token_type);
|
172
|
+
|
173
|
+
/**
|
174
|
+
* Returns the human name of the given token type.
|
175
|
+
*
|
176
|
+
* @param token_type The token type to convert to a human name.
|
177
|
+
* @return The human name of the given token type.
|
178
|
+
*/
|
179
|
+
const char * pm_token_type_human(pm_token_type_t token_type);
|
180
|
+
|
181
|
+
/**
|
182
|
+
* Format the errors on the parser into the given buffer.
|
183
|
+
*
|
184
|
+
* @param parser The parser to format the errors for.
|
185
|
+
* @param buffer The buffer to format the errors into.
|
186
|
+
* @param colorize Whether or not to colorize the errors with ANSI escape sequences.
|
187
|
+
*/
|
188
|
+
PRISM_EXPORTED_FUNCTION void pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool colorize);
|
189
|
+
|
190
|
+
/**
|
191
|
+
* @mainpage
|
192
|
+
*
|
193
|
+
* Prism is a parser for the Ruby programming language. It is designed to be
|
194
|
+
* portable, error tolerant, and maintainable. It is written in C99 and has no
|
195
|
+
* dependencies. It is currently being integrated into
|
196
|
+
* [CRuby](https://github.com/ruby/ruby),
|
197
|
+
* [JRuby](https://github.com/jruby/jruby),
|
198
|
+
* [TruffleRuby](https://github.com/oracle/truffleruby),
|
199
|
+
* [Sorbet](https://github.com/sorbet/sorbet), and
|
200
|
+
* [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree).
|
201
|
+
*
|
202
|
+
* @section getting-started Getting started
|
203
|
+
*
|
204
|
+
* If you're vendoring this project and compiling it statically then as long as
|
205
|
+
* you have a C99 compiler you will be fine. If you're linking against it as
|
206
|
+
* shared library, then you should compile with `-fvisibility=hidden` and
|
207
|
+
* `-DPRISM_EXPORT_SYMBOLS` to tell prism to make only its public interface
|
208
|
+
* visible.
|
209
|
+
*
|
210
|
+
* @section parsing Parsing
|
211
|
+
*
|
212
|
+
* In order to parse Ruby code, the structures and functions that you're going
|
213
|
+
* to want to use and be aware of are:
|
214
|
+
*
|
215
|
+
* * `pm_parser_t` - the main parser structure
|
216
|
+
* * `pm_parser_init` - initialize a parser
|
217
|
+
* * `pm_parse` - parse and return the root node
|
218
|
+
* * `pm_node_destroy` - deallocate the root node returned by `pm_parse`
|
219
|
+
* * `pm_parser_free` - free the internal memory of the parser
|
220
|
+
*
|
221
|
+
* Putting all of this together would look something like:
|
222
|
+
*
|
223
|
+
* ```c
|
224
|
+
* void parse(const uint8_t *source, size_t length) {
|
225
|
+
* pm_parser_t parser;
|
226
|
+
* pm_parser_init(&parser, source, length, NULL);
|
227
|
+
*
|
228
|
+
* pm_node_t *root = pm_parse(&parser);
|
229
|
+
* printf("PARSED!\n");
|
230
|
+
*
|
231
|
+
* pm_node_destroy(&parser, root);
|
232
|
+
* pm_parser_free(&parser);
|
233
|
+
* }
|
234
|
+
* ```
|
235
|
+
*
|
236
|
+
* All of the nodes "inherit" from `pm_node_t` by embedding those structures as
|
237
|
+
* their first member. This means you can downcast and upcast any node in the
|
238
|
+
* tree to a `pm_node_t`.
|
239
|
+
*
|
240
|
+
* @section serializing Serializing
|
241
|
+
*
|
242
|
+
* Prism provides the ability to serialize the AST and its related metadata into
|
243
|
+
* a binary format. This format is designed to be portable to different
|
244
|
+
* languages and runtimes so that you only need to make one FFI call in order to
|
245
|
+
* parse Ruby code. The structures and functions that you're going to want to
|
246
|
+
* use and be aware of are:
|
247
|
+
*
|
248
|
+
* * `pm_buffer_t` - a small buffer object that will hold the serialized AST
|
249
|
+
* * `pm_buffer_free` - free the memory associated with the buffer
|
250
|
+
* * `pm_serialize` - serialize the AST into a buffer
|
251
|
+
* * `pm_serialize_parse` - parse and serialize the AST into a buffer
|
252
|
+
*
|
253
|
+
* Putting all of this together would look something like:
|
254
|
+
*
|
255
|
+
* ```c
|
256
|
+
* void serialize(const uint8_t *source, size_t length) {
|
257
|
+
* pm_buffer_t buffer = { 0 };
|
258
|
+
*
|
259
|
+
* pm_serialize_parse(&buffer, source, length, NULL);
|
260
|
+
* printf("SERIALIZED!\n");
|
261
|
+
*
|
262
|
+
* pm_buffer_free(&buffer);
|
263
|
+
* }
|
264
|
+
* ```
|
265
|
+
*
|
266
|
+
* @section inspecting Inspecting
|
267
|
+
*
|
268
|
+
* Prism provides the ability to inspect the AST by pretty-printing nodes. You
|
269
|
+
* can do this with the `pm_prettyprint` function, which you would use like:
|
270
|
+
*
|
271
|
+
* ```c
|
272
|
+
* void prettyprint(const uint8_t *source, size_t length) {
|
273
|
+
* pm_parser_t parser;
|
274
|
+
* pm_parser_init(&parser, source, length, NULL);
|
275
|
+
*
|
276
|
+
* pm_node_t *root = pm_parse(&parser);
|
277
|
+
* pm_buffer_t buffer = { 0 };
|
278
|
+
*
|
279
|
+
* pm_prettyprint(&buffer, &parser, root);
|
280
|
+
* printf("%*.s\n", (int) buffer.length, buffer.value);
|
281
|
+
*
|
282
|
+
* pm_buffer_free(&buffer);
|
283
|
+
* pm_node_destroy(&parser, root);
|
284
|
+
* pm_parser_free(&parser);
|
285
|
+
* }
|
286
|
+
* ```
|
287
|
+
*/
|
288
|
+
|
289
|
+
#endif
|
data/jruby-prism.jar
ADDED
Binary file
|