@@ -10,6 +10,7 @@ VALUE rb_cPrismToken;
1010VALUE rb_cPrismLocation ;
1111
1212VALUE rb_cPrismComment ;
13+ VALUE rb_cPrismMagicComment ;
1314VALUE rb_cPrismParseError ;
1415VALUE rb_cPrismParseWarning ;
1516VALUE rb_cPrismParseResult ;
@@ -153,6 +154,35 @@ parser_comments(pm_parser_t *parser, VALUE source) {
153154 return comments ;
154155}
155156
157+ // Extract the magic comments out of the parser into an array.
158+ static VALUE
159+ parser_magic_comments (pm_parser_t * parser , VALUE source ) {
160+ VALUE magic_comments = rb_ary_new ();
161+
162+ for (pm_magic_comment_t * magic_comment = (pm_magic_comment_t * ) parser -> magic_comment_list .head ; magic_comment != NULL ; magic_comment = (pm_magic_comment_t * ) magic_comment -> node .next ) {
163+ VALUE key_loc_argv [] = {
164+ source ,
165+ LONG2FIX (magic_comment -> key_start - parser -> start ),
166+ LONG2FIX (magic_comment -> key_length )
167+ };
168+
169+ VALUE value_loc_argv [] = {
170+ source ,
171+ LONG2FIX (magic_comment -> value_start - parser -> start ),
172+ LONG2FIX (magic_comment -> value_length )
173+ };
174+
175+ VALUE magic_comment_argv [] = {
176+ rb_class_new_instance (3 , key_loc_argv , rb_cPrismLocation ),
177+ rb_class_new_instance (3 , value_loc_argv , rb_cPrismLocation )
178+ };
179+
180+ rb_ary_push (magic_comments , rb_class_new_instance (2 , magic_comment_argv , rb_cPrismMagicComment ));
181+ }
182+
183+ return magic_comments ;
184+ }
185+
156186// Extract the errors out of the parser into an array.
157187static VALUE
158188parser_errors (pm_parser_t * parser , rb_encoding * encoding , VALUE source ) {
@@ -297,14 +327,15 @@ parse_lex_input(pm_string_t *input, const char *filepath, bool return_nodes) {
297327 VALUE result_argv [] = {
298328 value ,
299329 parser_comments (& parser , source ),
330+ parser_magic_comments (& parser , source ),
300331 parser_errors (& parser , parse_lex_data .encoding , source ),
301332 parser_warnings (& parser , parse_lex_data .encoding , source ),
302333 source
303334 };
304335
305336 pm_node_destroy (& parser , node );
306337 pm_parser_free (& parser );
307- return rb_class_new_instance (5 , result_argv , rb_cPrismParseResult );
338+ return rb_class_new_instance (6 , result_argv , rb_cPrismParseResult );
308339}
309340
310341// Return an array of tokens corresponding to the given string.
@@ -351,12 +382,13 @@ parse_input(pm_string_t *input, const char *filepath) {
351382 VALUE result_argv [] = {
352383 pm_ast_new (& parser , node , encoding ),
353384 parser_comments (& parser , source ),
385+ parser_magic_comments (& parser , source ),
354386 parser_errors (& parser , encoding , source ),
355387 parser_warnings (& parser , encoding , source ),
356388 source
357389 };
358390
359- VALUE result = rb_class_new_instance (5 , result_argv , rb_cPrismParseResult );
391+ VALUE result = rb_class_new_instance (6 , result_argv , rb_cPrismParseResult );
360392
361393 pm_node_destroy (& parser , node );
362394 pm_parser_free (& parser );
@@ -547,6 +579,7 @@ Init_prism(void) {
547579 rb_cPrismToken = rb_define_class_under (rb_cPrism , "Token" , rb_cObject );
548580 rb_cPrismLocation = rb_define_class_under (rb_cPrism , "Location" , rb_cObject );
549581 rb_cPrismComment = rb_define_class_under (rb_cPrism , "Comment" , rb_cObject );
582+ rb_cPrismMagicComment = rb_define_class_under (rb_cPrism , "MagicComment" , rb_cObject );
550583 rb_cPrismParseError = rb_define_class_under (rb_cPrism , "ParseError" , rb_cObject );
551584 rb_cPrismParseWarning = rb_define_class_under (rb_cPrism , "ParseWarning" , rb_cObject );
552585 rb_cPrismParseResult = rb_define_class_under (rb_cPrism , "ParseResult" , rb_cObject );
0 commit comments