Skip to content

Commit c4c78b3

Browse files
authored
Merge pull request #295 from ruby-syntax-tree/parser-locations
Parser locations
2 parents 0411bdd + 3f30834 commit c4c78b3

16 files changed

+2566
-622
lines changed

.gitmodules

-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@
44
[submodule "spec"]
55
path = spec/ruby
66
url = [email protected]:ruby/spec.git
7-
[submodule "test/ruby-syntax-fixtures"]
8-
path = test/ruby-syntax-fixtures
9-
url = https://github.com/ruby-syntax-tree/ruby-syntax-fixtures
10-
[submodule "test/suites/parser"]
11-
path = test/suites/parser
12-
url = https://github.com/whitequark/parser

Rakefile

+3-15
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ require "bundler/gem_tasks"
44
require "rake/testtask"
55
require "syntax_tree/rake_tasks"
66

7+
Rake.add_rakelib "tasks"
8+
79
Rake::TestTask.new(:test) do |t|
810
t.libs << "test"
911
t.libs << "test/suites"
1012
t.libs << "lib"
11-
12-
# These are our own tests.
13-
test_files = FileList["test/**/*_test.rb"]
14-
15-
# This is a big test file from the parser gem that tests its functionality.
16-
test_files << "test/suites/parser/test/test_parser.rb"
17-
18-
t.test_files = test_files
13+
t.test_files = FileList["test/**/*_test.rb"]
1914
end
2015

2116
task default: :test
@@ -34,10 +29,3 @@ end
3429

3530
SyntaxTree::Rake::CheckTask.new(&configure)
3631
SyntaxTree::Rake::WriteTask.new(&configure)
37-
38-
desc "Run mspec tests using YARV emulation"
39-
task :spec do
40-
Dir["./spec/ruby/language/**/*_spec.rb"].each do |filepath|
41-
sh "exe/yarv ./spec/mspec/bin/mspec-tag #{filepath}"
42-
end
43-
end

lib/syntax_tree/node.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -11527,8 +11527,9 @@ def ===(other)
1152711527
#
1152811528
# To be clear, this method should just not exist. It's not good. It's a
1152911529
# place of shame. But it's necessary for now, so I'm keeping it.
11530-
def pin(parent)
11531-
replace = PinnedVarRef.new(value: value, location: location)
11530+
def pin(parent, pin)
11531+
replace =
11532+
PinnedVarRef.new(value: value, location: pin.location.to(location))
1153211533

1153311534
parent
1153411535
.deconstruct_keys([])

lib/syntax_tree/parser.rb

+24-9
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,7 @@ def visit(node)
641641
end
642642

643643
def visit_var_ref(node)
644-
pins.shift
645-
node.pin(stack[-2])
644+
node.pin(stack[-2], pins.shift)
646645
end
647646

648647
def self.visit(node, tokens)
@@ -1683,6 +1682,22 @@ def on_float(value)
16831682
# VarField right
16841683
# ) -> FndPtn
16851684
def on_fndptn(constant, left, values, right)
1685+
# The left and right of a find pattern are always going to be splats, so
1686+
# we're going to consume the * operators and use their location
1687+
# information to extend the location of the splats.
1688+
right, left =
1689+
[right, left].map do |node|
1690+
operator = consume_operator(:*)
1691+
location =
1692+
if node.value
1693+
operator.location.to(node.location)
1694+
else
1695+
operator.location
1696+
end
1697+
1698+
node.copy(location: location)
1699+
end
1700+
16861701
# The opening of this find pattern is either going to be a left bracket, a
16871702
# right left parenthesis, or the left splat. We're going to use this to
16881703
# determine how to find the closing of the pattern, as well as determining
@@ -1791,7 +1806,7 @@ def on_heredoc_beg(value)
17911806
line: lineno,
17921807
char: char_pos,
17931808
column: current_column,
1794-
size: value.size + 1
1809+
size: value.size
17951810
)
17961811

17971812
# Here we're going to artificially create an extra node type so that if
@@ -1826,7 +1841,7 @@ def on_heredoc_end(value)
18261841
line: lineno,
18271842
char: char_pos,
18281843
column: current_column,
1829-
size: value.size + 1
1844+
size: value.size
18301845
)
18311846

18321847
heredoc_end = HeredocEnd.new(value: value.chomp, location: location)
@@ -1841,9 +1856,9 @@ def on_heredoc_end(value)
18411856
start_line: heredoc.location.start_line,
18421857
start_char: heredoc.location.start_char,
18431858
start_column: heredoc.location.start_column,
1844-
end_line: lineno,
1845-
end_char: char_pos,
1846-
end_column: current_column
1859+
end_line: location.end_line,
1860+
end_char: location.end_char,
1861+
end_column: location.end_column
18471862
)
18481863
)
18491864
end
@@ -2357,14 +2372,14 @@ def on_method_add_arg(call, arguments)
23572372

23582373
# :call-seq:
23592374
# on_method_add_block: (
2360-
# (Break | Call | Command | CommandCall) call,
2375+
# (Break | Call | Command | CommandCall, Next) call,
23612376
# Block block
23622377
# ) -> Break | MethodAddBlock
23632378
def on_method_add_block(call, block)
23642379
location = call.location.to(block.location)
23652380

23662381
case call
2367-
when Break
2382+
when Break, Next
23682383
parts = call.arguments.parts
23692384

23702385
node = parts.pop

0 commit comments

Comments
 (0)