Skip to content

Commit d566ea1

Browse files
author
Zachary Scott
authored
Merge pull request #461 from aycabta/support-rational-and-imaginary
Support rational and imaginary
2 parents f72c753 + 9240e88 commit d566ea1

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ def identify_number(op = "")
11251125
type = TkINTEGER
11261126
allow_point = true
11271127
allow_e = true
1128+
allow_ri = true
11281129
non_digit = false
11291130
while ch = getc
11301131
num << ch
@@ -1154,8 +1155,25 @@ def identify_number(op = "")
11541155
num << getc
11551156
end
11561157
allow_e = false
1158+
allow_ri = false
11571159
allow_point = false
11581160
non_digit = ch
1161+
when allow_ri && "r"
1162+
if non_digit
1163+
raise Error, "trailing `#{non_digit}' in number"
1164+
end
1165+
type = TkRATIONAL
1166+
if peek(0) == 'i'
1167+
type = TkIMAGINARY
1168+
num << getc
1169+
end
1170+
break
1171+
when allow_ri && "i"
1172+
if non_digit && non_digit != "r"
1173+
raise Error, "trailing `#{non_digit}' in number"
1174+
end
1175+
type = TkIMAGINARY
1176+
break
11591177
else
11601178
if non_digit
11611179
raise Error, "trailing `#{non_digit}' in number"

lib/rdoc/ruby_token.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ def Token(token, value = nil)
331331

332332
[:TkINTEGER, TkVal],
333333
[:TkFLOAT, TkVal],
334+
[:TkRATIONAL, TkVal],
335+
[:TkIMAGINARY, TkVal],
334336
[:TkSTRING, TkVal],
335337
[:TkHEREDOC, TkVal],
336338
[:TkXSTRING, TkVal],

test/test_rdoc_ruby_lex.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,5 +418,24 @@ def test_unary_minus
418418
assert_equal("-0.1", ruby_lex.token.value)
419419
end
420420

421+
def test_rational_imaginary_tokenize
422+
tokens = RDoc::RubyLex.tokenize '1.11r + 2.34i + 5.55ri', nil
423+
424+
expected = [
425+
@TK::TkRATIONAL .new( 0, 1, 0, '1.11r'),
426+
@TK::TkSPACE .new( 5, 1, 5, ' '),
427+
@TK::TkPLUS .new( 6, 1, 6, '+'),
428+
@TK::TkSPACE .new( 7, 1, 7, ' '),
429+
@TK::TkIMAGINARY.new( 8, 1, 8, '2.34i'),
430+
@TK::TkSPACE .new(13, 1, 13, ' '),
431+
@TK::TkPLUS .new(14, 1, 14, '+'),
432+
@TK::TkSPACE .new(15, 1, 15, ' '),
433+
@TK::TkIMAGINARY.new(16, 1, 16, '5.55ri'),
434+
@TK::TkNL .new(22, 1, 22, "\n"),
435+
]
436+
437+
assert_equal expected, tokens
438+
end
439+
421440
end
422441

0 commit comments

Comments
 (0)