Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0dd0276

Browse files
committed
More utility functions
1 parent 0cf3e85 commit 0dd0276

File tree

4 files changed

+64
-54
lines changed

4 files changed

+64
-54
lines changed

lib/syntax_tree.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,36 @@ def self.format(
6060
maxwidth = DEFAULT_PRINT_WIDTH,
6161
base_indentation = DEFAULT_INDENTATION,
6262
options: Formatter::Options.new
63+
)
64+
format_node(
65+
source,
66+
parse(source),
67+
maxwidth,
68+
base_indentation,
69+
options: options
70+
)
71+
end
72+
73+
# Parses the given file and returns the formatted source.
74+
def self.format_file(
75+
filepath,
76+
maxwidth = DEFAULT_PRINT_WIDTH,
77+
base_indentation = DEFAULT_INDENTATION,
78+
options: Formatter::Options.new
79+
)
80+
format(read(filepath), maxwidth, base_indentation, options: options)
81+
end
82+
83+
# Accepts a node in the tree and returns the formatted source.
84+
def self.format_node(
85+
source,
86+
node,
87+
maxwidth = DEFAULT_PRINT_WIDTH,
88+
base_indentation = DEFAULT_INDENTATION,
89+
options: Formatter::Options.new
6390
)
6491
formatter = Formatter.new(source, [], maxwidth, options: options)
65-
parse(source).format(formatter)
92+
node.format(formatter)
6693

6794
formatter.flush(base_indentation)
6895
formatter.output.join
@@ -130,4 +157,10 @@ def self.search(source, query, &block)
130157

131158
Search.new(pattern).scan(program, &block)
132159
end
160+
161+
# Searches through the given file using the given pattern and yields each
162+
# node in the tree that matches the pattern to the given block.
163+
def self.search_file(filepath, query, &block)
164+
search(read(filepath), query, &block)
165+
end
133166
end

lib/syntax_tree/mermaid_visitor.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ def field(name, value)
2929
flowchart.link(target, visit(value), name)
3030
else
3131
to =
32-
flowchart.node(
33-
"#{target.id}_#{name}",
34-
value.inspect,
35-
shape: :stadium
36-
)
32+
flowchart.node("#{target.id}_#{name}", value.inspect, shape: :stadium)
3733
flowchart.link(target, to, name)
3834
end
3935
end

lib/syntax_tree/mutation_visitor.rb

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ def visit(node)
3535

3636
# Visit a BEGINBlock node.
3737
def visit_BEGIN(node)
38-
node.copy(
39-
lbrace: visit(node.lbrace),
40-
statements: visit(node.statements)
41-
)
38+
node.copy(lbrace: visit(node.lbrace), statements: visit(node.statements))
4239
end
4340

4441
# Visit a CHAR node.
@@ -48,10 +45,7 @@ def visit_CHAR(node)
4845

4946
# Visit a ENDBlock node.
5047
def visit_END(node)
51-
node.copy(
52-
lbrace: visit(node.lbrace),
53-
statements: visit(node.statements)
54-
)
48+
node.copy(lbrace: visit(node.lbrace), statements: visit(node.statements))
5549
end
5650

5751
# Visit a EndContent node.
@@ -101,10 +95,7 @@ def visit_args_forward(node)
10195

10296
# Visit a ArrayLiteral node.
10397
def visit_array(node)
104-
node.copy(
105-
lbracket: visit(node.lbracket),
106-
contents: visit(node.contents)
107-
)
98+
node.copy(lbracket: visit(node.lbracket), contents: visit(node.contents))
10899
end
109100

110101
# Visit a AryPtn node.
@@ -493,10 +484,7 @@ def visit_label_end(node)
493484

494485
# Visit a Lambda node.
495486
def visit_lambda(node)
496-
node.copy(
497-
params: visit(node.params),
498-
statements: visit(node.statements)
499-
)
487+
node.copy(params: visit(node.params), statements: visit(node.statements))
500488
end
501489

502490
# Visit a LambdaVar node.
@@ -541,10 +529,7 @@ def visit_mlhs_paren(node)
541529

542530
# Visit a ModuleDeclaration node.
543531
def visit_module(node)
544-
node.copy(
545-
constant: visit(node.constant),
546-
bodystmt: visit(node.bodystmt)
547-
)
532+
node.copy(constant: visit(node.constant), bodystmt: visit(node.bodystmt))
548533
end
549534

550535
# Visit a MRHS node.

