|
1 | | -# for ast.c |
2 | | - |
3 | | -class RubyVM |
4 | | - |
5 | | - # AbstractSyntaxTree provides methods to parse Ruby code into |
6 | | - # abstract syntax trees. The nodes in the tree |
7 | | - # are instances of RubyVM::AbstractSyntaxTree::Node. |
8 | | - # |
9 | | - # This class is MRI specific as it exposes implementation details |
10 | | - # of the MRI abstract syntax tree. |
11 | | - # |
12 | | - module AbstractSyntaxTree |
13 | | - |
14 | | - # call-seq: |
15 | | - # RubyVM::AbstractSyntaxTree.parse(string) -> RubyVM::AbstractSyntaxTree::Node |
16 | | - # |
17 | | - # Parses the given _string_ into an abstract syntax tree, |
18 | | - # returning the root node of that tree. |
19 | | - # |
20 | | - # SyntaxError is raised if the given _string_ is invalid syntax. |
21 | | - # |
22 | | - # RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") |
23 | | - # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9> |
24 | | - def self.parse string |
25 | | - __builtin_ast_s_parse string |
26 | | - end |
27 | | - |
28 | | - # call-seq: |
29 | | - # RubyVM::AbstractSyntaxTree.parse_file(pathname) -> RubyVM::AbstractSyntaxTree::Node |
30 | | - # |
31 | | - # Reads the file from _pathname_, then parses it like ::parse, |
32 | | - # returning the root node of the abstract syntax tree. |
33 | | - # |
34 | | - # SyntaxError is raised if _pathname_'s contents are not |
35 | | - # valid Ruby syntax. |
36 | | - # |
37 | | - # RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb") |
38 | | - # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3> |
39 | | - def self.parse_file pathname |
40 | | - __builtin_ast_s_parse_file pathname |
41 | | - end |
42 | | - |
43 | | - # call-seq: |
44 | | - # RubyVM::AbstractSyntaxTree.of(proc) -> RubyVM::AbstractSyntaxTree::Node |
45 | | - # RubyVM::AbstractSyntaxTree.of(method) -> RubyVM::AbstractSyntaxTree::Node |
46 | | - # |
47 | | - # Returns AST nodes of the given _proc_ or _method_. |
48 | | - # |
49 | | - # RubyVM::AbstractSyntaxTree.of(proc {1 + 2}) |
50 | | - # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:35-1:42> |
51 | | - # |
52 | | - # def hello |
53 | | - # puts "hello, world" |
54 | | - # end |
55 | | - # |
56 | | - # RubyVM::AbstractSyntaxTree.of(method(:hello)) |
57 | | - # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3> |
58 | | - def self.of body |
59 | | - __builtin_ast_s_of body |
60 | | - end |
61 | | - |
62 | | - # RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in |
63 | | - # RubyVM::AbstractSyntaxTree. |
64 | | - # |
65 | | - # This class is MRI specific. |
66 | | - # |
67 | | - class Node |
68 | | - |
69 | | - # call-seq: |
70 | | - # node.type -> symbol |
71 | | - # |
72 | | - # Returns the type of this node as a symbol. |
73 | | - # |
74 | | - # root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") |
75 | | - # root.type # => :SCOPE |
76 | | - # call = root.children[2] |
77 | | - # call.type # => :OPCALL |
78 | | - def type |
79 | | - __builtin_ast_node_type |
80 | | - end |
81 | | - |
82 | | - # call-seq: |
83 | | - # node.first_lineno -> integer |
84 | | - # |
85 | | - # The line number in the source code where this AST's text began. |
86 | | - def first_lineno |
87 | | - __builtin_ast_node_first_lineno |
88 | | - end |
89 | | - |
90 | | - # call-seq: |
91 | | - # node.first_column -> integer |
92 | | - # |
93 | | - # The column number in the source code where this AST's text began. |
94 | | - def first_column |
95 | | - __builtin_ast_node_first_column |
96 | | - end |
97 | | - |
98 | | - # call-seq: |
99 | | - # node.last_lineno -> integer |
100 | | - # |
101 | | - # The line number in the source code where this AST's text ended. |
102 | | - def last_lineno |
103 | | - __builtin_ast_node_last_lineno |
104 | | - end |
105 | | - |
106 | | - # call-seq: |
107 | | - # node.last_column -> integer |
108 | | - # |
109 | | - # The column number in the source code where this AST's text ended. |
110 | | - def last_column |
111 | | - __builtin_ast_node_last_column |
112 | | - end |
113 | | - |
114 | | - # call-seq: |
115 | | - # node.children -> array |
116 | | - # |
117 | | - # Returns AST nodes under this one. Each kind of node |
118 | | - # has different children, depending on what kind of node it is. |
119 | | - # |
120 | | - # The returned array may contain other nodes or <code>nil</code>. |
121 | | - def children |
122 | | - __builtin_ast_node_children |
123 | | - end |
124 | | - |
125 | | - # call-seq: |
126 | | - # node.inspect -> string |
127 | | - # |
128 | | - # Returns debugging information about this node as a string. |
129 | | - def inspect |
130 | | - __builtin_ast_node_inspect |
131 | | - end |
132 | | - end |
133 | | - end |
134 | | -end |
| 1 | +# for ast.c |
| 2 | + |
| 3 | +class RubyVM |
| 4 | + |
| 5 | + # AbstractSyntaxTree provides methods to parse Ruby code into |
| 6 | + # abstract syntax trees. The nodes in the tree |
| 7 | + # are instances of RubyVM::AbstractSyntaxTree::Node. |
| 8 | + # |
| 9 | + # This class is MRI specific as it exposes implementation details |
| 10 | + # of the MRI abstract syntax tree. |
| 11 | + # |
| 12 | + module AbstractSyntaxTree |
| 13 | + |
| 14 | + # call-seq: |
| 15 | + # RubyVM::AbstractSyntaxTree.parse(string) -> RubyVM::AbstractSyntaxTree::Node |
| 16 | + # |
| 17 | + # Parses the given _string_ into an abstract syntax tree, |
| 18 | + # returning the root node of that tree. |
| 19 | + # |
| 20 | + # SyntaxError is raised if the given _string_ is invalid syntax. |
| 21 | + # |
| 22 | + # RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") |
| 23 | + # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9> |
| 24 | + def self.parse string |
| 25 | + __builtin_ast_s_parse string |
| 26 | + end |
| 27 | + |
| 28 | + # call-seq: |
| 29 | + # RubyVM::AbstractSyntaxTree.parse_file(pathname) -> RubyVM::AbstractSyntaxTree::Node |
| 30 | + # |
| 31 | + # Reads the file from _pathname_, then parses it like ::parse, |
| 32 | + # returning the root node of the abstract syntax tree. |
| 33 | + # |
| 34 | + # SyntaxError is raised if _pathname_'s contents are not |
| 35 | + # valid Ruby syntax. |
| 36 | + # |
| 37 | + # RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb") |
| 38 | + # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3> |
| 39 | + def self.parse_file pathname |
| 40 | + __builtin_ast_s_parse_file pathname |
| 41 | + end |
| 42 | + |
| 43 | + # call-seq: |
| 44 | + # RubyVM::AbstractSyntaxTree.of(proc) -> RubyVM::AbstractSyntaxTree::Node |
| 45 | + # RubyVM::AbstractSyntaxTree.of(method) -> RubyVM::AbstractSyntaxTree::Node |
| 46 | + # |
| 47 | + # Returns AST nodes of the given _proc_ or _method_. |
| 48 | + # |
| 49 | + # RubyVM::AbstractSyntaxTree.of(proc {1 + 2}) |
| 50 | + # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:35-1:42> |
| 51 | + # |
| 52 | + # def hello |
| 53 | + # puts "hello, world" |
| 54 | + # end |
| 55 | + # |
| 56 | + # RubyVM::AbstractSyntaxTree.of(method(:hello)) |
| 57 | + # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3> |
| 58 | + def self.of body |
| 59 | + __builtin_ast_s_of body |
| 60 | + end |
| 61 | + |
| 62 | + # RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in |
| 63 | + # RubyVM::AbstractSyntaxTree. |
| 64 | + # |
| 65 | + # This class is MRI specific. |
| 66 | + # |
| 67 | + class Node |
| 68 | + |
| 69 | + # call-seq: |
| 70 | + # node.type -> symbol |
| 71 | + # |
| 72 | + # Returns the type of this node as a symbol. |
| 73 | + # |
| 74 | + # root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") |
| 75 | + # root.type # => :SCOPE |
| 76 | + # call = root.children[2] |
| 77 | + # call.type # => :OPCALL |
| 78 | + def type |
| 79 | + __builtin_ast_node_type |
| 80 | + end |
| 81 | + |
| 82 | + # call-seq: |
| 83 | + # node.first_lineno -> integer |
| 84 | + # |
| 85 | + # The line number in the source code where this AST's text began. |
| 86 | + def first_lineno |
| 87 | + __builtin_ast_node_first_lineno |
| 88 | + end |
| 89 | + |
| 90 | + # call-seq: |
| 91 | + # node.first_column -> integer |
| 92 | + # |
| 93 | + # The column number in the source code where this AST's text began. |
| 94 | + def first_column |
| 95 | + __builtin_ast_node_first_column |
| 96 | + end |
| 97 | + |
| 98 | + # call-seq: |
| 99 | + # node.last_lineno -> integer |
| 100 | + # |
| 101 | + # The line number in the source code where this AST's text ended. |
| 102 | + def last_lineno |
| 103 | + __builtin_ast_node_last_lineno |
| 104 | + end |
| 105 | + |
| 106 | + # call-seq: |
| 107 | + # node.last_column -> integer |
| 108 | + # |
| 109 | + # The column number in the source code where this AST's text ended. |
| 110 | + def last_column |
| 111 | + __builtin_ast_node_last_column |
| 112 | + end |
| 113 | + |
| 114 | + # call-seq: |
| 115 | + # node.children -> array |
| 116 | + # |
| 117 | + # Returns AST nodes under this one. Each kind of node |
| 118 | + # has different children, depending on what kind of node it is. |
| 119 | + # |
| 120 | + # The returned array may contain other nodes or <code>nil</code>. |
| 121 | + def children |
| 122 | + __builtin_ast_node_children |
| 123 | + end |
| 124 | + |
| 125 | + # call-seq: |
| 126 | + # node.inspect -> string |
| 127 | + # |
| 128 | + # Returns debugging information about this node as a string. |
| 129 | + def inspect |
| 130 | + __builtin_ast_node_inspect |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments