Skip to content

Parser locations #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@
[submodule "spec"]
path = spec/ruby
url = [email protected]:ruby/spec.git
[submodule "test/ruby-syntax-fixtures"]
path = test/ruby-syntax-fixtures
url = https://github.com/ruby-syntax-tree/ruby-syntax-fixtures
[submodule "test/suites/parser"]
path = test/suites/parser
url = https://github.com/whitequark/parser
18 changes: 3 additions & 15 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ require "bundler/gem_tasks"
require "rake/testtask"
require "syntax_tree/rake_tasks"

Rake.add_rakelib "tasks"

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "test/suites"
t.libs << "lib"

# These are our own tests.
test_files = FileList["test/**/*_test.rb"]

# This is a big test file from the parser gem that tests its functionality.
test_files << "test/suites/parser/test/test_parser.rb"

t.test_files = test_files
t.test_files = FileList["test/**/*_test.rb"]
end

task default: :test
Expand All @@ -34,10 +29,3 @@ end

SyntaxTree::Rake::CheckTask.new(&configure)
SyntaxTree::Rake::WriteTask.new(&configure)

desc "Run mspec tests using YARV emulation"
task :spec do
Dir["./spec/ruby/language/**/*_spec.rb"].each do |filepath|
sh "exe/yarv ./spec/mspec/bin/mspec-tag #{filepath}"
end
end
5 changes: 3 additions & 2 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11527,8 +11527,9 @@ def ===(other)
#
# To be clear, this method should just not exist. It's not good. It's a
# place of shame. But it's necessary for now, so I'm keeping it.
def pin(parent)
replace = PinnedVarRef.new(value: value, location: location)
def pin(parent, pin)
replace =
PinnedVarRef.new(value: value, location: pin.location.to(location))

parent
.deconstruct_keys([])
Expand Down
33 changes: 24 additions & 9 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,7 @@ def visit(node)
end

def visit_var_ref(node)
pins.shift
node.pin(stack[-2])
node.pin(stack[-2], pins.shift)
end

def self.visit(node, tokens)
Expand Down Expand Up @@ -1683,6 +1682,22 @@ def on_float(value)
# VarField right
# ) -> FndPtn
def on_fndptn(constant, left, values, right)
# The left and right of a find pattern are always going to be splats, so
# we're going to consume the * operators and use their location
# information to extend the location of the splats.
right, left =
[right, left].map do |node|
operator = consume_operator(:*)
location =
if node.value
operator.location.to(node.location)
else
operator.location
end

node.copy(location: location)
end

# The opening of this find pattern is either going to be a left bracket, a
# right left parenthesis, or the left splat. We're going to use this to
# determine how to find the closing of the pattern, as well as determining
Expand Down Expand Up @@ -1791,7 +1806,7 @@ def on_heredoc_beg(value)
line: lineno,
char: char_pos,
column: current_column,
size: value.size + 1
size: value.size
)

# Here we're going to artificially create an extra node type so that if
Expand Down Expand Up @@ -1826,7 +1841,7 @@ def on_heredoc_end(value)
line: lineno,
char: char_pos,
column: current_column,
size: value.size + 1
size: value.size
)

heredoc_end = HeredocEnd.new(value: value.chomp, location: location)
Expand All @@ -1841,9 +1856,9 @@ def on_heredoc_end(value)
start_line: heredoc.location.start_line,
start_char: heredoc.location.start_char,
start_column: heredoc.location.start_column,
end_line: lineno,
end_char: char_pos,
end_column: current_column
end_line: location.end_line,
end_char: location.end_char,
end_column: location.end_column
)
)
end
Expand Down Expand Up @@ -2357,14 +2372,14 @@ def on_method_add_arg(call, arguments)

# :call-seq:
# on_method_add_block: (
# (Break | Call | Command | CommandCall) call,
# (Break | Call | Command | CommandCall, Next) call,
# Block block
# ) -> Break | MethodAddBlock
def on_method_add_block(call, block)
location = call.location.to(block.location)

case call
when Break
when Break, Next
parts = call.arguments.parts

node = parts.pop
Expand Down
Loading