forked from whitequark/parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ rubynext.y: add method reference operator
From whitequark#556
- Loading branch information
Showing
6 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Ruby Next AST format additions | ||
======================= | ||
|
||
### Method reference operator | ||
|
||
Format: | ||
|
||
~~~ | ||
(meth-ref (self) :foo) | ||
"self.:foo" | ||
^^ dot | ||
^^^ selector | ||
^^^^^^^^^ expression | ||
~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require "parser/builders/default" | ||
require_relative "meta" | ||
|
||
module Parser | ||
# Add RubyNext specific builder methods | ||
module Builders::Next | ||
def method_ref(receiver, dot_t, selector_t) | ||
n(:meth_ref, [ receiver, value(selector_t).to_sym ], | ||
send_map(receiver, dot_t, selector_t, nil, [], nil)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# encoding: ascii-8bit | ||
# frozen_string_literal: true | ||
|
||
require 'helper' | ||
require 'complex' | ||
|
||
require 'parser/ruby-next/lexer' | ||
|
||
class TestLexerNext < Minitest::Test | ||
def setup_lexer(version) | ||
@lex = version == "next" ? Parser::Lexer::Next.new(28) : Parser::Lexer.new(version) | ||
|
||
@lex.comments = [] | ||
@lex.diagnostics = Parser::Diagnostic::Engine.new | ||
@lex.diagnostics.all_errors_are_fatal = true | ||
# @lex.diagnostics.consumer = lambda { |diag| $stderr.puts "", diag.render } | ||
end | ||
|
||
def setup | ||
setup_lexer 18 | ||
end | ||
|
||
def utf(str) | ||
str.dup.force_encoding(Encoding::UTF_8) | ||
end | ||
|
||
# | ||
# Additional matchers | ||
# | ||
|
||
def refute_scanned(s, *args) | ||
assert_raises Parser::SyntaxError do | ||
assert_scanned(s, *args) | ||
end | ||
end | ||
|
||
def assert_escape(expected, input) | ||
source_buffer = Parser::Source::Buffer.new('(assert_escape)') | ||
|
||
source_buffer.source = "\"\\#{input}\"".encode(input.encoding) | ||
|
||
@lex.reset | ||
@lex.source_buffer = source_buffer | ||
|
||
lex_token, (lex_value, *) = @lex.advance | ||
|
||
lex_value.force_encoding(Encoding::BINARY) | ||
|
||
assert_equal [:tSTRING, expected], | ||
[lex_token, lex_value], | ||
source_buffer.source | ||
end | ||
|
||
def refute_escape(input) | ||
err = assert_raises Parser::SyntaxError do | ||
@lex.state = :expr_beg | ||
assert_scanned "%Q[\\#{input}]" | ||
end | ||
assert_equal :fatal, err.diagnostic.level | ||
end | ||
|
||
def assert_lex_fname(name, type, range) | ||
begin_pos, end_pos = range | ||
assert_scanned("def #{name} ", | ||
:kDEF, 'def', [0, 3], | ||
type, name, [begin_pos + 4, end_pos + 4]) | ||
|
||
assert_equal :expr_endfn, @lex.state | ||
end | ||
|
||
def assert_scanned(input, *args) | ||
source_buffer = Parser::Source::Buffer.new('(assert_scanned)') | ||
source_buffer.source = input | ||
|
||
@lex.reset(false) | ||
@lex.source_buffer = source_buffer | ||
|
||
until args.empty? do | ||
token, value, (begin_pos, end_pos) = args.shift(3) | ||
|
||
lex_token, (lex_value, lex_range) = @lex.advance | ||
assert lex_token, 'no more tokens' | ||
assert_operator [lex_token, lex_value], :eql?, [token, value], input | ||
assert_equal begin_pos, lex_range.begin_pos | ||
assert_equal end_pos, lex_range.end_pos | ||
end | ||
|
||
lex_token, (lex_value, *) = @lex.advance | ||
refute lex_token, "must be empty, but had #{[lex_token, lex_value].inspect}" | ||
end | ||
|
||
def test_meth_ref | ||
setup_lexer "next" | ||
|
||
assert_scanned('foo.:bar', | ||
:tIDENTIFIER, 'foo', [0, 3], | ||
:tMETHREF, '.:', [3, 5], | ||
:tIDENTIFIER, 'bar', [5, 8]) | ||
|
||
assert_scanned('foo .:bar', | ||
:tIDENTIFIER, 'foo', [0, 3], | ||
:tMETHREF, '.:', [4, 6], | ||
:tIDENTIFIER, 'bar', [6, 9]) | ||
end | ||
|
||
def test_meth_ref_unary_op | ||
setup_lexer "next" | ||
|
||
assert_scanned('foo.:+', | ||
:tIDENTIFIER, 'foo', [0, 3], | ||
:tMETHREF, '.:', [3, 5], | ||
:tPLUS, '+', [5, 6]) | ||
|
||
assert_scanned('foo.:-@', | ||
:tIDENTIFIER, 'foo', [0, 3], | ||
:tMETHREF, '.:', [3, 5], | ||
:tUMINUS, '-@', [5, 7]) | ||
end | ||
|
||
def test_meth_ref_unsupported_newlines | ||
setup_lexer "next" | ||
|
||
# MRI emits exactly the same sequence of tokens, | ||
# the error happens later in the parser | ||
|
||
assert_scanned('foo. :+', | ||
:tIDENTIFIER, 'foo', [0, 3], | ||
:tDOT, '.', [3, 4], | ||
:tCOLON, ':', [5, 6], | ||
:tUPLUS, '+', [6, 7]) | ||
|
||
assert_scanned('foo.: +', | ||
:tIDENTIFIER, 'foo', [0, 3], | ||
:tDOT, '.', [3, 4], | ||
:tCOLON, ':', [4, 5], | ||
:tPLUS, '+', [6, 7]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
require 'helper' | ||
require 'parse_helper' | ||
|
||
Parser::Builders::Default.modernize | ||
|
||
class TestParser < Minitest::Test | ||
include ParseHelper | ||
|
||
def parser_for_ruby_version(version) | ||
parser = super | ||
parser.diagnostics.all_errors_are_fatal = true | ||
|
||
%w(foo bar baz).each do |metasyntactic_var| | ||
parser.static_env.declare(metasyntactic_var) | ||
end | ||
|
||
parser | ||
end | ||
|
||
SINCE_NEXT = %w(next) | ||
|
||
def test_meth_ref__27 | ||
assert_parses( | ||
s(:meth_ref, s(:lvar, :foo), :bar), | ||
%q{foo.:bar}, | ||
%q{ ^^ dot | ||
| ~~~ selector | ||
|~~~~~~~~ expression}, | ||
SINCE_NEXT) | ||
|
||
assert_parses( | ||
s(:meth_ref, s(:lvar, :foo), :+@), | ||
%q{foo.:+@}, | ||
%q{ ^^ dot | ||
| ~~ selector | ||
|~~~~~~~ expression}, | ||
SINCE_NEXT) | ||
end | ||
|
||
def test_meth_ref__before_27 | ||
assert_diagnoses( | ||
[:error, :unexpected_token, { :token => 'tCOLON' }], | ||
%q{foo.:bar}, | ||
%q{ ^ location }, | ||
ALL_VERSIONS - SINCE_NEXT) | ||
|
||
assert_diagnoses( | ||
[:error, :unexpected_token, { :token => 'tCOLON' }], | ||
%q{foo.:+@}, | ||
%q{ ^ location }, | ||
ALL_VERSIONS - SINCE_NEXT) | ||
end | ||
|
||
def test_meth_ref_unsupported_newlines | ||
assert_diagnoses( | ||
[:error, :unexpected_token, { :token => 'tCOLON' }], | ||
%Q{foo. :+}, | ||
%q{ ^ location}, | ||
SINCE_NEXT) | ||
|
||
assert_diagnoses( | ||
[:error, :unexpected_token, { :token => 'tCOLON' }], | ||
%Q{foo.: +}, | ||
%q{ ^ location}, | ||
SINCE_NEXT) | ||
end | ||
end |