Skip to content
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

Allow creating of Buffer with source #693

Merged
merged 1 commit into from May 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -59,8 +59,7 @@ Parse a chunk of code and display all diagnostics:
puts diag.render
end

buffer = Parser::Source::Buffer.new('(string)')
buffer.source = "foo *bar"
buffer = Parser::Source::Buffer.new('(string)', source: "foo *bar")

p parser.parse(buffer)
# (string):1:5: warning: `*' interpreted as argument prefix
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/diagnostic/engine.rb
Expand Up @@ -7,8 +7,7 @@ module Parser
# diagnostics by delegating them to registered consumers.
#
# @example
# buffer = Parser::Source::Buffer.new(__FILE__)
# buffer.code = 'foobar'
# buffer = Parser::Source::Buffer.new(__FILE__, source: 'foobar')
#
# consumer = lambda do |diagnostic|
# puts diagnostic.message
Expand Down
4 changes: 2 additions & 2 deletions lib/parser/runner/ruby_rewrite.rb
Expand Up @@ -55,8 +55,8 @@ def process(initial_buffer)
new_source = rewriter.rewrite(buffer, ast)

new_buffer = Source::Buffer.new(initial_buffer.name +
'|after ' + rewriter_class.name)
new_buffer.source = new_source
'|after ' + rewriter_class.name,
source: new_source)

@parser.reset
new_ast = @parser.parse(new_buffer)
Expand Down
4 changes: 3 additions & 1 deletion lib/parser/source/buffer.rb
Expand Up @@ -102,7 +102,7 @@ def self.reencode_string(input)
end
end

def initialize(name, first_line = 1)
def initialize(name, first_line = 1, source: nil)
@name = name.to_s
@source = nil
@first_line = first_line
Expand All @@ -116,6 +116,8 @@ def initialize(name, first_line = 1)
# Cache for fast lookup
@line_for_position = {}
@column_for_position = {}

self.source = source if source
end

##
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/tree_rewriter.rb
Expand Up @@ -28,8 +28,7 @@ module Parser
# EOF
#
# ast = Parser::CurrentRuby.parse code
# buffer = Parser::Source::Buffer.new('(example)')
# buffer.source = code
# buffer = Parser::Source::Buffer.new('(example)', source: code)
# rewriter = RemoveDo.new
#
# # Rewrite the AST, returns a String with the new form.
Expand Down
15 changes: 5 additions & 10 deletions test/parse_helper.rb
Expand Up @@ -85,8 +85,7 @@ def assert_parses(ast, code, source_maps='', versions=ALL_VERSIONS)
end

def try_parsing(ast, code, parser, source_maps, version)
source_file = Parser::Source::Buffer.new('(assert_parses)')
source_file.source = code
source_file = Parser::Source::Buffer.new('(assert_parses)', source: code)