lib/syntax_tree/with_environment.rb

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ module SyntaxTree
55
# from Visitor. The module overrides a few visit methods to automatically keep
66
# track of local variables and arguments defined in the current environment.
77
# Example usage:
8-
# class MyVisitor < Visitor
9-
# include WithEnvironment
108
#
11-
# def visit_ident(node)
12-
# # Check if we're visiting an identifier for an argument, a local
13-
# variable or something else
14-
# local = current_environment.find_local(node)
9+
# class MyVisitor < Visitor
10+
# include WithEnvironment
1511
#
16-
# if local.type == :argument
17-
# # handle identifiers for arguments
18-
# elsif local.type == :variable
19-
# # handle identifiers for variables
20-
# else
21-
# # handle other identifiers, such as method names
12+
# def visit_ident(node)
13+
# # Check if we're visiting an identifier for an argument, a local
14+
# # variable or something else
15+
# local = current_environment.find_local(node)
16+
#
17+
# if local.type == :argument
18+
# # handle identifiers for arguments
19+
# elsif local.type == :variable
20+
# # handle identifiers for variables
21+
# else
22+
# # handle other identifiers, such as method names
23+
# end
2224
# end
23-
# end
25+
# end
26+
#
2427
module WithEnvironment
2528
# The environment class is used to keep track of local variables and
2629
# arguments inside a particular scope
@@ -37,19 +40,16 @@ class Local
3740
# [Array[Location]] The locations of all usages of this local
3841
attr_reader :usages
3942

40-
# initialize: (Symbol type) -> void
4143
def initialize(type)
4244
@type = type
4345
@definitions = []
4446
@usages = []
4547
end
4648

47-
# add_definition: (Location location) -> void
4849
def add_definition(location)
4950
@definitions << location
5051
end
5152

52-
# add_usage: (Location location) -> void
5353
def add_usage(location)
5454
@usages << location
5555
end
@@ -62,17 +62,15 @@ def add_usage(location)
6262
# [Environment | nil] The parent environment
6363
attr_reader :parent
6464

65-
# initialize: (Environment | nil parent) -> void
6665
def initialize(parent = nil)
6766
@locals = {}
6867
@parent = parent
6968
end
7069

7170
# Adding a local definition will either insert a new entry in the locals
72-
# hash or append a new definition location to an existing local. Notice that
73-
# it's not possible to change the type of a local after it has been
74-
# registered
75-
# add_local_definition: (Ident | Label identifier, Symbol type) -> void
71+
# hash or append a new definition location to an existing local. Notice
72+
# that it's not possible to change the type of a local after it has been
73+
# registered.
7674
def add_local_definition(identifier, type)
7775
name = identifier.value.delete_suffix(":")
7876

@@ -83,8 +81,7 @@ def add_local_definition(identifier, type)
8381
# Adding a local usage will either insert a new entry in the locals
8482
# hash or append a new usage location to an existing local. Notice that
8583
# it's not possible to change the type of a local after it has been
86-
# registered
87-
# add_local_usage: (Ident | Label identifier, Symbol type) -> void
84+
# registered.
8885
def add_local_usage(identifier, type)
8986
name = identifier.value.delete_suffix(":")
9087

@@ -93,8 +90,7 @@ def add_local_usage(identifier, type)
9390
end
9491

9592
# Try to find the local given its name in this environment or any of its
96-
# parents
97-
# find_local: (String name) -> Local | nil
93+
# parents.
9894
def find_local(name)
9995
local = @locals[name]
10096
return local unless local.nil?
@@ -116,7 +112,7 @@ def with_new_environment
116112
end
117113

118114
# Visits for nodes that create new environments, such as classes, modules
119-
# and method definitions
115+
# and method definitions.
120116
def visit_class(node)
121117
with_new_environment { super }
122118
end
@@ -127,7 +123,7 @@ def visit_module(node)
127123

128124
# When we find a method invocation with a block, only the code that happens
129125
# inside of the block needs a fresh environment. The method invocation
130-
# itself happens in the same environment
126+
# itself happens in the same environment.
131127
def visit_method_add_block(node)
132128
visit(node.call)
133129
with_new_environment { visit(node.block) }
@@ -138,7 +134,7 @@ def visit_def(node)
138134
end
139135

140136
# Visit for keeping track of local arguments, such as method and block
141-
# arguments
137+
# arguments.
142138
def visit_params(node)
143139
add_argument_definitions(node.requireds)
144140

0 commit comments

Comments
 (0)