masamune-ast 1.2.1 → 2.0.0
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 +7 -0
- data/Gemfile.lock +4 -1
- data/README.md +30 -82
- data/lib/masamune/abstract_syntax_tree/prism/node_extensions.rb +82 -0
- data/lib/masamune/abstract_syntax_tree/visitors/block_parameters_visitor.rb +18 -0
- data/lib/masamune/abstract_syntax_tree/visitors/method_calls_visitor.rb +19 -0
- data/lib/masamune/abstract_syntax_tree/visitors/method_definitions_visitor.rb +17 -0
- data/lib/masamune/abstract_syntax_tree/visitors/parameters_visitor.rb +18 -0
- data/lib/masamune/abstract_syntax_tree/visitors/strings_visitor.rb +17 -0
- data/lib/masamune/abstract_syntax_tree/visitors/symbols_visitor.rb +17 -0
- data/lib/masamune/abstract_syntax_tree/visitors/variables_visitor.rb +34 -0
- data/lib/masamune/abstract_syntax_tree.rb +57 -134
- data/lib/masamune/lex_node.rb +6 -6
- data/lib/masamune/slasher.rb +9 -15
- data/lib/masamune/version.rb +1 -1
- data/lib/masamune.rb +10 -22
- metadata +29 -27
- data/lib/masamune/abstract_syntax_tree/data_node.rb +0 -49
- data/lib/masamune/abstract_syntax_tree/node.rb +0 -41
- data/lib/masamune/abstract_syntax_tree/nodes/assign.rb +0 -11
- data/lib/masamune/abstract_syntax_tree/nodes/blocks/brace_block.rb +0 -13
- data/lib/masamune/abstract_syntax_tree/nodes/blocks/do_block.rb +0 -13
- data/lib/masamune/abstract_syntax_tree/nodes/call.rb +0 -17
- data/lib/masamune/abstract_syntax_tree/nodes/command.rb +0 -64
- data/lib/masamune/abstract_syntax_tree/nodes/def.rb +0 -17
- data/lib/masamune/abstract_syntax_tree/nodes/params.rb +0 -23
- data/lib/masamune/abstract_syntax_tree/nodes/program.rb +0 -16
- data/lib/masamune/abstract_syntax_tree/nodes/string_content.rb +0 -17
- data/lib/masamune/abstract_syntax_tree/nodes/support_nodes/block.rb +0 -18
- data/lib/masamune/abstract_syntax_tree/nodes/support_nodes/comment.rb +0 -25
- data/lib/masamune/abstract_syntax_tree/nodes/symbol.rb +0 -17
- data/lib/masamune/abstract_syntax_tree/nodes/symbols/dyna_symbol.rb +0 -20
- data/lib/masamune/abstract_syntax_tree/nodes/symbols/symbol_literal.rb +0 -21
- data/lib/masamune/abstract_syntax_tree/nodes/variables/block_var.rb +0 -22
- data/lib/masamune/abstract_syntax_tree/nodes/variables/var_field.rb +0 -17
- data/lib/masamune/abstract_syntax_tree/nodes/variables/var_ref.rb +0 -19
- data/lib/masamune/abstract_syntax_tree/nodes/vcall.rb +0 -17
data/lib/masamune/lex_node.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
module Masamune
|
2
2
|
# https://docs.ruby-lang.org/en/3.0/Ripper.html#method-c-lex
|
3
3
|
#
|
4
|
-
# @
|
4
|
+
# @location: Line number and starting point on the line. i.e. - [4, 7].
|
5
5
|
# @type: The type of token. i.e. - :kw (is a keyword).
|
6
6
|
# @token: The raw string which represents the actual ruby code. i.e. - "do".
|
7
7
|
# @state: Ripper::Lexer::State. i.e. - CMDARG.
|
8
8
|
class LexNode
|
9
|
-
attr_accessor :
|
9
|
+
attr_accessor :location, :type, :token, :state, :ast_id
|
10
10
|
attr_reader :index
|
11
11
|
|
12
12
|
def initialize(index, raw_lex_node, ast_id)
|
13
13
|
@index = index
|
14
|
-
@
|
14
|
+
@location, @type, @token, @state = raw_lex_node
|
15
15
|
@type = @type.to_s.gsub(/^[a-z]*_/, "").to_sym
|
16
16
|
|
17
17
|
# Since the Abstract Syntax Tree can get very large,
|
@@ -25,15 +25,15 @@ module Masamune
|
|
25
25
|
|
26
26
|
def variable?
|
27
27
|
return false unless identifier?
|
28
|
-
ast.variables(
|
28
|
+
ast.variables(token_value: @token).any?
|
29
29
|
end
|
30
30
|
|
31
31
|
def method_definition?
|
32
|
-
ast.method_definitions(
|
32
|
+
ast.method_definitions(token_value: @token).any?
|
33
33
|
end
|
34
34
|
|
35
35
|
def method_call?
|
36
|
-
ast.method_calls(
|
36
|
+
ast.method_calls(token_value: @token).any?
|
37
37
|
end
|
38
38
|
|
39
39
|
def method?
|
data/lib/masamune/slasher.rb
CHANGED
@@ -1,34 +1,28 @@
|
|
1
1
|
module Masamune
|
2
2
|
module Slasher
|
3
|
-
def self.replace(type:,
|
4
|
-
# `type`
|
5
|
-
|
6
|
-
line_data_and_token_ary = if type.is_a?(Symbol)
|
3
|
+
def self.replace(type:, old_token_value:, new_token_value:, code:, ast:)
|
4
|
+
# `type` refers to the singluar form of a method name from the ast like `method_definitions`.
|
5
|
+
nodes = if type.is_a?(Symbol)
|
7
6
|
type_to_method = type.to_s.pluralize.to_sym
|
8
7
|
ast.send(type_to_method)
|
9
|
-
elsif type.is_a?(Array)
|
10
|
-
type.map {|klass| ast.find_nodes(klass)}.flatten
|
11
8
|
end
|
12
9
|
|
13
|
-
|
14
|
-
|
10
|
+
nodes_of_tokens_to_replace = nodes.select do |node|
|
11
|
+
node.token_value == old_token_value
|
15
12
|
end
|
16
13
|
|
17
14
|
# Build from lex nodes
|
18
15
|
result = ast.lex_nodes.map do |lex_node|
|
19
16
|
match_found = false
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
line_data_and_token[:index_on_line]
|
24
|
-
]
|
25
|
-
if position == lex_node.position
|
17
|
+
nodes_of_tokens_to_replace.each do |node|
|
18
|
+
location = [node.token_location.start_line, node.token_location.start_column]
|
19
|
+
if location == lex_node.location
|
26
20
|
match_found = true
|
27
21
|
break
|
28
22
|
end
|
29
23
|
end
|
30
24
|
|
31
|
-
match_found ?
|
25
|
+
match_found ? new_token_value : lex_node.token
|
32
26
|
end
|
33
27
|
|
34
28
|
result.join
|
data/lib/masamune/version.rb
CHANGED
data/lib/masamune.rb
CHANGED
@@ -2,32 +2,20 @@
|
|
2
2
|
|
3
3
|
require_relative "masamune/version"
|
4
4
|
require "ripper"
|
5
|
+
require "prism"
|
5
6
|
require "active_support/core_ext/string/inflections"
|
7
|
+
|
8
|
+
require "masamune/abstract_syntax_tree/visitors/block_parameters_visitor"
|
9
|
+
require "masamune/abstract_syntax_tree/visitors/method_definitions_visitor"
|
10
|
+
require "masamune/abstract_syntax_tree/visitors/method_calls_visitor"
|
11
|
+
require "masamune/abstract_syntax_tree/visitors/parameters_visitor"
|
12
|
+
require "masamune/abstract_syntax_tree/visitors/strings_visitor"
|
13
|
+
require "masamune/abstract_syntax_tree/visitors/symbols_visitor"
|
14
|
+
require "masamune/abstract_syntax_tree/visitors/variables_visitor"
|
15
|
+
|
6
16
|
require "masamune/lex_node"
|
7
17
|
require "masamune/slasher"
|
8
18
|
require "masamune/abstract_syntax_tree"
|
9
|
-
require "masamune/abstract_syntax_tree/node"
|
10
|
-
require "masamune/abstract_syntax_tree/data_node"
|
11
|
-
|
12
|
-
require "masamune/abstract_syntax_tree/nodes/support_nodes/block"
|
13
|
-
require "masamune/abstract_syntax_tree/nodes/support_nodes/comment"
|
14
|
-
|
15
|
-
require "masamune/abstract_syntax_tree/nodes/blocks/brace_block"
|
16
|
-
require "masamune/abstract_syntax_tree/nodes/blocks/do_block"
|
17
|
-
require "masamune/abstract_syntax_tree/nodes/symbols/dyna_symbol"
|
18
|
-
require "masamune/abstract_syntax_tree/nodes/symbols/symbol_literal"
|
19
|
-
require "masamune/abstract_syntax_tree/nodes/variables/block_var"
|
20
|
-
require "masamune/abstract_syntax_tree/nodes/variables/var_field"
|
21
|
-
require "masamune/abstract_syntax_tree/nodes/variables/var_ref"
|
22
|
-
require "masamune/abstract_syntax_tree/nodes/assign"
|
23
|
-
require "masamune/abstract_syntax_tree/nodes/call"
|
24
|
-
require "masamune/abstract_syntax_tree/nodes/command"
|
25
|
-
require "masamune/abstract_syntax_tree/nodes/def"
|
26
|
-
require "masamune/abstract_syntax_tree/nodes/params"
|
27
|
-
require "masamune/abstract_syntax_tree/nodes/program"
|
28
|
-
require "masamune/abstract_syntax_tree/nodes/string_content"
|
29
|
-
require "masamune/abstract_syntax_tree/nodes/symbol"
|
30
|
-
require "masamune/abstract_syntax_tree/nodes/vcall"
|
31
19
|
|
32
20
|
require "pp"
|
33
21
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: masamune-ast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Zayas
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: prism
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.13.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.13.0
|
27
41
|
description: A layer of abstraction on top of Ripper for handling Abstract Syntax
|
28
42
|
Trees in Ruby.
|
29
43
|
email:
|
@@ -40,26 +54,14 @@ files:
|
|
40
54
|
- Rakefile
|
41
55
|
- lib/masamune.rb
|
42
56
|
- lib/masamune/abstract_syntax_tree.rb
|
43
|
-
- lib/masamune/abstract_syntax_tree/
|
44
|
-
- lib/masamune/abstract_syntax_tree/
|
45
|
-
- lib/masamune/abstract_syntax_tree/
|
46
|
-
- lib/masamune/abstract_syntax_tree/
|
47
|
-
- lib/masamune/abstract_syntax_tree/
|
48
|
-
- lib/masamune/abstract_syntax_tree/
|
49
|
-
- lib/masamune/abstract_syntax_tree/
|
50
|
-
- lib/masamune/abstract_syntax_tree/
|
51
|
-
- lib/masamune/abstract_syntax_tree/nodes/params.rb
|
52
|
-
- lib/masamune/abstract_syntax_tree/nodes/program.rb
|
53
|
-
- lib/masamune/abstract_syntax_tree/nodes/string_content.rb
|
54
|
-
- lib/masamune/abstract_syntax_tree/nodes/support_nodes/block.rb
|
55
|
-
- lib/masamune/abstract_syntax_tree/nodes/support_nodes/comment.rb
|
56
|
-
- lib/masamune/abstract_syntax_tree/nodes/symbol.rb
|
57
|
-
- lib/masamune/abstract_syntax_tree/nodes/symbols/dyna_symbol.rb
|
58
|
-
- lib/masamune/abstract_syntax_tree/nodes/symbols/symbol_literal.rb
|
59
|
-
- lib/masamune/abstract_syntax_tree/nodes/variables/block_var.rb
|
60
|
-
- lib/masamune/abstract_syntax_tree/nodes/variables/var_field.rb
|
61
|
-
- lib/masamune/abstract_syntax_tree/nodes/variables/var_ref.rb
|
62
|
-
- lib/masamune/abstract_syntax_tree/nodes/vcall.rb
|
57
|
+
- lib/masamune/abstract_syntax_tree/prism/node_extensions.rb
|
58
|
+
- lib/masamune/abstract_syntax_tree/visitors/block_parameters_visitor.rb
|
59
|
+
- lib/masamune/abstract_syntax_tree/visitors/method_calls_visitor.rb
|
60
|
+
- lib/masamune/abstract_syntax_tree/visitors/method_definitions_visitor.rb
|
61
|
+
- lib/masamune/abstract_syntax_tree/visitors/parameters_visitor.rb
|
62
|
+
- lib/masamune/abstract_syntax_tree/visitors/strings_visitor.rb
|
63
|
+
- lib/masamune/abstract_syntax_tree/visitors/symbols_visitor.rb
|
64
|
+
- lib/masamune/abstract_syntax_tree/visitors/variables_visitor.rb
|
63
65
|
- lib/masamune/lex_node.rb
|
64
66
|
- lib/masamune/slasher.rb
|
65
67
|
- lib/masamune/version.rb
|
@@ -71,7 +73,7 @@ metadata:
|
|
71
73
|
homepage_uri: https://www.github.com/gazayas/masamune-ast
|
72
74
|
source_code_uri: https://www.github.com/gazayas/masamune-ast
|
73
75
|
changelog_uri: https://www.github.com/gazayas/masamune-ast
|
74
|
-
post_install_message:
|
76
|
+
post_install_message:
|
75
77
|
rdoc_options: []
|
76
78
|
require_paths:
|
77
79
|
- lib
|
@@ -79,15 +81,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
81
|
requirements:
|
80
82
|
- - ">="
|
81
83
|
- !ruby/object:Gem::Version
|
82
|
-
version: 2.
|
84
|
+
version: 2.7.0
|
83
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
86
|
requirements:
|
85
87
|
- - ">="
|
86
88
|
- !ruby/object:Gem::Version
|
87
89
|
version: '0'
|
88
90
|
requirements: []
|
89
|
-
rubygems_version: 3.
|
90
|
-
signing_key:
|
91
|
+
rubygems_version: 3.5.0.dev
|
92
|
+
signing_key:
|
91
93
|
specification_version: 4
|
92
94
|
summary: Masamune
|
93
95
|
test_files: []
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# A data node represents an abstraction in the AST which has details about a specific type.
|
2
|
-
# i.e. - [:@ident, "variable_name", [4, 7]]
|
3
|
-
# These values are the `type`, `token`, and `position`, respectively.
|
4
|
-
# It is simliar to what you see in `Ripper.lex(code)` and `Masamune::AbstractSyntaxTree's @lex_nodes`.
|
5
|
-
|
6
|
-
# We break this down into a simpler structure, `line_data_and_token`,
|
7
|
-
# which looks like this: {line_number: 4, index_on_line: 7, token: "ruby"}
|
8
|
-
|
9
|
-
module Masamune
|
10
|
-
class AbstractSyntaxTree
|
11
|
-
class DataNode < Node
|
12
|
-
attr_reader :type, :token, :line_number, :index_on_line
|
13
|
-
|
14
|
-
def initialize(contents, ast_id, parent)
|
15
|
-
@type, @token, line_position = contents
|
16
|
-
@line_number, @index_on_line = line_position
|
17
|
-
@parent = parent
|
18
|
-
super(contents, ast_id)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Results here represent the position and token of the
|
22
|
-
# data we're searching in the form of a Hash like the following:
|
23
|
-
# [
|
24
|
-
# {line_number: 4, index_on_line: 7, token: "ruby"},
|
25
|
-
# {line_number: 7, index_on_line: 7, token: "rails"}
|
26
|
-
# ]
|
27
|
-
# TODO: Worry about using a faster sorting algorithm later.
|
28
|
-
def self.order_results_by_position(results)
|
29
|
-
results.sort do |a, b|
|
30
|
-
by_line = a[:line_number] <=> b[:line_number]
|
31
|
-
# If we're on the same line, refer to the inner index for order.
|
32
|
-
by_line.zero? ? a[:index_on_line] <=> b[:index_on_line] : by_line
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def line_data_and_token
|
37
|
-
{
|
38
|
-
line_number: @line_number,
|
39
|
-
index_on_line: @index_on_line,
|
40
|
-
token: @token
|
41
|
-
}
|
42
|
-
end
|
43
|
-
|
44
|
-
def position
|
45
|
-
[@line_number, @index_on_line]
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# TODO: Add description
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class Node
|
6
|
-
attr_reader :ast_id, :contents, :index_stack, :data_nodes
|
7
|
-
|
8
|
-
def initialize(contents, ast_id)
|
9
|
-
@ast_id = ast_id
|
10
|
-
@contents = contents
|
11
|
-
@data_nodes = extract_data_nodes
|
12
|
-
end
|
13
|
-
|
14
|
-
def ast
|
15
|
-
ObjectSpace._id2ref(@ast_id)
|
16
|
-
end
|
17
|
-
|
18
|
-
# TODO: Consider removing the :type and :typeless methods.
|
19
|
-
|
20
|
-
# Ripper's abstract syntax tree nodes are either typed or typeless.
|
21
|
-
# Types consist of values such as :def, :do_block, :var_ref, etc.
|
22
|
-
# Typed nodes house one of these types as its first element,
|
23
|
-
# whereas typeless nodes simply have arrays as top-level elements.
|
24
|
-
# Example of a typed node: [:@tstring_content, "ruby", [4, 7]]
|
25
|
-
def typed?
|
26
|
-
@contents.is_a?(Array) && @contents.first.is_a?(Symbol)
|
27
|
-
end
|
28
|
-
|
29
|
-
def typeless?
|
30
|
-
!typed?
|
31
|
-
end
|
32
|
-
|
33
|
-
# By default, we assume a node has no data nodes to extract.
|
34
|
-
# Because the structure for each node is different, we handle
|
35
|
-
# extraction separately for each node within its respective class.
|
36
|
-
def extract_data_nodes
|
37
|
-
# TODO: Might want to make this [] instead of nil.
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class Call < Node
|
6
|
-
def initialize(contents, ast_id)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
def extract_data_nodes
|
11
|
-
[
|
12
|
-
DataNode.new(@contents.last, @ast_id, self)
|
13
|
-
]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
# Code example:
|
2
|
-
# puts "hello"
|
3
|
-
#
|
4
|
-
# syntax_tree output:
|
5
|
-
# SyntaxTree::Command[
|
6
|
-
# message: SyntaxTree::Ident[value: "puts"],
|
7
|
-
# arguments: SyntaxTree::Args[
|
8
|
-
# parts: [
|
9
|
-
# SyntaxTree::StringLiteral[
|
10
|
-
# parts: [SyntaxTree::TStringContent[value: "greetings"]]
|
11
|
-
# ]
|
12
|
-
# ]
|
13
|
-
# ]
|
14
|
-
# ]
|
15
|
-
|
16
|
-
# Code example:
|
17
|
-
# namespace :project
|
18
|
-
# resources :pages
|
19
|
-
# end
|
20
|
-
#
|
21
|
-
# syntax_tree output:
|
22
|
-
# SyntaxTree::Command[
|
23
|
-
# message: SyntaxTree::Ident[value: "namespace"],
|
24
|
-
# arguments: SyntaxTree::Args[
|
25
|
-
# parts: [SyntaxTree::SymbolLiteral[value: SyntaxTree::Ident[value: "project"]]]
|
26
|
-
# ],
|
27
|
-
# block: SyntaxTree::BlockNode[
|
28
|
-
# bodystmt: SyntaxTree::BodyStmt[
|
29
|
-
# statements: SyntaxTree::Statements[
|
30
|
-
# body: [
|
31
|
-
# SyntaxTree::Command[
|
32
|
-
# message: SyntaxTree::Ident[value: "resources"],
|
33
|
-
# arguments: SyntaxTree::Args[
|
34
|
-
# parts: [
|
35
|
-
# SyntaxTree::SymbolLiteral[
|
36
|
-
# value: SyntaxTree::Ident[value: "pages"]
|
37
|
-
# ]
|
38
|
-
# ]
|
39
|
-
# ]
|
40
|
-
# ]
|
41
|
-
# ]
|
42
|
-
# ]
|
43
|
-
# ]
|
44
|
-
# ]
|
45
|
-
# ]
|
46
|
-
|
47
|
-
|
48
|
-
# TODO: As you can see in the second example above, `command` can receive a
|
49
|
-
# block, so we might want to have a way to access the block node in the future.
|
50
|
-
module Masamune
|
51
|
-
class AbstractSyntaxTree
|
52
|
-
class Command < Node
|
53
|
-
def initialize(contents, ast_id)
|
54
|
-
super
|
55
|
-
end
|
56
|
-
|
57
|
-
def extract_data_nodes
|
58
|
-
[
|
59
|
-
DataNode.new(@contents[1], @ast_id, self)
|
60
|
-
]
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class Def < Node
|
6
|
-
def initialize(contents, ast_id)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
def extract_data_nodes
|
11
|
-
[
|
12
|
-
DataNode.new(@contents[1], @ast_id, self)
|
13
|
-
]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class Params < Node
|
6
|
-
def initialize(contents, ast_id)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
def extract_data_nodes
|
11
|
-
# TODO: Sometimes the params node looks like this:
|
12
|
-
# [:params, nil, nil, nil, nil, nil, nil, nil]
|
13
|
-
# Add a description for this, and review this portion
|
14
|
-
# to ensure that it's being handled properly.
|
15
|
-
unless @contents[1].nil?
|
16
|
-
@contents[1].map do |content|
|
17
|
-
DataNode.new(content, @ast_id, self)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# Program represents the top layer of the abstract syntax tree,
|
2
|
-
# and the second element of the array houses the entire program.
|
3
|
-
|
4
|
-
module Masamune
|
5
|
-
class AbstractSyntaxTree
|
6
|
-
class Program < Node
|
7
|
-
def initialize(contents, ast_id)
|
8
|
-
super
|
9
|
-
end
|
10
|
-
|
11
|
-
def extract_data_nodes
|
12
|
-
# No data nodes to extract.
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class StringContent < Node
|
6
|
-
def initialize(contents, ast_id)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
def extract_data_nodes
|
11
|
-
[
|
12
|
-
DataNode.new(@contents[1], @ast_id, self)
|
13
|
-
]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
# DoBlock and BraceBlock share some of
|
2
|
-
# the same logic, so we share that here.
|
3
|
-
|
4
|
-
module Masamune
|
5
|
-
class AbstractSyntaxTree
|
6
|
-
class Block < Node
|
7
|
-
def initialize(contents, ast_id)
|
8
|
-
super
|
9
|
-
end
|
10
|
-
|
11
|
-
def params
|
12
|
-
# This node should exist already, so we search for it in the ast object.
|
13
|
-
block_var = BlockVar.new(contents[1], ast_id)
|
14
|
-
ast.node_list.find {|node| node.contents == block_var.contents}
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# In the abstract syntax tree created by Ripper.sexp,
|
2
|
-
# the type for comments is :void_stmt, and it doesn't have
|
3
|
-
# a data node within it either. This means that the text
|
4
|
-
# for the comment doesn't exist in the ast. It DOES exist
|
5
|
-
# in LexNode though, so we grab from them there instead.
|
6
|
-
|
7
|
-
module Masamune
|
8
|
-
class AbstractSyntaxTree
|
9
|
-
class Comment < Node
|
10
|
-
def initialize(contents, ast_id)
|
11
|
-
super
|
12
|
-
|
13
|
-
# Since this is techincally supposed to be a :void_stmt
|
14
|
-
# in this ast, we just leave this as nil.
|
15
|
-
@contents = nil
|
16
|
-
end
|
17
|
-
|
18
|
-
def extract_data_nodes
|
19
|
-
[
|
20
|
-
DataNode.new([contents.type, contents.token, contents.position], @ast_id, self)
|
21
|
-
]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class Symbol < Node
|
6
|
-
def initialize(contents, ast_id)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
def extract_data_nodes
|
11
|
-
[
|
12
|
-
DataNode.new(@contents[1], @ast_id, self)
|
13
|
-
]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class DynaSymbol < Node
|
6
|
-
def initialize(contents, ast_id)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
# Dyna Symbols are symbols created from strings, i.e. - :"project".
|
11
|
-
# Since they have :@tstring_content inside of them, the data node
|
12
|
-
# is technically for a String, so instead of duplicating it here,
|
13
|
-
# we just use a method specifically for getting the symbol.
|
14
|
-
# This should be the same as the :symbol_literal type.
|
15
|
-
def get_symbol_data
|
16
|
-
DataNode.new(@contents[1][1], @ast_id, self)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class SymbolLiteral < Node
|
6
|
-
def initialize(contents, ast_id)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
# Symbol Literals are a node type of :symbol_literal,
|
11
|
-
# but have a node type of :symbol nested within them.
|
12
|
-
# For the reason, the data node is technically for :symbol
|
13
|
-
# and not :symbol_literal, so instead of duplicating it here,
|
14
|
-
# we just use a method specifically for getting the symbol.
|
15
|
-
# This should be the same as the :dyna_symbol type.
|
16
|
-
def get_symbol_data
|
17
|
-
DataNode.new(@contents[1][1], @ast_id, self)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# TODO: Add description.
|
2
|
-
|
3
|
-
module Masamune
|
4
|
-
class AbstractSyntaxTree
|
5
|
-
class BlockVar < Node
|
6
|
-
attr_accessor :ast_id
|
7
|
-
|
8
|
-
def initialize(contents, ast_id)
|
9
|
-
super
|
10
|
-
end
|
11
|
-
|
12
|
-
# :block_var has a lot of nil values within in.
|
13
|
-
# I'm not sure what this represents, but it would be
|
14
|
-
# nice to find out and document/implement it somewhere.
|
15
|
-
def extract_data_nodes
|
16
|
-
@contents[1][1].map do |content|
|
17
|
-
DataNode.new(content, @ast_id, self)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|