begin
parsed_ast = parser.parse(source_file)
Expand Down Expand Up @@ -142,8 +141,7 @@ def try_parsing(ast, code, parser, source_maps, version)
# ~~~
def assert_diagnoses(diagnostic, code, source_maps='', versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(assert_diagnoses)')
source_file.source = code
source_file = Parser::Source::Buffer.new('(assert_diagnoses)', source: code)

begin
parser = parser.parse(source_file)
Expand Down Expand Up @@ -200,8 +198,7 @@ def assert_diagnoses(diagnostic, code, source_maps='', versions=ALL_VERSIONS)
# ~~~
def assert_diagnoses_many(diagnostics, code, versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(assert_diagnoses_many)')
source_file.source = code
source_file = Parser::Source::Buffer.new('(assert_diagnoses_many)', source: code)

begin
parser = parser.parse(source_file)
Expand All @@ -226,8 +223,7 @@ def assert_diagnoses_many(diagnostics, code, versions=ALL_VERSIONS)

def refute_diagnoses(code, versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(refute_diagnoses)')
source_file.source = code
source_file = Parser::Source::Buffer.new('(refute_diagnoses)', source: code)

begin
parser = parser.parse(source_file)
Expand All @@ -243,8 +239,7 @@ def refute_diagnoses(code, versions=ALL_VERSIONS)

def assert_context(context, code, versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(assert_context)')
source_file.source = code
source_file = Parser::Source::Buffer.new('(assert_context)', source: code)

begin
parser.parse(source_file)
Expand Down
11 changes: 5 additions & 6 deletions test/test_diagnostic.rb
Expand Up @@ -4,8 +4,8 @@

class TestDiagnostic < Minitest::Test
def setup
@buffer = Parser::Source::Buffer.new('(string)')
@buffer.source = 'if (this is some bad code + bugs)'
@buffer = Parser::Source::Buffer.new('(string)',
source: 'if (this is some bad code + bugs)')

@range1 = Parser::Source::Range.new(@buffer, 0, 2) # if
@range2 = Parser::Source::Range.new(@buffer, 4, 8) # this
Expand Down Expand Up @@ -50,8 +50,8 @@ def test_render
end

def test_multiline_render
@buffer = Parser::Source::Buffer.new('(string)')
@buffer.source = "abc abc abc\ndef def def\nghi ghi ghi\n"
@buffer = Parser::Source::Buffer.new('(string)',
source: "abc abc abc\ndef def def\nghi ghi ghi\n")

location = Parser::Source::Range.new(@buffer, 4, 27)

Expand Down Expand Up @@ -80,8 +80,7 @@ def test_bug_error_on_newline
}
}
CODE
@buffer = Parser::Source::Buffer.new('(string)')
@buffer.source = source
@buffer = Parser::Source::Buffer.new('(string)', source: source)

location = Parser::Source::Range.new(@buffer, 33, 34)
diag = Parser::Diagnostic.new(:error, :unexpected_token, { :token => 'tNL' },
Expand Down
11 changes: 4 additions & 7 deletions test/test_lexer.rb
Expand Up @@ -37,9 +37,8 @@ def refute_scanned(s, *args)
end

def assert_escape(expected, input)
source_buffer = Parser::Source::Buffer.new('(assert_escape)')

source_buffer.source = "\"\\#{input}\"".encode(input.encoding)
source_buffer = Parser::Source::Buffer.new('(assert_escape)',
source: "\"\\#{input}\"".encode(input.encoding))

@lex.reset
@lex.source_buffer = source_buffer
Expand Down Expand Up @@ -71,8 +70,7 @@ def assert_lex_fname(name, type, range)
end

def assert_scanned(input, *args)
source_buffer = Parser::Source::Buffer.new('(assert_scanned)')
source_buffer.source = input
source_buffer = Parser::Source::Buffer.new('(assert_scanned)', source: input)

@lex.reset(false)
@lex.source_buffer = source_buffer
Expand Down Expand Up @@ -3587,8 +3585,7 @@ def lex_numbered_parameter(input)
@lex.context = Parser::Context.new
@lex.context.push(:block)

source_buffer = Parser::Source::Buffer.new('(assert_lex_numbered_parameter)')
source_buffer.source = input
source_buffer = Parser::Source::Buffer.new('(assert_lex_numbered_parameter)', source: input)

@lex.source_buffer = source_buffer

Expand Down
20 changes: 8 additions & 12 deletions test/test_parser.rb
Expand Up @@ -5360,8 +5360,7 @@ def test_kwbegin_compstmt

def test_crlf_line_endings
with_versions(ALL_VERSIONS) do |_ver, parser|
source_file = Parser::Source::Buffer.new('(comments)')
source_file.source = "\r\nfoo"
source_file = Parser::Source::Buffer.new('(comments)', source: "\r\nfoo")

range = lambda do |from, to|
Parser::Source::Range.new(source_file, from, to)
Expand Down Expand Up @@ -5434,8 +5433,7 @@ def test_file_line_non_literals
with_versions(ALL_VERSIONS) do |_ver, parser|
parser.builder.emit_file_line_as_literals = false

source_file = Parser::Source::Buffer.new('(comments)')
source_file.source = "[__FILE__, __LINE__]"
source_file = Parser::Source::Buffer.new('(comments)', source: "[__FILE__, __LINE__]")

ast = parser.parse(source_file)

Expand Down Expand Up @@ -5519,8 +5517,7 @@ def test_on_error

def assert_parses_with_comments(ast_pattern, source, comments_pattern)
with_versions(ALL_VERSIONS) do |_ver, parser|
source_file = Parser::Source::Buffer.new('(comments)')
source_file.source = source
source_file = Parser::Source::Buffer.new('(comments)', source: source)

comments_pattern_here = comments_pattern.map do |(from, to)|
range = Parser::Source::Range.new(source_file, from, to)
Expand Down Expand Up @@ -5551,8 +5548,8 @@ def test_comment_single

def test_tokenize
with_versions(ALL_VERSIONS) do |_ver, parser|
source_file = Parser::Source::Buffer.new('(tokenize)')
source_file.source = "1 + # foo\n 2"
source_file = Parser::Source::Buffer.new('(tokenize)',
source: "1 + # foo\n 2")

range = lambda do |from, to|
Parser::Source::Range.new(source_file, from, to)
Expand All @@ -5578,8 +5575,8 @@ def test_tokenize

def test_tokenize_recover
with_versions(ALL_VERSIONS) do |_ver, parser|
source_file = Parser::Source::Buffer.new('(tokenize)')
source_file.source = "1 + # foo\n "
source_file = Parser::Source::Buffer.new('(tokenize)',
source: "1 + # foo\n ")

range = lambda do |from, to|
Parser::Source::Range.new(source_file, from, to)
Expand Down Expand Up @@ -9227,8 +9224,7 @@ def assert_pattern_matching_defines_local_variables(match_code, lvar_names, vers
code = "case 1; #{match_code}; then [#{lvar_names.join(', ')}]; end"

with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(assert_context)')
source_file.source = code
source_file = Parser::Source::Buffer.new('(assert_context)', source: code)

lvar_names.each do |lvar_name|
refute parser.static_env.declared?(lvar_name),
Expand Down
3 changes: 3 additions & 0 deletions test/test_source_buffer.rb
Expand Up @@ -20,6 +20,9 @@ def test_initialize

buffer = Parser::Source::Buffer.new('(string)', 5)
assert_equal 5, buffer.first_line

buffer = Parser::Source::Buffer.new('(string)', source: '2+2')
assert_equal '2+2', buffer.source
end

def test_source_setter
Expand Down
4 changes: 2 additions & 2 deletions test/test_source_comment.rb
Expand Up @@ -4,8 +4,8 @@

class TestSourceComment < Minitest::Test
def setup
@buf = Parser::Source::Buffer.new('(string)')
@buf.source = "# foo\n=begin foo\nbar\n=end baz\n"
@buf = Parser::Source::Buffer.new('(string)',
source: "# foo\n=begin foo\nbar\n=end baz\n")
end

def range(s, e)
Expand Down
3 changes: 1 addition & 2 deletions test/test_source_map.rb
Expand Up @@ -7,8 +7,7 @@ class TestSourceMap < Minitest::Test
include ParseHelper

def test_to_hash
buf = Parser::Source::Buffer.new("<input>")
buf.source = "1"
buf = Parser::Source::Buffer.new("<input>", source: "1")
ast = parser_for_ruby_version('1.8').parse(buf)
assert_equal [:expression, :operator], ast.loc.to_hash.keys.sort_by(&:to_s)
end
Expand Down
8 changes: 4 additions & 4 deletions test/test_source_range.rb
Expand Up @@ -4,8 +4,8 @@

class TestSourceRange < Minitest::Test
def setup
@buf = Parser::Source::Buffer.new('(string)')
@buf.source = "foobar\nbaz"
@buf = Parser::Source::Buffer.new('(string)',
source: "foobar\nbaz")
@sr1_3 = Parser::Source::Range.new(@buf, 1, 3)
@sr2_2 = Parser::Source::Range.new(@buf, 2, 2)
@sr3_3 = Parser::Source::Range.new(@buf, 3, 3)
Expand Down Expand Up @@ -178,8 +178,8 @@ def test_eql_and_hash
assert_equal true, @sr1_3.eql?(also_1_3)
assert_equal @sr1_3.hash, also_1_3.hash

buf2 = Parser::Source::Buffer.new('(string)')
buf2.source = "foobar\nbaz"
buf2 = Parser::Source::Buffer.new('(string)',
source: "foobar\nbaz")
from_other_buf = Parser::Source::Range.new(buf2, 1, 3)
assert_equal false, @sr1_3.eql?(from_other_buf)
assert @sr1_3.hash != from_other_buf.hash
Expand Down
4 changes: 2 additions & 2 deletions test/test_source_rewriter.rb
Expand Up @@ -4,8 +4,8 @@

class TestSourceRewriter < Minitest::Test
def setup
@buf = Parser::Source::Buffer.new('(rewriter)')
@buf.source = 'foo bar baz'
@buf = Parser::Source::Buffer.new('(rewriter)',
source: 'foo bar baz')
Parser::Source::Rewriter.warned_of_deprecation = true
@rewriter = Parser::Source::Rewriter.new(@buf)
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_source_rewriter_action.rb
Expand Up @@ -4,8 +4,8 @@

class TestSourceRewriterAction < Minitest::Test
def setup
@buf = Parser::Source::Buffer.new('(rewriter_action)')
@buf.source = 'foo bar baz'
@buf = Parser::Source::Buffer.new('(rewriter_action)',
source: 'foo bar baz')
end

def range(from, len)
Expand Down
4 changes: 2 additions & 2 deletions test/test_source_tree_rewriter.rb
Expand Up @@ -4,8 +4,8 @@

class TestSourceTreeRewriter < Minitest::Test
def setup
@buf = Parser::Source::Buffer.new('(rewriter)')
@buf.source = 'puts(:hello, :world)'
@buf = Parser::Source::Buffer.new('(rewriter)',
source: 'puts(:hello, :world)')

@hello = range(5, 6)
@ll = range(7, 2)
Expand Down