Skip to content

Commit 27f129e

Browse files
authored
Merge pull request #476 from aycabta/support-lambda-literal
Support lambda literal
2 parents 130bd3d + 771315a commit 27f129e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ def lex_init()
509509
tk
510510
end
511511

512+
@OP.def_rules("->") do
513+
|op, io|
514+
@lex_state = :EXPR_ENDFN
515+
Token(op)
516+
end
517+
512518
@OP.def_rules("!", "!=", "!~") do
513519
|op, io|
514520
case @lex_state

lib/rdoc/ruby_token.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ def Token(token, value = nil)
371371
[:TkCOLON3, TkOp, '::'],
372372
#[:OPASGN, TkOp], # +=, -= etc. #
373373
[:TkASSOC, TkOp, "=>"],
374+
[:TkLAMBDA, TkOp, "->"],
374375
[:TkQUESTION, TkOp, "?"], #?
375376
[:TkCOLON, TkOp, ":"], #:
376377

test/test_rdoc_ruby_lex.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,36 @@ def test_class_tokenize_identifier_high_unicode
253253
assert_equal expected, tokens.first
254254
end
255255

256+
def test_class_tokenize_lambda
257+
tokens = RDoc::RubyLex.tokenize 'a = -> x, y { x + y }', nil
258+
259+
expected = [
260+
@TK::TkIDENTIFIER.new( 0, 1, 0, 'a'),
261+
@TK::TkSPACE .new( 1, 1, 1, ' '),
262+
@TK::TkASSIGN .new( 2, 1, 2, '='),
263+
@TK::TkSPACE .new( 3, 1, 3, ' '),
264+
@TK::TkLAMBDA .new( 4, 1, 4, '->'),
265+
@TK::TkSPACE .new( 6, 1, 6, ' '),
266+
@TK::TkIDENTIFIER.new( 7, 1, 7, 'x'),
267+
@TK::TkCOMMA .new( 8, 1, 8, ','),
268+
@TK::TkSPACE .new( 9, 1, 9, ' '),
269+
@TK::TkIDENTIFIER.new(10, 1, 10, 'y'),
270+
@TK::TkSPACE .new(11, 1, 11, ' '),
271+
@TK::TkfLBRACE .new(12, 1, 12, '{'),
272+
@TK::TkSPACE .new(13, 1, 13, ' '),
273+
@TK::TkIDENTIFIER.new(14, 1, 14, 'x'),
274+
@TK::TkSPACE .new(15, 1, 15, ' '),
275+
@TK::TkPLUS .new(16, 1, 16, '+'),
276+
@TK::TkSPACE .new(17, 1, 17, ' '),
277+
@TK::TkIDENTIFIER.new(18, 1, 18, 'y'),
278+
@TK::TkSPACE .new(19, 1, 19, ' '),
279+
@TK::TkRBRACE .new(20, 1, 20, '}'),
280+
@TK::TkNL .new(21, 1, 21, "\n")
281+
]
282+
283+
assert_equal expected, tokens
284+
end
285+
256286
def test_class_tokenize_percent_1
257287
tokens = RDoc::RubyLex.tokenize 'v%10==10', nil
258288

0 commit comments

Comments
 (0)