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

Support safe navigation operator #487

Merged
merged 2 commits into from
Aug 15, 2017
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: 3 additions & 3 deletions lib/rdoc/ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -668,16 +668,16 @@ def lex_init()
end
end

@OP.def_rule(".") do
@OP.def_rules(".", "&.") do
|op, io|
@lex_state = :EXPR_BEG
if peek(0) =~ /[0-9]/
ungetc
identify_number
else
# for "obj.if" etc.
# for "obj.if" or "obj&.if" etc.
@lex_state = :EXPR_DOT
Token(TkDOT)
Token(op)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/rdoc/ruby_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def Token(token, value = nil)

[:TkASSIGN, Token, "="],
[:TkDOT, Token, "."],
[:TkSAFENAV, Token, "&."],
[:TkLPAREN, Token, "("], #(exp)
[:TkLBRACK, Token, "["], #[arry]
[:TkLBRACE, Token, "{"], #{hash}
Expand Down
13 changes: 13 additions & 0 deletions test/test_rdoc_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ def test_class_tokenize_double_colon_is_not_hash_symbol
assert_equal expected, tokens
end

def test_class_tokenize_safe_nav_operator
tokens = RDoc::RubyLex.tokenize 'receiver&.meth', nil

expected = [
@TK::TkIDENTIFIER.new( 0, 1, 0, "receiver"),
@TK::TkSAFENAV .new( 8, 1, 8, "&."),
@TK::TkIDENTIFIER.new(10, 1, 10, "meth"),
@TK::TkNL .new(14, 1, 14, "\n"),
]

assert_equal expected, tokens
end

def test_class_tokenize_hash_rocket
tokens = RDoc::RubyLex.tokenize '{ :class => "foo" }', nil

Expand Down