6
6
#include "node.h"
7
7
#include "vm_core.h"
8
8
#include "iseq.h"
9
+ #include "builtin.h"
9
10
10
11
static VALUE rb_mAST ;
11
12
static VALUE rb_cNode ;
@@ -80,20 +81,8 @@ ast_parse_done(rb_ast_t *ast)
80
81
return ast_new_internal (ast , (NODE * )ast -> body .root );
81
82
}
82
83
83
- /*
84
- * call-seq:
85
- * RubyVM::AbstractSyntaxTree.parse(string) -> RubyVM::AbstractSyntaxTree::Node
86
- *
87
- * Parses the given _string_ into an abstract syntax tree,
88
- * returning the root node of that tree.
89
- *
90
- * SyntaxError is raised if the given _string_ is invalid syntax.
91
- *
92
- * RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
93
- * # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9>
94
- */
95
84
static VALUE
96
- rb_ast_s_parse ( VALUE module , VALUE str )
85
+ ast_s_parse ( rb_execution_context_t * ec , VALUE module , VALUE str )
97
86
{
98
87
return rb_ast_parse_str (str );
99
88
}
@@ -108,21 +97,8 @@ rb_ast_parse_str(VALUE str)
108
97
return ast_parse_done (ast );
109
98
}
110
99
111
- /*
112
- * call-seq:
113
- * RubyVM::AbstractSyntaxTree.parse_file(pathname) -> RubyVM::AbstractSyntaxTree::Node
114
- *
115
- * Reads the file from _pathname_, then parses it like ::parse,
116
- * returning the root node of the abstract syntax tree.
117
- *
118
- * SyntaxError is raised if _pathname_'s contents are not
119
- * valid Ruby syntax.
120
- *
121
- * RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb")
122
- * # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3>
123
- */
124
100
static VALUE
125
- rb_ast_s_parse_file ( VALUE module , VALUE path )
101
+ ast_s_parse_file ( rb_execution_context_t * ec , VALUE module , VALUE path )
126
102
{
127
103
return rb_ast_parse_file (path );
128
104
}
@@ -207,25 +183,8 @@ script_lines(VALUE path)
207
183
return lines ;
208
184
}
209
185
210
- /*
211
- * call-seq:
212
- * RubyVM::AbstractSyntaxTree.of(proc) -> RubyVM::AbstractSyntaxTree::Node
213
- * RubyVM::AbstractSyntaxTree.of(method) -> RubyVM::AbstractSyntaxTree::Node
214
- *
215
- * Returns AST nodes of the given _proc_ or _method_.
216
- *
217
- * RubyVM::AbstractSyntaxTree.of(proc {1 + 2})
218
- * # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:35-1:42>
219
- *
220
- * def hello
221
- * puts "hello, world"
222
- * end
223
- *
224
- * RubyVM::AbstractSyntaxTree.of(method(:hello))
225
- * # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3>
226
- */
227
186
static VALUE
228
- rb_ast_s_of ( VALUE module , VALUE body )
187
+ ast_s_of ( rb_execution_context_t * ec , VALUE module , VALUE body )
229
188
{
230
189
VALUE path , node , lines ;
231
190
int node_id ;
@@ -274,19 +233,8 @@ node_type_to_str(const NODE *node)
274
233
return (ruby_node_name (nd_type (node )) + rb_strlen_lit ("NODE_" ));
275
234
}
276
235
277
- /*
278
- * call-seq:
279
- * node.type -> symbol
280
- *
281
- * Returns the type of this node as a symbol.
282
- *
283
- * root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
284
- * root.type # => :SCOPE
285
- * call = root.children[2]
286
- * call.type # => :OPCALL
287
- */
288
236
static VALUE
289
- rb_ast_node_type ( VALUE self )
237
+ ast_node_type ( rb_execution_context_t * ec , VALUE self )
290
238
{
291
239
struct ASTNodeData * data ;
292
240
TypedData_Get_Struct (self , struct ASTNodeData , & rb_node_type , data );
@@ -668,92 +616,53 @@ node_children(rb_ast_t *ast, NODE *node)
668
616
rb_bug ("node_children: unknown node: %s" , ruby_node_name (type ));
669
617
}
670
618
671
- /*
672
- * call-seq:
673
- * node.children -> array
674
- *
675
- * Returns AST nodes under this one. Each kind of node
676
- * has different children, depending on what kind of node it is.
677
- *
678
- * The returned array may contain other nodes or <code>nil</code>.
679
- */
680
619
static VALUE
681
- rb_ast_node_children ( VALUE self )
620
+ ast_node_children ( rb_execution_context_t * ec , VALUE self )
682
621
{
683
622
struct ASTNodeData * data ;
684
623
TypedData_Get_Struct (self , struct ASTNodeData , & rb_node_type , data );
685
624
686
625
return node_children (data -> ast , data -> node );
687
626
}
688
627
689
- /*
690
- * call-seq:
691
- * node.first_lineno -> integer
692
- *
693
- * The line number in the source code where this AST's text began.
694
- */
695
628
static VALUE
696
- rb_ast_node_first_lineno ( VALUE self )
629
+ ast_node_first_lineno ( rb_execution_context_t * ec , VALUE self )
697
630
{
698
631
struct ASTNodeData * data ;
699
632
TypedData_Get_Struct (self , struct ASTNodeData , & rb_node_type , data );
700
633
701
634
return INT2NUM (nd_first_lineno (data -> node ));
702
635
}
703
636
704
- /*
705
- * call-seq:
706
- * node.first_column -> integer
707
- *
708
- * The column number in the source code where this AST's text began.
709
- */
710
637
static VALUE
711
- rb_ast_node_first_column ( VALUE self )
638
+ ast_node_first_column ( rb_execution_context_t * ec , VALUE self )
712
639
{
713
640
struct ASTNodeData * data ;
714
641
TypedData_Get_Struct (self , struct ASTNodeData , & rb_node_type , data );
715
642
716
643
return INT2NUM (nd_first_column (data -> node ));
717
644
}
718
645
719
- /*
720
- * call-seq:
721
- * node.last_lineno -> integer
722
- *
723
- * The line number in the source code where this AST's text ended.
724
- */
725
646
static VALUE
726
- rb_ast_node_last_lineno ( VALUE self )
647
+ ast_node_last_lineno ( rb_execution_context_t * ec , VALUE self )
727
648
{
728
649
struct ASTNodeData * data ;
729
650
TypedData_Get_Struct (self , struct ASTNodeData , & rb_node_type , data );
730
651
731
652
return INT2NUM (nd_last_lineno (data -> node ));
732
653
}
733
654
734
- /*
735
- * call-seq:
736
- * node.last_column -> integer
737
- *
738
- * The column number in the source code where this AST's text ended.
739
- */
740
655
static VALUE
741
- rb_ast_node_last_column ( VALUE self )
656
+ ast_node_last_column ( rb_execution_context_t * ec , VALUE self )
742
657
{
743
658
struct ASTNodeData * data ;
744
659
TypedData_Get_Struct (self , struct ASTNodeData , & rb_node_type , data );
745
660
746
661
return INT2NUM (nd_last_column (data -> node ));
747
662
}
748
663
749
- /*
750
- * call-seq:
751
- * node.inspect -> string
752
- *
753
- * Returns debugging information about this node as a string.
754
- */
755
664
static VALUE
756
- rb_ast_node_inspect ( VALUE self )
665
+ ast_node_inspect ( rb_execution_context_t * ec , VALUE self )
757
666
{
758
667
VALUE str ;
759
668
VALUE cname ;
@@ -772,35 +681,14 @@ rb_ast_node_inspect(VALUE self)
772
681
return str ;
773
682
}
774
683
684
+ #include "load_ast.inc"
685
+
775
686
void
776
687
Init_ast (void )
777
688
{
778
- /*
779
- * AbstractSyntaxTree provides methods to parse Ruby code into
780
- * abstract syntax trees. The nodes in the tree
781
- * are instances of RubyVM::AbstractSyntaxTree::Node.
782
- *
783
- * This class is MRI specific as it exposes implementation details
784
- * of the MRI abstract syntax tree.
785
- */
786
689
rb_mAST = rb_define_module_under (rb_cRubyVM , "AbstractSyntaxTree" );
787
- /*
788
- * RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in
789
- * RubyVM::AbstractSyntaxTree.
790
- *
791
- * This class is MRI specific.
792
- */
793
690
rb_cNode = rb_define_class_under (rb_mAST , "Node" , rb_cObject );
794
-
795
691
rb_undef_alloc_func (rb_cNode );
796
- rb_define_singleton_method (rb_mAST , "parse" , rb_ast_s_parse , 1 );
797
- rb_define_singleton_method (rb_mAST , "parse_file" , rb_ast_s_parse_file , 1 );
798
- rb_define_singleton_method (rb_mAST , "of" , rb_ast_s_of , 1 );
799
- rb_define_method (rb_cNode , "type" , rb_ast_node_type , 0 );
800
- rb_define_method (rb_cNode , "first_lineno" , rb_ast_node_first_lineno , 0 );
801
- rb_define_method (rb_cNode , "first_column" , rb_ast_node_first_column , 0 );
802
- rb_define_method (rb_cNode , "last_lineno" , rb_ast_node_last_lineno , 0 );
803
- rb_define_method (rb_cNode , "last_column" , rb_ast_node_last_column , 0 );
804
- rb_define_method (rb_cNode , "children" , rb_ast_node_children , 0 );
805
- rb_define_method (rb_cNode , "inspect" , rb_ast_node_inspect , 0 );
692
+
693
+ load_ast ();
806
694
}
0 commit comments